3 * Cache of the contents of localisation files.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use CLDRPluralRuleParser\Evaluator
;
24 use CLDRPluralRuleParser\Error
as CLDRPluralRuleError
;
25 use MediaWiki\Config\ServiceOptions
;
26 use MediaWiki\Languages\LanguageNameUtils
;
27 use Psr\Log\LoggerInterface
;
30 * Class for caching the contents of localisation files, Messages*.php
33 * An instance of this class is available using MediaWikiServices.
35 * The values retrieved from here are merged, containing items from extension
36 * files, core messages files and the language fallback sequence (e.g. zh-cn ->
37 * zh-hans -> en ). Some common errors are corrected, for example namespace
38 * names with spaces instead of underscores, but heavyweight processing, such
39 * as grammatical transformation, is done by the caller.
41 class LocalisationCache
{
44 /** @var ServiceOptions */
48 * True if recaching should only be done on an explicit call to recache().
49 * Setting this reduces the overhead of cache freshness checking, which
50 * requires doing a stat() for every extension i18n file.
52 private $manualRecache = false;
55 * The cache data. 3-d array, where the first key is the language code,
56 * the second key is the item key e.g. 'messages', and the third key is
57 * an item specific subkey index. Some items are not arrays and so for those
58 * items, there are no subkeys.
63 * The persistent store object. An instance of LCStore.
70 * @var LoggerInterface
74 /** @var callable[] See comment for parameter in constructor */
75 private $clearStoreCallbacks;
77 /** @var LanguageNameUtils */
78 private $langNameUtils;
81 * A 2-d associative array, code/key, where presence indicates that the item
82 * is loaded. Value arbitrary.
84 * For split items, if set, this indicates that all of the subitems have been
88 private $loadedItems = [];
91 * A 3-d associative array, code/key/subkey, where presence indicates that
92 * the subitem is loaded. Only used for the split items, i.e. messages.
94 private $loadedSubitems = [];
97 * An array where presence of a key indicates that that language has been
98 * initialised. Initialisation includes checking for cache expiry and doing
99 * any necessary updates.
101 private $initialisedLangs = [];
104 * An array mapping non-existent pseudo-languages to fallback languages. This
105 * is filled by initShallowFallback() when data is requested from a language
106 * that lacks a Messages*.php file.
108 private $shallowFallbacks = [];
111 * An array where the keys are codes that have been recached by this instance.
113 private $recachedLangs = [];
118 public static $allKeys = [
119 'fallback', 'namespaceNames', 'bookstoreList',
120 'magicWords', 'messages', 'rtl', 'capitalizeAllNouns',
121 'digitTransformTable', 'separatorTransformTable',
122 'minimumGroupingDigits', 'fallback8bitEncoding',
123 'linkPrefixExtension', 'linkTrail', 'linkPrefixCharset',
124 'namespaceAliases', 'dateFormats', 'datePreferences',
125 'datePreferenceMigrationMap', 'defaultDateFormat',
126 'specialPageAliases', 'imageFiles', 'preloadedMessages',
127 'namespaceGenderAliases', 'digitGroupingPattern', 'pluralRules',
128 'pluralRuleTypes', 'compiledPluralRules',
132 * Keys for items which consist of associative arrays, which may be merged
133 * by a fallback sequence.
135 public static $mergeableMapKeys = [ 'messages', 'namespaceNames',
136 'namespaceAliases', 'dateFormats', 'imageFiles', 'preloadedMessages'
140 * Keys for items which are a numbered array.
142 public static $mergeableListKeys = [];
145 * Keys for items which contain an array of arrays of equivalent aliases
146 * for each subitem. The aliases may be merged by a fallback sequence.
148 public static $mergeableAliasListKeys = [ 'specialPageAliases' ];
151 * Keys for items which contain an associative array, and may be merged if
152 * the primary value contains the special array key "inherit". That array
153 * key is removed after the first merge.
155 public static $optionalMergeKeys = [ 'bookstoreList' ];
158 * Keys for items that are formatted like $magicWords
160 public static $magicWordKeys = [ 'magicWords' ];
163 * Keys for items where the subitems are stored in the backend separately.
165 public static $splitKeys = [ 'messages' ];
168 * Keys which are loaded automatically by initLanguage()
170 public static $preloadedKeys = [ 'dateFormats', 'namespaceNames' ];
173 * Associative array of cached plural rules. The key is the language code,
174 * the value is an array of plural rules for that language.
176 private $pluralRules = null;
179 * Associative array of cached plural rule types. The key is the language
180 * code, the value is an array of plural rule types for that language. For
181 * example, $pluralRuleTypes['ar'] = ['zero', 'one', 'two', 'few', 'many'].
182 * The index for each rule type matches the index for the rule in
183 * $pluralRules, thus allowing correlation between the two. The reason we
184 * don't just use the type names as the keys in $pluralRules is because
185 * Language::convertPlural applies the rules based on numeric order (or
186 * explicit numeric parameter), not based on the name of the rule type. For
187 * example, {{plural:count|wordform1|wordform2|wordform3}}, rather than
188 * {{plural:count|one=wordform1|two=wordform2|many=wordform3}}.
190 private $pluralRuleTypes = null;
192 private $mergeableKeys = null;
195 * Return a suitable LCStore as specified by the given configuration.
198 * @param array $conf In the format of $wgLocalisationCacheConf
199 * @param string|false|null $fallbackCacheDir In case 'storeDirectory' isn't specified
202 public static function getStoreFromConf( array $conf, $fallbackCacheDir ) : LCStore
{
204 $storeArg['directory'] =
205 $conf['storeDirectory'] ?
: $fallbackCacheDir;
207 if ( !empty( $conf['storeClass'] ) ) {
208 $storeClass = $conf['storeClass'];
209 } elseif ( $conf['store'] === 'files' ||
$conf['store'] === 'file' ||
210 ( $conf['store'] === 'detect' && $storeArg['directory'] )
212 $storeClass = LCStoreCDB
::class;
213 } elseif ( $conf['store'] === 'db' ||
$conf['store'] === 'detect' ) {
214 $storeClass = LCStoreDB
::class;
215 $storeArg['server'] = $conf['storeServer'] ??
[];
216 } elseif ( $conf['store'] === 'array' ) {
217 $storeClass = LCStoreStaticArray
::class;
219 throw new MWException(
220 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
224 return new $storeClass( $storeArg );
231 public const CONSTRUCTOR_OPTIONS
= [
232 // True to treat all files as expired until they are regenerated by this object.
235 'ExtensionMessagesFiles',
240 * For constructor parameters, see the documentation in DefaultSettings.php
241 * for $wgLocalisationCacheConf.
243 * Do not construct this directly. Use MediaWikiServices.
245 * @param ServiceOptions $options
246 * @param LCStore $store What backend to use for storage
247 * @param LoggerInterface $logger
248 * @param callable[] $clearStoreCallbacks To be called whenever the cache is cleared. Can be
249 * used to clear other caches that depend on this one, such as ResourceLoader's
251 * @param LanguageNameUtils $langNameUtils
252 * @throws MWException
254 function __construct(
255 ServiceOptions
$options,
257 LoggerInterface
$logger,
258 array $clearStoreCallbacks,
259 LanguageNameUtils
$langNameUtils
261 $options->assertRequiredOptions( self
::CONSTRUCTOR_OPTIONS
);
263 $this->options
= $options;
264 $this->store
= $store;
265 $this->logger
= $logger;
266 $this->clearStoreCallbacks
= $clearStoreCallbacks;
267 $this->langNameUtils
= $langNameUtils;
269 // Keep this separate from $this->options so it can be mutable
270 $this->manualRecache
= $options->get( 'manualRecache' );
274 * Returns true if the given key is mergeable, that is, if it is an associative
275 * array which can be merged through a fallback sequence.
279 public function isMergeableKey( $key ) {
280 if ( $this->mergeableKeys
=== null ) {
281 $this->mergeableKeys
= array_flip( array_merge(
282 self
::$mergeableMapKeys,
283 self
::$mergeableListKeys,
284 self
::$mergeableAliasListKeys,
285 self
::$optionalMergeKeys,
290 return isset( $this->mergeableKeys
[$key] );
296 * Warning: this may be slow for split items (messages), since it will
297 * need to fetch all of the subitems from the cache individually.
298 * @param string $code
302 public function getItem( $code, $key ) {
303 if ( !isset( $this->loadedItems
[$code][$key] ) ) {
304 $this->loadItem( $code, $key );
307 if ( $key === 'fallback' && isset( $this->shallowFallbacks
[$code] ) ) {
308 return $this->shallowFallbacks
[$code];
311 return $this->data
[$code][$key];
315 * Get a subitem, for instance a single message for a given language.
316 * @param string $code
318 * @param string $subkey
321 public function getSubitem( $code, $key, $subkey ) {
322 if ( !isset( $this->loadedSubitems
[$code][$key][$subkey] ) &&
323 !isset( $this->loadedItems
[$code][$key] )
325 $this->loadSubitem( $code, $key, $subkey );
328 return $this->data
[$code][$key][$subkey] ??
null;
332 * Get the list of subitem keys for a given item.
334 * This is faster than array_keys($lc->getItem(...)) for the items listed in
337 * Will return null if the item is not found, or false if the item is not an
339 * @param string $code
341 * @return bool|null|string|string[]
343 public function getSubitemList( $code, $key ) {
344 if ( in_array( $key, self
::$splitKeys ) ) {
345 return $this->getSubitem( $code, 'list', $key );
347 $item = $this->getItem( $code, $key );
348 if ( is_array( $item ) ) {
349 return array_keys( $item );
357 * Load an item into the cache.
358 * @param string $code
361 protected function loadItem( $code, $key ) {
362 if ( !isset( $this->initialisedLangs
[$code] ) ) {
363 $this->initLanguage( $code );
366 // Check to see if initLanguage() loaded it for us
367 if ( isset( $this->loadedItems
[$code][$key] ) ) {
371 if ( isset( $this->shallowFallbacks
[$code] ) ) {
372 $this->loadItem( $this->shallowFallbacks
[$code], $key );
377 if ( in_array( $key, self
::$splitKeys ) ) {
378 $subkeyList = $this->getSubitem( $code, 'list', $key );
379 foreach ( $subkeyList as $subkey ) {
380 if ( isset( $this->data
[$code][$key][$subkey] ) ) {
383 $this->data
[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
386 $this->data
[$code][$key] = $this->store
->get( $code, $key );
389 $this->loadedItems
[$code][$key] = true;
393 * Load a subitem into the cache
394 * @param string $code
396 * @param string $subkey
398 protected function loadSubitem( $code, $key, $subkey ) {
399 if ( !in_array( $key, self
::$splitKeys ) ) {
400 $this->loadItem( $code, $key );
405 if ( !isset( $this->initialisedLangs
[$code] ) ) {
406 $this->initLanguage( $code );
409 // Check to see if initLanguage() loaded it for us
410 if ( isset( $this->loadedItems
[$code][$key] ) ||
411 isset( $this->loadedSubitems
[$code][$key][$subkey] )
416 if ( isset( $this->shallowFallbacks
[$code] ) ) {
417 $this->loadSubitem( $this->shallowFallbacks
[$code], $key, $subkey );
422 $value = $this->store
->get( $code, "$key:$subkey" );
423 $this->data
[$code][$key][$subkey] = $value;
424 $this->loadedSubitems
[$code][$key][$subkey] = true;
428 * Returns true if the cache identified by $code is missing or expired.
430 * @param string $code
434 public function isExpired( $code ) {
435 if ( $this->options
->get( 'forceRecache' ) && !isset( $this->recachedLangs
[$code] ) ) {
436 $this->logger
->debug( __METHOD__
. "($code): forced reload" );
441 $deps = $this->store
->get( $code, 'deps' );
442 $keys = $this->store
->get( $code, 'list' );
443 $preload = $this->store
->get( $code, 'preload' );
444 // Different keys may expire separately for some stores
445 if ( $deps === null ||
$keys === null ||
$preload === null ) {
446 $this->logger
->debug( __METHOD__
. "($code): cache missing, need to make one" );
451 foreach ( $deps as $dep ) {
452 // Because we're unserializing stuff from cache, we
453 // could receive objects of classes that don't exist
454 // anymore (e.g. uninstalled extensions)
455 // When this happens, always expire the cache
456 if ( !$dep instanceof CacheDependency ||
$dep->isExpired() ) {
457 $this->logger
->debug( __METHOD__
. "($code): cache for $code expired due to " .
468 * Initialise a language in this object. Rebuild the cache if necessary.
469 * @param string $code
470 * @throws MWException
472 protected function initLanguage( $code ) {
473 if ( isset( $this->initialisedLangs
[$code] ) ) {
477 $this->initialisedLangs
[$code] = true;
479 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
480 if ( !$this->langNameUtils
->isValidBuiltInCode( $code ) ) {
481 $this->initShallowFallback( $code, 'en' );
486 # Recache the data if necessary
487 if ( !$this->manualRecache
&& $this->isExpired( $code ) ) {
488 if ( $this->langNameUtils
->isSupportedLanguage( $code ) ) {
489 $this->recache( $code );
490 } elseif ( $code === 'en' ) {
491 throw new MWException( 'MessagesEn.php is missing.' );
493 $this->initShallowFallback( $code, 'en' );
500 $preload = $this->getItem( $code, 'preload' );
501 if ( $preload === null ) {
502 if ( $this->manualRecache
) {
503 // No Messages*.php file. Do shallow fallback to en.
504 if ( $code === 'en' ) {
505 throw new MWException( 'No localisation cache found for English. ' .
506 'Please run maintenance/rebuildLocalisationCache.php.' );
508 $this->initShallowFallback( $code, 'en' );
512 throw new MWException( 'Invalid or missing localisation cache.' );
515 $this->data
[$code] = $preload;
516 foreach ( $preload as $key => $item ) {
517 if ( in_array( $key, self
::$splitKeys ) ) {
518 foreach ( $item as $subkey => $subitem ) {
519 $this->loadedSubitems
[$code][$key][$subkey] = true;
522 $this->loadedItems
[$code][$key] = true;
528 * Create a fallback from one language to another, without creating a
529 * complete persistent cache.
530 * @param string $primaryCode
531 * @param string $fallbackCode
533 public function initShallowFallback( $primaryCode, $fallbackCode ) {
534 $this->data
[$primaryCode] =& $this->data
[$fallbackCode];
535 $this->loadedItems
[$primaryCode] =& $this->loadedItems
[$fallbackCode];
536 $this->loadedSubitems
[$primaryCode] =& $this->loadedSubitems
[$fallbackCode];
537 $this->shallowFallbacks
[$primaryCode] = $fallbackCode;
541 * Read a PHP file containing localisation data.
542 * @param string $_fileName
543 * @param string $_fileType
544 * @throws MWException
547 protected function readPHPFile( $_fileName, $_fileType ) {
551 if ( $_fileType == 'core' ||
$_fileType == 'extension' ) {
552 foreach ( self
::$allKeys as $key ) {
553 // Not all keys are set in language files, so
554 // check they exist first
555 if ( isset( $
$key ) ) {
559 } elseif ( $_fileType == 'aliases' ) {
560 if ( isset( $aliases ) ) {
561 $data['aliases'] = $aliases;
564 throw new MWException( __METHOD__
. ": Invalid file type: $_fileType" );
571 * Read a JSON file containing localisation messages.
572 * @param string $fileName Name of file to read
573 * @throws MWException If there is a syntax error in the JSON file
574 * @return array Array with a 'messages' key, or empty array if the file doesn't exist
576 public function readJSONFile( $fileName ) {
577 if ( !is_readable( $fileName ) ) {
581 $json = file_get_contents( $fileName );
582 if ( $json === false ) {
586 $data = FormatJson
::decode( $json, true );
587 if ( $data === null ) {
588 throw new MWException( __METHOD__
. ": Invalid JSON file: $fileName" );
591 // Remove keys starting with '@', they're reserved for metadata and non-message data
592 foreach ( $data as $key => $unused ) {
593 if ( $key === '' ||
$key[0] === '@' ) {
594 unset( $data[$key] );
598 // The JSON format only supports messages, none of the other variables, so wrap the data
599 return [ 'messages' => $data ];
603 * Get the compiled plural rules for a given language from the XML files.
605 * @param string $code
608 public function getCompiledPluralRules( $code ) {
609 $rules = $this->getPluralRules( $code );
610 if ( $rules === null ) {
614 $compiledRules = Evaluator
::compile( $rules );
615 } catch ( CLDRPluralRuleError
$e ) {
616 $this->logger
->debug( $e->getMessage() );
621 return $compiledRules;
625 * Get the plural rules for a given language from the XML files.
628 * @param string $code
631 public function getPluralRules( $code ) {
632 if ( $this->pluralRules
=== null ) {
633 $this->loadPluralFiles();
635 return $this->pluralRules
[$code] ??
null;
639 * Get the plural rule types for a given language from the XML files.
642 * @param string $code
645 public function getPluralRuleTypes( $code ) {
646 if ( $this->pluralRuleTypes
=== null ) {
647 $this->loadPluralFiles();
649 return $this->pluralRuleTypes
[$code] ??
null;
653 * Load the plural XML files.
655 protected function loadPluralFiles() {
657 $cldrPlural = "$IP/languages/data/plurals.xml";
658 $mwPlural = "$IP/languages/data/plurals-mediawiki.xml";
659 // Load CLDR plural rules
660 $this->loadPluralFile( $cldrPlural );
661 if ( file_exists( $mwPlural ) ) {
662 // Override or extend
663 $this->loadPluralFile( $mwPlural );
668 * Load a plural XML file with the given filename, compile the relevant
669 * rules, and save the compiled rules in a process-local cache.
671 * @param string $fileName
672 * @throws MWException
674 protected function loadPluralFile( $fileName ) {
675 // Use file_get_contents instead of DOMDocument::load (T58439)
676 $xml = file_get_contents( $fileName );
678 throw new MWException( "Unable to read plurals file $fileName" );
680 $doc = new DOMDocument
;
681 $doc->loadXML( $xml );
682 $rulesets = $doc->getElementsByTagName( "pluralRules" );
683 foreach ( $rulesets as $ruleset ) {
684 $codes = $ruleset->getAttribute( 'locales' );
687 $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
688 foreach ( $ruleElements as $elt ) {
689 $ruleType = $elt->getAttribute( 'count' );
690 if ( $ruleType === 'other' ) {
691 // Don't record "other" rules, which have an empty condition
694 $rules[] = $elt->nodeValue
;
695 $ruleTypes[] = $ruleType;
697 foreach ( explode( ' ', $codes ) as $code ) {
698 $this->pluralRules
[$code] = $rules;
699 $this->pluralRuleTypes
[$code] = $ruleTypes;
705 * Read the data from the source files for a given language, and register
706 * the relevant dependencies in the $deps array. If the localisation
707 * exists, the data array is returned, otherwise false is returned.
709 * @param string $code
710 * @param array &$deps
713 protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
716 // This reads in the PHP i18n file with non-messages l10n data
717 $fileName = $this->langNameUtils
->getMessagesFileName( $code );
718 if ( !file_exists( $fileName ) ) {
721 $deps[] = new FileDependency( $fileName );
722 $data = $this->readPHPFile( $fileName, 'core' );
725 # Load CLDR plural rules for JavaScript
726 $data['pluralRules'] = $this->getPluralRules( $code );
728 $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
729 # Load plural rule types
730 $data['pluralRuleTypes'] = $this->getPluralRuleTypes( $code );
732 $deps['plurals'] = new FileDependency( "$IP/languages/data/plurals.xml" );
733 $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
739 * Merge two localisation values, a primary and a fallback, overwriting the
740 * primary value in place.
742 * @param mixed &$value
743 * @param mixed $fallbackValue
745 protected function mergeItem( $key, &$value, $fallbackValue ) {
746 if ( !is_null( $value ) ) {
747 if ( !is_null( $fallbackValue ) ) {
748 if ( in_array( $key, self
::$mergeableMapKeys ) ) {
749 $value = $value +
$fallbackValue;
750 } elseif ( in_array( $key, self
::$mergeableListKeys ) ) {
751 // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
752 $value = array_unique( array_merge( $fallbackValue, $value ) );
753 } elseif ( in_array( $key, self
::$mergeableAliasListKeys ) ) {
754 $value = array_merge_recursive( $value, $fallbackValue );
755 } elseif ( in_array( $key, self
::$optionalMergeKeys ) ) {
756 if ( !empty( $value['inherit'] ) ) {
757 $value = array_merge( $fallbackValue, $value );
760 if ( isset( $value['inherit'] ) ) {
761 unset( $value['inherit'] );
763 } elseif ( in_array( $key, self
::$magicWordKeys ) ) {
764 $this->mergeMagicWords( $value, $fallbackValue );
768 $value = $fallbackValue;
773 * @param mixed &$value
774 * @param mixed $fallbackValue
776 protected function mergeMagicWords( &$value, $fallbackValue ) {
777 foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
778 if ( !isset( $value[$magicName] ) ) {
779 $value[$magicName] = $fallbackInfo;
781 $oldSynonyms = array_slice( $fallbackInfo, 1 );
782 $newSynonyms = array_slice( $value[$magicName], 1 );
783 $synonyms = array_values( array_unique( array_merge(
784 $newSynonyms, $oldSynonyms ) ) );
785 $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
791 * Given an array mapping language code to localisation value, such as is
792 * found in extension *.i18n.php files, iterate through a fallback sequence
793 * to merge the given data with an existing primary value.
795 * Returns true if any data from the extension array was used, false
797 * @param array $codeSequence
799 * @param mixed &$value
800 * @param mixed $fallbackValue
803 protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
805 foreach ( $codeSequence as $code ) {
806 if ( isset( $fallbackValue[$code] ) ) {
807 $this->mergeItem( $key, $value, $fallbackValue[$code] );
816 * Gets the combined list of messages dirs from
817 * core and extensions
822 public function getMessagesDirs() {
826 'core' => "$IP/languages/i18n",
827 'exif' => "$IP/languages/i18n/exif",
828 'api' => "$IP/includes/api/i18n",
829 'oojs-ui' => "$IP/resources/lib/ooui/i18n",
830 ] +
$this->options
->get( 'MessagesDirs' );
834 * Load localisation data for a given language for both core and extensions
835 * and save it to the persistent cache store and the process cache
836 * @param string $code
837 * @throws MWException
839 public function recache( $code ) {
841 throw new MWException( "Invalid language code requested" );
843 $this->recachedLangs
[ $code ] = true;
846 $initialData = array_fill_keys( self
::$allKeys, null );
847 $coreData = $initialData;
850 # Load the primary localisation from the source file
851 $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
852 $this->logger
->debug( __METHOD__
. ": got localisation for $code from source" );
854 # Merge primary localisation
855 foreach ( $data as $key => $value ) {
856 $this->mergeItem( $key, $coreData[ $key ], $value );
859 # Fill in the fallback if it's not there already
860 if ( ( is_null( $coreData['fallback'] ) ||
$coreData['fallback'] === false ) && $code === 'en' ) {
861 $coreData['fallback'] = false;
862 $coreData['originalFallbackSequence'] = $coreData['fallbackSequence'] = [];
864 if ( !is_null( $coreData['fallback'] ) ) {
865 $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
867 $coreData['fallbackSequence'] = [];
869 $len = count( $coreData['fallbackSequence'] );
871 # Before we add the 'en' fallback for messages, keep a copy of
872 # the original fallback sequence
873 $coreData['originalFallbackSequence'] = $coreData['fallbackSequence'];
875 # Ensure that the sequence ends at 'en' for messages
876 if ( !$len ||
$coreData['fallbackSequence'][$len - 1] !== 'en' ) {
877 $coreData['fallbackSequence'][] = 'en';
881 $codeSequence = array_merge( [ $code ], $coreData['fallbackSequence'] );
882 $messageDirs = $this->getMessagesDirs();
884 # Load non-JSON localisation data for extensions
885 $extensionData = array_fill_keys( $codeSequence, $initialData );
886 foreach ( $this->options
->get( 'ExtensionMessagesFiles' ) as $extension => $fileName ) {
887 if ( isset( $messageDirs[$extension] ) ) {
888 # This extension has JSON message data; skip the PHP shim
892 $data = $this->readPHPFile( $fileName, 'extension' );
895 foreach ( $data as $key => $item ) {
896 foreach ( $codeSequence as $csCode ) {
897 if ( isset( $item[$csCode] ) ) {
898 $this->mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] );
905 $deps[] = new FileDependency( $fileName );
909 # Load the localisation data for each fallback, then merge it into the full array
910 $allData = $initialData;
911 foreach ( $codeSequence as $csCode ) {
912 $csData = $initialData;
914 # Load core messages and the extension localisations.
915 foreach ( $messageDirs as $dirs ) {
916 foreach ( (array)$dirs as $dir ) {
917 $fileName = "$dir/$csCode.json";
918 $data = $this->readJSONFile( $fileName );
920 foreach ( $data as $key => $item ) {
921 $this->mergeItem( $key, $csData[$key], $item );
924 $deps[] = new FileDependency( $fileName );
928 # Merge non-JSON extension data
929 if ( isset( $extensionData[$csCode] ) ) {
930 foreach ( $extensionData[$csCode] as $key => $item ) {
931 $this->mergeItem( $key, $csData[$key], $item );
935 if ( $csCode === $code ) {
936 # Merge core data into extension data
937 foreach ( $coreData as $key => $item ) {
938 $this->mergeItem( $key, $csData[$key], $item );
941 # Load the secondary localisation from the source file to
942 # avoid infinite cycles on cyclic fallbacks
943 $fbData = $this->readSourceFilesAndRegisterDeps( $csCode, $deps );
944 # Only merge the keys that make sense to merge
945 foreach ( self
::$allKeys as $key ) {
946 if ( !isset( $fbData[ $key ] ) ) {
950 if ( is_null( $coreData[ $key ] ) ||
$this->isMergeableKey( $key ) ) {
951 $this->mergeItem( $key, $csData[ $key ], $fbData[ $key ] );
956 # Allow extensions an opportunity to adjust the data for this
958 Hooks
::run( 'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
960 # Merge the data for this fallback into the final array
961 if ( $csCode === $code ) {
964 foreach ( self
::$allKeys as $key ) {
965 if ( !isset( $csData[$key] ) ) {
969 if ( is_null( $allData[$key] ) ||
$this->isMergeableKey( $key ) ) {
970 $this->mergeItem( $key, $allData[$key], $csData[$key] );
976 # Add cache dependencies for any referenced globals
977 $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
978 // The 'MessagesDirs' config setting is used in LocalisationCache::getMessagesDirs().
979 // We use the key 'wgMessagesDirs' for historical reasons.
980 $deps['wgMessagesDirs'] = new MainConfigDependency( 'MessagesDirs' );
981 $deps['version'] = new ConstantDependency( 'LocalisationCache::VERSION' );
983 # Add dependencies to the cache entry
984 $allData['deps'] = $deps;
986 # Replace spaces with underscores in namespace names
987 $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
989 # And do the same for special page aliases. $page is an array.
990 foreach ( $allData['specialPageAliases'] as &$page ) {
991 $page = str_replace( ' ', '_', $page );
993 # Decouple the reference to prevent accidental damage
996 # If there were no plural rules, return an empty array
997 if ( $allData['pluralRules'] === null ) {
998 $allData['pluralRules'] = [];
1000 if ( $allData['compiledPluralRules'] === null ) {
1001 $allData['compiledPluralRules'] = [];
1003 # If there were no plural rule types, return an empty array
1004 if ( $allData['pluralRuleTypes'] === null ) {
1005 $allData['pluralRuleTypes'] = [];
1009 $allData['list'] = [];
1010 foreach ( self
::$splitKeys as $key ) {
1011 $allData['list'][$key] = array_keys( $allData[$key] );
1014 $unused = true; // Used to be $purgeBlobs, removed in 1.34
1015 Hooks
::run( 'LocalisationCacheRecache', [ $this, $code, &$allData, &$unused ] );
1017 if ( is_null( $allData['namespaceNames'] ) ) {
1018 throw new MWException( __METHOD__
. ': Localisation data failed sanity check! ' .
1019 'Check that your languages/messages/MessagesEn.php file is intact.' );
1022 # Set the preload key
1023 $allData['preload'] = $this->buildPreload( $allData );
1025 # Save to the process cache and register the items loaded
1026 $this->data
[$code] = $allData;
1027 foreach ( $allData as $key => $item ) {
1028 $this->loadedItems
[$code][$key] = true;
1031 # Save to the persistent cache
1032 $this->store
->startWrite( $code );
1033 foreach ( $allData as $key => $value ) {
1034 if ( in_array( $key, self
::$splitKeys ) ) {
1035 foreach ( $value as $subkey => $subvalue ) {
1036 $this->store
->set( "$key:$subkey", $subvalue );
1039 $this->store
->set( $key, $value );
1042 $this->store
->finishWrite();
1044 # Clear out the MessageBlobStore
1045 # HACK: If using a null (i.e. disabled) storage backend, we
1046 # can't write to the MessageBlobStore either
1047 if ( !$this->store
instanceof LCStoreNull
) {
1048 foreach ( $this->clearStoreCallbacks
as $callback ) {
1055 * Build the preload item from the given pre-cache data.
1057 * The preload item will be loaded automatically, improving performance
1058 * for the commonly-requested items it contains.
1059 * @param array $data
1062 protected function buildPreload( $data ) {
1063 $preload = [ 'messages' => [] ];
1064 foreach ( self
::$preloadedKeys as $key ) {
1065 $preload[$key] = $data[$key];
1068 foreach ( $data['preloadedMessages'] as $subkey ) {
1069 $subitem = $data['messages'][$subkey] ??
null;
1070 $preload['messages'][$subkey] = $subitem;
1077 * Unload the data for a given language from the object cache.
1078 * Reduces memory usage.
1079 * @param string $code
1081 public function unload( $code ) {
1082 unset( $this->data
[$code] );
1083 unset( $this->loadedItems
[$code] );
1084 unset( $this->loadedSubitems
[$code] );
1085 unset( $this->initialisedLangs
[$code] );
1086 unset( $this->shallowFallbacks
[$code] );
1088 foreach ( $this->shallowFallbacks
as $shallowCode => $fbCode ) {
1089 if ( $fbCode === $code ) {
1090 $this->unload( $shallowCode );
1098 public function unloadAll() {
1099 foreach ( $this->initialisedLangs
as $lang => $unused ) {
1100 $this->unload( $lang );
1105 * Disable the storage backend
1107 public function disableBackend() {
1108 $this->store
= new LCStoreNull
;
1109 $this->manualRecache
= false;