23 use CLDRPluralRuleParser\Evaluator;
24 use CLDRPluralRuleParser\Error
as CLDRPluralRuleError;
110 'fallback',
'namespaceNames',
'bookstoreList',
111 'magicWords',
'messages',
'rtl',
'capitalizeAllNouns',
'digitTransformTable',
112 'separatorTransformTable',
'fallback8bitEncoding',
'linkPrefixExtension',
113 'linkTrail',
'linkPrefixCharset',
'namespaceAliases',
114 'dateFormats',
'datePreferences',
'datePreferenceMigrationMap',
115 'defaultDateFormat',
'extraUserToggles',
'specialPageAliases',
116 'imageFiles',
'preloadedMessages',
'namespaceGenderAliases',
117 'digitGroupingPattern',
'pluralRules',
'pluralRuleTypes',
'compiledPluralRules',
125 'namespaceAliases',
'dateFormats',
'imageFiles',
'preloadedMessages'
195 if ( !empty(
$conf[
'storeClass'] ) ) {
196 $storeClass =
$conf[
'storeClass'];
198 switch (
$conf[
'store'] ) {
201 $storeClass =
'LCStoreCDB';
204 $storeClass =
'LCStoreDB';
207 $storeClass =
'LCStoreStaticArray';
210 if ( !empty(
$conf[
'storeDirectory'] ) ) {
211 $storeClass =
'LCStoreCDB';
214 $storeClass =
'LCStoreCDB';
216 $storeClass =
'LCStoreDB';
221 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
227 if ( !empty(
$conf[
'storeDirectory'] ) ) {
228 $storeConf[
'directory'] =
$conf[
'storeDirectory'];
231 $this->
store =
new $storeClass( $storeConf );
232 foreach ( [
'manualRecache',
'forceRecache' ]
as $var ) {
233 if ( isset(
$conf[$var] ) ) {
234 $this->$var =
$conf[$var];
246 if ( $this->mergeableKeys ===
null ) {
247 $this->mergeableKeys = array_flip( array_merge(
248 self::$mergeableMapKeys,
249 self::$mergeableListKeys,
250 self::$mergeableAliasListKeys,
251 self::$optionalMergeKeys,
256 return isset( $this->mergeableKeys[$key] );
269 if ( !isset( $this->loadedItems[
$code][$key] ) ) {
273 if ( $key ===
'fallback' && isset( $this->shallowFallbacks[
$code] ) ) {
274 return $this->shallowFallbacks[
$code];
288 if ( !isset( $this->loadedSubitems[
$code][$key][$subkey] ) &&
289 !isset( $this->loadedItems[
$code][$key] )
294 if ( isset( $this->
data[
$code][$key][$subkey] ) ) {
295 return $this->
data[
$code][$key][$subkey];
314 if ( in_array( $key, self::$splitKeys ) ) {
318 if ( is_array( $item ) ) {
319 return array_keys( $item );
332 if ( !isset( $this->initialisedLangs[
$code] ) ) {
337 if ( isset( $this->loadedItems[
$code][$key] ) ) {
341 if ( isset( $this->shallowFallbacks[
$code] ) ) {
347 if ( in_array( $key, self::$splitKeys ) ) {
348 $subkeyList = $this->
getSubitem( $code,
'list', $key );
349 foreach ( $subkeyList
as $subkey ) {
350 if ( isset( $this->
data[$code][$key][$subkey] ) ) {
359 $this->loadedItems[
$code][$key] =
true;
369 if ( !in_array( $key, self::$splitKeys ) ) {
375 if ( !isset( $this->initialisedLangs[
$code] ) ) {
380 if ( isset( $this->loadedItems[
$code][$key] ) ||
381 isset( $this->loadedSubitems[
$code][$key][$subkey] )
386 if ( isset( $this->shallowFallbacks[
$code] ) ) {
392 $value = $this->
store->get( $code,
"$key:$subkey" );
394 $this->loadedSubitems[
$code][$key][$subkey] =
true;
405 if ( $this->forceRecache && !isset( $this->recachedLangs[
$code] ) ) {
406 wfDebug( __METHOD__ .
"($code): forced reload\n" );
411 $deps = $this->
store->get( $code,
'deps' );
413 $preload = $this->
store->get( $code,
'preload' );
415 if ( $deps ===
null ||
$keys ===
null || $preload ===
null ) {
416 wfDebug( __METHOD__ .
"($code): cache missing, need to make one\n" );
421 foreach ( $deps
as $dep ) {
427 wfDebug( __METHOD__ .
"($code): cache for $code expired due to " .
428 get_class( $dep ) .
"\n" );
443 if ( isset( $this->initialisedLangs[
$code] ) ) {
447 $this->initialisedLangs[
$code] =
true;
449 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
456 # Recache the data if necessary
457 if ( !$this->manualRecache && $this->
isExpired( $code ) ) {
460 } elseif (
$code ===
'en' ) {
461 throw new MWException(
'MessagesEn.php is missing.' );
470 $preload = $this->
getItem( $code,
'preload' );
471 if ( $preload ===
null ) {
472 if ( $this->manualRecache ) {
474 if (
$code ===
'en' ) {
475 throw new MWException(
'No localisation cache found for English. ' .
476 'Please run maintenance/rebuildLocalisationCache.php.' );
482 throw new MWException(
'Invalid or missing localisation cache.' );
486 foreach ( $preload
as $key => $item ) {
487 if ( in_array( $key, self::$splitKeys ) ) {
488 foreach ( $item
as $subkey => $subitem ) {
489 $this->loadedSubitems[
$code][$key][$subkey] =
true;
492 $this->loadedItems[
$code][$key] =
true;
504 $this->
data[$primaryCode] =& $this->
data[$fallbackCode];
505 $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
506 $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
507 $this->shallowFallbacks[$primaryCode] = $fallbackCode;
519 MediaWiki\suppressWarnings();
520 $_apcEnabled = ini_set(
'apc.cache_by_default',
'0' );
521 MediaWiki\restoreWarnings();
525 MediaWiki\suppressWarnings();
526 ini_set(
'apc.cache_by_default', $_apcEnabled );
527 MediaWiki\restoreWarnings();
529 if ( $_fileType ==
'core' || $_fileType ==
'extension' ) {
530 $data = compact( self::$allKeys );
531 } elseif ( $_fileType ==
'aliases' ) {
532 $data = compact(
'aliases' );
534 throw new MWException( __METHOD__ .
": Invalid file type: $_fileType" );
547 if ( !is_readable( $fileName ) ) {
551 $json = file_get_contents( $fileName );
552 if ( $json ===
false ) {
557 if (
$data ===
null ) {
558 throw new MWException( __METHOD__ .
": Invalid JSON file: $fileName" );
562 foreach (
$data as $key => $unused ) {
563 if ( $key ===
'' || $key[0] ===
'@' ) {
564 unset(
$data[$key] );
569 return [
'messages' =>
$data ];
580 if ( $rules ===
null ) {
584 $compiledRules = Evaluator::compile( $rules );
585 }
catch ( CLDRPluralRuleError
$e ) {
591 return $compiledRules;
602 if ( $this->pluralRules ===
null ) {
605 if ( !isset( $this->pluralRules[
$code] ) ) {
608 return $this->pluralRules[
$code];
620 if ( $this->pluralRuleTypes ===
null ) {
623 if ( !isset( $this->pluralRuleTypes[
$code] ) ) {
626 return $this->pluralRuleTypes[
$code];
635 $cldrPlural =
"$IP/languages/data/plurals.xml";
636 $mwPlural =
"$IP/languages/data/plurals-mediawiki.xml";
639 if ( file_exists( $mwPlural ) ) {
654 $xml = file_get_contents( $fileName );
656 throw new MWException(
"Unable to read plurals file $fileName" );
658 $doc =
new DOMDocument;
659 $doc->loadXML( $xml );
660 $rulesets = $doc->getElementsByTagName(
"pluralRules" );
661 foreach ( $rulesets
as $ruleset ) {
662 $codes = $ruleset->getAttribute(
'locales' );
665 $ruleElements = $ruleset->getElementsByTagName(
"pluralRule" );
666 foreach ( $ruleElements
as $elt ) {
667 $ruleType = $elt->getAttribute(
'count' );
668 if ( $ruleType ===
'other' ) {
672 $rules[] = $elt->nodeValue;
673 $ruleTypes[] = $ruleType;
675 foreach ( explode(
' ', $codes )
as $code ) {
676 $this->pluralRules[
$code] = $rules;
677 $this->pluralRuleTypes[
$code] = $ruleTypes;
696 if ( !file_exists( $fileName ) ) {
703 # Load CLDR plural rules for JavaScript
707 # Load plural rule types
710 $deps[
'plurals'] =
new FileDependency(
"$IP/languages/data/plurals.xml" );
711 $deps[
'plurals-mw'] =
new FileDependency(
"$IP/languages/data/plurals-mediawiki.xml" );
724 if ( !is_null(
$value ) ) {
725 if ( !is_null( $fallbackValue ) ) {
726 if ( in_array( $key, self::$mergeableMapKeys ) ) {
728 } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
729 $value = array_unique( array_merge( $fallbackValue,
$value ) );
730 } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
731 $value = array_merge_recursive(
$value, $fallbackValue );
732 } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
733 if ( !empty(
$value[
'inherit'] ) ) {
737 if ( isset(
$value[
'inherit'] ) ) {
738 unset(
$value[
'inherit'] );
740 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
754 foreach ( $fallbackValue
as $magicName => $fallbackInfo ) {
755 if ( !isset(
$value[$magicName] ) ) {
756 $value[$magicName] = $fallbackInfo;
758 $oldSynonyms = array_slice( $fallbackInfo, 1 );
759 $newSynonyms = array_slice(
$value[$magicName], 1 );
760 $synonyms = array_values( array_unique( array_merge(
761 $newSynonyms, $oldSynonyms ) ) );
762 $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
782 foreach ( $codeSequence
as $code ) {
783 if ( isset( $fallbackValue[
$code] ) ) {
802 $config = MediaWikiServices::getInstance()->getMainConfig();
803 $messagesDirs = $config->get(
'MessagesDirs' );
805 'core' =>
"$IP/languages/i18n",
806 'api' =>
"$IP/includes/api/i18n",
807 'oojs-ui' =>
"$IP/resources/lib/oojs-ui/i18n",
821 throw new MWException(
"Invalid language code requested" );
823 $this->recachedLangs[
$code] =
true;
826 $initialData = array_fill_keys( self::$allKeys,
null );
827 $coreData = $initialData;
830 # Load the primary localisation from the source file
832 if (
$data ===
false ) {
833 wfDebug( __METHOD__ .
": no localisation file for $code, using fallback to en\n" );
834 $coreData[
'fallback'] =
'en';
836 wfDebug( __METHOD__ .
": got localisation for $code from source\n" );
838 # Merge primary localisation
844 # Fill in the fallback if it's not there already
845 if ( is_null( $coreData[
'fallback'] ) ) {
846 $coreData[
'fallback'] =
$code ===
'en' ?
false :
'en';
848 if ( $coreData[
'fallback'] ===
false ) {
849 $coreData[
'fallbackSequence'] = [];
851 $coreData[
'fallbackSequence'] = array_map(
'trim', explode(
',', $coreData[
'fallback'] ) );
852 $len =
count( $coreData[
'fallbackSequence'] );
854 # Ensure that the sequence ends at en
855 if ( $coreData[
'fallbackSequence'][$len - 1] !==
'en' ) {
856 $coreData[
'fallbackSequence'][] =
'en';
860 $codeSequence = array_merge( [
$code ], $coreData[
'fallbackSequence'] );
863 # Load non-JSON localisation data for extensions
864 $extensionData = array_fill_keys( $codeSequence, $initialData );
866 if ( isset( $messageDirs[$extension] ) ) {
867 # This extension has JSON message data; skip the PHP shim
874 foreach (
$data as $key => $item ) {
875 foreach ( $codeSequence
as $csCode ) {
876 if ( isset( $item[$csCode] ) ) {
877 $this->
mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] );
888 # Load the localisation data for each fallback, then merge it into the full array
889 $allData = $initialData;
890 foreach ( $codeSequence
as $csCode ) {
891 $csData = $initialData;
893 # Load core messages and the extension localisations.
894 foreach ( $messageDirs
as $dirs ) {
896 $fileName =
"$dir/$csCode.json";
899 foreach (
$data as $key => $item ) {
900 $this->
mergeItem( $key, $csData[$key], $item );
907 # Merge non-JSON extension data
908 if ( isset( $extensionData[$csCode] ) ) {
909 foreach ( $extensionData[$csCode]
as $key => $item ) {
910 $this->
mergeItem( $key, $csData[$key], $item );
914 if ( $csCode ===
$code ) {
915 # Merge core data into extension data
916 foreach ( $coreData
as $key => $item ) {
917 $this->
mergeItem( $key, $csData[$key], $item );
920 # Load the secondary localisation from the source file to
921 # avoid infinite cycles on cyclic fallbacks
923 if ( $fbData !==
false ) {
924 # Only merge the keys that make sense to merge
925 foreach ( self::$allKeys
as $key ) {
926 if ( !isset( $fbData[$key] ) ) {
930 if ( is_null( $coreData[$key] ) || $this->
isMergeableKey( $key ) ) {
931 $this->
mergeItem( $key, $csData[$key], $fbData[$key] );
937 # Allow extensions an opportunity to adjust the data for this
939 Hooks::run(
'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
941 # Merge the data for this fallback into the final array
942 if ( $csCode ===
$code ) {
945 foreach ( self::$allKeys
as $key ) {
946 if ( !isset( $csData[$key] ) ) {
950 if ( is_null( $allData[$key] ) || $this->
isMergeableKey( $key ) ) {
951 $this->
mergeItem( $key, $allData[$key], $csData[$key] );
957 # Add cache dependencies for any referenced globals
958 $deps[
'wgExtensionMessagesFiles'] =
new GlobalDependency(
'wgExtensionMessagesFiles' );
964 # Add dependencies to the cache entry
965 $allData[
'deps'] = $deps;
967 # Replace spaces with underscores in namespace names
968 $allData[
'namespaceNames'] = str_replace(
' ',
'_', $allData[
'namespaceNames'] );
970 # And do the same for special page aliases. $page is an array.
971 foreach ( $allData[
'specialPageAliases']
as &$page ) {
972 $page = str_replace(
' ',
'_', $page );
974 # Decouple the reference to prevent accidental damage
977 # If there were no plural rules, return an empty array
978 if ( $allData[
'pluralRules'] ===
null ) {
979 $allData[
'pluralRules'] = [];
981 if ( $allData[
'compiledPluralRules'] ===
null ) {
982 $allData[
'compiledPluralRules'] = [];
984 # If there were no plural rule types, return an empty array
985 if ( $allData[
'pluralRuleTypes'] ===
null ) {
986 $allData[
'pluralRuleTypes'] = [];
990 $allData[
'list'] = [];
991 foreach ( self::$splitKeys
as $key ) {
992 $allData[
'list'][$key] = array_keys( $allData[$key] );
996 Hooks::run(
'LocalisationCacheRecache', [ $this,
$code, &$allData, &$purgeBlobs ] );
998 if ( is_null( $allData[
'namespaceNames'] ) ) {
999 throw new MWException( __METHOD__ .
': Localisation data failed sanity check! ' .
1000 'Check that your languages/messages/MessagesEn.php file is intact.' );
1003 # Set the preload key
1006 # Save to the process cache and register the items loaded
1008 foreach ( $allData
as $key => $item ) {
1009 $this->loadedItems[
$code][$key] =
true;
1012 # Save to the persistent cache
1014 foreach ( $allData
as $key =>
$value ) {
1015 if ( in_array( $key, self::$splitKeys ) ) {
1016 foreach (
$value as $subkey => $subvalue ) {
1017 $this->
store->set(
"$key:$subkey", $subvalue );
1023 $this->
store->finishWrite();
1025 # Clear out the MessageBlobStore
1026 # HACK: If using a null (i.e. disabled) storage backend, we
1027 # can't write to the MessageBlobStore either
1030 $blobStore->clear();
1043 $preload = [
'messages' => [] ];
1044 foreach ( self::$preloadedKeys
as $key ) {
1045 $preload[$key] =
$data[$key];
1048 foreach (
$data[
'preloadedMessages']
as $subkey ) {
1049 if ( isset(
$data[
'messages'][$subkey] ) ) {
1050 $subitem =
$data[
'messages'][$subkey];
1054 $preload[
'messages'][$subkey] = $subitem;
1067 unset( $this->loadedItems[
$code] );
1068 unset( $this->loadedSubitems[
$code] );
1069 unset( $this->initialisedLangs[
$code] );
1070 unset( $this->shallowFallbacks[
$code] );
1072 foreach ( $this->shallowFallbacks
as $shallowCode => $fbCode ) {
1073 if ( $fbCode ===
$code ) {
1074 $this->
unload( $shallowCode );
1083 foreach ( $this->initialisedLangs
as $lang => $unused ) {
1093 $this->manualRecache =
false;