2015-10-30 22:04:52 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2017-12-28 15:30:05 +00:00
|
|
|
|
|
|
|
|
namespace MediaWiki\Interwiki;
|
|
|
|
|
|
2015-10-30 22:04:52 +00:00
|
|
|
use Interwiki;
|
|
|
|
|
use MapCacheLRU;
|
2023-01-27 09:31:49 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2024-08-08 09:39:26 +00:00
|
|
|
use MediaWiki\Language\Language;
|
2023-01-27 09:31:49 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-02-23 20:44:38 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2024-09-27 18:13:02 +00:00
|
|
|
use Wikimedia\ObjectCache\WANObjectCache;
|
2023-08-24 11:28:19 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2015-10-30 22:04:52 +00:00
|
|
|
|
|
|
|
|
/**
|
2022-08-16 13:01:51 +00:00
|
|
|
* InterwikiLookup backed by the `interwiki` database table or $wgInterwikiCache.
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
2022-08-16 13:01:51 +00:00
|
|
|
* By default this uses the SQL backend (`interwiki` database table) and includes
|
|
|
|
|
* two levels of caching. When parsing a wiki page, many interwiki lookups may
|
|
|
|
|
* be required and thus there is in-class caching for repeat lookups. To reduce
|
|
|
|
|
* database pressure, there is also WANObjectCache for each prefix.
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
2022-08-16 13:01:51 +00:00
|
|
|
* Optionally, a pregenerated dataset can be statically set via $wgInterwikiCache,
|
|
|
|
|
* in which case there are no calls to either database or WANObjectCache.
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
class ClassicInterwikiLookup implements InterwikiLookup {
|
2023-01-27 09:31:49 +00:00
|
|
|
/**
|
|
|
|
|
* @internal For use by ServiceWiring
|
|
|
|
|
*/
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
|
MainConfigNames::InterwikiExpiry,
|
|
|
|
|
MainConfigNames::InterwikiCache,
|
|
|
|
|
MainConfigNames::InterwikiScopes,
|
|
|
|
|
MainConfigNames::InterwikiFallbackSite,
|
|
|
|
|
'wikiId',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
private ServiceOptions $options;
|
2022-08-16 21:14:40 +00:00
|
|
|
/** @var Language */
|
2018-07-29 12:24:54 +00:00
|
|
|
private $contLang;
|
2022-08-16 21:14:40 +00:00
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
private $wanCache;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
/** @var HookRunner */
|
|
|
|
|
private $hookRunner;
|
2023-08-24 11:28:19 +00:00
|
|
|
/** @var IConnectionProvider */
|
|
|
|
|
private $dbProvider;
|
2021-04-01 20:48:53 +00:00
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
/** @var MapCacheLRU<Interwiki|false> */
|
|
|
|
|
private $instances;
|
2023-01-27 09:31:49 +00:00
|
|
|
/**
|
|
|
|
|
* Specify number of domains to check for messages:
|
|
|
|
|
* - 1: Just local wiki level
|
|
|
|
|
* - 2: wiki and global levels
|
|
|
|
|
* - 3: site level as well as wiki and global levels
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2022-08-16 21:14:40 +00:00
|
|
|
private $interwikiScopes;
|
|
|
|
|
/** @var array|null Complete pregenerated data if available */
|
|
|
|
|
private $data;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $wikiId;
|
|
|
|
|
/** @var string|null */
|
|
|
|
|
private $thisSite = null;
|
|
|
|
|
|
2015-10-30 22:04:52 +00:00
|
|
|
/**
|
2023-01-27 09:31:49 +00:00
|
|
|
* @param ServiceOptions $options
|
2018-07-29 12:24:54 +00:00
|
|
|
* @param Language $contLang Language object used to convert prefixes to lower case
|
2022-08-16 21:14:40 +00:00
|
|
|
* @param WANObjectCache $wanCache Cache for interwiki info retrieved from the database
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
* @param HookContainer $hookContainer
|
2023-08-24 11:28:19 +00:00
|
|
|
* @param IConnectionProvider $dbProvider
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
2019-11-30 23:03:59 +00:00
|
|
|
public function __construct(
|
2023-01-27 09:31:49 +00:00
|
|
|
ServiceOptions $options,
|
2018-07-29 12:24:54 +00:00
|
|
|
Language $contLang,
|
2022-08-16 21:14:40 +00:00
|
|
|
WANObjectCache $wanCache,
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
HookContainer $hookContainer,
|
2023-08-24 11:28:19 +00:00
|
|
|
IConnectionProvider $dbProvider
|
2015-10-30 22:04:52 +00:00
|
|
|
) {
|
2023-01-27 09:31:49 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->options = $options;
|
|
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
$this->contLang = $contLang;
|
2022-08-16 21:14:40 +00:00
|
|
|
$this->wanCache = $wanCache;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
2023-08-24 11:28:19 +00:00
|
|
|
$this->dbProvider = $dbProvider;
|
2022-08-16 13:01:51 +00:00
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
$this->instances = new MapCacheLRU( 1000 );
|
2023-01-27 09:31:49 +00:00
|
|
|
$this->interwikiScopes = $options->get( MainConfigNames::InterwikiScopes );
|
2022-08-16 21:14:40 +00:00
|
|
|
|
2023-01-27 09:31:49 +00:00
|
|
|
$interwikiData = $options->get( MainConfigNames::InterwikiCache );
|
|
|
|
|
$this->data = is_array( $interwikiData ) ? $interwikiData : null;
|
|
|
|
|
$this->wikiId = $options->get( 'wikiId' );
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* @inheritDoc
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
* @return bool
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
|
|
|
|
public function isValidInterwiki( $prefix ) {
|
2022-08-16 21:14:40 +00:00
|
|
|
$iw = $this->fetch( $prefix );
|
|
|
|
|
return (bool)$iw;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* @inheritDoc
|
|
|
|
|
* @param string|null $prefix
|
|
|
|
|
* @return Interwiki|null|false
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
|
|
|
|
public function fetch( $prefix ) {
|
2022-08-16 21:14:40 +00:00
|
|
|
if ( $prefix === null || $prefix === '' ) {
|
2015-10-30 22:04:52 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
$prefix = $this->contLang->lc( $prefix );
|
2022-02-08 01:35:25 +00:00
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
return $this->instances->getWithSetCallback(
|
2021-10-21 14:11:56 +00:00
|
|
|
$prefix,
|
|
|
|
|
function () use ( $prefix ) {
|
2022-08-16 21:14:40 +00:00
|
|
|
return $this->load( $prefix );
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
2021-10-21 14:11:56 +00:00
|
|
|
);
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* Purge the instance cache and memcached for an interwiki prefix
|
|
|
|
|
*
|
|
|
|
|
* Note that memcached is not used when $wgInterwikiCache
|
|
|
|
|
* is enabled, as the pregenerated data will be used statically
|
|
|
|
|
* without need for memcached.
|
2022-08-16 13:01:51 +00:00
|
|
|
*
|
2015-10-30 22:04:52 +00:00
|
|
|
* @param string $prefix
|
|
|
|
|
*/
|
|
|
|
|
public function invalidateCache( $prefix ) {
|
2022-08-16 21:14:40 +00:00
|
|
|
$this->instances->clear( $prefix );
|
2015-10-30 22:04:52 +00:00
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
$key = $this->wanCache->makeKey( 'interwiki', $prefix );
|
|
|
|
|
$this->wanCache->delete( $key );
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* Get value from pregenerated data
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
2022-08-16 21:14:40 +00:00
|
|
|
* @param string $prefix
|
|
|
|
|
* @return string|false The pregen value or false if prefix is not known
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
2022-08-16 21:14:40 +00:00
|
|
|
private function getPregenValue( string $prefix ) {
|
|
|
|
|
// Lazily resolve site name
|
2022-02-08 01:35:25 +00:00
|
|
|
if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
|
2023-01-27 09:31:49 +00:00
|
|
|
$this->thisSite = $this->data['__sites:' . $this->wikiId]
|
|
|
|
|
?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
$value = $this->data[$this->wikiId . ':' . $prefix] ?? false;
|
2022-02-08 01:35:25 +00:00
|
|
|
// Site level
|
|
|
|
|
if ( $value === false && $this->interwikiScopes >= 3 ) {
|
|
|
|
|
$value = $this->data["_{$this->thisSite}:{$prefix}"] ?? false;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
2022-08-16 21:14:40 +00:00
|
|
|
// Global level
|
2022-02-08 01:35:25 +00:00
|
|
|
if ( $value === false && $this->interwikiScopes >= 2 ) {
|
|
|
|
|
$value = $this->data["__global:{$prefix}"] ?? false;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
2022-02-08 01:35:25 +00:00
|
|
|
|
|
|
|
|
return $value;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* Fetch interwiki data and create an Interwiki object.
|
|
|
|
|
*
|
|
|
|
|
* Use pregenerated data if enabled. Otherwise try memcached first
|
|
|
|
|
* and fallback to a DB query.
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
|
|
|
|
* @param string $prefix The interwiki prefix
|
2022-08-16 21:14:40 +00:00
|
|
|
* @return Interwiki|false False is prefix is invalid
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
|
|
|
|
private function load( $prefix ) {
|
2022-08-16 21:14:40 +00:00
|
|
|
if ( $this->data !== null ) {
|
|
|
|
|
$value = $this->getPregenValue( $prefix );
|
|
|
|
|
return $value ? $this->makeFromPregen( $prefix, $value ) : false;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
$iwData = [];
|
|
|
|
|
$abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
|
|
|
|
|
if ( isset( $iwData['iw_url'] ) ) {
|
|
|
|
|
// Hook provided data
|
|
|
|
|
return $this->makeFromRow( $iwData );
|
|
|
|
|
}
|
|
|
|
|
if ( $abort ) {
|
|
|
|
|
// Hook indicated no other source may be considered
|
|
|
|
|
return false;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-30 11:09:46 +00:00
|
|
|
$fname = __METHOD__;
|
2022-08-16 21:14:40 +00:00
|
|
|
$iwData = $this->wanCache->getWithSetCallback(
|
|
|
|
|
$this->wanCache->makeKey( 'interwiki', $prefix ),
|
2023-01-27 09:31:49 +00:00
|
|
|
$this->options->get( MainConfigNames::InterwikiExpiry ),
|
2018-09-30 11:09:46 +00:00
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix, $fname ) {
|
2023-08-24 11:28:19 +00:00
|
|
|
$dbr = $this->dbProvider->getReplicaDatabase();
|
2023-07-25 13:37:41 +00:00
|
|
|
$row = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( self::selectFields() )
|
|
|
|
|
->from( 'interwiki' )
|
|
|
|
|
->where( [ 'iw_prefix' => $prefix ] )
|
|
|
|
|
->caller( $fname )->fetchRow();
|
2015-10-30 22:04:52 +00:00
|
|
|
|
|
|
|
|
return $row ? (array)$row : '!NONEXISTENT';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
// Handle non-existent case
|
|
|
|
|
return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* @param array $row Row from the interwiki table, possibly via memcached
|
2023-03-24 02:48:54 +00:00
|
|
|
* @return Interwiki
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
2022-08-16 21:14:40 +00:00
|
|
|
private function makeFromRow( array $row ) {
|
|
|
|
|
$url = $row['iw_url'];
|
|
|
|
|
$local = $row['iw_local'] ?? 0;
|
|
|
|
|
$trans = $row['iw_trans'] ?? 0;
|
|
|
|
|
$api = $row['iw_api'] ?? '';
|
|
|
|
|
$wikiId = $row['iw_wikiid'] ?? '';
|
|
|
|
|
|
|
|
|
|
return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
|
|
|
|
|
}
|
2015-10-30 22:04:52 +00:00
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return Interwiki
|
|
|
|
|
*/
|
|
|
|
|
private function makeFromPregen( string $prefix, string $value ) {
|
|
|
|
|
// Split values
|
|
|
|
|
[ $local, $url ] = explode( ' ', $value, 2 );
|
|
|
|
|
return new Interwiki( $prefix, $url, '', '', (int)$local );
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-02-08 01:35:25 +00:00
|
|
|
* Fetch all interwiki prefixes from pregenerated data
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
2022-08-16 21:14:40 +00:00
|
|
|
* @param null|string $local
|
|
|
|
|
* @return array Database-like rows
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
2022-02-08 01:35:25 +00:00
|
|
|
private function getAllPrefixesPregenerated( $local ) {
|
2022-08-16 21:14:40 +00:00
|
|
|
// Lazily resolve site name
|
2022-02-08 01:35:25 +00:00
|
|
|
if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
|
2023-01-27 09:31:49 +00:00
|
|
|
$this->thisSite = $this->data['__sites:' . $this->wikiId]
|
|
|
|
|
?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
|
2022-02-08 01:35:25 +00:00
|
|
|
}
|
2015-10-30 22:04:52 +00:00
|
|
|
|
2022-02-08 01:35:25 +00:00
|
|
|
// List of interwiki sources
|
|
|
|
|
$sources = [];
|
2022-08-16 21:14:40 +00:00
|
|
|
// Global level
|
2022-02-08 01:35:25 +00:00
|
|
|
if ( $this->interwikiScopes >= 2 ) {
|
|
|
|
|
$sources[] = '__global';
|
|
|
|
|
}
|
|
|
|
|
// Site level
|
|
|
|
|
if ( $this->interwikiScopes >= 3 ) {
|
|
|
|
|
$sources[] = '_' . $this->thisSite;
|
|
|
|
|
}
|
2022-08-16 21:14:40 +00:00
|
|
|
$sources[] = $this->wikiId;
|
2022-02-08 01:35:25 +00:00
|
|
|
|
2022-08-16 21:14:40 +00:00
|
|
|
$data = [];
|
2022-02-08 01:35:25 +00:00
|
|
|
foreach ( $sources as $source ) {
|
|
|
|
|
$list = $this->data['__list:' . $source] ?? '';
|
|
|
|
|
foreach ( explode( ' ', $list ) as $iw_prefix ) {
|
|
|
|
|
$row = $this->data["{$source}:{$iw_prefix}"] ?? null;
|
|
|
|
|
if ( !$row ) {
|
|
|
|
|
continue;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 01:35:25 +00:00
|
|
|
[ $iw_local, $iw_url ] = explode( ' ', $row );
|
2015-10-30 22:04:52 +00:00
|
|
|
|
2022-02-08 01:35:25 +00:00
|
|
|
if ( $local !== null && $local != $iw_local ) {
|
|
|
|
|
continue;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
2022-02-08 01:35:25 +00:00
|
|
|
|
|
|
|
|
$data[$iw_prefix] = [
|
|
|
|
|
'iw_prefix' => $iw_prefix,
|
|
|
|
|
'iw_url' => $iw_url,
|
|
|
|
|
'iw_local' => $iw_local,
|
|
|
|
|
];
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_values( $data );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 22:32:27 +00:00
|
|
|
/**
|
2022-08-16 13:01:51 +00:00
|
|
|
* Build an array in the format accepted by $wgInterwikiCache.
|
|
|
|
|
*
|
|
|
|
|
* Given the array returned by getAllPrefixes(), build a PHP array which
|
2022-02-08 01:35:25 +00:00
|
|
|
* can be given to self::__construct() as $interwikiData, i.e. as the
|
2020-12-18 22:32:27 +00:00
|
|
|
* value of $wgInterwikiCache. This is used to construct mock
|
|
|
|
|
* interwiki lookup services for testing (in particular, parsertests).
|
2022-08-16 13:01:51 +00:00
|
|
|
*
|
2020-12-18 22:32:27 +00:00
|
|
|
* @param array $allPrefixes An array of interwiki information such as
|
|
|
|
|
* would be returned by ::getAllPrefixes()
|
|
|
|
|
* @param int $scope The scope at which to insert interwiki prefixes.
|
|
|
|
|
* See the $interwikiScopes parameter to ::__construct().
|
|
|
|
|
* @param ?string $thisSite The value of $thisSite, if $scope is 3.
|
2022-08-16 13:01:51 +00:00
|
|
|
* @return array
|
2020-12-18 22:32:27 +00:00
|
|
|
*/
|
|
|
|
|
public static function buildCdbHash(
|
|
|
|
|
array $allPrefixes, int $scope = 1, ?string $thisSite = null
|
|
|
|
|
): array {
|
|
|
|
|
$result = [];
|
|
|
|
|
$wikiId = WikiMap::getCurrentWikiId();
|
|
|
|
|
$keyPrefix = ( $scope >= 2 ) ? '__global' : $wikiId;
|
|
|
|
|
if ( $scope >= 3 && $thisSite ) {
|
|
|
|
|
$result[ "__sites:$wikiId" ] = $thisSite;
|
|
|
|
|
$keyPrefix = "_$thisSite";
|
|
|
|
|
}
|
|
|
|
|
$list = [];
|
|
|
|
|
foreach ( $allPrefixes as $iwInfo ) {
|
|
|
|
|
$prefix = $iwInfo['iw_prefix'];
|
|
|
|
|
$result["$keyPrefix:$prefix"] = implode( ' ', [
|
|
|
|
|
$iwInfo['iw_local'] ?? 0, $iwInfo['iw_url']
|
|
|
|
|
] );
|
|
|
|
|
$list[] = $prefix;
|
|
|
|
|
}
|
|
|
|
|
$result["__list:$keyPrefix"] = implode( ' ', $list );
|
|
|
|
|
$result["__list:__sites"] = $wikiId;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 22:04:52 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch all interwiki prefixes from DB
|
|
|
|
|
*
|
2022-08-16 21:14:40 +00:00
|
|
|
* @param bool|null $local
|
|
|
|
|
* @return array[] Database rows
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
|
|
|
|
private function getAllPrefixesDB( $local ) {
|
|
|
|
|
$where = [];
|
|
|
|
|
if ( $local !== null ) {
|
2022-02-19 11:54:08 +00:00
|
|
|
$where['iw_local'] = (int)$local;
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-24 11:28:19 +00:00
|
|
|
$dbr = $this->dbProvider->getReplicaDatabase();
|
2023-07-25 13:37:41 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( self::selectFields() )
|
|
|
|
|
->from( 'interwiki' )
|
|
|
|
|
->where( $where )
|
|
|
|
|
->orderBy( 'iw_prefix' )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2015-10-30 22:04:52 +00:00
|
|
|
|
|
|
|
|
$retval = [];
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$retval[] = (array)$row;
|
|
|
|
|
}
|
|
|
|
|
return $retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* Fetch all interwiki data
|
2015-10-30 22:04:52 +00:00
|
|
|
*
|
2022-08-16 21:14:40 +00:00
|
|
|
* @param string|null $local If set, limit returned data to local or non-local interwikis
|
|
|
|
|
* @return array[] Database-like interwiki rows
|
2015-10-30 22:04:52 +00:00
|
|
|
*/
|
|
|
|
|
public function getAllPrefixes( $local = null ) {
|
2022-02-08 01:35:25 +00:00
|
|
|
if ( $this->data !== null ) {
|
|
|
|
|
return $this->getAllPrefixesPregenerated( $local );
|
2022-08-16 21:14:40 +00:00
|
|
|
} else {
|
|
|
|
|
return $this->getAllPrefixesDB( $local );
|
2015-10-30 22:04:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-16 21:14:40 +00:00
|
|
|
* List of interwiki table fields to select.
|
|
|
|
|
*
|
2015-10-30 22:04:52 +00:00
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
private static function selectFields() {
|
|
|
|
|
return [
|
|
|
|
|
'iw_prefix',
|
|
|
|
|
'iw_url',
|
|
|
|
|
'iw_api',
|
|
|
|
|
'iw_wikiid',
|
|
|
|
|
'iw_local',
|
|
|
|
|
'iw_trans'
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|