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'] ) {
206 $storeConf[
'server'] =
$conf[
'storeServer'] ?? [];
212 if ( !empty(
$conf[
'storeDirectory'] ) ) {
219 $storeConf[
'server'] =
$conf[
'storeServer'] ?? [];
224 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
230 if ( !empty(
$conf[
'storeDirectory'] ) ) {
231 $storeConf[
'directory'] =
$conf[
'storeDirectory'];
234 $this->
store =
new $storeClass( $storeConf );
235 foreach ( [
'manualRecache',
'forceRecache' ]
as $var ) {
236 if ( isset(
$conf[$var] ) ) {
237 $this->$var =
$conf[$var];
249 if ( $this->mergeableKeys ===
null ) {
250 $this->mergeableKeys = array_flip( array_merge(
251 self::$mergeableMapKeys,
252 self::$mergeableListKeys,
253 self::$mergeableAliasListKeys,
254 self::$optionalMergeKeys,
259 return isset( $this->mergeableKeys[$key] );
272 if ( !isset( $this->loadedItems[
$code][$key] ) ) {
276 if ( $key ===
'fallback' && isset( $this->shallowFallbacks[
$code] ) ) {
277 return $this->shallowFallbacks[
$code];
291 if ( !isset( $this->loadedSubitems[
$code][$key][$subkey] ) &&
292 !isset( $this->loadedItems[
$code][$key] )
297 return $this->
data[
$code][$key][$subkey] ??
null;
313 if ( in_array( $key, self::$splitKeys ) ) {
317 if ( is_array( $item ) ) {
318 return array_keys( $item );
331 if ( !isset( $this->initialisedLangs[
$code] ) ) {
336 if ( isset( $this->loadedItems[
$code][$key] ) ) {
340 if ( isset( $this->shallowFallbacks[
$code] ) ) {
346 if ( in_array( $key, self::$splitKeys ) ) {
347 $subkeyList = $this->
getSubitem( $code,
'list', $key );
348 foreach ( $subkeyList
as $subkey ) {
349 if ( isset( $this->
data[$code][$key][$subkey] ) ) {
358 $this->loadedItems[
$code][$key] =
true;
368 if ( !in_array( $key, self::$splitKeys ) ) {
374 if ( !isset( $this->initialisedLangs[
$code] ) ) {
379 if ( isset( $this->loadedItems[
$code][$key] ) ||
380 isset( $this->loadedSubitems[
$code][$key][$subkey] )
385 if ( isset( $this->shallowFallbacks[
$code] ) ) {
391 $value = $this->
store->get( $code,
"$key:$subkey" );
393 $this->loadedSubitems[
$code][$key][$subkey] =
true;
404 if ( $this->forceRecache && !isset( $this->recachedLangs[
$code] ) ) {
405 wfDebug( __METHOD__ .
"($code): forced reload\n" );
410 $deps = $this->
store->get( $code,
'deps' );
412 $preload = $this->
store->get( $code,
'preload' );
414 if ( $deps ===
null ||
$keys ===
null || $preload ===
null ) {
415 wfDebug( __METHOD__ .
"($code): cache missing, need to make one\n" );
420 foreach ( $deps
as $dep ) {
426 wfDebug( __METHOD__ .
"($code): cache for $code expired due to " .
427 get_class( $dep ) .
"\n" );
442 if ( isset( $this->initialisedLangs[
$code] ) ) {
446 $this->initialisedLangs[
$code] =
true;
448 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
455 # Recache the data if necessary
456 if ( !$this->manualRecache && $this->
isExpired( $code ) ) {
459 } elseif (
$code ===
'en' ) {
460 throw new MWException(
'MessagesEn.php is missing.' );
469 $preload = $this->
getItem( $code,
'preload' );
470 if ( $preload ===
null ) {
471 if ( $this->manualRecache ) {
473 if (
$code ===
'en' ) {
474 throw new MWException(
'No localisation cache found for English. ' .
475 'Please run maintenance/rebuildLocalisationCache.php.' );
481 throw new MWException(
'Invalid or missing localisation cache.' );
485 foreach ( $preload
as $key => $item ) {
486 if ( in_array( $key, self::$splitKeys ) ) {
487 foreach ( $item
as $subkey => $subitem ) {
488 $this->loadedSubitems[
$code][$key][$subkey] =
true;
491 $this->loadedItems[
$code][$key] =
true;
503 $this->
data[$primaryCode] =& $this->
data[$fallbackCode];
504 $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
505 $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
506 $this->shallowFallbacks[$primaryCode] = $fallbackCode;
518 Wikimedia\suppressWarnings();
519 $_apcEnabled = ini_set(
'apc.cache_by_default',
'0' );
520 Wikimedia\restoreWarnings();
524 Wikimedia\suppressWarnings();
525 ini_set(
'apc.cache_by_default', $_apcEnabled );
526 Wikimedia\restoreWarnings();
529 if ( $_fileType ==
'core' || $_fileType ==
'extension' ) {
530 foreach ( self::$allKeys
as $key ) {
533 if ( isset( $$key ) ) {
537 } elseif ( $_fileType ==
'aliases' ) {
538 if ( isset( $aliases ) ) {
540 $data[
'aliases'] = $aliases;
543 throw new MWException( __METHOD__ .
": Invalid file type: $_fileType" );
556 if ( !is_readable( $fileName ) ) {
560 $json = file_get_contents( $fileName );
561 if ( $json ===
false ) {
566 if (
$data ===
null ) {
567 throw new MWException( __METHOD__ .
": Invalid JSON file: $fileName" );
571 foreach (
$data as $key => $unused ) {
572 if ( $key ===
'' || $key[0] ===
'@' ) {
573 unset(
$data[$key] );
578 return [
'messages' =>
$data ];
589 if ( $rules ===
null ) {
593 $compiledRules = Evaluator::compile( $rules );
594 }
catch ( CLDRPluralRuleError
$e ) {
600 return $compiledRules;
611 if ( $this->pluralRules ===
null ) {
614 return $this->pluralRules[
$code] ??
null;
625 if ( $this->pluralRuleTypes ===
null ) {
628 return $this->pluralRuleTypes[
$code] ??
null;
636 $cldrPlural =
"$IP/languages/data/plurals.xml";
637 $mwPlural =
"$IP/languages/data/plurals-mediawiki.xml";
640 if ( file_exists( $mwPlural ) ) {
655 $xml = file_get_contents( $fileName );
657 throw new MWException(
"Unable to read plurals file $fileName" );
659 $doc =
new DOMDocument;
660 $doc->loadXML( $xml );
661 $rulesets = $doc->getElementsByTagName(
"pluralRules" );
662 foreach ( $rulesets
as $ruleset ) {
663 $codes = $ruleset->getAttribute(
'locales' );
666 $ruleElements = $ruleset->getElementsByTagName(
"pluralRule" );
667 foreach ( $ruleElements
as $elt ) {
668 $ruleType = $elt->getAttribute(
'count' );
669 if ( $ruleType ===
'other' ) {
673 $rules[] = $elt->nodeValue;
674 $ruleTypes[] = $ruleType;
676 foreach ( explode(
' ', $codes )
as $code ) {
677 $this->pluralRules[
$code] = $rules;
678 $this->pluralRuleTypes[
$code] = $ruleTypes;
697 if ( !file_exists( $fileName ) ) {
704 # Load CLDR plural rules for JavaScript
708 # Load plural rule types
711 $deps[
'plurals'] =
new FileDependency(
"$IP/languages/data/plurals.xml" );
712 $deps[
'plurals-mw'] =
new FileDependency(
"$IP/languages/data/plurals-mediawiki.xml" );
725 if ( !is_null(
$value ) ) {
726 if ( !is_null( $fallbackValue ) ) {
727 if ( in_array( $key, self::$mergeableMapKeys ) ) {
729 } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
730 $value = array_unique( array_merge( $fallbackValue,
$value ) );
731 } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
732 $value = array_merge_recursive(
$value, $fallbackValue );
733 } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
734 if ( !empty(
$value[
'inherit'] ) ) {
738 if ( isset(
$value[
'inherit'] ) ) {
739 unset(
$value[
'inherit'] );
741 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
755 foreach ( $fallbackValue
as $magicName => $fallbackInfo ) {
756 if ( !isset(
$value[$magicName] ) ) {
757 $value[$magicName] = $fallbackInfo;
759 $oldSynonyms = array_slice( $fallbackInfo, 1 );
760 $newSynonyms = array_slice(
$value[$magicName], 1 );
761 $synonyms = array_values( array_unique( array_merge(
762 $newSynonyms, $oldSynonyms ) ) );
763 $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
783 foreach ( $codeSequence
as $code ) {
784 if ( isset( $fallbackValue[
$code] ) ) {
803 $config = MediaWikiServices::getInstance()->getMainConfig();
804 $messagesDirs = $config->get(
'MessagesDirs' );
806 'core' =>
"$IP/languages/i18n",
807 'api' =>
"$IP/includes/api/i18n",
808 'oojs-ui' =>
"$IP/resources/lib/ooui/i18n",
822 throw new MWException(
"Invalid language code requested" );
824 $this->recachedLangs[
$code] =
true;
827 $initialData = array_fill_keys( self::$allKeys,
null );
828 $coreData = $initialData;
831 # Load the primary localisation from the source file
833 if (
$data ===
false ) {
834 wfDebug( __METHOD__ .
": no localisation file for $code, using fallback to en\n" );
835 $coreData[
'fallback'] =
'en';
837 wfDebug( __METHOD__ .
": got localisation for $code from source\n" );
839 # Merge primary localisation
845 # Fill in the fallback if it's not there already
846 if ( ( is_null( $coreData[
'fallback'] ) || $coreData[
'fallback'] ===
false ) &&
$code ===
'en' ) {
847 $coreData[
'fallback'] =
false;
848 $coreData[
'originalFallbackSequence'] = $coreData[
'fallbackSequence'] = [];
850 if ( !is_null( $coreData[
'fallback'] ) ) {
851 $coreData[
'fallbackSequence'] = array_map(
'trim', explode(
',', $coreData[
'fallback'] ) );
853 $coreData[
'fallbackSequence'] = [];
855 $len =
count( $coreData[
'fallbackSequence'] );
857 # Before we add the 'en' fallback for messages, keep a copy of
858 # the original fallback sequence
859 $coreData[
'originalFallbackSequence'] = $coreData[
'fallbackSequence'];
861 # Ensure that the sequence ends at 'en' for messages
862 if ( !$len || $coreData[
'fallbackSequence'][$len - 1] !==
'en' ) {
863 $coreData[
'fallbackSequence'][] =
'en';
867 $codeSequence = array_merge( [
$code ], $coreData[
'fallbackSequence'] );
870 # Load non-JSON localisation data for extensions
871 $extensionData = array_fill_keys( $codeSequence, $initialData );
873 if ( isset( $messageDirs[$extension] ) ) {
874 # This extension has JSON message data; skip the PHP shim
881 foreach (
$data as $key => $item ) {
882 foreach ( $codeSequence
as $csCode ) {
883 if ( isset( $item[$csCode] ) ) {
884 $this->
mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] );
895 # Load the localisation data for each fallback, then merge it into the full array
896 $allData = $initialData;
897 foreach ( $codeSequence
as $csCode ) {
898 $csData = $initialData;
900 # Load core messages and the extension localisations.
901 foreach ( $messageDirs
as $dirs ) {
903 $fileName =
"$dir/$csCode.json";
906 foreach (
$data as $key => $item ) {
907 $this->
mergeItem( $key, $csData[$key], $item );
914 # Merge non-JSON extension data
915 if ( isset( $extensionData[$csCode] ) ) {
916 foreach ( $extensionData[$csCode]
as $key => $item ) {
917 $this->
mergeItem( $key, $csData[$key], $item );
921 if ( $csCode ===
$code ) {
922 # Merge core data into extension data
923 foreach ( $coreData
as $key => $item ) {
924 $this->
mergeItem( $key, $csData[$key], $item );
927 # Load the secondary localisation from the source file to
928 # avoid infinite cycles on cyclic fallbacks
930 if ( $fbData !==
false ) {
931 # Only merge the keys that make sense to merge
932 foreach ( self::$allKeys
as $key ) {
933 if ( !isset( $fbData[$key] ) ) {
937 if ( is_null( $coreData[$key] ) || $this->
isMergeableKey( $key ) ) {
938 $this->
mergeItem( $key, $csData[$key], $fbData[$key] );
944 # Allow extensions an opportunity to adjust the data for this
946 Hooks::run(
'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
948 # Merge the data for this fallback into the final array
949 if ( $csCode ===
$code ) {
952 foreach ( self::$allKeys
as $key ) {
953 if ( !isset( $csData[$key] ) ) {
957 if ( is_null( $allData[$key] ) || $this->
isMergeableKey( $key ) ) {
958 $this->
mergeItem( $key, $allData[$key], $csData[$key] );
964 # Add cache dependencies for any referenced globals
965 $deps[
'wgExtensionMessagesFiles'] =
new GlobalDependency(
'wgExtensionMessagesFiles' );
971 # Add dependencies to the cache entry
972 $allData[
'deps'] = $deps;
974 # Replace spaces with underscores in namespace names
975 $allData[
'namespaceNames'] = str_replace(
' ',
'_', $allData[
'namespaceNames'] );
977 # And do the same for special page aliases. $page is an array.
978 foreach ( $allData[
'specialPageAliases']
as &$page ) {
979 $page = str_replace(
' ',
'_', $page );
981 # Decouple the reference to prevent accidental damage
984 # If there were no plural rules, return an empty array
985 if ( $allData[
'pluralRules'] ===
null ) {
986 $allData[
'pluralRules'] = [];
988 if ( $allData[
'compiledPluralRules'] ===
null ) {
989 $allData[
'compiledPluralRules'] = [];
991 # If there were no plural rule types, return an empty array
992 if ( $allData[
'pluralRuleTypes'] ===
null ) {
993 $allData[
'pluralRuleTypes'] = [];
997 $allData[
'list'] = [];
998 foreach ( self::$splitKeys
as $key ) {
999 $allData[
'list'][$key] = array_keys( $allData[$key] );
1003 Hooks::run(
'LocalisationCacheRecache', [ $this,
$code, &$allData, &$purgeBlobs ] );
1005 if ( is_null( $allData[
'namespaceNames'] ) ) {
1006 throw new MWException( __METHOD__ .
': Localisation data failed sanity check! ' .
1007 'Check that your languages/messages/MessagesEn.php file is intact.' );
1010 # Set the preload key
1013 # Save to the process cache and register the items loaded
1015 foreach ( $allData
as $key => $item ) {
1016 $this->loadedItems[
$code][$key] =
true;
1019 # Save to the persistent cache
1021 foreach ( $allData
as $key =>
$value ) {
1022 if ( in_array( $key, self::$splitKeys ) ) {
1023 foreach (
$value as $subkey => $subvalue ) {
1024 $this->
store->set(
"$key:$subkey", $subvalue );
1030 $this->
store->finishWrite();
1032 # Clear out the MessageBlobStore
1033 # HACK: If using a null (i.e. disabled) storage backend, we
1034 # can't write to the MessageBlobStore either
1037 $blobStore->clear();
1050 $preload = [
'messages' => [] ];
1051 foreach ( self::$preloadedKeys
as $key ) {
1052 $preload[$key] =
$data[$key];
1055 foreach (
$data[
'preloadedMessages']
as $subkey ) {
1056 $subitem =
$data[
'messages'][$subkey] ??
null;
1057 $preload[
'messages'][$subkey] = $subitem;
1070 unset( $this->loadedItems[
$code] );
1071 unset( $this->loadedSubitems[
$code] );
1072 unset( $this->initialisedLangs[
$code] );
1073 unset( $this->shallowFallbacks[
$code] );
1075 foreach ( $this->shallowFallbacks
as $shallowCode => $fbCode ) {
1076 if ( $fbCode ===
$code ) {
1077 $this->
unload( $shallowCode );
1086 foreach ( $this->initialisedLangs
as $lang => $unused ) {
1096 $this->manualRecache =
false;