diff options
author | Amir Sarabadani <Ladsgroup@gmail.com> | 2019-08-26 18:28:26 +0200 |
---|---|---|
committer | Amir Sarabadani <Ladsgroup@gmail.com> | 2019-08-26 18:28:26 +0200 |
commit | 308e6427aef169a575a339e6a8e0558d29403a1d (patch) | |
tree | f4ed26450037b88a09167dc3ab6116e160acb8b0 /includes/cache/localisation/LocalisationCache.php | |
parent | c63a37679c8e96940702fa84e4def02484dd8e24 (diff) | |
download | mediawikicore-308e6427aef169a575a339e6a8e0558d29403a1d.tar.gz mediawikicore-308e6427aef169a575a339e6a8e0558d29403a1d.zip |
Revert "Make LocalisationCache a service"
This reverts commits:
- 76a940350d36c323ebedb4ab45cc81ed1c6b6c92
- b78b8804d076618e967c7b31ec15a1bd9e35d1d0
- 2e52f48c2ed8dcf480843e2186f685a86810e2ac
- e4468a1d6b6b9fdc5b64800febdc8748d21f213d
Bug: T231200
Bug: T231198
Change-Id: I1a7e46a979ae5c9c8130dd3927f6663a216ba753
Diffstat (limited to 'includes/cache/localisation/LocalisationCache.php')
-rw-r--r-- | includes/cache/localisation/LocalisationCache.php | 158 |
1 files changed, 68 insertions, 90 deletions
diff --git a/includes/cache/localisation/LocalisationCache.php b/includes/cache/localisation/LocalisationCache.php index ed9421e255e9..ffc7cd00d6c1 100644 --- a/includes/cache/localisation/LocalisationCache.php +++ b/includes/cache/localisation/LocalisationCache.php @@ -22,15 +22,14 @@ use CLDRPluralRuleParser\Evaluator; use CLDRPluralRuleParser\Error as CLDRPluralRuleError; -use MediaWiki\Config\ServiceOptions; -use MediaWiki\Languages\LanguageNameUtils; -use Psr\Log\LoggerInterface; +use MediaWiki\Logger\LoggerFactory; +use MediaWiki\MediaWikiServices; /** * Class for caching the contents of localisation files, Messages*.php * and *.i18n.php. * - * An instance of this class is available using MediaWikiServices. + * An instance of this class is available using Language::getLocalisationCache(). * * The values retrieved from here are merged, containing items from extension * files, core messages files and the language fallback sequence (e.g. zh-cn -> @@ -41,8 +40,8 @@ use Psr\Log\LoggerInterface; class LocalisationCache { const VERSION = 4; - /** @var ServiceOptions */ - private $options; + /** Configuration associative array */ + private $conf; /** * True if recaching should only be done on an explicit call to recache(). @@ -52,6 +51,11 @@ class LocalisationCache { private $manualRecache = false; /** + * True to treat all files as expired until they are regenerated by this object. + */ + private $forceRecache = false; + + /** * The cache data. 3-d array, where the first key is the language code, * the second key is the item key e.g. 'messages', and the third key is * an item specific subkey index. Some items are not arrays and so for those @@ -67,16 +71,10 @@ class LocalisationCache { private $store; /** - * @var LoggerInterface + * @var \Psr\Log\LoggerInterface */ private $logger; - /** @var callable[] See comment for parameter in constructor */ - private $clearStoreCallbacks; - - /** @var LanguageNameUtils */ - private $langNameUtils; - /** * A 2-d associative array, code/key, where presence indicates that the item * is loaded. Value arbitrary. @@ -191,83 +189,59 @@ class LocalisationCache { private $mergeableKeys = null; /** - * Return a suitable LCStore as specified by the given configuration. + * For constructor parameters, see the documentation in DefaultSettings.php + * for $wgLocalisationCacheConf. * - * @param array $conf In the format of $wgLocalisationCacheConf - * @param string|false|null $fallbackCacheDir In case 'storeDirectory' isn't specified - * @return LCStore + * @param array $conf + * @throws MWException */ - public static function getStoreFromConf( array $conf, $fallbackCacheDir ) : LCStore { + function __construct( $conf ) { + global $wgCacheDirectory; + + $this->conf = $conf; + $this->logger = LoggerFactory::getInstance( 'localisation' ); + + $directory = !empty( $conf['storeDirectory'] ) ? $conf['storeDirectory'] : $wgCacheDirectory; $storeArg = []; - $storeArg['directory'] = - $conf['storeDirectory'] ?: $fallbackCacheDir; + $storeArg['directory'] = $directory; if ( !empty( $conf['storeClass'] ) ) { $storeClass = $conf['storeClass']; - } elseif ( $conf['store'] === 'files' || $conf['store'] === 'file' || - ( $conf['store'] === 'detect' && $storeArg['directory'] ) - ) { - $storeClass = LCStoreCDB::class; - } elseif ( $conf['store'] === 'db' || $conf['store'] === 'detect' ) { - $storeClass = LCStoreDB::class; - $storeArg['server'] = $conf['storeServer'] ?? []; - } elseif ( $conf['store'] === 'array' ) { - $storeClass = LCStoreStaticArray::class; } else { - throw new MWException( - 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' - ); + switch ( $conf['store'] ) { + case 'files': + case 'file': + $storeClass = LCStoreCDB::class; + break; + case 'db': + $storeClass = LCStoreDB::class; + $storeArg['server'] = $conf['storeServer'] ?? []; + break; + case 'array': + $storeClass = LCStoreStaticArray::class; + break; + case 'detect': + if ( $directory ) { + $storeClass = LCStoreCDB::class; + } else { + $storeClass = LCStoreDB::class; + $storeArg['server'] = $conf['storeServer'] ?? []; + } + break; + default: + throw new MWException( + 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' + ); + } } + $this->logger->debug( static::class . ": using store $storeClass" ); - return new $storeClass( $storeArg ); - } - - /** - * @todo Make this a const when HHVM support is dropped (T192166) - * - * @var array - * @since 1.34 - */ - public static $constructorOptions = [ - // True to treat all files as expired until they are regenerated by this object. - 'forceRecache', - 'manualRecache', - 'ExtensionMessagesFiles', - 'MessagesDirs', - ]; - - /** - * For constructor parameters, see the documentation in DefaultSettings.php - * for $wgLocalisationCacheConf. - * - * Do not construct this directly. Use MediaWikiServices. - * - * @param ServiceOptions $options - * @param LCStore $store What backend to use for storage - * @param LoggerInterface $logger - * @param callable[] $clearStoreCallbacks To be called whenever the cache is cleared. Can be - * used to clear other caches that depend on this one, such as ResourceLoader's - * MessageBlobStore. - * @param LanguageNameUtils $langNameUtils - * @throws MWException - */ - function __construct( - ServiceOptions $options, - LCStore $store, - LoggerInterface $logger, - array $clearStoreCallbacks, - LanguageNameUtils $langNameUtils - ) { - $options->assertRequiredOptions( self::$constructorOptions ); - - $this->options = $options; - $this->store = $store; - $this->logger = $logger; - $this->clearStoreCallbacks = $clearStoreCallbacks; - $this->langNameUtils = $langNameUtils; - - // Keep this separate from $this->options so it can be mutable - $this->manualRecache = $options->get( 'manualRecache' ); + $this->store = new $storeClass( $storeArg ); + foreach ( [ 'manualRecache', 'forceRecache' ] as $var ) { + if ( isset( $conf[$var] ) ) { + $this->$var = $conf[$var]; + } + } } /** @@ -432,7 +406,7 @@ class LocalisationCache { * @return bool */ public function isExpired( $code ) { - if ( $this->options->get( 'forceRecache' ) && !isset( $this->recachedLangs[$code] ) ) { + if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) { $this->logger->debug( __METHOD__ . "($code): forced reload" ); return true; @@ -477,7 +451,7 @@ class LocalisationCache { $this->initialisedLangs[$code] = true; # If the code is of the wrong form for a Messages*.php file, do a shallow fallback - if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) { + if ( !Language::isValidBuiltInCode( $code ) ) { $this->initShallowFallback( $code, 'en' ); return; @@ -485,7 +459,7 @@ class LocalisationCache { # Recache the data if necessary if ( !$this->manualRecache && $this->isExpired( $code ) ) { - if ( $this->langNameUtils->isSupportedLanguage( $code ) ) { + if ( Language::isSupportedLanguage( $code ) ) { $this->recache( $code ); } elseif ( $code === 'en' ) { throw new MWException( 'MessagesEn.php is missing.' ); @@ -723,7 +697,7 @@ class LocalisationCache { global $IP; // This reads in the PHP i18n file with non-messages l10n data - $fileName = $this->langNameUtils->getMessagesFileName( $code ); + $fileName = Language::getMessagesFileName( $code ); if ( !file_exists( $fileName ) ) { $data = []; } else { @@ -830,12 +804,14 @@ class LocalisationCache { public function getMessagesDirs() { global $IP; + $config = MediaWikiServices::getInstance()->getMainConfig(); + $messagesDirs = $config->get( 'MessagesDirs' ); return [ 'core' => "$IP/languages/i18n", 'exif' => "$IP/languages/i18n/exif", 'api' => "$IP/includes/api/i18n", 'oojs-ui' => "$IP/resources/lib/ooui/i18n", - ] + $this->options->get( 'MessagesDirs' ); + ] + $messagesDirs; } /** @@ -845,6 +821,8 @@ class LocalisationCache { * @throws MWException */ public function recache( $code ) { + global $wgExtensionMessagesFiles; + if ( !$code ) { throw new MWException( "Invalid language code requested" ); } @@ -896,7 +874,7 @@ class LocalisationCache { # Load non-JSON localisation data for extensions $extensionData = array_fill_keys( $codeSequence, $initialData ); - foreach ( $this->options->get( 'ExtensionMessagesFiles' ) as $extension => $fileName ) { + foreach ( $wgExtensionMessagesFiles as $extension => $fileName ) { if ( isset( $messageDirs[$extension] ) ) { # This extension has JSON message data; skip the PHP shim continue; @@ -1060,9 +1038,8 @@ class LocalisationCache { # HACK: If using a null (i.e. disabled) storage backend, we # can't write to the MessageBlobStore either if ( !$this->store instanceof LCStoreNull ) { - foreach ( $this->clearStoreCallbacks as $callback ) { - $callback(); - } + $blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore(); + $blobStore->clear(); } } @@ -1123,4 +1100,5 @@ class LocalisationCache { $this->store = new LCStoreNull; $this->manualRecache = false; } + } |