wiki.techinc.nl/includes/interwiki/ClassicInterwikiLookup.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

406 lines
11 KiB
PHP
Raw Normal View History

<?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
*/
namespace MediaWiki\Interwiki;
use Interwiki;
use MapCacheLRU;
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;
use MediaWiki\Language\Language;
use MediaWiki\MainConfigNames;
use MediaWiki\WikiMap\WikiMap;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\IConnectionProvider;
/**
* InterwikiLookup backed by the `interwiki` database table or $wgInterwikiCache.
*
* 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.
*
* Optionally, a pregenerated dataset can be statically set via $wgInterwikiCache,
* in which case there are no calls to either database or WANObjectCache.
*
* @since 1.28
*/
class ClassicInterwikiLookup implements InterwikiLookup {
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::InterwikiExpiry,
MainConfigNames::InterwikiCache,
MainConfigNames::InterwikiScopes,
MainConfigNames::InterwikiFallbackSite,
'wikiId',
];
private ServiceOptions $options;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
/** @var Language */
private $contLang;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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;
/** @var IConnectionProvider */
private $dbProvider;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
/** @var MapCacheLRU<Interwiki|false> */
private $instances;
/**
* 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
*/
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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;
/**
* @param ServiceOptions $options
* @param Language $contLang Language object used to convert prefixes to lower case
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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
* @param IConnectionProvider $dbProvider
*/
public function __construct(
ServiceOptions $options,
Language $contLang,
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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,
IConnectionProvider $dbProvider
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
$this->contLang = $contLang;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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 );
$this->dbProvider = $dbProvider;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$this->instances = new MapCacheLRU( 1000 );
$this->interwikiScopes = $options->get( MainConfigNames::InterwikiScopes );
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$interwikiData = $options->get( MainConfigNames::InterwikiCache );
$this->data = is_array( $interwikiData ) ? $interwikiData : null;
$this->wikiId = $options->get( 'wikiId' );
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @inheritDoc
* @param string $prefix
* @return bool
*/
public function isValidInterwiki( $prefix ) {
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$iw = $this->fetch( $prefix );
return (bool)$iw;
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @inheritDoc
* @param string|null $prefix
* @return Interwiki|null|false
*/
public function fetch( $prefix ) {
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
if ( $prefix === null || $prefix === '' ) {
return null;
}
$prefix = $this->contLang->lc( $prefix );
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
return $this->instances->getWithSetCallback(
$prefix,
function () use ( $prefix ) {
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
return $this->load( $prefix );
}
);
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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.
*
* @param string $prefix
*/
public function invalidateCache( $prefix ) {
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$this->instances->clear( $prefix );
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$key = $this->wanCache->makeKey( 'interwiki', $prefix );
$this->wanCache->delete( $key );
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* Get value from pregenerated data
*
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @param string $prefix
* @return string|false The pregen value or false if prefix is not known
*/
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
private function getPregenValue( string $prefix ) {
// Lazily resolve site name
if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
$this->thisSite = $this->data['__sites:' . $this->wikiId]
?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
}
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$value = $this->data[$this->wikiId . ':' . $prefix] ?? false;
// Site level
if ( $value === false && $this->interwikiScopes >= 3 ) {
$value = $this->data["_{$this->thisSite}:{$prefix}"] ?? false;
}
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
// Global level
if ( $value === false && $this->interwikiScopes >= 2 ) {
$value = $this->data["__global:{$prefix}"] ?? false;
}
return $value;
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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.
*
* @param string $prefix The interwiki prefix
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @return Interwiki|false False is prefix is invalid
*/
private function load( $prefix ) {
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
if ( $this->data !== null ) {
$value = $this->getPregenValue( $prefix );
return $value ? $this->makeFromPregen( $prefix, $value ) : false;
}
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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;
}
$fname = __METHOD__;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$iwData = $this->wanCache->getWithSetCallback(
$this->wanCache->makeKey( 'interwiki', $prefix ),
$this->options->get( MainConfigNames::InterwikiExpiry ),
function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix, $fname ) {
$dbr = $this->dbProvider->getReplicaDatabase();
$row = $dbr->newSelectQueryBuilder()
->select( self::selectFields() )
->from( 'interwiki' )
->where( [ 'iw_prefix' => $prefix ] )
->caller( $fname )->fetchRow();
return $row ? (array)$row : '!NONEXISTENT';
}
);
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
// Handle non-existent case
return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @param array $row Row from the interwiki table, possibly via memcached
* @return Interwiki
*/
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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 );
}
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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 );
}
/**
* Fetch all interwiki prefixes from pregenerated data
*
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @param null|string $local
* @return array Database-like rows
*/
private function getAllPrefixesPregenerated( $local ) {
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
// Lazily resolve site name
if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
$this->thisSite = $this->data['__sites:' . $this->wikiId]
?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
}
// List of interwiki sources
$sources = [];
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
// Global level
if ( $this->interwikiScopes >= 2 ) {
$sources[] = '__global';
}
// Site level
if ( $this->interwikiScopes >= 3 ) {
$sources[] = '_' . $this->thisSite;
}
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$sources[] = $this->wikiId;
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
$data = [];
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;
}
[ $iw_local, $iw_url ] = explode( ' ', $row );
if ( $local !== null && $local != $iw_local ) {
continue;
}
$data[$iw_prefix] = [
'iw_prefix' => $iw_prefix,
'iw_url' => $iw_url,
'iw_local' => $iw_local,
];
}
}
return array_values( $data );
}
/**
* Build an array in the format accepted by $wgInterwikiCache.
*
* Given the array returned by getAllPrefixes(), build a PHP array which
* can be given to self::__construct() as $interwikiData, i.e. as the
* value of $wgInterwikiCache. This is used to construct mock
* interwiki lookup services for testing (in particular, parsertests).
*
* @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.
* @return array
*/
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;
}
/**
* Fetch all interwiki prefixes from DB
*
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* @param bool|null $local
* @return array[] Database rows
*/
private function getAllPrefixesDB( $local ) {
$where = [];
if ( $local !== null ) {
$where['iw_local'] = (int)$local;
}
$dbr = $this->dbProvider->getReplicaDatabase();
$res = $dbr->newSelectQueryBuilder()
->select( self::selectFields() )
->from( 'interwiki' )
->where( $where )
->orderBy( 'iw_prefix' )
->caller( __METHOD__ )->fetchResultSet();
$retval = [];
foreach ( $res as $row ) {
$retval[] = (array)$row;
}
return $retval;
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* Fetch all interwiki data
*
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
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
*/
public function getAllPrefixes( $local = null ) {
if ( $this->data !== null ) {
return $this->getAllPrefixesPregenerated( $local );
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
} else {
return $this->getAllPrefixesDB( $local );
}
}
/**
interwiki: Overall clean up and simplification * Remove inline `false` fallback in fetch(). load() does this already and can't return other falsey values like null. * Remove use of Database::getCacheSetOptions as this data is generally not mutable. * Remove foreach-wiki logic from clearInterwikiCache.php. Usually to make scripts apply cross-wiki they are iterated externally via something like foreachwiki (as WMF does) and by using the `--wiki` parameter that core supports. Iterating in-process is somewhat fragile and depended on various hardcoding and assumptions, including e.g. BagOStuff::makeKey() and how ClassicInterwikiLookup calls this. Also fix clearInterwikiCache.php to actually work on simple wikis. Previously it did nothing locally as $wgLocalDatabases is empty. * Remove non-confident code from loadFromArray() for tolerating absence of iw_url. Move the isset() check to the one place that needs it, namely the hook handler in load(), and document what it is for. * Simplify load() usage by inlining and moving fetchPregenerated() responsibility there. * Simplify load() by merging the aborted and non-aborted hook cases, and document the reasons explicitly. * Move use of WikiMap::getCurrentWikiId() to the constructor with TODO for proper injection. * Rename a bunch of variables and apply more consistent terminology within interwiki-related source code. In particular, it was confusing that the variable 'objectCache' in fact did not cache the objects, which are cached in 'localCache' instead. Use the common naming found in other MW components instead ("wanCache" and "instances"). Bug: T315315 Change-Id: If3429b0604974efd4ff5914660979d3e49266848
2022-08-16 21:14:40 +00:00
* List of interwiki table fields to select.
*
* @return string[]
*/
private static function selectFields() {
return [
'iw_prefix',
'iw_url',
'iw_api',
'iw_wikiid',
'iw_local',
'iw_trans'
];
}
}