MediaWiki  1.29.2
LocalisationCache.php
Go to the documentation of this file.
1 <?php
23 use Cdb\Reader as CdbReader;
24 use Cdb\Writer as CdbWriter;
25 use CLDRPluralRuleParser\Evaluator;
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  } elseif ( $wgCacheDirectory ) {
216  $storeConf['directory'] = $wgCacheDirectory;
217  $storeClass = 'LCStoreCDB';
218  } else {
219  $storeClass = 'LCStoreDB';
220  }
221  break;
222  default:
223  throw new MWException(
224  'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
225  );
226  }
227  }
228 
229  wfDebugLog( 'caches', static::class . ": using store $storeClass" );
230  if ( !empty( $conf['storeDirectory'] ) ) {
231  $storeConf['directory'] = $conf['storeDirectory'];
232  }
233 
234  $this->store = new $storeClass( $storeConf );
235  foreach ( [ 'manualRecache', 'forceRecache' ] as $var ) {
236  if ( isset( $conf[$var] ) ) {
237  $this->$var = $conf[$var];
238  }
239  }
240  }
241 
248  public function isMergeableKey( $key ) {
249  if ( $this->mergeableKeys === null ) {
250  $this->mergeableKeys = array_flip( array_merge(
251  self::$mergeableMapKeys,
252  self::$mergeableListKeys,
253  self::$mergeableAliasListKeys,
254  self::$optionalMergeKeys,
255  self::$magicWordKeys
256  ) );
257  }
258 
259  return isset( $this->mergeableKeys[$key] );
260  }
261 
271  public function getItem( $code, $key ) {
272  if ( !isset( $this->loadedItems[$code][$key] ) ) {
273  $this->loadItem( $code, $key );
274  }
275 
276  if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
277  return $this->shallowFallbacks[$code];
278  }
279 
280  return $this->data[$code][$key];
281  }
282 
290  public function getSubitem( $code, $key, $subkey ) {
291  if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) &&
292  !isset( $this->loadedItems[$code][$key] )
293  ) {
294  $this->loadSubitem( $code, $key, $subkey );
295  }
296 
297  if ( isset( $this->data[$code][$key][$subkey] ) ) {
298  return $this->data[$code][$key][$subkey];
299  } else {
300  return null;
301  }
302  }
303 
316  public function getSubitemList( $code, $key ) {
317  if ( in_array( $key, self::$splitKeys ) ) {
318  return $this->getSubitem( $code, 'list', $key );
319  } else {
320  $item = $this->getItem( $code, $key );
321  if ( is_array( $item ) ) {
322  return array_keys( $item );
323  } else {
324  return false;
325  }
326  }
327  }
328 
334  protected function loadItem( $code, $key ) {
335  if ( !isset( $this->initialisedLangs[$code] ) ) {
336  $this->initLanguage( $code );
337  }
338 
339  // Check to see if initLanguage() loaded it for us
340  if ( isset( $this->loadedItems[$code][$key] ) ) {
341  return;
342  }
343 
344  if ( isset( $this->shallowFallbacks[$code] ) ) {
345  $this->loadItem( $this->shallowFallbacks[$code], $key );
346 
347  return;
348  }
349 
350  if ( in_array( $key, self::$splitKeys ) ) {
351  $subkeyList = $this->getSubitem( $code, 'list', $key );
352  foreach ( $subkeyList as $subkey ) {
353  if ( isset( $this->data[$code][$key][$subkey] ) ) {
354  continue;
355  }
356  $this->data[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
357  }
358  } else {
359  $this->data[$code][$key] = $this->store->get( $code, $key );
360  }
361 
362  $this->loadedItems[$code][$key] = true;
363  }
364 
371  protected function loadSubitem( $code, $key, $subkey ) {
372  if ( !in_array( $key, self::$splitKeys ) ) {
373  $this->loadItem( $code, $key );
374 
375  return;
376  }
377 
378  if ( !isset( $this->initialisedLangs[$code] ) ) {
379  $this->initLanguage( $code );
380  }
381 
382  // Check to see if initLanguage() loaded it for us
383  if ( isset( $this->loadedItems[$code][$key] ) ||
384  isset( $this->loadedSubitems[$code][$key][$subkey] )
385  ) {
386  return;
387  }
388 
389  if ( isset( $this->shallowFallbacks[$code] ) ) {
390  $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
391 
392  return;
393  }
394 
395  $value = $this->store->get( $code, "$key:$subkey" );
396  $this->data[$code][$key][$subkey] = $value;
397  $this->loadedSubitems[$code][$key][$subkey] = true;
398  }
399 
407  public function isExpired( $code ) {
408  if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
409  wfDebug( __METHOD__ . "($code): forced reload\n" );
410 
411  return true;
412  }
413 
414  $deps = $this->store->get( $code, 'deps' );
415  $keys = $this->store->get( $code, 'list' );
416  $preload = $this->store->get( $code, 'preload' );
417  // Different keys may expire separately for some stores
418  if ( $deps === null || $keys === null || $preload === null ) {
419  wfDebug( __METHOD__ . "($code): cache missing, need to make one\n" );
420 
421  return true;
422  }
423 
424  foreach ( $deps as $dep ) {
425  // Because we're unserializing stuff from cache, we
426  // could receive objects of classes that don't exist
427  // anymore (e.g. uninstalled extensions)
428  // When this happens, always expire the cache
429  if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
430  wfDebug( __METHOD__ . "($code): cache for $code expired due to " .
431  get_class( $dep ) . "\n" );
432 
433  return true;
434  }
435  }
436 
437  return false;
438  }
439 
445  protected function initLanguage( $code ) {
446  if ( isset( $this->initialisedLangs[$code] ) ) {
447  return;
448  }
449 
450  $this->initialisedLangs[$code] = true;
451 
452  # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
454  $this->initShallowFallback( $code, 'en' );
455 
456  return;
457  }
458 
459  # Recache the data if necessary
460  if ( !$this->manualRecache && $this->isExpired( $code ) ) {
462  $this->recache( $code );
463  } elseif ( $code === 'en' ) {
464  throw new MWException( 'MessagesEn.php is missing.' );
465  } else {
466  $this->initShallowFallback( $code, 'en' );
467  }
468 
469  return;
470  }
471 
472  # Preload some stuff
473  $preload = $this->getItem( $code, 'preload' );
474  if ( $preload === null ) {
475  if ( $this->manualRecache ) {
476  // No Messages*.php file. Do shallow fallback to en.
477  if ( $code === 'en' ) {
478  throw new MWException( 'No localisation cache found for English. ' .
479  'Please run maintenance/rebuildLocalisationCache.php.' );
480  }
481  $this->initShallowFallback( $code, 'en' );
482 
483  return;
484  } else {
485  throw new MWException( 'Invalid or missing localisation cache.' );
486  }
487  }
488  $this->data[$code] = $preload;
489  foreach ( $preload as $key => $item ) {
490  if ( in_array( $key, self::$splitKeys ) ) {
491  foreach ( $item as $subkey => $subitem ) {
492  $this->loadedSubitems[$code][$key][$subkey] = true;
493  }
494  } else {
495  $this->loadedItems[$code][$key] = true;
496  }
497  }
498  }
499 
506  public function initShallowFallback( $primaryCode, $fallbackCode ) {
507  $this->data[$primaryCode] =& $this->data[$fallbackCode];
508  $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
509  $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
510  $this->shallowFallbacks[$primaryCode] = $fallbackCode;
511  }
512 
520  protected function readPHPFile( $_fileName, $_fileType ) {
521  // Disable APC caching
522  MediaWiki\suppressWarnings();
523  $_apcEnabled = ini_set( 'apc.cache_by_default', '0' );
524  MediaWiki\restoreWarnings();
525 
526  include $_fileName;
527 
528  MediaWiki\suppressWarnings();
529  ini_set( 'apc.cache_by_default', $_apcEnabled );
530  MediaWiki\restoreWarnings();
531 
532  if ( $_fileType == 'core' || $_fileType == 'extension' ) {
533  $data = compact( self::$allKeys );
534  } elseif ( $_fileType == 'aliases' ) {
535  $data = compact( 'aliases' );
536  } else {
537  throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" );
538  }
539 
540  return $data;
541  }
542 
549  public function readJSONFile( $fileName ) {
550  if ( !is_readable( $fileName ) ) {
551  return [];
552  }
553 
554  $json = file_get_contents( $fileName );
555  if ( $json === false ) {
556  return [];
557  }
558 
559  $data = FormatJson::decode( $json, true );
560  if ( $data === null ) {
561  throw new MWException( __METHOD__ . ": Invalid JSON file: $fileName" );
562  }
563 
564  // Remove keys starting with '@', they're reserved for metadata and non-message data
565  foreach ( $data as $key => $unused ) {
566  if ( $key === '' || $key[0] === '@' ) {
567  unset( $data[$key] );
568  }
569  }
570 
571  // The JSON format only supports messages, none of the other variables, so wrap the data
572  return [ 'messages' => $data ];
573  }
574 
581  public function getCompiledPluralRules( $code ) {
582  $rules = $this->getPluralRules( $code );
583  if ( $rules === null ) {
584  return null;
585  }
586  try {
587  $compiledRules = Evaluator::compile( $rules );
588  } catch ( CLDRPluralRuleError $e ) {
589  wfDebugLog( 'l10n', $e->getMessage() );
590 
591  return [];
592  }
593 
594  return $compiledRules;
595  }
596 
604  public function getPluralRules( $code ) {
605  if ( $this->pluralRules === null ) {
606  $this->loadPluralFiles();
607  }
608  if ( !isset( $this->pluralRules[$code] ) ) {
609  return null;
610  } else {
611  return $this->pluralRules[$code];
612  }
613  }
614 
622  public function getPluralRuleTypes( $code ) {
623  if ( $this->pluralRuleTypes === null ) {
624  $this->loadPluralFiles();
625  }
626  if ( !isset( $this->pluralRuleTypes[$code] ) ) {
627  return null;
628  } else {
629  return $this->pluralRuleTypes[$code];
630  }
631  }
632 
636  protected function loadPluralFiles() {
637  global $IP;
638  $cldrPlural = "$IP/languages/data/plurals.xml";
639  $mwPlural = "$IP/languages/data/plurals-mediawiki.xml";
640  // Load CLDR plural rules
641  $this->loadPluralFile( $cldrPlural );
642  if ( file_exists( $mwPlural ) ) {
643  // Override or extend
644  $this->loadPluralFile( $mwPlural );
645  }
646  }
647 
655  protected function loadPluralFile( $fileName ) {
656  // Use file_get_contents instead of DOMDocument::load (T58439)
657  $xml = file_get_contents( $fileName );
658  if ( !$xml ) {
659  throw new MWException( "Unable to read plurals file $fileName" );
660  }
661  $doc = new DOMDocument;
662  $doc->loadXML( $xml );
663  $rulesets = $doc->getElementsByTagName( "pluralRules" );
664  foreach ( $rulesets as $ruleset ) {
665  $codes = $ruleset->getAttribute( 'locales' );
666  $rules = [];
667  $ruleTypes = [];
668  $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
669  foreach ( $ruleElements as $elt ) {
670  $ruleType = $elt->getAttribute( 'count' );
671  if ( $ruleType === 'other' ) {
672  // Don't record "other" rules, which have an empty condition
673  continue;
674  }
675  $rules[] = $elt->nodeValue;
676  $ruleTypes[] = $ruleType;
677  }
678  foreach ( explode( ' ', $codes ) as $code ) {
679  $this->pluralRules[$code] = $rules;
680  $this->pluralRuleTypes[$code] = $ruleTypes;
681  }
682  }
683  }
684 
694  protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
695  global $IP;
696 
697  // This reads in the PHP i18n file with non-messages l10n data
698  $fileName = Language::getMessagesFileName( $code );
699  if ( !file_exists( $fileName ) ) {
700  $data = [];
701  } else {
702  $deps[] = new FileDependency( $fileName );
703  $data = $this->readPHPFile( $fileName, 'core' );
704  }
705 
706  # Load CLDR plural rules for JavaScript
707  $data['pluralRules'] = $this->getPluralRules( $code );
708  # And for PHP
709  $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
710  # Load plural rule types
711  $data['pluralRuleTypes'] = $this->getPluralRuleTypes( $code );
712 
713  $deps['plurals'] = new FileDependency( "$IP/languages/data/plurals.xml" );
714  $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
715 
716  return $data;
717  }
718 
726  protected function mergeItem( $key, &$value, $fallbackValue ) {
727  if ( !is_null( $value ) ) {
728  if ( !is_null( $fallbackValue ) ) {
729  if ( in_array( $key, self::$mergeableMapKeys ) ) {
730  $value = $value + $fallbackValue;
731  } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
732  $value = array_unique( array_merge( $fallbackValue, $value ) );
733  } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
734  $value = array_merge_recursive( $value, $fallbackValue );
735  } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
736  if ( !empty( $value['inherit'] ) ) {
737  $value = array_merge( $fallbackValue, $value );
738  }
739 
740  if ( isset( $value['inherit'] ) ) {
741  unset( $value['inherit'] );
742  }
743  } elseif ( in_array( $key, self::$magicWordKeys ) ) {
744  $this->mergeMagicWords( $value, $fallbackValue );
745  }
746  }
747  } else {
748  $value = $fallbackValue;
749  }
750  }
751 
756  protected function mergeMagicWords( &$value, $fallbackValue ) {
757  foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
758  if ( !isset( $value[$magicName] ) ) {
759  $value[$magicName] = $fallbackInfo;
760  } else {
761  $oldSynonyms = array_slice( $fallbackInfo, 1 );
762  $newSynonyms = array_slice( $value[$magicName], 1 );
763  $synonyms = array_values( array_unique( array_merge(
764  $newSynonyms, $oldSynonyms ) ) );
765  $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
766  }
767  }
768  }
769 
783  protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
784  $used = false;
785  foreach ( $codeSequence as $code ) {
786  if ( isset( $fallbackValue[$code] ) ) {
787  $this->mergeItem( $key, $value, $fallbackValue[$code] );
788  $used = true;
789  }
790  }
791 
792  return $used;
793  }
794 
802  public function getMessagesDirs() {
803  global $IP;
804 
805  $config = MediaWikiServices::getInstance()->getMainConfig();
806  $messagesDirs = $config->get( 'MessagesDirs' );
807  return [
808  'core' => "$IP/languages/i18n",
809  'api' => "$IP/includes/api/i18n",
810  'oojs-ui' => "$IP/resources/lib/oojs-ui/i18n",
811  ] + $messagesDirs;
812  }
813 
820  public function recache( $code ) {
822 
823  if ( !$code ) {
824  throw new MWException( "Invalid language code requested" );
825  }
826  $this->recachedLangs[$code] = true;
827 
828  # Initial values
829  $initialData = array_fill_keys( self::$allKeys, null );
830  $coreData = $initialData;
831  $deps = [];
832 
833  # Load the primary localisation from the source file
834  $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
835  if ( $data === false ) {
836  wfDebug( __METHOD__ . ": no localisation file for $code, using fallback to en\n" );
837  $coreData['fallback'] = 'en';
838  } else {
839  wfDebug( __METHOD__ . ": got localisation for $code from source\n" );
840 
841  # Merge primary localisation
842  foreach ( $data as $key => $value ) {
843  $this->mergeItem( $key, $coreData[$key], $value );
844  }
845  }
846 
847  # Fill in the fallback if it's not there already
848  if ( is_null( $coreData['fallback'] ) ) {
849  $coreData['fallback'] = $code === 'en' ? false : 'en';
850  }
851  if ( $coreData['fallback'] === false ) {
852  $coreData['fallbackSequence'] = [];
853  } else {
854  $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
855  $len = count( $coreData['fallbackSequence'] );
856 
857  # Ensure that the sequence ends at en
858  if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
859  $coreData['fallbackSequence'][] = 'en';
860  }
861  }
862 
863  $codeSequence = array_merge( [ $code ], $coreData['fallbackSequence'] );
864  $messageDirs = $this->getMessagesDirs();
865 
866  # Load non-JSON localisation data for extensions
867  $extensionData = array_fill_keys( $codeSequence, $initialData );
868  foreach ( $wgExtensionMessagesFiles as $extension => $fileName ) {
869  if ( isset( $messageDirs[$extension] ) ) {
870  # This extension has JSON message data; skip the PHP shim
871  continue;
872  }
873 
874  $data = $this->readPHPFile( $fileName, 'extension' );
875  $used = false;
876 
877  foreach ( $data as $key => $item ) {
878  foreach ( $codeSequence as $csCode ) {
879  if ( isset( $item[$csCode] ) ) {
880  $this->mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] );
881  $used = true;
882  }
883  }
884  }
885 
886  if ( $used ) {
887  $deps[] = new FileDependency( $fileName );
888  }
889  }
890 
891  # Load the localisation data for each fallback, then merge it into the full array
892  $allData = $initialData;
893  foreach ( $codeSequence as $csCode ) {
894  $csData = $initialData;
895 
896  # Load core messages and the extension localisations.
897  foreach ( $messageDirs as $dirs ) {
898  foreach ( (array)$dirs as $dir ) {
899  $fileName = "$dir/$csCode.json";
900  $data = $this->readJSONFile( $fileName );
901 
902  foreach ( $data as $key => $item ) {
903  $this->mergeItem( $key, $csData[$key], $item );
904  }
905 
906  $deps[] = new FileDependency( $fileName );
907  }
908  }
909 
910  # Merge non-JSON extension data
911  if ( isset( $extensionData[$csCode] ) ) {
912  foreach ( $extensionData[$csCode] as $key => $item ) {
913  $this->mergeItem( $key, $csData[$key], $item );
914  }
915  }
916 
917  if ( $csCode === $code ) {
918  # Merge core data into extension data
919  foreach ( $coreData as $key => $item ) {
920  $this->mergeItem( $key, $csData[$key], $item );
921  }
922  } else {
923  # Load the secondary localisation from the source file to
924  # avoid infinite cycles on cyclic fallbacks
925  $fbData = $this->readSourceFilesAndRegisterDeps( $csCode, $deps );
926  if ( $fbData !== false ) {
927  # Only merge the keys that make sense to merge
928  foreach ( self::$allKeys as $key ) {
929  if ( !isset( $fbData[$key] ) ) {
930  continue;
931  }
932 
933  if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
934  $this->mergeItem( $key, $csData[$key], $fbData[$key] );
935  }
936  }
937  }
938  }
939 
940  # Allow extensions an opportunity to adjust the data for this
941  # fallback
942  Hooks::run( 'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
943 
944  # Merge the data for this fallback into the final array
945  if ( $csCode === $code ) {
946  $allData = $csData;
947  } else {
948  foreach ( self::$allKeys as $key ) {
949  if ( !isset( $csData[$key] ) ) {
950  continue;
951  }
952 
953  if ( is_null( $allData[$key] ) || $this->isMergeableKey( $key ) ) {
954  $this->mergeItem( $key, $allData[$key], $csData[$key] );
955  }
956  }
957  }
958  }
959 
960  # Add cache dependencies for any referenced globals
961  $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
962  // The 'MessagesDirs' config setting is used in LocalisationCache::getMessagesDirs().
963  // We use the key 'wgMessagesDirs' for historical reasons.
964  $deps['wgMessagesDirs'] = new MainConfigDependency( 'MessagesDirs' );
965  $deps['version'] = new ConstantDependency( 'LocalisationCache::VERSION' );
966 
967  # Add dependencies to the cache entry
968  $allData['deps'] = $deps;
969 
970  # Replace spaces with underscores in namespace names
971  $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
972 
973  # And do the same for special page aliases. $page is an array.
974  foreach ( $allData['specialPageAliases'] as &$page ) {
975  $page = str_replace( ' ', '_', $page );
976  }
977  # Decouple the reference to prevent accidental damage
978  unset( $page );
979 
980  # If there were no plural rules, return an empty array
981  if ( $allData['pluralRules'] === null ) {
982  $allData['pluralRules'] = [];
983  }
984  if ( $allData['compiledPluralRules'] === null ) {
985  $allData['compiledPluralRules'] = [];
986  }
987  # If there were no plural rule types, return an empty array
988  if ( $allData['pluralRuleTypes'] === null ) {
989  $allData['pluralRuleTypes'] = [];
990  }
991 
992  # Set the list keys
993  $allData['list'] = [];
994  foreach ( self::$splitKeys as $key ) {
995  $allData['list'][$key] = array_keys( $allData[$key] );
996  }
997  # Run hooks
998  $purgeBlobs = true;
999  Hooks::run( 'LocalisationCacheRecache', [ $this, $code, &$allData, &$purgeBlobs ] );
1000 
1001  if ( is_null( $allData['namespaceNames'] ) ) {
1002  throw new MWException( __METHOD__ . ': Localisation data failed sanity check! ' .
1003  'Check that your languages/messages/MessagesEn.php file is intact.' );
1004  }
1005 
1006  # Set the preload key
1007  $allData['preload'] = $this->buildPreload( $allData );
1008 
1009  # Save to the process cache and register the items loaded
1010  $this->data[$code] = $allData;
1011  foreach ( $allData as $key => $item ) {
1012  $this->loadedItems[$code][$key] = true;
1013  }
1014 
1015  # Save to the persistent cache
1016  $this->store->startWrite( $code );
1017  foreach ( $allData as $key => $value ) {
1018  if ( in_array( $key, self::$splitKeys ) ) {
1019  foreach ( $value as $subkey => $subvalue ) {
1020  $this->store->set( "$key:$subkey", $subvalue );
1021  }
1022  } else {
1023  $this->store->set( $key, $value );
1024  }
1025  }
1026  $this->store->finishWrite();
1027 
1028  # Clear out the MessageBlobStore
1029  # HACK: If using a null (i.e. disabled) storage backend, we
1030  # can't write to the MessageBlobStore either
1031  if ( $purgeBlobs && !$this->store instanceof LCStoreNull ) {
1032  $blobStore = new MessageBlobStore();
1033  $blobStore->clear();
1034  }
1035  }
1036 
1045  protected function buildPreload( $data ) {
1046  $preload = [ 'messages' => [] ];
1047  foreach ( self::$preloadedKeys as $key ) {
1048  $preload[$key] = $data[$key];
1049  }
1050 
1051  foreach ( $data['preloadedMessages'] as $subkey ) {
1052  if ( isset( $data['messages'][$subkey] ) ) {
1053  $subitem = $data['messages'][$subkey];
1054  } else {
1055  $subitem = null;
1056  }
1057  $preload['messages'][$subkey] = $subitem;
1058  }
1059 
1060  return $preload;
1061  }
1062 
1068  public function unload( $code ) {
1069  unset( $this->data[$code] );
1070  unset( $this->loadedItems[$code] );
1071  unset( $this->loadedSubitems[$code] );
1072  unset( $this->initialisedLangs[$code] );
1073  unset( $this->shallowFallbacks[$code] );
1074 
1075  foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
1076  if ( $fbCode === $code ) {
1077  $this->unload( $shallowCode );
1078  }
1079  }
1080  }
1081 
1085  public function unloadAll() {
1086  foreach ( $this->initialisedLangs as $lang => $unused ) {
1087  $this->unload( $lang );
1088  }
1089  }
1090 
1094  public function disableBackend() {
1095  $this->store = new LCStoreNull;
1096  $this->manualRecache = false;
1097  }
1098 
1099 }
FileDependency
Definition: CacheDependency.php:153
LocalisationCache\loadSubitem
loadSubitem( $code, $key, $subkey)
Load a subitem into the cache.
Definition: LocalisationCache.php:371
LocalisationCache\$initialisedLangs
$initialisedLangs
An array where presence of a key indicates that that language has been initialised.
Definition: LocalisationCache.php:94
LocalisationCache\$manualRecache
$manualRecache
True if recaching should only be done on an explicit call to recache().
Definition: LocalisationCache.php:52
LocalisationCache\initLanguage
initLanguage( $code)
Initialise a language in this object.
Definition: LocalisationCache.php:445
LocalisationCache\$conf
$conf
Configuration associative array.
Definition: LocalisationCache.php:45
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
LocalisationCache\__construct
__construct( $conf)
Constructor.
Definition: LocalisationCache.php:193
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
captcha-old.count
count
Definition: captcha-old.py:225
LocalisationCache\isExpired
isExpired( $code)
Returns true if the cache identified by $code is missing or expired.
Definition: LocalisationCache.php:407
LocalisationCache\getSubitemList
getSubitemList( $code, $key)
Get the list of subitem keys for a given item.
Definition: LocalisationCache.php:316
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
LocalisationCache\$recachedLangs
$recachedLangs
An array where the keys are codes that have been recached by this instance.
Definition: LocalisationCache.php:106
ConstantDependency
Definition: CacheDependency.php:279
MainConfigDependency
Definition: CacheDependency.php:251
$wgCacheDirectory
$wgCacheDirectory
Directory for caching data in the local filesystem.
Definition: DefaultSettings.php:2205
LCStore
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
Language\getMessagesFileName
static getMessagesFileName( $code)
Definition: Language.php:4371
LocalisationCache\$mergeableAliasListKeys
static $mergeableAliasListKeys
Keys for items which contain an array of arrays of equivalent aliases for each subitem.
Definition: LocalisationCache.php:139
LocalisationCache\readSourceFilesAndRegisterDeps
readSourceFilesAndRegisterDeps( $code, &$deps)
Read the data from the source files for a given language, and register the relevant dependencies in t...
Definition: LocalisationCache.php:694
LocalisationCache\getItem
getItem( $code, $key)
Get a cache item.
Definition: LocalisationCache.php:271
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1092
GlobalDependency
Definition: CacheDependency.php:227
php
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
LocalisationCache\recache
recache( $code)
Load localisation data for a given language for both core and extensions and save it to the persisten...
Definition: LocalisationCache.php:820
LocalisationCache\readJSONFile
readJSONFile( $fileName)
Read a JSON file containing localisation messages.
Definition: LocalisationCache.php:549
LocalisationCache\$pluralRules
$pluralRules
Associative array of cached plural rules.
Definition: LocalisationCache.php:167
LocalisationCache\mergeExtensionItem
mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue)
Given an array mapping language code to localisation value, such as is found in extension *....
Definition: LocalisationCache.php:783
LocalisationCache\$preloadedKeys
static $preloadedKeys
Keys which are loaded automatically by initLanguage()
Definition: LocalisationCache.php:161
LocalisationCache\getPluralRules
getPluralRules( $code)
Get the plural rules for a given language from the XML files.
Definition: LocalisationCache.php:604
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
MWException
MediaWiki exception.
Definition: MWException.php:26
LocalisationCache\unload
unload( $code)
Unload the data for a given language from the object cache.
Definition: LocalisationCache.php:1068
LocalisationCache\getMessagesDirs
getMessagesDirs()
Gets the combined list of messages dirs from core and extensions.
Definition: LocalisationCache.php:802
LocalisationCache\mergeMagicWords
mergeMagicWords(&$value, $fallbackValue)
Definition: LocalisationCache.php:756
$page
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:2536
$IP
$IP
Definition: update.php:3
store
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
LocalisationCache\disableBackend
disableBackend()
Disable the storage backend.
Definition: LocalisationCache.php:1094
LocalisationCache\getCompiledPluralRules
getCompiledPluralRules( $code)
Get the compiled plural rules for a given language from the XML files.
Definition: LocalisationCache.php:581
$dirs
$dirs
Definition: mergeMessageFileList.php:195
LocalisationCache\$optionalMergeKeys
static $optionalMergeKeys
Keys for items which contain an associative array, and may be merged if the primary value contains th...
Definition: LocalisationCache.php:146
LocalisationCache\$loadedItems
$loadedItems
A 2-d associative array, code/key, where presence indicates that the item is loaded.
Definition: LocalisationCache.php:81
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:999
LocalisationCache\readPHPFile
readPHPFile( $_fileName, $_fileType)
Read a PHP file containing localisation data.
Definition: LocalisationCache.php:520
$dir
$dir
Definition: Autoload.php:8
Language\isValidBuiltInCode
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:362
LocalisationCache\$mergeableMapKeys
static $mergeableMapKeys
Keys for items which consist of associative arrays, which may be merged by a fallback sequence.
Definition: LocalisationCache.php:126
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
LocalisationCache\buildPreload
buildPreload( $data)
Build the preload item from the given pre-cache data.
Definition: LocalisationCache.php:1045
LocalisationCache\$mergeableKeys
$mergeableKeys
Definition: LocalisationCache.php:183
$value
$value
Definition: styleTest.css.php:45
LocalisationCache\loadPluralFile
loadPluralFile( $fileName)
Load a plural XML file with the given filename, compile the relevant rules, and save the compiled rul...
Definition: LocalisationCache.php:655
LocalisationCache
Class for caching the contents of localisation files, Messages*.php and *.i18n.php.
Definition: LocalisationCache.php:41
LCStoreNull
Null store backend, used to avoid DB errors during install.
Definition: LCStoreNull.php:24
LocalisationCache\$forceRecache
$forceRecache
True to treat all files as expired until they are regenerated by this object.
Definition: LocalisationCache.php:57
LocalisationCache\loadItem
loadItem( $code, $key)
Load an item into the cache.
Definition: LocalisationCache.php:334
CacheDependency
Definition: CacheDependency.php:137
LocalisationCache\$pluralRuleTypes
$pluralRuleTypes
Associative array of cached plural rule types.
Definition: LocalisationCache.php:181
CdbReader
Definition: CdbCompat.php:31
$code
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:783
LocalisationCache\initShallowFallback
initShallowFallback( $primaryCode, $fallbackCode)
Create a fallback from one language to another, without creating a complete persistent cache.
Definition: LocalisationCache.php:506
$wgExtensionMessagesFiles
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition: magicword.txt:43
CacheDependency\isExpired
isExpired()
Returns true if the dependency is expired, false otherwise.
CdbWriter
Definition: CdbCompat.php:37
as
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
LocalisationCache\VERSION
const VERSION
Definition: LocalisationCache.php:42
LocalisationCache\isMergeableKey
isMergeableKey( $key)
Returns true if the given key is mergeable, that is, if it is an associative array which can be merge...
Definition: LocalisationCache.php:248
LocalisationCache\mergeItem
mergeItem( $key, &$value, $fallbackValue)
Merge two localisation values, a primary and a fallback, overwriting the primary value in place.
Definition: LocalisationCache.php:726
MessageBlobStore
This class generates message blobs for use by ResourceLoader modules.
Definition: MessageBlobStore.php:37
$keys
$keys
Definition: testCompression.php:65
LocalisationCache\unloadAll
unloadAll()
Unload all data.
Definition: LocalisationCache.php:1085
LocalisationCache\getPluralRuleTypes
getPluralRuleTypes( $code)
Get the plural rule types for a given language from the XML files.
Definition: LocalisationCache.php:622
LocalisationCache\$data
$data
The cache data.
Definition: LocalisationCache.php:65
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
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
Language\isSupportedLanguage
static isSupportedLanguage( $code)
Checks whether any localisation is available for that language tag in MediaWiki (MessagesXx....
Definition: Language.php:255
LocalisationCache\$splitKeys
static $splitKeys
Keys for items where the subitems are stored in the backend separately.
Definition: LocalisationCache.php:156
LocalisationCache\$mergeableListKeys
static $mergeableListKeys
Keys for items which are a numbered array.
Definition: LocalisationCache.php:133
LocalisationCache\getSubitem
getSubitem( $code, $key, $subkey)
Get a subitem, for instance a single message for a given language.
Definition: LocalisationCache.php:290
LocalisationCache\$shallowFallbacks
$shallowFallbacks
An array mapping non-existent pseudo-languages to fallback languages.
Definition: LocalisationCache.php:101
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
LocalisationCache\$loadedSubitems
$loadedSubitems
A 3-d associative array, code/key/subkey, where presence indicates that the subitem is loaded.
Definition: LocalisationCache.php:87
LocalisationCache\loadPluralFiles
loadPluralFiles()
Load the plural XML files.
Definition: LocalisationCache.php:636
data
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
LocalisationCache\$allKeys
static $allKeys
All item keys.
Definition: LocalisationCache.php:111
LocalisationCache\$magicWordKeys
static $magicWordKeys
Keys for items that are formatted like $magicWords.
Definition: LocalisationCache.php:151
array
the array() calling protocol came about after MediaWiki 1.4rc1.
LocalisationCache\$store
LCStore $store
The persistent store object.
Definition: LocalisationCache.php:72