23 use CLDRPluralRuleParser\Evaluator;
24 use CLDRPluralRuleParser\Error
as CLDRPluralRuleError;
110 'fallback',
'namespaceNames',
'bookstoreList',
111 'magicWords',
'messages',
'rtl',
'capitalizeAllNouns',
'digitTransformTable',
112 'separatorTransformTable',
'minimumGroupingDigits',
113 'fallback8bitEncoding',
'linkPrefixExtension',
114 'linkTrail',
'linkPrefixCharset',
'namespaceAliases',
115 'dateFormats',
'datePreferences',
'datePreferenceMigrationMap',
116 'defaultDateFormat',
'extraUserToggles',
'specialPageAliases',
117 'imageFiles',
'preloadedMessages',
'namespaceGenderAliases',
118 'digitGroupingPattern',
'pluralRules',
'pluralRuleTypes',
'compiledPluralRules',
126 'namespaceAliases',
'dateFormats',
'imageFiles',
'preloadedMessages'
196 if ( !empty(
$conf[
'storeClass'] ) ) {
197 $storeClass =
$conf[
'storeClass'];
199 switch (
$conf[
'store'] ) {
211 if ( !empty(
$conf[
'storeDirectory'] ) ) {
222 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
228 if ( !empty(
$conf[
'storeDirectory'] ) ) {
229 $storeConf[
'directory'] =
$conf[
'storeDirectory'];
232 $this->
store =
new $storeClass( $storeConf );
233 foreach ( [
'manualRecache',
'forceRecache' ]
as $var ) {
234 if ( isset(
$conf[$var] ) ) {
235 $this->$var =
$conf[$var];
247 if ( $this->mergeableKeys ===
null ) {
248 $this->mergeableKeys = array_flip( array_merge(
249 self::$mergeableMapKeys,
250 self::$mergeableListKeys,
251 self::$mergeableAliasListKeys,
252 self::$optionalMergeKeys,
257 return isset( $this->mergeableKeys[$key] );
270 if ( !isset( $this->loadedItems[
$code][$key] ) ) {
274 if ( $key ===
'fallback' && isset( $this->shallowFallbacks[
$code] ) ) {
275 return $this->shallowFallbacks[
$code];
289 if ( !isset( $this->loadedSubitems[
$code][$key][$subkey] ) &&
290 !isset( $this->loadedItems[
$code][$key] )
295 if ( isset( $this->
data[
$code][$key][$subkey] ) ) {
296 return $this->
data[
$code][$key][$subkey];
315 if ( in_array( $key, self::$splitKeys ) ) {
319 if ( is_array( $item ) ) {
320 return array_keys( $item );
333 if ( !isset( $this->initialisedLangs[
$code] ) ) {
338 if ( isset( $this->loadedItems[
$code][$key] ) ) {
342 if ( isset( $this->shallowFallbacks[
$code] ) ) {
348 if ( in_array( $key, self::$splitKeys ) ) {
349 $subkeyList = $this->
getSubitem( $code,
'list', $key );
350 foreach ( $subkeyList
as $subkey ) {
351 if ( isset( $this->
data[$code][$key][$subkey] ) ) {
360 $this->loadedItems[
$code][$key] =
true;
370 if ( !in_array( $key, self::$splitKeys ) ) {
376 if ( !isset( $this->initialisedLangs[
$code] ) ) {
381 if ( isset( $this->loadedItems[
$code][$key] ) ||
382 isset( $this->loadedSubitems[
$code][$key][$subkey] )
387 if ( isset( $this->shallowFallbacks[
$code] ) ) {
393 $value = $this->
store->get( $code,
"$key:$subkey" );
395 $this->loadedSubitems[
$code][$key][$subkey] =
true;
406 if ( $this->forceRecache && !isset( $this->recachedLangs[
$code] ) ) {
407 wfDebug( __METHOD__ .
"($code): forced reload\n" );
412 $deps = $this->
store->get( $code,
'deps' );
414 $preload = $this->
store->get( $code,
'preload' );
416 if ( $deps ===
null ||
$keys ===
null || $preload ===
null ) {
417 wfDebug( __METHOD__ .
"($code): cache missing, need to make one\n" );
422 foreach ( $deps
as $dep ) {
428 wfDebug( __METHOD__ .
"($code): cache for $code expired due to " .
429 get_class( $dep ) .
"\n" );
444 if ( isset( $this->initialisedLangs[
$code] ) ) {
448 $this->initialisedLangs[
$code] =
true;
450 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
457 # Recache the data if necessary
458 if ( !$this->manualRecache && $this->
isExpired( $code ) ) {
461 } elseif (
$code ===
'en' ) {
462 throw new MWException(
'MessagesEn.php is missing.' );
471 $preload = $this->
getItem( $code,
'preload' );
472 if ( $preload ===
null ) {
473 if ( $this->manualRecache ) {
475 if (
$code ===
'en' ) {
476 throw new MWException(
'No localisation cache found for English. ' .
477 'Please run maintenance/rebuildLocalisationCache.php.' );
483 throw new MWException(
'Invalid or missing localisation cache.' );
487 foreach ( $preload
as $key => $item ) {
488 if ( in_array( $key, self::$splitKeys ) ) {
489 foreach ( $item
as $subkey => $subitem ) {
490 $this->loadedSubitems[
$code][$key][$subkey] =
true;
493 $this->loadedItems[
$code][$key] =
true;
505 $this->
data[$primaryCode] =& $this->
data[$fallbackCode];
506 $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
507 $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
508 $this->shallowFallbacks[$primaryCode] = $fallbackCode;
520 Wikimedia\suppressWarnings();
521 $_apcEnabled = ini_set(
'apc.cache_by_default',
'0' );
522 Wikimedia\restoreWarnings();
526 Wikimedia\suppressWarnings();
527 ini_set(
'apc.cache_by_default', $_apcEnabled );
528 Wikimedia\restoreWarnings();
531 if ( $_fileType ==
'core' || $_fileType ==
'extension' ) {
532 foreach ( self::$allKeys
as $key ) {
535 if ( isset( $$key ) ) {
539 } elseif ( $_fileType ==
'aliases' ) {
540 if ( isset( $aliases ) ) {
542 $data[
'aliases'] = $aliases;
545 throw new MWException( __METHOD__ .
": Invalid file type: $_fileType" );
558 if ( !is_readable( $fileName ) ) {
562 $json = file_get_contents( $fileName );
563 if ( $json ===
false ) {
568 if (
$data ===
null ) {
569 throw new MWException( __METHOD__ .
": Invalid JSON file: $fileName" );
573 foreach (
$data as $key => $unused ) {
574 if ( $key ===
'' || $key[0] ===
'@' ) {
575 unset(
$data[$key] );
580 return [
'messages' =>
$data ];
591 if ( $rules ===
null ) {
595 $compiledRules = Evaluator::compile( $rules );
596 }
catch ( CLDRPluralRuleError
$e ) {
602 return $compiledRules;
613 if ( $this->pluralRules ===
null ) {
616 if ( !isset( $this->pluralRules[
$code] ) ) {
619 return $this->pluralRules[
$code];
631 if ( $this->pluralRuleTypes ===
null ) {
634 if ( !isset( $this->pluralRuleTypes[
$code] ) ) {
637 return $this->pluralRuleTypes[
$code];
646 $cldrPlural =
"$IP/languages/data/plurals.xml";
647 $mwPlural =
"$IP/languages/data/plurals-mediawiki.xml";
650 if ( file_exists( $mwPlural ) ) {
665 $xml = file_get_contents( $fileName );
667 throw new MWException(
"Unable to read plurals file $fileName" );
669 $doc =
new DOMDocument;
670 $doc->loadXML( $xml );
671 $rulesets = $doc->getElementsByTagName(
"pluralRules" );
672 foreach ( $rulesets
as $ruleset ) {
673 $codes = $ruleset->getAttribute(
'locales' );
676 $ruleElements = $ruleset->getElementsByTagName(
"pluralRule" );
677 foreach ( $ruleElements
as $elt ) {
678 $ruleType = $elt->getAttribute(
'count' );
679 if ( $ruleType ===
'other' ) {
683 $rules[] = $elt->nodeValue;
684 $ruleTypes[] = $ruleType;
686 foreach ( explode(
' ', $codes )
as $code ) {
687 $this->pluralRules[
$code] = $rules;
688 $this->pluralRuleTypes[
$code] = $ruleTypes;
707 if ( !file_exists( $fileName ) ) {
714 # Load CLDR plural rules for JavaScript
718 # Load plural rule types
721 $deps[
'plurals'] =
new FileDependency(
"$IP/languages/data/plurals.xml" );
722 $deps[
'plurals-mw'] =
new FileDependency(
"$IP/languages/data/plurals-mediawiki.xml" );
735 if ( !is_null(
$value ) ) {
736 if ( !is_null( $fallbackValue ) ) {
737 if ( in_array( $key, self::$mergeableMapKeys ) ) {
739 } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
740 $value = array_unique( array_merge( $fallbackValue,
$value ) );
741 } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
742 $value = array_merge_recursive(
$value, $fallbackValue );
743 } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
744 if ( !empty(
$value[
'inherit'] ) ) {
748 if ( isset(
$value[
'inherit'] ) ) {
749 unset(
$value[
'inherit'] );
751 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
765 foreach ( $fallbackValue
as $magicName => $fallbackInfo ) {
766 if ( !isset(
$value[$magicName] ) ) {
767 $value[$magicName] = $fallbackInfo;
769 $oldSynonyms = array_slice( $fallbackInfo, 1 );
770 $newSynonyms = array_slice(
$value[$magicName], 1 );
771 $synonyms = array_values( array_unique( array_merge(
772 $newSynonyms, $oldSynonyms ) ) );
773 $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
793 foreach ( $codeSequence
as $code ) {
794 if ( isset( $fallbackValue[
$code] ) ) {
813 $config = MediaWikiServices::getInstance()->getMainConfig();
814 $messagesDirs = $config->get(
'MessagesDirs' );
816 'core' =>
"$IP/languages/i18n",
817 'api' =>
"$IP/includes/api/i18n",
818 'oojs-ui' =>
"$IP/resources/lib/oojs-ui/i18n",
832 throw new MWException(
"Invalid language code requested" );
834 $this->recachedLangs[
$code] =
true;
837 $initialData = array_fill_keys( self::$allKeys,
null );
838 $coreData = $initialData;
841 # Load the primary localisation from the source file
843 if (
$data ===
false ) {
844 wfDebug( __METHOD__ .
": no localisation file for $code, using fallback to en\n" );
845 $coreData[
'fallback'] =
'en';
847 wfDebug( __METHOD__ .
": got localisation for $code from source\n" );
849 # Merge primary localisation
855 # Fill in the fallback if it's not there already
856 if ( is_null( $coreData[
'fallback'] ) ) {
857 $coreData[
'fallback'] =
$code ===
'en' ?
false :
'en';
859 if ( $coreData[
'fallback'] ===
false ) {
860 $coreData[
'fallbackSequence'] = [];
862 $coreData[
'fallbackSequence'] = array_map(
'trim', explode(
',', $coreData[
'fallback'] ) );
863 $len = count( $coreData[
'fallbackSequence'] );
865 # Ensure that the sequence ends at en
866 if ( $coreData[
'fallbackSequence'][$len - 1] !==
'en' ) {
867 $coreData[
'fallbackSequence'][] =
'en';
871 $codeSequence = array_merge( [
$code ], $coreData[
'fallbackSequence'] );
874 # Load non-JSON localisation data for extensions
875 $extensionData = array_fill_keys( $codeSequence, $initialData );
877 if ( isset( $messageDirs[$extension] ) ) {
878 # This extension has JSON message data; skip the PHP shim
885 foreach (
$data as $key => $item ) {
886 foreach ( $codeSequence
as $csCode ) {
887 if ( isset( $item[$csCode] ) ) {
888 $this->
mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] );
899 # Load the localisation data for each fallback, then merge it into the full array
900 $allData = $initialData;
901 foreach ( $codeSequence
as $csCode ) {
902 $csData = $initialData;
904 # Load core messages and the extension localisations.
905 foreach ( $messageDirs
as $dirs ) {
907 $fileName =
"$dir/$csCode.json";
910 foreach (
$data as $key => $item ) {
911 $this->
mergeItem( $key, $csData[$key], $item );
918 # Merge non-JSON extension data
919 if ( isset( $extensionData[$csCode] ) ) {
920 foreach ( $extensionData[$csCode]
as $key => $item ) {
921 $this->
mergeItem( $key, $csData[$key], $item );
925 if ( $csCode ===
$code ) {
926 # Merge core data into extension data
927 foreach ( $coreData
as $key => $item ) {
928 $this->
mergeItem( $key, $csData[$key], $item );
931 # Load the secondary localisation from the source file to
932 # avoid infinite cycles on cyclic fallbacks
934 if ( $fbData !==
false ) {
935 # Only merge the keys that make sense to merge
936 foreach ( self::$allKeys
as $key ) {
937 if ( !isset( $fbData[$key] ) ) {
941 if ( is_null( $coreData[$key] ) || $this->
isMergeableKey( $key ) ) {
942 $this->
mergeItem( $key, $csData[$key], $fbData[$key] );
948 # Allow extensions an opportunity to adjust the data for this
950 Hooks::run(
'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
952 # Merge the data for this fallback into the final array
953 if ( $csCode ===
$code ) {
956 foreach ( self::$allKeys
as $key ) {
957 if ( !isset( $csData[$key] ) ) {
961 if ( is_null( $allData[$key] ) || $this->
isMergeableKey( $key ) ) {
962 $this->
mergeItem( $key, $allData[$key], $csData[$key] );
968 # Add cache dependencies for any referenced globals
969 $deps[
'wgExtensionMessagesFiles'] =
new GlobalDependency(
'wgExtensionMessagesFiles' );
975 # Add dependencies to the cache entry
976 $allData[
'deps'] = $deps;
978 # Replace spaces with underscores in namespace names
979 $allData[
'namespaceNames'] = str_replace(
' ',
'_', $allData[
'namespaceNames'] );
981 # And do the same for special page aliases. $page is an array.
982 foreach ( $allData[
'specialPageAliases']
as &$page ) {
983 $page = str_replace(
' ',
'_', $page );
985 # Decouple the reference to prevent accidental damage
988 # If there were no plural rules, return an empty array
989 if ( $allData[
'pluralRules'] ===
null ) {
990 $allData[
'pluralRules'] = [];
992 if ( $allData[
'compiledPluralRules'] ===
null ) {
993 $allData[
'compiledPluralRules'] = [];
995 # If there were no plural rule types, return an empty array
996 if ( $allData[
'pluralRuleTypes'] ===
null ) {
997 $allData[
'pluralRuleTypes'] = [];
1001 $allData[
'list'] = [];
1002 foreach ( self::$splitKeys
as $key ) {
1003 $allData[
'list'][$key] = array_keys( $allData[$key] );
1007 Hooks::run(
'LocalisationCacheRecache', [ $this,
$code, &$allData, &$purgeBlobs ] );
1009 if ( is_null( $allData[
'namespaceNames'] ) ) {
1010 throw new MWException( __METHOD__ .
': Localisation data failed sanity check! ' .
1011 'Check that your languages/messages/MessagesEn.php file is intact.' );
1014 # Set the preload key
1017 # Save to the process cache and register the items loaded
1019 foreach ( $allData
as $key => $item ) {
1020 $this->loadedItems[
$code][$key] =
true;
1023 # Save to the persistent cache
1025 foreach ( $allData
as $key =>
$value ) {
1026 if ( in_array( $key, self::$splitKeys ) ) {
1027 foreach (
$value as $subkey => $subvalue ) {
1028 $this->
store->set(
"$key:$subkey", $subvalue );
1034 $this->
store->finishWrite();
1036 # Clear out the MessageBlobStore
1037 # HACK: If using a null (i.e. disabled) storage backend, we
1038 # can't write to the MessageBlobStore either
1041 $blobStore->clear();
1054 $preload = [
'messages' => [] ];
1055 foreach ( self::$preloadedKeys
as $key ) {
1056 $preload[$key] =
$data[$key];
1059 foreach (
$data[
'preloadedMessages']
as $subkey ) {
1060 if ( isset(
$data[
'messages'][$subkey] ) ) {
1061 $subitem =
$data[
'messages'][$subkey];
1065 $preload[
'messages'][$subkey] = $subitem;
1078 unset( $this->loadedItems[
$code] );
1079 unset( $this->loadedSubitems[
$code] );
1080 unset( $this->initialisedLangs[
$code] );
1081 unset( $this->shallowFallbacks[
$code] );
1083 foreach ( $this->shallowFallbacks
as $shallowCode => $fbCode ) {
1084 if ( $fbCode ===
$code ) {
1085 $this->
unload( $shallowCode );
1094 foreach ( $this->initialisedLangs
as $lang => $unused ) {
1104 $this->manualRecache =
false;