MediaWiki  1.28.0
LocalisationCache.php
Go to the documentation of this file.
1 <?php
26 use CLDRPluralRuleParser\Error as CLDRPluralRuleError;
28 
42  const VERSION = 4;
43 
45  private $conf;
46 
52  private $manualRecache = false;
53 
57  private $forceRecache = false;
58 
65  protected $data = [];
66 
72  private $store;
73 
81  private $loadedItems = [];
82 
87  private $loadedSubitems = [];
88 
94  private $initialisedLangs = [];
95 
101  private $shallowFallbacks = [];
102 
106  private $recachedLangs = [];
107 
111  static public $allKeys = [
112  'fallback', 'namespaceNames', 'bookstoreList',
113  'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable',
114  'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
115  'linkTrail', 'linkPrefixCharset', 'namespaceAliases',
116  'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
117  'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
118  'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
119  'digitGroupingPattern', 'pluralRules', 'pluralRuleTypes', 'compiledPluralRules',
120  ];
121 
126  static public $mergeableMapKeys = [ 'messages', 'namespaceNames',
127  'namespaceAliases', 'dateFormats', 'imageFiles', 'preloadedMessages'
128  ];
129 
133  static public $mergeableListKeys = [ 'extraUserToggles' ];
134 
139  static public $mergeableAliasListKeys = [ 'specialPageAliases' ];
140 
146  static public $optionalMergeKeys = [ 'bookstoreList' ];
147 
151  static public $magicWordKeys = [ 'magicWords' ];
152 
156  static public $splitKeys = [ 'messages' ];
157 
161  static public $preloadedKeys = [ 'dateFormats', 'namespaceNames' ];
162 
167  private $pluralRules = null;
168 
181  private $pluralRuleTypes = null;
182 
183  private $mergeableKeys = null;
184 
193  function __construct( $conf ) {
195 
196  $this->conf = $conf;
197  $storeConf = [];
198  if ( !empty( $conf['storeClass'] ) ) {
199  $storeClass = $conf['storeClass'];
200  } else {
201  switch ( $conf['store'] ) {
202  case 'files':
203  case 'file':
204  $storeClass = 'LCStoreCDB';
205  break;
206  case 'db':
207  $storeClass = 'LCStoreDB';
208  break;
209  case 'array':
210  $storeClass = 'LCStoreStaticArray';
211  break;
212  case 'detect':
213  if ( !empty( $conf['storeDirectory'] ) ) {
214  $storeClass = 'LCStoreCDB';
215  } else {
216  $cacheDir = $wgCacheDirectory ?: wfTempDir();
217  if ( $cacheDir ) {
218  $storeConf['directory'] = $cacheDir;
219  $storeClass = 'LCStoreCDB';
220  } else {
221  $storeClass = 'LCStoreDB';
222  }
223  }
224  break;
225  default:
226  throw new MWException(
227  'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' );
228  }
229  }
230 
231  wfDebugLog( 'caches', get_class( $this ) . ": using store $storeClass" );
232  if ( !empty( $conf['storeDirectory'] ) ) {
233  $storeConf['directory'] = $conf['storeDirectory'];
234  }
235 
236  $this->store = new $storeClass( $storeConf );
237  foreach ( [ 'manualRecache', 'forceRecache' ] as $var ) {
238  if ( isset( $conf[$var] ) ) {
239  $this->$var = $conf[$var];
240  }
241  }
242  }
243 
250  public function isMergeableKey( $key ) {
251  if ( $this->mergeableKeys === null ) {
252  $this->mergeableKeys = array_flip( array_merge(
253  self::$mergeableMapKeys,
254  self::$mergeableListKeys,
255  self::$mergeableAliasListKeys,
256  self::$optionalMergeKeys,
257  self::$magicWordKeys
258  ) );
259  }
260 
261  return isset( $this->mergeableKeys[$key] );
262  }
263 
273  public function getItem( $code, $key ) {
274  if ( !isset( $this->loadedItems[$code][$key] ) ) {
275  $this->loadItem( $code, $key );
276  }
277 
278  if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
279  return $this->shallowFallbacks[$code];
280  }
281 
282  return $this->data[$code][$key];
283  }
284 
292  public function getSubitem( $code, $key, $subkey ) {
293  if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) &&
294  !isset( $this->loadedItems[$code][$key] )
295  ) {
296  $this->loadSubitem( $code, $key, $subkey );
297  }
298 
299  if ( isset( $this->data[$code][$key][$subkey] ) ) {
300  return $this->data[$code][$key][$subkey];
301  } else {
302  return null;
303  }
304  }
305 
318  public function getSubitemList( $code, $key ) {
319  if ( in_array( $key, self::$splitKeys ) ) {
320  return $this->getSubitem( $code, 'list', $key );
321  } else {
322  $item = $this->getItem( $code, $key );
323  if ( is_array( $item ) ) {
324  return array_keys( $item );
325  } else {
326  return false;
327  }
328  }
329  }
330 
336  protected function loadItem( $code, $key ) {
337  if ( !isset( $this->initialisedLangs[$code] ) ) {
338  $this->initLanguage( $code );
339  }
340 
341  // Check to see if initLanguage() loaded it for us
342  if ( isset( $this->loadedItems[$code][$key] ) ) {
343  return;
344  }
345 
346  if ( isset( $this->shallowFallbacks[$code] ) ) {
347  $this->loadItem( $this->shallowFallbacks[$code], $key );
348 
349  return;
350  }
351 
352  if ( in_array( $key, self::$splitKeys ) ) {
353  $subkeyList = $this->getSubitem( $code, 'list', $key );
354  foreach ( $subkeyList as $subkey ) {
355  if ( isset( $this->data[$code][$key][$subkey] ) ) {
356  continue;
357  }
358  $this->data[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
359  }
360  } else {
361  $this->data[$code][$key] = $this->store->get( $code, $key );
362  }
363 
364  $this->loadedItems[$code][$key] = true;
365  }
366 
373  protected function loadSubitem( $code, $key, $subkey ) {
374  if ( !in_array( $key, self::$splitKeys ) ) {
375  $this->loadItem( $code, $key );
376 
377  return;
378  }
379 
380  if ( !isset( $this->initialisedLangs[$code] ) ) {
381  $this->initLanguage( $code );
382  }
383 
384  // Check to see if initLanguage() loaded it for us
385  if ( isset( $this->loadedItems[$code][$key] ) ||
386  isset( $this->loadedSubitems[$code][$key][$subkey] )
387  ) {
388  return;
389  }
390 
391  if ( isset( $this->shallowFallbacks[$code] ) ) {
392  $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
393 
394  return;
395  }
396 
397  $value = $this->store->get( $code, "$key:$subkey" );
398  $this->data[$code][$key][$subkey] = $value;
399  $this->loadedSubitems[$code][$key][$subkey] = true;
400  }
401 
409  public function isExpired( $code ) {
410  if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
411  wfDebug( __METHOD__ . "($code): forced reload\n" );
412 
413  return true;
414  }
415 
416  $deps = $this->store->get( $code, 'deps' );
417  $keys = $this->store->get( $code, 'list' );
418  $preload = $this->store->get( $code, 'preload' );
419  // Different keys may expire separately for some stores
420  if ( $deps === null || $keys === null || $preload === null ) {
421  wfDebug( __METHOD__ . "($code): cache missing, need to make one\n" );
422 
423  return true;
424  }
425 
426  foreach ( $deps as $dep ) {
427  // Because we're unserializing stuff from cache, we
428  // could receive objects of classes that don't exist
429  // anymore (e.g. uninstalled extensions)
430  // When this happens, always expire the cache
431  if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
432  wfDebug( __METHOD__ . "($code): cache for $code expired due to " .
433  get_class( $dep ) . "\n" );
434 
435  return true;
436  }
437  }
438 
439  return false;
440  }
441 
447  protected function initLanguage( $code ) {
448  if ( isset( $this->initialisedLangs[$code] ) ) {
449  return;
450  }
451 
452  $this->initialisedLangs[$code] = true;
453 
454  # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
455  if ( !Language::isValidBuiltInCode( $code ) ) {
456  $this->initShallowFallback( $code, 'en' );
457 
458  return;
459  }
460 
461  # Recache the data if necessary
462  if ( !$this->manualRecache && $this->isExpired( $code ) ) {
463  if ( Language::isSupportedLanguage( $code ) ) {
464  $this->recache( $code );
465  } elseif ( $code === 'en' ) {
466  throw new MWException( 'MessagesEn.php is missing.' );
467  } else {
468  $this->initShallowFallback( $code, 'en' );
469  }
470 
471  return;
472  }
473 
474  # Preload some stuff
475  $preload = $this->getItem( $code, 'preload' );
476  if ( $preload === null ) {
477  if ( $this->manualRecache ) {
478  // No Messages*.php file. Do shallow fallback to en.
479  if ( $code === 'en' ) {
480  throw new MWException( 'No localisation cache found for English. ' .
481  'Please run maintenance/rebuildLocalisationCache.php.' );
482  }
483  $this->initShallowFallback( $code, 'en' );
484 
485  return;
486  } else {
487  throw new MWException( 'Invalid or missing localisation cache.' );
488  }
489  }
490  $this->data[$code] = $preload;
491  foreach ( $preload as $key => $item ) {
492  if ( in_array( $key, self::$splitKeys ) ) {
493  foreach ( $item as $subkey => $subitem ) {
494  $this->loadedSubitems[$code][$key][$subkey] = true;
495  }
496  } else {
497  $this->loadedItems[$code][$key] = true;
498  }
499  }
500  }
501 
508  public function initShallowFallback( $primaryCode, $fallbackCode ) {
509  $this->data[$primaryCode] =& $this->data[$fallbackCode];
510  $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
511  $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
512  $this->shallowFallbacks[$primaryCode] = $fallbackCode;
513  }
514 
522  protected function readPHPFile( $_fileName, $_fileType ) {
523  // Disable APC caching
524  MediaWiki\suppressWarnings();
525  $_apcEnabled = ini_set( 'apc.cache_by_default', '0' );
526  MediaWiki\restoreWarnings();
527 
528  include $_fileName;
529 
530  MediaWiki\suppressWarnings();
531  ini_set( 'apc.cache_by_default', $_apcEnabled );
532  MediaWiki\restoreWarnings();
533 
534  if ( $_fileType == 'core' || $_fileType == 'extension' ) {
535  $data = compact( self::$allKeys );
536  } elseif ( $_fileType == 'aliases' ) {
537  $data = compact( 'aliases' );
538  } else {
539  throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" );
540  }
541 
542  return $data;
543  }
544 
551  public function readJSONFile( $fileName ) {
552 
553  if ( !is_readable( $fileName ) ) {
554  return [];
555  }
556 
557  $json = file_get_contents( $fileName );
558  if ( $json === false ) {
559  return [];
560  }
561 
562  $data = FormatJson::decode( $json, true );
563  if ( $data === null ) {
564 
565  throw new MWException( __METHOD__ . ": Invalid JSON file: $fileName" );
566  }
567 
568  // Remove keys starting with '@', they're reserved for metadata and non-message data
569  foreach ( $data as $key => $unused ) {
570  if ( $key === '' || $key[0] === '@' ) {
571  unset( $data[$key] );
572  }
573  }
574 
575  // The JSON format only supports messages, none of the other variables, so wrap the data
576  return [ 'messages' => $data ];
577  }
578 
585  public function getCompiledPluralRules( $code ) {
586  $rules = $this->getPluralRules( $code );
587  if ( $rules === null ) {
588  return null;
589  }
590  try {
591  $compiledRules = Evaluator::compile( $rules );
592  } catch ( CLDRPluralRuleError $e ) {
593  wfDebugLog( 'l10n', $e->getMessage() );
594 
595  return [];
596  }
597 
598  return $compiledRules;
599  }
600 
608  public function getPluralRules( $code ) {
609  if ( $this->pluralRules === null ) {
610  $this->loadPluralFiles();
611  }
612  if ( !isset( $this->pluralRules[$code] ) ) {
613  return null;
614  } else {
615  return $this->pluralRules[$code];
616  }
617  }
618 
626  public function getPluralRuleTypes( $code ) {
627  if ( $this->pluralRuleTypes === null ) {
628  $this->loadPluralFiles();
629  }
630  if ( !isset( $this->pluralRuleTypes[$code] ) ) {
631  return null;
632  } else {
633  return $this->pluralRuleTypes[$code];
634  }
635  }
636 
640  protected function loadPluralFiles() {
641  global $IP;
642  $cldrPlural = "$IP/languages/data/plurals.xml";
643  $mwPlural = "$IP/languages/data/plurals-mediawiki.xml";
644  // Load CLDR plural rules
645  $this->loadPluralFile( $cldrPlural );
646  if ( file_exists( $mwPlural ) ) {
647  // Override or extend
648  $this->loadPluralFile( $mwPlural );
649  }
650  }
651 
659  protected function loadPluralFile( $fileName ) {
660  // Use file_get_contents instead of DOMDocument::load (T58439)
661  $xml = file_get_contents( $fileName );
662  if ( !$xml ) {
663  throw new MWException( "Unable to read plurals file $fileName" );
664  }
665  $doc = new DOMDocument;
666  $doc->loadXML( $xml );
667  $rulesets = $doc->getElementsByTagName( "pluralRules" );
668  foreach ( $rulesets as $ruleset ) {
669  $codes = $ruleset->getAttribute( 'locales' );
670  $rules = [];
671  $ruleTypes = [];
672  $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
673  foreach ( $ruleElements as $elt ) {
674  $ruleType = $elt->getAttribute( 'count' );
675  if ( $ruleType === 'other' ) {
676  // Don't record "other" rules, which have an empty condition
677  continue;
678  }
679  $rules[] = $elt->nodeValue;
680  $ruleTypes[] = $ruleType;
681  }
682  foreach ( explode( ' ', $codes ) as $code ) {
683  $this->pluralRules[$code] = $rules;
684  $this->pluralRuleTypes[$code] = $ruleTypes;
685  }
686  }
687  }
688 
698  protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
699  global $IP;
700 
701  // This reads in the PHP i18n file with non-messages l10n data
702  $fileName = Language::getMessagesFileName( $code );
703  if ( !file_exists( $fileName ) ) {
704  $data = [];
705  } else {
706  $deps[] = new FileDependency( $fileName );
707  $data = $this->readPHPFile( $fileName, 'core' );
708  }
709 
710  # Load CLDR plural rules for JavaScript
711  $data['pluralRules'] = $this->getPluralRules( $code );
712  # And for PHP
713  $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
714  # Load plural rule types
715  $data['pluralRuleTypes'] = $this->getPluralRuleTypes( $code );
716 
717  $deps['plurals'] = new FileDependency( "$IP/languages/data/plurals.xml" );
718  $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
719 
720  return $data;
721  }
722 
730  protected function mergeItem( $key, &$value, $fallbackValue ) {
731  if ( !is_null( $value ) ) {
732  if ( !is_null( $fallbackValue ) ) {
733  if ( in_array( $key, self::$mergeableMapKeys ) ) {
734  $value = $value + $fallbackValue;
735  } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
736  $value = array_unique( array_merge( $fallbackValue, $value ) );
737  } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
738  $value = array_merge_recursive( $value, $fallbackValue );
739  } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
740  if ( !empty( $value['inherit'] ) ) {
741  $value = array_merge( $fallbackValue, $value );
742  }
743 
744  if ( isset( $value['inherit'] ) ) {
745  unset( $value['inherit'] );
746  }
747  } elseif ( in_array( $key, self::$magicWordKeys ) ) {
748  $this->mergeMagicWords( $value, $fallbackValue );
749  }
750  }
751  } else {
752  $value = $fallbackValue;
753  }
754  }
755 
760  protected function mergeMagicWords( &$value, $fallbackValue ) {
761  foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
762  if ( !isset( $value[$magicName] ) ) {
763  $value[$magicName] = $fallbackInfo;
764  } else {
765  $oldSynonyms = array_slice( $fallbackInfo, 1 );
766  $newSynonyms = array_slice( $value[$magicName], 1 );
767  $synonyms = array_values( array_unique( array_merge(
768  $newSynonyms, $oldSynonyms ) ) );
769  $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
770  }
771  }
772  }
773 
787  protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
788  $used = false;
789  foreach ( $codeSequence as $code ) {
790  if ( isset( $fallbackValue[$code] ) ) {
791  $this->mergeItem( $key, $value, $fallbackValue[$code] );
792  $used = true;
793  }
794  }
795 
796  return $used;
797  }
798 
806  public function getMessagesDirs() {
807  global $IP;
808 
809  $config = MediaWikiServices::getInstance()->getMainConfig();
810  $messagesDirs = $config->get( 'MessagesDirs' );
811  return [
812  'core' => "$IP/languages/i18n",
813  'api' => "$IP/includes/api/i18n",
814  'oojs-ui' => "$IP/resources/lib/oojs-ui/i18n",
815  ] + $messagesDirs;
816  }
817 
824  public function recache( $code ) {
826 
827  if ( !$code ) {
828  throw new MWException( "Invalid language code requested" );
829  }
830  $this->recachedLangs[$code] = true;
831 
832  # Initial values
833  $initialData = array_fill_keys( self::$allKeys, null );
834  $coreData = $initialData;
835  $deps = [];
836 
837  # Load the primary localisation from the source file
838  $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
839  if ( $data === false ) {
840  wfDebug( __METHOD__ . ": no localisation file for $code, using fallback to en\n" );
841  $coreData['fallback'] = 'en';
842  } else {
843  wfDebug( __METHOD__ . ": got localisation for $code from source\n" );
844 
845  # Merge primary localisation
846  foreach ( $data as $key => $value ) {
847  $this->mergeItem( $key, $coreData[$key], $value );
848  }
849  }
850 
851  # Fill in the fallback if it's not there already
852  if ( is_null( $coreData['fallback'] ) ) {
853  $coreData['fallback'] = $code === 'en' ? false : 'en';
854  }
855  if ( $coreData['fallback'] === false ) {
856  $coreData['fallbackSequence'] = [];
857  } else {
858  $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
859  $len = count( $coreData['fallbackSequence'] );
860 
861  # Ensure that the sequence ends at en
862  if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
863  $coreData['fallbackSequence'][] = 'en';
864  }
865  }
866 
867  $codeSequence = array_merge( [ $code ], $coreData['fallbackSequence'] );
868  $messageDirs = $this->getMessagesDirs();
869 
870  # Load non-JSON localisation data for extensions
871  $extensionData = array_fill_keys( $codeSequence, $initialData );
872  foreach ( $wgExtensionMessagesFiles as $extension => $fileName ) {
873  if ( isset( $messageDirs[$extension] ) ) {
874  # This extension has JSON message data; skip the PHP shim
875  continue;
876  }
877 
878  $data = $this->readPHPFile( $fileName, 'extension' );
879  $used = false;
880 
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] );
885  $used = true;
886  }
887  }
888  }
889 
890  if ( $used ) {
891  $deps[] = new FileDependency( $fileName );
892  }
893  }
894 
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;
899 
900  # Load core messages and the extension localisations.
901  foreach ( $messageDirs as $dirs ) {
902  foreach ( (array)$dirs as $dir ) {
903  $fileName = "$dir/$csCode.json";
904  $data = $this->readJSONFile( $fileName );
905 
906  foreach ( $data as $key => $item ) {
907  $this->mergeItem( $key, $csData[$key], $item );
908  }
909 
910  $deps[] = new FileDependency( $fileName );
911  }
912  }
913 
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 );
918  }
919  }
920 
921  if ( $csCode === $code ) {
922  # Merge core data into extension data
923  foreach ( $coreData as $key => $item ) {
924  $this->mergeItem( $key, $csData[$key], $item );
925  }
926  } else {
927  # Load the secondary localisation from the source file to
928  # avoid infinite cycles on cyclic fallbacks
929  $fbData = $this->readSourceFilesAndRegisterDeps( $csCode, $deps );
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] ) ) {
934  continue;
935  }
936 
937  if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
938  $this->mergeItem( $key, $csData[$key], $fbData[$key] );
939  }
940  }
941  }
942  }
943 
944  # Allow extensions an opportunity to adjust the data for this
945  # fallback
946  Hooks::run( 'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
947 
948  # Merge the data for this fallback into the final array
949  if ( $csCode === $code ) {
950  $allData = $csData;
951  } else {
952  foreach ( self::$allKeys as $key ) {
953  if ( !isset( $csData[$key] ) ) {
954  continue;
955  }
956 
957  if ( is_null( $allData[$key] ) || $this->isMergeableKey( $key ) ) {
958  $this->mergeItem( $key, $allData[$key], $csData[$key] );
959  }
960  }
961  }
962  }
963 
964  # Add cache dependencies for any referenced globals
965  $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
966  // The 'MessagesDirs' config setting is used in LocalisationCache::getMessagesDirs().
967  // We use the key 'wgMessagesDirs' for historical reasons.
968  $deps['wgMessagesDirs'] = new MainConfigDependency( 'MessagesDirs' );
969  $deps['version'] = new ConstantDependency( 'LocalisationCache::VERSION' );
970 
971  # Add dependencies to the cache entry
972  $allData['deps'] = $deps;
973 
974  # Replace spaces with underscores in namespace names
975  $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
976 
977  # And do the same for special page aliases. $page is an array.
978  foreach ( $allData['specialPageAliases'] as &$page ) {
979  $page = str_replace( ' ', '_', $page );
980  }
981  # Decouple the reference to prevent accidental damage
982  unset( $page );
983 
984  # If there were no plural rules, return an empty array
985  if ( $allData['pluralRules'] === null ) {
986  $allData['pluralRules'] = [];
987  }
988  if ( $allData['compiledPluralRules'] === null ) {
989  $allData['compiledPluralRules'] = [];
990  }
991  # If there were no plural rule types, return an empty array
992  if ( $allData['pluralRuleTypes'] === null ) {
993  $allData['pluralRuleTypes'] = [];
994  }
995 
996  # Set the list keys
997  $allData['list'] = [];
998  foreach ( self::$splitKeys as $key ) {
999  $allData['list'][$key] = array_keys( $allData[$key] );
1000  }
1001  # Run hooks
1002  $purgeBlobs = true;
1003  Hooks::run( 'LocalisationCacheRecache', [ $this, $code, &$allData, &$purgeBlobs ] );
1004 
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.' );
1008  }
1009 
1010  # Set the preload key
1011  $allData['preload'] = $this->buildPreload( $allData );
1012 
1013  # Save to the process cache and register the items loaded
1014  $this->data[$code] = $allData;
1015  foreach ( $allData as $key => $item ) {
1016  $this->loadedItems[$code][$key] = true;
1017  }
1018 
1019  # Save to the persistent cache
1020  $this->store->startWrite( $code );
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 );
1025  }
1026  } else {
1027  $this->store->set( $key, $value );
1028  }
1029  }
1030  $this->store->finishWrite();
1031 
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
1035  if ( $purgeBlobs && !$this->store instanceof LCStoreNull ) {
1036  $blobStore = new MessageBlobStore();
1037  $blobStore->clear();
1038  }
1039 
1040  }
1041 
1050  protected function buildPreload( $data ) {
1051  $preload = [ 'messages' => [] ];
1052  foreach ( self::$preloadedKeys as $key ) {
1053  $preload[$key] = $data[$key];
1054  }
1055 
1056  foreach ( $data['preloadedMessages'] as $subkey ) {
1057  if ( isset( $data['messages'][$subkey] ) ) {
1058  $subitem = $data['messages'][$subkey];
1059  } else {
1060  $subitem = null;
1061  }
1062  $preload['messages'][$subkey] = $subitem;
1063  }
1064 
1065  return $preload;
1066  }
1067 
1073  public function unload( $code ) {
1074  unset( $this->data[$code] );
1075  unset( $this->loadedItems[$code] );
1076  unset( $this->loadedSubitems[$code] );
1077  unset( $this->initialisedLangs[$code] );
1078  unset( $this->shallowFallbacks[$code] );
1079 
1080  foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
1081  if ( $fbCode === $code ) {
1082  $this->unload( $shallowCode );
1083  }
1084  }
1085  }
1086 
1090  public function unloadAll() {
1091  foreach ( $this->initialisedLangs as $lang => $unused ) {
1092  $this->unload( $lang );
1093  }
1094  }
1095 
1099  public function disableBackend() {
1100  $this->store = new LCStoreNull;
1101  $this->manualRecache = false;
1102  }
1103 
1104 }
unloadAll()
Unload all data.
readJSONFile($fileName)
Read a JSON file containing localisation messages.
This class generates message blobs for use by ResourceLoader modules.
static $mergeableAliasListKeys
Keys for items which contain an array of arrays of equivalent aliases for each subitem.
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
the array() calling protocol came about after MediaWiki 1.4rc1.
readPHPFile($_fileName, $_fileType)
Read a PHP file containing localisation data.
if(count($args)==0) $dir
readSourceFilesAndRegisterDeps($code, &$deps)
Read the data from the source files for a given language, and register the relevant dependencies in t...
$IP
Definition: WebStart.php:58
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
unload($code)
Unload the data for a given language from the object cache.
loadItem($code, $key)
Load an item into the cache.
LCStore $store
The persistent store object.
if(!isset($args[0])) $lang
$shallowFallbacks
An array mapping non-existent pseudo-languages to fallback languages.
$initialisedLangs
An array where presence of a key indicates that that language has been initialised.
$value
$conf
Configuration associative array.
recache($code)
Load localisation data for a given language for both core and extensions and save it to the persisten...
static $allKeys
All item keys.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
isExpired()
Returns true if the dependency is expired, false otherwise.
getMessagesDirs()
Gets the combined list of messages dirs from core and extensions.
__construct($conf)
Constructor.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
mergeMagicWords(&$value, $fallbackValue)
$manualRecache
True if recaching should only be done on an explicit call to recache().
loadSubitem($code, $key, $subkey)
Load a subitem into the cache.
getItem($code, $key)
Get a cache item.
MediaWiki s SiteStore can be cached and stored in a flat in a json format If the SiteStore is frequently the file cache may provide a performance benefit over a database store
Definition: sitescache.txt:1
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$wgCacheDirectory
Directory for caching data in the local filesystem.
disableBackend()
Disable the storage backend.
static $optionalMergeKeys
Keys for items which contain an associative array, and may be merged if the primary value contains th...
$recachedLangs
An array where the keys are codes that have been recached by this instance.
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
wfTempDir()
Tries to get the system directory for temporary files.
isExpired($code)
Returns true if the cache identified by $code is missing or expired.
mergeItem($key, &$value, $fallbackValue)
Merge two localisation values, a primary and a fallback, overwriting the primary value in place...
$pluralRules
Associative array of cached plural rules.
static getMessagesFileName($code)
Definition: Language.php:4333
static $preloadedKeys
Keys which are loaded automatically by initLanguage()
static isValidBuiltInCode($code)
Returns true if a language code is of a valid form for the purposes of internal customisation of Medi...
Definition: Language.php:360
$forceRecache
True to treat all files as expired until they are regenerated by this object.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
Class for caching the contents of localisation files, Messages*.php and *.i18n.php.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$data
The cache data.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:802
static $mergeableListKeys
Keys for items which are a numbered array.
loadPluralFiles()
Load the plural XML files.
getSubitemList($code, $key)
Get the list of subitem keys for a given item.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static $magicWordKeys
Keys for items that are formatted like $magicWords.
initLanguage($code)
Initialise a language in this object.
$loadedItems
A 2-d associative array, code/key, where presence indicates that the item is loaded.
static $splitKeys
Keys for items where the subitems are stored in the backend separately.
$loadedSubitems
A 3-d associative array, code/key/subkey, where presence indicates that the subitem is loaded...
getPluralRules($code)
Get the plural rules for a given language from the XML files.
Null store backend, used to avoid DB errors during install.
Definition: LCStoreNull.php:24
mergeExtensionItem($codeSequence, $key, &$value, $fallbackValue)
Given an array mapping language code to localisation value, such as is found in extension *...
static $mergeableMapKeys
Keys for items which consist of associative arrays, which may be merged by a fallback sequence...
buildPreload($data)
Build the preload item from the given pre-cache data.
getSubitem($code, $key, $subkey)
Get a subitem, for instance a single message for a given language.
$pluralRuleTypes
Associative array of cached plural rule types.
getCompiledPluralRules($code)
Get the compiled plural rules for a given language from the XML files.
static decode($value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
getPluralRuleTypes($code)
Get the plural rule types for a given language from the XML files.
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition: magicword.txt:43
static isSupportedLanguage($code)
Checks whether any localisation is available for that language tag in MediaWiki (MessagesXx.php exists).
Definition: Language.php:253
initShallowFallback($primaryCode, $fallbackCode)
Create a fallback from one language to another, without creating a complete persistent cache...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2491
isMergeableKey($key)
Returns true if the given key is mergeable, that is, if it is an associative array which can be merge...
loadPluralFile($fileName)
Load a plural XML file with the given filename, compile the relevant rules, and save the compiled rul...