Merge "registration: Only allow one extension to set a specific config setting"
[lhc/web/wiklou.git] / includes / cache / localisation / LocalisationCache.php
1 <?php
2 /**
3 * Cache of the contents of localisation files.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use CLDRPluralRuleParser\Evaluator;
24 use CLDRPluralRuleParser\Error as CLDRPluralRuleError;
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Class for caching the contents of localisation files, Messages*.php
29 * and *.i18n.php.
30 *
31 * An instance of this class is available using Language::getLocalisationCache().
32 *
33 * The values retrieved from here are merged, containing items from extension
34 * files, core messages files and the language fallback sequence (e.g. zh-cn ->
35 * zh-hans -> en ). Some common errors are corrected, for example namespace
36 * names with spaces instead of underscores, but heavyweight processing, such
37 * as grammatical transformation, is done by the caller.
38 */
39 class LocalisationCache {
40 const VERSION = 4;
41
42 /** Configuration associative array */
43 private $conf;
44
45 /**
46 * True if recaching should only be done on an explicit call to recache().
47 * Setting this reduces the overhead of cache freshness checking, which
48 * requires doing a stat() for every extension i18n file.
49 */
50 private $manualRecache = false;
51
52 /**
53 * True to treat all files as expired until they are regenerated by this object.
54 */
55 private $forceRecache = false;
56
57 /**
58 * The cache data. 3-d array, where the first key is the language code,
59 * the second key is the item key e.g. 'messages', and the third key is
60 * an item specific subkey index. Some items are not arrays and so for those
61 * items, there are no subkeys.
62 */
63 protected $data = [];
64
65 /**
66 * The persistent store object. An instance of LCStore.
67 *
68 * @var LCStore
69 */
70 private $store;
71
72 /**
73 * A 2-d associative array, code/key, where presence indicates that the item
74 * is loaded. Value arbitrary.
75 *
76 * For split items, if set, this indicates that all of the subitems have been
77 * loaded.
78 */
79 private $loadedItems = [];
80
81 /**
82 * A 3-d associative array, code/key/subkey, where presence indicates that
83 * the subitem is loaded. Only used for the split items, i.e. messages.
84 */
85 private $loadedSubitems = [];
86
87 /**
88 * An array where presence of a key indicates that that language has been
89 * initialised. Initialisation includes checking for cache expiry and doing
90 * any necessary updates.
91 */
92 private $initialisedLangs = [];
93
94 /**
95 * An array mapping non-existent pseudo-languages to fallback languages. This
96 * is filled by initShallowFallback() when data is requested from a language
97 * that lacks a Messages*.php file.
98 */
99 private $shallowFallbacks = [];
100
101 /**
102 * An array where the keys are codes that have been recached by this instance.
103 */
104 private $recachedLangs = [];
105
106 /**
107 * All item keys
108 */
109 static public $allKeys = [
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',
118 ];
119
120 /**
121 * Keys for items which consist of associative arrays, which may be merged
122 * by a fallback sequence.
123 */
124 static public $mergeableMapKeys = [ 'messages', 'namespaceNames',
125 'namespaceAliases', 'dateFormats', 'imageFiles', 'preloadedMessages'
126 ];
127
128 /**
129 * Keys for items which are a numbered array.
130 */
131 static public $mergeableListKeys = [ 'extraUserToggles' ];
132
133 /**
134 * Keys for items which contain an array of arrays of equivalent aliases
135 * for each subitem. The aliases may be merged by a fallback sequence.
136 */
137 static public $mergeableAliasListKeys = [ 'specialPageAliases' ];
138
139 /**
140 * Keys for items which contain an associative array, and may be merged if
141 * the primary value contains the special array key "inherit". That array
142 * key is removed after the first merge.
143 */
144 static public $optionalMergeKeys = [ 'bookstoreList' ];
145
146 /**
147 * Keys for items that are formatted like $magicWords
148 */
149 static public $magicWordKeys = [ 'magicWords' ];
150
151 /**
152 * Keys for items where the subitems are stored in the backend separately.
153 */
154 static public $splitKeys = [ 'messages' ];
155
156 /**
157 * Keys which are loaded automatically by initLanguage()
158 */
159 static public $preloadedKeys = [ 'dateFormats', 'namespaceNames' ];
160
161 /**
162 * Associative array of cached plural rules. The key is the language code,
163 * the value is an array of plural rules for that language.
164 */
165 private $pluralRules = null;
166
167 /**
168 * Associative array of cached plural rule types. The key is the language
169 * code, the value is an array of plural rule types for that language. For
170 * example, $pluralRuleTypes['ar'] = ['zero', 'one', 'two', 'few', 'many'].
171 * The index for each rule type matches the index for the rule in
172 * $pluralRules, thus allowing correlation between the two. The reason we
173 * don't just use the type names as the keys in $pluralRules is because
174 * Language::convertPlural applies the rules based on numeric order (or
175 * explicit numeric parameter), not based on the name of the rule type. For
176 * example, {{plural:count|wordform1|wordform2|wordform3}}, rather than
177 * {{plural:count|one=wordform1|two=wordform2|many=wordform3}}.
178 */
179 private $pluralRuleTypes = null;
180
181 private $mergeableKeys = null;
182
183 /**
184 * For constructor parameters, see the documentation in DefaultSettings.php
185 * for $wgLocalisationCacheConf.
186 *
187 * @param array $conf
188 * @throws MWException
189 */
190 function __construct( $conf ) {
191 global $wgCacheDirectory;
192
193 $this->conf = $conf;
194 $storeConf = [];
195 if ( !empty( $conf['storeClass'] ) ) {
196 $storeClass = $conf['storeClass'];
197 } else {
198 switch ( $conf['store'] ) {
199 case 'files':
200 case 'file':
201 $storeClass = 'LCStoreCDB';
202 break;
203 case 'db':
204 $storeClass = 'LCStoreDB';
205 break;
206 case 'array':
207 $storeClass = 'LCStoreStaticArray';
208 break;
209 case 'detect':
210 if ( !empty( $conf['storeDirectory'] ) ) {
211 $storeClass = 'LCStoreCDB';
212 } elseif ( $wgCacheDirectory ) {
213 $storeConf['directory'] = $wgCacheDirectory;
214 $storeClass = 'LCStoreCDB';
215 } else {
216 $storeClass = 'LCStoreDB';
217 }
218 break;
219 default:
220 throw new MWException(
221 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
222 );
223 }
224 }
225
226 wfDebugLog( 'caches', static::class . ": using store $storeClass" );
227 if ( !empty( $conf['storeDirectory'] ) ) {
228 $storeConf['directory'] = $conf['storeDirectory'];
229 }
230
231 $this->store = new $storeClass( $storeConf );
232 foreach ( [ 'manualRecache', 'forceRecache' ] as $var ) {
233 if ( isset( $conf[$var] ) ) {
234 $this->$var = $conf[$var];
235 }
236 }
237 }
238
239 /**
240 * Returns true if the given key is mergeable, that is, if it is an associative
241 * array which can be merged through a fallback sequence.
242 * @param string $key
243 * @return bool
244 */
245 public function isMergeableKey( $key ) {
246 if ( $this->mergeableKeys === null ) {
247 $this->mergeableKeys = array_flip( array_merge(
248 self::$mergeableMapKeys,
249 self::$mergeableListKeys,
250 self::$mergeableAliasListKeys,
251 self::$optionalMergeKeys,
252 self::$magicWordKeys
253 ) );
254 }
255
256 return isset( $this->mergeableKeys[$key] );
257 }
258
259 /**
260 * Get a cache item.
261 *
262 * Warning: this may be slow for split items (messages), since it will
263 * need to fetch all of the subitems from the cache individually.
264 * @param string $code
265 * @param string $key
266 * @return mixed
267 */
268 public function getItem( $code, $key ) {
269 if ( !isset( $this->loadedItems[$code][$key] ) ) {
270 $this->loadItem( $code, $key );
271 }
272
273 if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
274 return $this->shallowFallbacks[$code];
275 }
276
277 return $this->data[$code][$key];
278 }
279
280 /**
281 * Get a subitem, for instance a single message for a given language.
282 * @param string $code
283 * @param string $key
284 * @param string $subkey
285 * @return mixed|null
286 */
287 public function getSubitem( $code, $key, $subkey ) {
288 if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) &&
289 !isset( $this->loadedItems[$code][$key] )
290 ) {
291 $this->loadSubitem( $code, $key, $subkey );
292 }
293
294 if ( isset( $this->data[$code][$key][$subkey] ) ) {
295 return $this->data[$code][$key][$subkey];
296 } else {
297 return null;
298 }
299 }
300
301 /**
302 * Get the list of subitem keys for a given item.
303 *
304 * This is faster than array_keys($lc->getItem(...)) for the items listed in
305 * self::$splitKeys.
306 *
307 * Will return null if the item is not found, or false if the item is not an
308 * array.
309 * @param string $code
310 * @param string $key
311 * @return bool|null|string|string[]
312 */
313 public function getSubitemList( $code, $key ) {
314 if ( in_array( $key, self::$splitKeys ) ) {
315 return $this->getSubitem( $code, 'list', $key );
316 } else {
317 $item = $this->getItem( $code, $key );
318 if ( is_array( $item ) ) {
319 return array_keys( $item );
320 } else {
321 return false;
322 }
323 }
324 }
325
326 /**
327 * Load an item into the cache.
328 * @param string $code
329 * @param string $key
330 */
331 protected function loadItem( $code, $key ) {
332 if ( !isset( $this->initialisedLangs[$code] ) ) {
333 $this->initLanguage( $code );
334 }
335
336 // Check to see if initLanguage() loaded it for us
337 if ( isset( $this->loadedItems[$code][$key] ) ) {
338 return;
339 }
340
341 if ( isset( $this->shallowFallbacks[$code] ) ) {
342 $this->loadItem( $this->shallowFallbacks[$code], $key );
343
344 return;
345 }
346
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] ) ) {
351 continue;
352 }
353 $this->data[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
354 }
355 } else {
356 $this->data[$code][$key] = $this->store->get( $code, $key );
357 }
358
359 $this->loadedItems[$code][$key] = true;
360 }
361
362 /**
363 * Load a subitem into the cache
364 * @param string $code
365 * @param string $key
366 * @param string $subkey
367 */
368 protected function loadSubitem( $code, $key, $subkey ) {
369 if ( !in_array( $key, self::$splitKeys ) ) {
370 $this->loadItem( $code, $key );
371
372 return;
373 }
374
375 if ( !isset( $this->initialisedLangs[$code] ) ) {
376 $this->initLanguage( $code );
377 }
378
379 // Check to see if initLanguage() loaded it for us
380 if ( isset( $this->loadedItems[$code][$key] ) ||
381 isset( $this->loadedSubitems[$code][$key][$subkey] )
382 ) {
383 return;
384 }
385
386 if ( isset( $this->shallowFallbacks[$code] ) ) {
387 $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
388
389 return;
390 }
391
392 $value = $this->store->get( $code, "$key:$subkey" );
393 $this->data[$code][$key][$subkey] = $value;
394 $this->loadedSubitems[$code][$key][$subkey] = true;
395 }
396
397 /**
398 * Returns true if the cache identified by $code is missing or expired.
399 *
400 * @param string $code
401 *
402 * @return bool
403 */
404 public function isExpired( $code ) {
405 if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
406 wfDebug( __METHOD__ . "($code): forced reload\n" );
407
408 return true;
409 }
410
411 $deps = $this->store->get( $code, 'deps' );
412 $keys = $this->store->get( $code, 'list' );
413 $preload = $this->store->get( $code, 'preload' );
414 // Different keys may expire separately for some stores
415 if ( $deps === null || $keys === null || $preload === null ) {
416 wfDebug( __METHOD__ . "($code): cache missing, need to make one\n" );
417
418 return true;
419 }
420
421 foreach ( $deps as $dep ) {
422 // Because we're unserializing stuff from cache, we
423 // could receive objects of classes that don't exist
424 // anymore (e.g. uninstalled extensions)
425 // When this happens, always expire the cache
426 if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
427 wfDebug( __METHOD__ . "($code): cache for $code expired due to " .
428 get_class( $dep ) . "\n" );
429
430 return true;
431 }
432 }
433
434 return false;
435 }
436
437 /**
438 * Initialise a language in this object. Rebuild the cache if necessary.
439 * @param string $code
440 * @throws MWException
441 */
442 protected function initLanguage( $code ) {
443 if ( isset( $this->initialisedLangs[$code] ) ) {
444 return;
445 }
446
447 $this->initialisedLangs[$code] = true;
448
449 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
450 if ( !Language::isValidBuiltInCode( $code ) ) {
451 $this->initShallowFallback( $code, 'en' );
452
453 return;
454 }
455
456 # Recache the data if necessary
457 if ( !$this->manualRecache && $this->isExpired( $code ) ) {
458 if ( Language::isSupportedLanguage( $code ) ) {
459 $this->recache( $code );
460 } elseif ( $code === 'en' ) {
461 throw new MWException( 'MessagesEn.php is missing.' );
462 } else {
463 $this->initShallowFallback( $code, 'en' );
464 }
465
466 return;
467 }
468
469 # Preload some stuff
470 $preload = $this->getItem( $code, 'preload' );
471 if ( $preload === null ) {
472 if ( $this->manualRecache ) {
473 // No Messages*.php file. Do shallow fallback to en.
474 if ( $code === 'en' ) {
475 throw new MWException( 'No localisation cache found for English. ' .
476 'Please run maintenance/rebuildLocalisationCache.php.' );
477 }
478 $this->initShallowFallback( $code, 'en' );
479
480 return;
481 } else {
482 throw new MWException( 'Invalid or missing localisation cache.' );
483 }
484 }
485 $this->data[$code] = $preload;
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;
490 }
491 } else {
492 $this->loadedItems[$code][$key] = true;
493 }
494 }
495 }
496
497 /**
498 * Create a fallback from one language to another, without creating a
499 * complete persistent cache.
500 * @param string $primaryCode
501 * @param string $fallbackCode
502 */
503 public function initShallowFallback( $primaryCode, $fallbackCode ) {
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;
508 }
509
510 /**
511 * Read a PHP file containing localisation data.
512 * @param string $_fileName
513 * @param string $_fileType
514 * @throws MWException
515 * @return array
516 */
517 protected function readPHPFile( $_fileName, $_fileType ) {
518 // Disable APC caching
519 MediaWiki\suppressWarnings();
520 $_apcEnabled = ini_set( 'apc.cache_by_default', '0' );
521 MediaWiki\restoreWarnings();
522
523 include $_fileName;
524
525 MediaWiki\suppressWarnings();
526 ini_set( 'apc.cache_by_default', $_apcEnabled );
527 MediaWiki\restoreWarnings();
528
529 if ( $_fileType == 'core' || $_fileType == 'extension' ) {
530 $data = compact( self::$allKeys );
531 } elseif ( $_fileType == 'aliases' ) {
532 $data = compact( 'aliases' );
533 } else {
534 throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" );
535 }
536
537 return $data;
538 }
539
540 /**
541 * Read a JSON file containing localisation messages.
542 * @param string $fileName Name of file to read
543 * @throws MWException If there is a syntax error in the JSON file
544 * @return array Array with a 'messages' key, or empty array if the file doesn't exist
545 */
546 public function readJSONFile( $fileName ) {
547 if ( !is_readable( $fileName ) ) {
548 return [];
549 }
550
551 $json = file_get_contents( $fileName );
552 if ( $json === false ) {
553 return [];
554 }
555
556 $data = FormatJson::decode( $json, true );
557 if ( $data === null ) {
558 throw new MWException( __METHOD__ . ": Invalid JSON file: $fileName" );
559 }
560
561 // Remove keys starting with '@', they're reserved for metadata and non-message data
562 foreach ( $data as $key => $unused ) {
563 if ( $key === '' || $key[0] === '@' ) {
564 unset( $data[$key] );
565 }
566 }
567
568 // The JSON format only supports messages, none of the other variables, so wrap the data
569 return [ 'messages' => $data ];
570 }
571
572 /**
573 * Get the compiled plural rules for a given language from the XML files.
574 * @since 1.20
575 * @param string $code
576 * @return array|null
577 */
578 public function getCompiledPluralRules( $code ) {
579 $rules = $this->getPluralRules( $code );
580 if ( $rules === null ) {
581 return null;
582 }
583 try {
584 $compiledRules = Evaluator::compile( $rules );
585 } catch ( CLDRPluralRuleError $e ) {
586 wfDebugLog( 'l10n', $e->getMessage() );
587
588 return [];
589 }
590
591 return $compiledRules;
592 }
593
594 /**
595 * Get the plural rules for a given language from the XML files.
596 * Cached.
597 * @since 1.20
598 * @param string $code
599 * @return array|null
600 */
601 public function getPluralRules( $code ) {
602 if ( $this->pluralRules === null ) {
603 $this->loadPluralFiles();
604 }
605 if ( !isset( $this->pluralRules[$code] ) ) {
606 return null;
607 } else {
608 return $this->pluralRules[$code];
609 }
610 }
611
612 /**
613 * Get the plural rule types for a given language from the XML files.
614 * Cached.
615 * @since 1.22
616 * @param string $code
617 * @return array|null
618 */
619 public function getPluralRuleTypes( $code ) {
620 if ( $this->pluralRuleTypes === null ) {
621 $this->loadPluralFiles();
622 }
623 if ( !isset( $this->pluralRuleTypes[$code] ) ) {
624 return null;
625 } else {
626 return $this->pluralRuleTypes[$code];
627 }
628 }
629
630 /**
631 * Load the plural XML files.
632 */
633 protected function loadPluralFiles() {
634 global $IP;
635 $cldrPlural = "$IP/languages/data/plurals.xml";
636 $mwPlural = "$IP/languages/data/plurals-mediawiki.xml";
637 // Load CLDR plural rules
638 $this->loadPluralFile( $cldrPlural );
639 if ( file_exists( $mwPlural ) ) {
640 // Override or extend
641 $this->loadPluralFile( $mwPlural );
642 }
643 }
644
645 /**
646 * Load a plural XML file with the given filename, compile the relevant
647 * rules, and save the compiled rules in a process-local cache.
648 *
649 * @param string $fileName
650 * @throws MWException
651 */
652 protected function loadPluralFile( $fileName ) {
653 // Use file_get_contents instead of DOMDocument::load (T58439)
654 $xml = file_get_contents( $fileName );
655 if ( !$xml ) {
656 throw new MWException( "Unable to read plurals file $fileName" );
657 }
658 $doc = new DOMDocument;
659 $doc->loadXML( $xml );
660 $rulesets = $doc->getElementsByTagName( "pluralRules" );
661 foreach ( $rulesets as $ruleset ) {
662 $codes = $ruleset->getAttribute( 'locales' );
663 $rules = [];
664 $ruleTypes = [];
665 $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
666 foreach ( $ruleElements as $elt ) {
667 $ruleType = $elt->getAttribute( 'count' );
668 if ( $ruleType === 'other' ) {
669 // Don't record "other" rules, which have an empty condition
670 continue;
671 }
672 $rules[] = $elt->nodeValue;
673 $ruleTypes[] = $ruleType;
674 }
675 foreach ( explode( ' ', $codes ) as $code ) {
676 $this->pluralRules[$code] = $rules;
677 $this->pluralRuleTypes[$code] = $ruleTypes;
678 }
679 }
680 }
681
682 /**
683 * Read the data from the source files for a given language, and register
684 * the relevant dependencies in the $deps array. If the localisation
685 * exists, the data array is returned, otherwise false is returned.
686 *
687 * @param string $code
688 * @param array &$deps
689 * @return array
690 */
691 protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
692 global $IP;
693
694 // This reads in the PHP i18n file with non-messages l10n data
695 $fileName = Language::getMessagesFileName( $code );
696 if ( !file_exists( $fileName ) ) {
697 $data = [];
698 } else {
699 $deps[] = new FileDependency( $fileName );
700 $data = $this->readPHPFile( $fileName, 'core' );
701 }
702
703 # Load CLDR plural rules for JavaScript
704 $data['pluralRules'] = $this->getPluralRules( $code );
705 # And for PHP
706 $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
707 # Load plural rule types
708 $data['pluralRuleTypes'] = $this->getPluralRuleTypes( $code );
709
710 $deps['plurals'] = new FileDependency( "$IP/languages/data/plurals.xml" );
711 $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
712
713 return $data;
714 }
715
716 /**
717 * Merge two localisation values, a primary and a fallback, overwriting the
718 * primary value in place.
719 * @param string $key
720 * @param mixed &$value
721 * @param mixed $fallbackValue
722 */
723 protected function mergeItem( $key, &$value, $fallbackValue ) {
724 if ( !is_null( $value ) ) {
725 if ( !is_null( $fallbackValue ) ) {
726 if ( in_array( $key, self::$mergeableMapKeys ) ) {
727 $value = $value + $fallbackValue;
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'] ) ) {
734 $value = array_merge( $fallbackValue, $value );
735 }
736
737 if ( isset( $value['inherit'] ) ) {
738 unset( $value['inherit'] );
739 }
740 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
741 $this->mergeMagicWords( $value, $fallbackValue );
742 }
743 }
744 } else {
745 $value = $fallbackValue;
746 }
747 }
748
749 /**
750 * @param mixed &$value
751 * @param mixed $fallbackValue
752 */
753 protected function mergeMagicWords( &$value, $fallbackValue ) {
754 foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
755 if ( !isset( $value[$magicName] ) ) {
756 $value[$magicName] = $fallbackInfo;
757 } else {
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 );
763 }
764 }
765 }
766
767 /**
768 * Given an array mapping language code to localisation value, such as is
769 * found in extension *.i18n.php files, iterate through a fallback sequence
770 * to merge the given data with an existing primary value.
771 *
772 * Returns true if any data from the extension array was used, false
773 * otherwise.
774 * @param array $codeSequence
775 * @param string $key
776 * @param mixed &$value
777 * @param mixed $fallbackValue
778 * @return bool
779 */
780 protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
781 $used = false;
782 foreach ( $codeSequence as $code ) {
783 if ( isset( $fallbackValue[$code] ) ) {
784 $this->mergeItem( $key, $value, $fallbackValue[$code] );
785 $used = true;
786 }
787 }
788
789 return $used;
790 }
791
792 /**
793 * Gets the combined list of messages dirs from
794 * core and extensions
795 *
796 * @since 1.25
797 * @return array
798 */
799 public function getMessagesDirs() {
800 global $IP;
801
802 $config = MediaWikiServices::getInstance()->getMainConfig();
803 $messagesDirs = $config->get( 'MessagesDirs' );
804 return [
805 'core' => "$IP/languages/i18n",
806 'api' => "$IP/includes/api/i18n",
807 'oojs-ui' => "$IP/resources/lib/oojs-ui/i18n",
808 ] + $messagesDirs;
809 }
810
811 /**
812 * Load localisation data for a given language for both core and extensions
813 * and save it to the persistent cache store and the process cache
814 * @param string $code
815 * @throws MWException
816 */
817 public function recache( $code ) {
818 global $wgExtensionMessagesFiles;
819
820 if ( !$code ) {
821 throw new MWException( "Invalid language code requested" );
822 }
823 $this->recachedLangs[$code] = true;
824
825 # Initial values
826 $initialData = array_fill_keys( self::$allKeys, null );
827 $coreData = $initialData;
828 $deps = [];
829
830 # Load the primary localisation from the source file
831 $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
832 if ( $data === false ) {
833 wfDebug( __METHOD__ . ": no localisation file for $code, using fallback to en\n" );
834 $coreData['fallback'] = 'en';
835 } else {
836 wfDebug( __METHOD__ . ": got localisation for $code from source\n" );
837
838 # Merge primary localisation
839 foreach ( $data as $key => $value ) {
840 $this->mergeItem( $key, $coreData[$key], $value );
841 }
842 }
843
844 # Fill in the fallback if it's not there already
845 if ( is_null( $coreData['fallback'] ) ) {
846 $coreData['fallback'] = $code === 'en' ? false : 'en';
847 }
848 if ( $coreData['fallback'] === false ) {
849 $coreData['fallbackSequence'] = [];
850 } else {
851 $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
852 $len = count( $coreData['fallbackSequence'] );
853
854 # Ensure that the sequence ends at en
855 if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
856 $coreData['fallbackSequence'][] = 'en';
857 }
858 }
859
860 $codeSequence = array_merge( [ $code ], $coreData['fallbackSequence'] );
861 $messageDirs = $this->getMessagesDirs();
862
863 # Load non-JSON localisation data for extensions
864 $extensionData = array_fill_keys( $codeSequence, $initialData );
865 foreach ( $wgExtensionMessagesFiles as $extension => $fileName ) {
866 if ( isset( $messageDirs[$extension] ) ) {
867 # This extension has JSON message data; skip the PHP shim
868 continue;
869 }
870
871 $data = $this->readPHPFile( $fileName, 'extension' );
872 $used = false;
873
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] );
878 $used = true;
879 }
880 }
881 }
882
883 if ( $used ) {
884 $deps[] = new FileDependency( $fileName );
885 }
886 }
887
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;
892
893 # Load core messages and the extension localisations.
894 foreach ( $messageDirs as $dirs ) {
895 foreach ( (array)$dirs as $dir ) {
896 $fileName = "$dir/$csCode.json";
897 $data = $this->readJSONFile( $fileName );
898
899 foreach ( $data as $key => $item ) {
900 $this->mergeItem( $key, $csData[$key], $item );
901 }
902
903 $deps[] = new FileDependency( $fileName );
904 }
905 }
906
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 );
911 }
912 }
913
914 if ( $csCode === $code ) {
915 # Merge core data into extension data
916 foreach ( $coreData as $key => $item ) {
917 $this->mergeItem( $key, $csData[$key], $item );
918 }
919 } else {
920 # Load the secondary localisation from the source file to
921 # avoid infinite cycles on cyclic fallbacks
922 $fbData = $this->readSourceFilesAndRegisterDeps( $csCode, $deps );
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] ) ) {
927 continue;
928 }
929
930 if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
931 $this->mergeItem( $key, $csData[$key], $fbData[$key] );
932 }
933 }
934 }
935 }
936
937 # Allow extensions an opportunity to adjust the data for this
938 # fallback
939 Hooks::run( 'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
940
941 # Merge the data for this fallback into the final array
942 if ( $csCode === $code ) {
943 $allData = $csData;
944 } else {
945 foreach ( self::$allKeys as $key ) {
946 if ( !isset( $csData[$key] ) ) {
947 continue;
948 }
949
950 if ( is_null( $allData[$key] ) || $this->isMergeableKey( $key ) ) {
951 $this->mergeItem( $key, $allData[$key], $csData[$key] );
952 }
953 }
954 }
955 }
956
957 # Add cache dependencies for any referenced globals
958 $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
959 // The 'MessagesDirs' config setting is used in LocalisationCache::getMessagesDirs().
960 // We use the key 'wgMessagesDirs' for historical reasons.
961 $deps['wgMessagesDirs'] = new MainConfigDependency( 'MessagesDirs' );
962 $deps['version'] = new ConstantDependency( 'LocalisationCache::VERSION' );
963
964 # Add dependencies to the cache entry
965 $allData['deps'] = $deps;
966
967 # Replace spaces with underscores in namespace names
968 $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
969
970 # And do the same for special page aliases. $page is an array.
971 foreach ( $allData['specialPageAliases'] as &$page ) {
972 $page = str_replace( ' ', '_', $page );
973 }
974 # Decouple the reference to prevent accidental damage
975 unset( $page );
976
977 # If there were no plural rules, return an empty array
978 if ( $allData['pluralRules'] === null ) {
979 $allData['pluralRules'] = [];
980 }
981 if ( $allData['compiledPluralRules'] === null ) {
982 $allData['compiledPluralRules'] = [];
983 }
984 # If there were no plural rule types, return an empty array
985 if ( $allData['pluralRuleTypes'] === null ) {
986 $allData['pluralRuleTypes'] = [];
987 }
988
989 # Set the list keys
990 $allData['list'] = [];
991 foreach ( self::$splitKeys as $key ) {
992 $allData['list'][$key] = array_keys( $allData[$key] );
993 }
994 # Run hooks
995 $purgeBlobs = true;
996 Hooks::run( 'LocalisationCacheRecache', [ $this, $code, &$allData, &$purgeBlobs ] );
997
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.' );
1001 }
1002
1003 # Set the preload key
1004 $allData['preload'] = $this->buildPreload( $allData );
1005
1006 # Save to the process cache and register the items loaded
1007 $this->data[$code] = $allData;
1008 foreach ( $allData as $key => $item ) {
1009 $this->loadedItems[$code][$key] = true;
1010 }
1011
1012 # Save to the persistent cache
1013 $this->store->startWrite( $code );
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 );
1018 }
1019 } else {
1020 $this->store->set( $key, $value );
1021 }
1022 }
1023 $this->store->finishWrite();
1024
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
1028 if ( $purgeBlobs && !$this->store instanceof LCStoreNull ) {
1029 $blobStore = new MessageBlobStore();
1030 $blobStore->clear();
1031 }
1032 }
1033
1034 /**
1035 * Build the preload item from the given pre-cache data.
1036 *
1037 * The preload item will be loaded automatically, improving performance
1038 * for the commonly-requested items it contains.
1039 * @param array $data
1040 * @return array
1041 */
1042 protected function buildPreload( $data ) {
1043 $preload = [ 'messages' => [] ];
1044 foreach ( self::$preloadedKeys as $key ) {
1045 $preload[$key] = $data[$key];
1046 }
1047
1048 foreach ( $data['preloadedMessages'] as $subkey ) {
1049 if ( isset( $data['messages'][$subkey] ) ) {
1050 $subitem = $data['messages'][$subkey];
1051 } else {
1052 $subitem = null;
1053 }
1054 $preload['messages'][$subkey] = $subitem;
1055 }
1056
1057 return $preload;
1058 }
1059
1060 /**
1061 * Unload the data for a given language from the object cache.
1062 * Reduces memory usage.
1063 * @param string $code
1064 */
1065 public function unload( $code ) {
1066 unset( $this->data[$code] );
1067 unset( $this->loadedItems[$code] );
1068 unset( $this->loadedSubitems[$code] );
1069 unset( $this->initialisedLangs[$code] );
1070 unset( $this->shallowFallbacks[$code] );
1071
1072 foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
1073 if ( $fbCode === $code ) {
1074 $this->unload( $shallowCode );
1075 }
1076 }
1077 }
1078
1079 /**
1080 * Unload all data
1081 */
1082 public function unloadAll() {
1083 foreach ( $this->initialisedLangs as $lang => $unused ) {
1084 $this->unload( $lang );
1085 }
1086 }
1087
1088 /**
1089 * Disable the storage backend
1090 */
1091 public function disableBackend() {
1092 $this->store = new LCStoreNull;
1093 $this->manualRecache = false;
1094 }
1095
1096 }