Fix documentation in Linker::formatTemplates.
[mediawiki.git] / includes / cache / LocalisationCache.php
blob7cc1b9c13b7085e8ed4416fe659ac2272a7f7456
1 <?php
2 /**
3 * Cache of the contents of localisation files.
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.
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.
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
20 * @file
23 define( 'MW_LC_VERSION', 2 );
25 /**
26 * Class for caching the contents of localisation files, Messages*.php
27 * and *.i18n.php.
29 * An instance of this class is available using Language::getLocalisationCache().
31 * The values retrieved from here are merged, containing items from extension
32 * files, core messages files and the language fallback sequence (e.g. zh-cn ->
33 * zh-hans -> en ). Some common errors are corrected, for example namespace
34 * names with spaces instead of underscores, but heavyweight processing, such
35 * as grammatical transformation, is done by the caller.
37 class LocalisationCache {
38 /** Configuration associative array */
39 var $conf;
41 /**
42 * True if recaching should only be done on an explicit call to recache().
43 * Setting this reduces the overhead of cache freshness checking, which
44 * requires doing a stat() for every extension i18n file.
46 var $manualRecache = false;
48 /**
49 * True to treat all files as expired until they are regenerated by this object.
51 var $forceRecache = false;
53 /**
54 * The cache data. 3-d array, where the first key is the language code,
55 * the second key is the item key e.g. 'messages', and the third key is
56 * an item specific subkey index. Some items are not arrays and so for those
57 * items, there are no subkeys.
59 var $data = array();
61 /**
62 * The persistent store object. An instance of LCStore.
64 * @var LCStore
66 var $store;
68 /**
69 * A 2-d associative array, code/key, where presence indicates that the item
70 * is loaded. Value arbitrary.
72 * For split items, if set, this indicates that all of the subitems have been
73 * loaded.
75 var $loadedItems = array();
77 /**
78 * A 3-d associative array, code/key/subkey, where presence indicates that
79 * the subitem is loaded. Only used for the split items, i.e. messages.
81 var $loadedSubitems = array();
83 /**
84 * An array where presence of a key indicates that that language has been
85 * initialised. Initialisation includes checking for cache expiry and doing
86 * any necessary updates.
88 var $initialisedLangs = array();
90 /**
91 * An array mapping non-existent pseudo-languages to fallback languages. This
92 * is filled by initShallowFallback() when data is requested from a language
93 * that lacks a Messages*.php file.
95 var $shallowFallbacks = array();
97 /**
98 * An array where the keys are codes that have been recached by this instance.
100 var $recachedLangs = array();
103 * All item keys
105 static public $allKeys = array(
106 'fallback', 'namespaceNames', 'bookstoreList',
107 'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable',
108 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
109 'linkTrail', 'namespaceAliases',
110 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
111 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
112 'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
113 'digitGroupingPattern', 'pluralRules', 'compiledPluralRules',
117 * Keys for items which consist of associative arrays, which may be merged
118 * by a fallback sequence.
120 static public $mergeableMapKeys = array( 'messages', 'namespaceNames',
121 'dateFormats', 'imageFiles', 'preloadedMessages'
125 * Keys for items which are a numbered array.
127 static public $mergeableListKeys = array( 'extraUserToggles' );
130 * Keys for items which contain an array of arrays of equivalent aliases
131 * for each subitem. The aliases may be merged by a fallback sequence.
133 static public $mergeableAliasListKeys = array( 'specialPageAliases' );
136 * Keys for items which contain an associative array, and may be merged if
137 * the primary value contains the special array key "inherit". That array
138 * key is removed after the first merge.
140 static public $optionalMergeKeys = array( 'bookstoreList' );
143 * Keys for items that are formatted like $magicWords
145 static public $magicWordKeys = array( 'magicWords' );
148 * Keys for items where the subitems are stored in the backend separately.
150 static public $splitKeys = array( 'messages' );
153 * Keys which are loaded automatically by initLanguage()
155 static public $preloadedKeys = array( 'dateFormats', 'namespaceNames' );
158 * Associative array of cached plural rules. The key is the language code,
159 * the value is an array of plural rules for that language.
161 var $pluralRules = null;
163 var $mergeableKeys = null;
166 * Constructor.
167 * For constructor parameters, see the documentation in DefaultSettings.php
168 * for $wgLocalisationCacheConf.
170 * @param $conf Array
171 * @throws MWException
173 function __construct( $conf ) {
174 global $wgCacheDirectory;
176 $this->conf = $conf;
177 $storeConf = array();
178 if ( !empty( $conf['storeClass'] ) ) {
179 $storeClass = $conf['storeClass'];
180 } else {
181 switch ( $conf['store'] ) {
182 case 'files':
183 case 'file':
184 $storeClass = 'LCStore_CDB';
185 break;
186 case 'db':
187 $storeClass = 'LCStore_DB';
188 break;
189 case 'accel':
190 $storeClass = 'LCStore_Accel';
191 break;
192 case 'detect':
193 $storeClass = $wgCacheDirectory ? 'LCStore_CDB' : 'LCStore_DB';
194 break;
195 default:
196 throw new MWException(
197 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' );
201 wfDebug( get_class( $this ) . ": using store $storeClass\n" );
202 if ( !empty( $conf['storeDirectory'] ) ) {
203 $storeConf['directory'] = $conf['storeDirectory'];
206 $this->store = new $storeClass( $storeConf );
207 foreach ( array( 'manualRecache', 'forceRecache' ) as $var ) {
208 if ( isset( $conf[$var] ) ) {
209 $this->$var = $conf[$var];
215 * Returns true if the given key is mergeable, that is, if it is an associative
216 * array which can be merged through a fallback sequence.
217 * @param $key
218 * @return bool
220 public function isMergeableKey( $key ) {
221 if ( $this->mergeableKeys === null ) {
222 $this->mergeableKeys = array_flip( array_merge(
223 self::$mergeableMapKeys,
224 self::$mergeableListKeys,
225 self::$mergeableAliasListKeys,
226 self::$optionalMergeKeys,
227 self::$magicWordKeys
228 ) );
230 return isset( $this->mergeableKeys[$key] );
234 * Get a cache item.
236 * Warning: this may be slow for split items (messages), since it will
237 * need to fetch all of the subitems from the cache individually.
238 * @param $code
239 * @param $key
240 * @return mixed
242 public function getItem( $code, $key ) {
243 if ( !isset( $this->loadedItems[$code][$key] ) ) {
244 wfProfileIn( __METHOD__ . '-load' );
245 $this->loadItem( $code, $key );
246 wfProfileOut( __METHOD__ . '-load' );
249 if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
250 return $this->shallowFallbacks[$code];
253 return $this->data[$code][$key];
257 * Get a subitem, for instance a single message for a given language.
258 * @param $code
259 * @param $key
260 * @param $subkey
261 * @return null
263 public function getSubitem( $code, $key, $subkey ) {
264 if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) &&
265 !isset( $this->loadedItems[$code][$key] ) ) {
266 wfProfileIn( __METHOD__ . '-load' );
267 $this->loadSubitem( $code, $key, $subkey );
268 wfProfileOut( __METHOD__ . '-load' );
271 if ( isset( $this->data[$code][$key][$subkey] ) ) {
272 return $this->data[$code][$key][$subkey];
273 } else {
274 return null;
279 * Get the list of subitem keys for a given item.
281 * This is faster than array_keys($lc->getItem(...)) for the items listed in
282 * self::$splitKeys.
284 * Will return null if the item is not found, or false if the item is not an
285 * array.
286 * @param $code
287 * @param $key
288 * @return bool|null|string
290 public function getSubitemList( $code, $key ) {
291 if ( in_array( $key, self::$splitKeys ) ) {
292 return $this->getSubitem( $code, 'list', $key );
293 } else {
294 $item = $this->getItem( $code, $key );
295 if ( is_array( $item ) ) {
296 return array_keys( $item );
297 } else {
298 return false;
304 * Load an item into the cache.
305 * @param $code
306 * @param $key
308 protected function loadItem( $code, $key ) {
309 if ( !isset( $this->initialisedLangs[$code] ) ) {
310 $this->initLanguage( $code );
313 // Check to see if initLanguage() loaded it for us
314 if ( isset( $this->loadedItems[$code][$key] ) ) {
315 return;
318 if ( isset( $this->shallowFallbacks[$code] ) ) {
319 $this->loadItem( $this->shallowFallbacks[$code], $key );
320 return;
323 if ( in_array( $key, self::$splitKeys ) ) {
324 $subkeyList = $this->getSubitem( $code, 'list', $key );
325 foreach ( $subkeyList as $subkey ) {
326 if ( isset( $this->data[$code][$key][$subkey] ) ) {
327 continue;
329 $this->data[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
331 } else {
332 $this->data[$code][$key] = $this->store->get( $code, $key );
335 $this->loadedItems[$code][$key] = true;
339 * Load a subitem into the cache
340 * @param $code
341 * @param $key
342 * @param $subkey
343 * @return
345 protected function loadSubitem( $code, $key, $subkey ) {
346 if ( !in_array( $key, self::$splitKeys ) ) {
347 $this->loadItem( $code, $key );
348 return;
351 if ( !isset( $this->initialisedLangs[$code] ) ) {
352 $this->initLanguage( $code );
355 // Check to see if initLanguage() loaded it for us
356 if ( isset( $this->loadedItems[$code][$key] ) ||
357 isset( $this->loadedSubitems[$code][$key][$subkey] ) ) {
358 return;
361 if ( isset( $this->shallowFallbacks[$code] ) ) {
362 $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
363 return;
366 $value = $this->store->get( $code, "$key:$subkey" );
367 $this->data[$code][$key][$subkey] = $value;
368 $this->loadedSubitems[$code][$key][$subkey] = true;
372 * Returns true if the cache identified by $code is missing or expired.
373 * @return bool
375 public function isExpired( $code ) {
376 if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
377 wfDebug( __METHOD__ . "($code): forced reload\n" );
378 return true;
381 $deps = $this->store->get( $code, 'deps' );
382 $keys = $this->store->get( $code, 'list' );
383 $preload = $this->store->get( $code, 'preload' );
384 // Different keys may expire separately, at least in LCStore_Accel
385 if ( $deps === null || $keys === null || $preload === null ) {
386 wfDebug( __METHOD__ . "($code): cache missing, need to make one\n" );
387 return true;
390 foreach ( $deps as $dep ) {
391 // Because we're unserializing stuff from cache, we
392 // could receive objects of classes that don't exist
393 // anymore (e.g. uninstalled extensions)
394 // When this happens, always expire the cache
395 if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
396 wfDebug( __METHOD__ . "($code): cache for $code expired due to " .
397 get_class( $dep ) . "\n" );
398 return true;
402 return false;
406 * Initialise a language in this object. Rebuild the cache if necessary.
407 * @param $code
408 * @throws MWException
410 protected function initLanguage( $code ) {
411 if ( isset( $this->initialisedLangs[$code] ) ) {
412 return;
415 $this->initialisedLangs[$code] = true;
417 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
418 if ( !Language::isValidBuiltInCode( $code ) ) {
419 $this->initShallowFallback( $code, 'en' );
420 return;
423 # Recache the data if necessary
424 if ( !$this->manualRecache && $this->isExpired( $code ) ) {
425 if ( file_exists( Language::getMessagesFileName( $code ) ) ) {
426 $this->recache( $code );
427 } elseif ( $code === 'en' ) {
428 throw new MWException( 'MessagesEn.php is missing.' );
429 } else {
430 $this->initShallowFallback( $code, 'en' );
432 return;
435 # Preload some stuff
436 $preload = $this->getItem( $code, 'preload' );
437 if ( $preload === null ) {
438 if ( $this->manualRecache ) {
439 // No Messages*.php file. Do shallow fallback to en.
440 if ( $code === 'en' ) {
441 throw new MWException( 'No localisation cache found for English. ' .
442 'Please run maintenance/rebuildLocalisationCache.php.' );
444 $this->initShallowFallback( $code, 'en' );
445 return;
446 } else {
447 throw new MWException( 'Invalid or missing localisation cache.' );
450 $this->data[$code] = $preload;
451 foreach ( $preload as $key => $item ) {
452 if ( in_array( $key, self::$splitKeys ) ) {
453 foreach ( $item as $subkey => $subitem ) {
454 $this->loadedSubitems[$code][$key][$subkey] = true;
456 } else {
457 $this->loadedItems[$code][$key] = true;
463 * Create a fallback from one language to another, without creating a
464 * complete persistent cache.
465 * @param $primaryCode
466 * @param $fallbackCode
468 public function initShallowFallback( $primaryCode, $fallbackCode ) {
469 $this->data[$primaryCode] =& $this->data[$fallbackCode];
470 $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
471 $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
472 $this->shallowFallbacks[$primaryCode] = $fallbackCode;
476 * Read a PHP file containing localisation data.
477 * @param $_fileName
478 * @param $_fileType
479 * @throws MWException
480 * @return array
482 protected function readPHPFile( $_fileName, $_fileType ) {
483 // Disable APC caching
484 $_apcEnabled = ini_set( 'apc.cache_by_default', '0' );
485 include( $_fileName );
486 ini_set( 'apc.cache_by_default', $_apcEnabled );
488 if ( $_fileType == 'core' || $_fileType == 'extension' ) {
489 $data = compact( self::$allKeys );
490 } elseif ( $_fileType == 'aliases' ) {
491 $data = compact( 'aliases' );
492 } else {
493 throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" );
495 return $data;
499 * Get the compiled plural rules for a given language from the XML files.
500 * @since 1.20
502 public function getCompiledPluralRules( $code ) {
503 $rules = $this->getPluralRules( $code );
504 if ( $rules === null ) {
505 return null;
507 try {
508 $compiledRules = CLDRPluralRuleEvaluator::compile( $rules );
509 } catch( CLDRPluralRuleError $e ) {
510 wfDebugLog( 'l10n', $e->getMessage() . "\n" );
511 return array();
513 return $compiledRules;
517 * Get the plural rules for a given language from the XML files.
518 * Cached.
519 * @since 1.20
521 public function getPluralRules( $code ) {
522 global $IP;
524 if ( $this->pluralRules === null ) {
525 $cldrPlural = "$IP/languages/data/plurals.xml";
526 $mwPlural = "$IP/languages/data/plurals-mediawiki.xml";
527 // Load CLDR plural rules
528 $this->loadPluralFile( $cldrPlural );
529 if ( file_exists( $mwPlural ) ) {
530 // Override or extend
531 $this->loadPluralFile( $mwPlural );
534 if ( !isset( $this->pluralRules[$code] ) ) {
535 return null;
536 } else {
537 return $this->pluralRules[$code];
543 * Load a plural XML file with the given filename, compile the relevant
544 * rules, and save the compiled rules in a process-local cache.
546 protected function loadPluralFile( $fileName ) {
547 $doc = new DOMDocument;
548 $doc->load( $fileName );
549 $rulesets = $doc->getElementsByTagName( "pluralRules" );
550 foreach ( $rulesets as $ruleset ) {
551 $codes = $ruleset->getAttribute( 'locales' );
552 $rules = array();
553 $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
554 foreach ( $ruleElements as $elt ) {
555 $rules[] = $elt->nodeValue;
557 foreach ( explode( ' ', $codes ) as $code ) {
558 $this->pluralRules[$code] = $rules;
564 * Read the data from the source files for a given language, and register
565 * the relevant dependencies in the $deps array. If the localisation
566 * exists, the data array is returned, otherwise false is returned.
568 protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
569 global $IP;
571 $fileName = Language::getMessagesFileName( $code );
572 if ( !file_exists( $fileName ) ) {
573 return false;
576 $deps[] = new FileDependency( $fileName );
577 $data = $this->readPHPFile( $fileName, 'core' );
579 # Load CLDR plural rules for JavaScript
580 $data['pluralRules'] = $this->getPluralRules( $code );
581 # And for PHP
582 $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
584 $deps['plurals'] = new FileDependency( "$IP/languages/data/plurals.xml" );
585 $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
587 return $data;
591 * Merge two localisation values, a primary and a fallback, overwriting the
592 * primary value in place.
593 * @param $key
594 * @param $value
595 * @param $fallbackValue
597 protected function mergeItem( $key, &$value, $fallbackValue ) {
598 if ( !is_null( $value ) ) {
599 if ( !is_null( $fallbackValue ) ) {
600 if ( in_array( $key, self::$mergeableMapKeys ) ) {
601 $value = $value + $fallbackValue;
602 } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
603 $value = array_unique( array_merge( $fallbackValue, $value ) );
604 } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
605 $value = array_merge_recursive( $value, $fallbackValue );
606 } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
607 if ( !empty( $value['inherit'] ) ) {
608 $value = array_merge( $fallbackValue, $value );
611 if ( isset( $value['inherit'] ) ) {
612 unset( $value['inherit'] );
614 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
615 $this->mergeMagicWords( $value, $fallbackValue );
618 } else {
619 $value = $fallbackValue;
624 * @param $value
625 * @param $fallbackValue
627 protected function mergeMagicWords( &$value, $fallbackValue ) {
628 foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
629 if ( !isset( $value[$magicName] ) ) {
630 $value[$magicName] = $fallbackInfo;
631 } else {
632 $oldSynonyms = array_slice( $fallbackInfo, 1 );
633 $newSynonyms = array_slice( $value[$magicName], 1 );
634 $synonyms = array_values( array_unique( array_merge(
635 $newSynonyms, $oldSynonyms ) ) );
636 $value[$magicName] = array_merge( array( $fallbackInfo[0] ), $synonyms );
642 * Given an array mapping language code to localisation value, such as is
643 * found in extension *.i18n.php files, iterate through a fallback sequence
644 * to merge the given data with an existing primary value.
646 * Returns true if any data from the extension array was used, false
647 * otherwise.
648 * @param $codeSequence
649 * @param $key
650 * @param $value
651 * @param $fallbackValue
652 * @return bool
654 protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
655 $used = false;
656 foreach ( $codeSequence as $code ) {
657 if ( isset( $fallbackValue[$code] ) ) {
658 $this->mergeItem( $key, $value, $fallbackValue[$code] );
659 $used = true;
663 return $used;
667 * Load localisation data for a given language for both core and extensions
668 * and save it to the persistent cache store and the process cache
669 * @param $code
670 * @throws MWException
672 public function recache( $code ) {
673 global $wgExtensionMessagesFiles;
674 wfProfileIn( __METHOD__ );
676 if ( !$code ) {
677 throw new MWException( "Invalid language code requested" );
679 $this->recachedLangs[$code] = true;
681 # Initial values
682 $initialData = array_combine(
683 self::$allKeys,
684 array_fill( 0, count( self::$allKeys ), null ) );
685 $coreData = $initialData;
686 $deps = array();
688 # Load the primary localisation from the source file
689 $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
690 if ( $data === false ) {
691 wfDebug( __METHOD__ . ": no localisation file for $code, using fallback to en\n" );
692 $coreData['fallback'] = 'en';
693 } else {
694 wfDebug( __METHOD__ . ": got localisation for $code from source\n" );
696 # Merge primary localisation
697 foreach ( $data as $key => $value ) {
698 $this->mergeItem( $key, $coreData[$key], $value );
703 # Fill in the fallback if it's not there already
704 if ( is_null( $coreData['fallback'] ) ) {
705 $coreData['fallback'] = $code === 'en' ? false : 'en';
707 if ( $coreData['fallback'] === false ) {
708 $coreData['fallbackSequence'] = array();
709 } else {
710 $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
711 $len = count( $coreData['fallbackSequence'] );
713 # Ensure that the sequence ends at en
714 if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
715 $coreData['fallbackSequence'][] = 'en';
718 # Load the fallback localisation item by item and merge it
719 foreach ( $coreData['fallbackSequence'] as $fbCode ) {
720 # Load the secondary localisation from the source file to
721 # avoid infinite cycles on cyclic fallbacks
722 $fbData = $this->readSourceFilesAndRegisterDeps( $fbCode, $deps );
723 if ( $fbData === false ) {
724 continue;
727 foreach ( self::$allKeys as $key ) {
728 if ( !isset( $fbData[$key] ) ) {
729 continue;
732 if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
733 $this->mergeItem( $key, $coreData[$key], $fbData[$key] );
739 $codeSequence = array_merge( array( $code ), $coreData['fallbackSequence'] );
741 # Load the extension localisations
742 # This is done after the core because we know the fallback sequence now.
743 # But it has a higher precedence for merging so that we can support things
744 # like site-specific message overrides.
745 $allData = $initialData;
746 foreach ( $wgExtensionMessagesFiles as $fileName ) {
747 $data = $this->readPHPFile( $fileName, 'extension' );
748 $used = false;
750 foreach ( $data as $key => $item ) {
751 if ( $this->mergeExtensionItem( $codeSequence, $key, $allData[$key], $item ) ) {
752 $used = true;
756 if ( $used ) {
757 $deps[] = new FileDependency( $fileName );
761 # Merge core data into extension data
762 foreach ( $coreData as $key => $item ) {
763 $this->mergeItem( $key, $allData[$key], $item );
766 # Add cache dependencies for any referenced globals
767 $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
768 $deps['version'] = new ConstantDependency( 'MW_LC_VERSION' );
770 # Add dependencies to the cache entry
771 $allData['deps'] = $deps;
773 # Replace spaces with underscores in namespace names
774 $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
776 # And do the same for special page aliases. $page is an array.
777 foreach ( $allData['specialPageAliases'] as &$page ) {
778 $page = str_replace( ' ', '_', $page );
780 # Decouple the reference to prevent accidental damage
781 unset( $page );
783 # If there were no plural rules, return an empty array
784 if ( $allData['pluralRules'] === null ) {
785 $allData['pluralRules'] = array();
787 if ( $allData['compiledPluralRules'] === null ) {
788 $allData['compiledPluralRules'] = array();
791 # Set the list keys
792 $allData['list'] = array();
793 foreach ( self::$splitKeys as $key ) {
794 $allData['list'][$key] = array_keys( $allData[$key] );
796 # Run hooks
797 wfRunHooks( 'LocalisationCacheRecache', array( $this, $code, &$allData ) );
799 if ( is_null( $allData['namespaceNames'] ) ) {
800 throw new MWException( __METHOD__ . ': Localisation data failed sanity check! ' .
801 'Check that your languages/messages/MessagesEn.php file is intact.' );
804 # Set the preload key
805 $allData['preload'] = $this->buildPreload( $allData );
807 # Save to the process cache and register the items loaded
808 $this->data[$code] = $allData;
809 foreach ( $allData as $key => $item ) {
810 $this->loadedItems[$code][$key] = true;
813 # Save to the persistent cache
814 $this->store->startWrite( $code );
815 foreach ( $allData as $key => $value ) {
816 if ( in_array( $key, self::$splitKeys ) ) {
817 foreach ( $value as $subkey => $subvalue ) {
818 $this->store->set( "$key:$subkey", $subvalue );
820 } else {
821 $this->store->set( $key, $value );
824 $this->store->finishWrite();
826 # Clear out the MessageBlobStore
827 # HACK: If using a null (i.e. disabled) storage backend, we
828 # can't write to the MessageBlobStore either
829 if ( !$this->store instanceof LCStore_Null ) {
830 MessageBlobStore::clear();
833 wfProfileOut( __METHOD__ );
837 * Build the preload item from the given pre-cache data.
839 * The preload item will be loaded automatically, improving performance
840 * for the commonly-requested items it contains.
841 * @param $data
842 * @return array
844 protected function buildPreload( $data ) {
845 $preload = array( 'messages' => array() );
846 foreach ( self::$preloadedKeys as $key ) {
847 $preload[$key] = $data[$key];
850 foreach ( $data['preloadedMessages'] as $subkey ) {
851 if ( isset( $data['messages'][$subkey] ) ) {
852 $subitem = $data['messages'][$subkey];
853 } else {
854 $subitem = null;
856 $preload['messages'][$subkey] = $subitem;
859 return $preload;
863 * Unload the data for a given language from the object cache.
864 * Reduces memory usage.
865 * @param $code
867 public function unload( $code ) {
868 unset( $this->data[$code] );
869 unset( $this->loadedItems[$code] );
870 unset( $this->loadedSubitems[$code] );
871 unset( $this->initialisedLangs[$code] );
873 foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
874 if ( $fbCode === $code ) {
875 $this->unload( $shallowCode );
881 * Unload all data
883 public function unloadAll() {
884 foreach ( $this->initialisedLangs as $lang => $unused ) {
885 $this->unload( $lang );
890 * Disable the storage backend
892 public function disableBackend() {
893 $this->store = new LCStore_Null;
894 $this->manualRecache = false;
899 * Interface for the persistence layer of LocalisationCache.
901 * The persistence layer is two-level hierarchical cache. The first level
902 * is the language, the second level is the item or subitem.
904 * Since the data for a whole language is rebuilt in one operation, it needs
905 * to have a fast and atomic method for deleting or replacing all of the
906 * current data for a given language. The interface reflects this bulk update
907 * operation. Callers writing to the cache must first call startWrite(), then
908 * will call set() a couple of thousand times, then will call finishWrite()
909 * to commit the operation. When finishWrite() is called, the cache is
910 * expected to delete all data previously stored for that language.
912 * The values stored are PHP variables suitable for serialize(). Implementations
913 * of LCStore are responsible for serializing and unserializing.
915 interface LCStore {
917 * Get a value.
918 * @param $code string Language code
919 * @param $key string Cache key
921 function get( $code, $key );
924 * Start a write transaction.
925 * @param $code Language code
927 function startWrite( $code );
930 * Finish a write transaction.
932 function finishWrite();
935 * Set a key to a given value. startWrite() must be called before this
936 * is called, and finishWrite() must be called afterwards.
937 * @param $key
938 * @param $value
940 function set( $key, $value );
944 * LCStore implementation which uses PHP accelerator to store data.
945 * This will work if one of XCache, WinCache or APC cacher is configured.
946 * (See ObjectCache.php)
948 class LCStore_Accel implements LCStore {
949 var $currentLang;
950 var $keys;
952 public function __construct() {
953 $this->cache = wfGetCache( CACHE_ACCEL );
956 public function get( $code, $key ) {
957 $k = wfMemcKey( 'l10n', $code, 'k', $key );
958 $r = $this->cache->get( $k );
959 return $r === false ? null : $r;
962 public function startWrite( $code ) {
963 $k = wfMemcKey( 'l10n', $code, 'l' );
964 $keys = $this->cache->get( $k );
965 if ( $keys ) {
966 foreach ( $keys as $k ) {
967 $this->cache->delete( $k );
970 $this->currentLang = $code;
971 $this->keys = array();
974 public function finishWrite() {
975 if ( $this->currentLang ) {
976 $k = wfMemcKey( 'l10n', $this->currentLang, 'l' );
977 $this->cache->set( $k, array_keys( $this->keys ) );
979 $this->currentLang = null;
980 $this->keys = array();
983 public function set( $key, $value ) {
984 if ( $this->currentLang ) {
985 $k = wfMemcKey( 'l10n', $this->currentLang, 'k', $key );
986 $this->keys[$k] = true;
987 $this->cache->set( $k, $value );
993 * LCStore implementation which uses the standard DB functions to store data.
994 * This will work on any MediaWiki installation.
996 class LCStore_DB implements LCStore {
997 var $currentLang;
998 var $writesDone = false;
1001 * @var DatabaseBase
1003 var $dbw;
1004 var $batch;
1005 var $readOnly = false;
1007 public function get( $code, $key ) {
1008 if ( $this->writesDone ) {
1009 $db = wfGetDB( DB_MASTER );
1010 } else {
1011 $db = wfGetDB( DB_SLAVE );
1013 $row = $db->selectRow( 'l10n_cache', array( 'lc_value' ),
1014 array( 'lc_lang' => $code, 'lc_key' => $key ), __METHOD__ );
1015 if ( $row ) {
1016 return unserialize( $row->lc_value );
1017 } else {
1018 return null;
1022 public function startWrite( $code ) {
1023 if ( $this->readOnly ) {
1024 return;
1027 if ( !$code ) {
1028 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
1031 $this->dbw = wfGetDB( DB_MASTER );
1032 try {
1033 $this->dbw->begin( __METHOD__ );
1034 $this->dbw->delete( 'l10n_cache', array( 'lc_lang' => $code ), __METHOD__ );
1035 } catch ( DBQueryError $e ) {
1036 if ( $this->dbw->wasReadOnlyError() ) {
1037 $this->readOnly = true;
1038 $this->dbw->rollback( __METHOD__ );
1039 return;
1040 } else {
1041 throw $e;
1045 $this->currentLang = $code;
1046 $this->batch = array();
1049 public function finishWrite() {
1050 if ( $this->readOnly ) {
1051 return;
1054 if ( $this->batch ) {
1055 $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
1058 $this->dbw->commit( __METHOD__ );
1059 $this->currentLang = null;
1060 $this->dbw = null;
1061 $this->batch = array();
1062 $this->writesDone = true;
1065 public function set( $key, $value ) {
1066 if ( $this->readOnly ) {
1067 return;
1070 if ( is_null( $this->currentLang ) ) {
1071 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
1074 $this->batch[] = array(
1075 'lc_lang' => $this->currentLang,
1076 'lc_key' => $key,
1077 'lc_value' => serialize( $value ) );
1079 if ( count( $this->batch ) >= 100 ) {
1080 $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
1081 $this->batch = array();
1087 * LCStore implementation which stores data as a collection of CDB files in the
1088 * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
1089 * will throw an exception.
1091 * Profiling indicates that on Linux, this implementation outperforms MySQL if
1092 * the directory is on a local filesystem and there is ample kernel cache
1093 * space. The performance advantage is greater when the DBA extension is
1094 * available than it is with the PHP port.
1096 * See Cdb.php and http://cr.yp.to/cdb.html
1098 class LCStore_CDB implements LCStore {
1099 var $readers, $writer, $currentLang, $directory;
1101 function __construct( $conf = array() ) {
1102 global $wgCacheDirectory;
1104 if ( isset( $conf['directory'] ) ) {
1105 $this->directory = $conf['directory'];
1106 } else {
1107 $this->directory = $wgCacheDirectory;
1111 public function get( $code, $key ) {
1112 if ( !isset( $this->readers[$code] ) ) {
1113 $fileName = $this->getFileName( $code );
1115 if ( !file_exists( $fileName ) ) {
1116 $this->readers[$code] = false;
1117 } else {
1118 $this->readers[$code] = CdbReader::open( $fileName );
1122 if ( !$this->readers[$code] ) {
1123 return null;
1124 } else {
1125 $value = $this->readers[$code]->get( $key );
1127 if ( $value === false ) {
1128 return null;
1130 return unserialize( $value );
1134 public function startWrite( $code ) {
1135 if ( !file_exists( $this->directory ) ) {
1136 if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
1137 throw new MWException( "Unable to create the localisation store " .
1138 "directory \"{$this->directory}\"" );
1142 // Close reader to stop permission errors on write
1143 if ( !empty( $this->readers[$code] ) ) {
1144 $this->readers[$code]->close();
1147 $this->writer = CdbWriter::open( $this->getFileName( $code ) );
1148 $this->currentLang = $code;
1151 public function finishWrite() {
1152 // Close the writer
1153 $this->writer->close();
1154 $this->writer = null;
1155 unset( $this->readers[$this->currentLang] );
1156 $this->currentLang = null;
1159 public function set( $key, $value ) {
1160 if ( is_null( $this->writer ) ) {
1161 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
1163 $this->writer->set( $key, serialize( $value ) );
1166 protected function getFileName( $code ) {
1167 if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
1168 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
1170 return "{$this->directory}/l10n_cache-$code.cdb";
1175 * Null store backend, used to avoid DB errors during install
1177 class LCStore_Null implements LCStore {
1178 public function get( $code, $key ) {
1179 return null;
1182 public function startWrite( $code ) {}
1183 public function finishWrite() {}
1184 public function set( $key, $value ) {}
1188 * A localisation cache optimised for loading large amounts of data for many
1189 * languages. Used by rebuildLocalisationCache.php.
1191 class LocalisationCache_BulkLoad extends LocalisationCache {
1193 * A cache of the contents of data files.
1194 * Core files are serialized to avoid using ~1GB of RAM during a recache.
1196 var $fileCache = array();
1199 * Most recently used languages. Uses the linked-list aspect of PHP hashtables
1200 * to keep the most recently used language codes at the end of the array, and
1201 * the language codes that are ready to be deleted at the beginning.
1203 var $mruLangs = array();
1206 * Maximum number of languages that may be loaded into $this->data
1208 var $maxLoadedLangs = 10;
1211 * @param $fileName
1212 * @param $fileType
1213 * @return array|mixed
1215 protected function readPHPFile( $fileName, $fileType ) {
1216 $serialize = $fileType === 'core';
1217 if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
1218 $data = parent::readPHPFile( $fileName, $fileType );
1220 if ( $serialize ) {
1221 $encData = serialize( $data );
1222 } else {
1223 $encData = $data;
1226 $this->fileCache[$fileName][$fileType] = $encData;
1228 return $data;
1229 } elseif ( $serialize ) {
1230 return unserialize( $this->fileCache[$fileName][$fileType] );
1231 } else {
1232 return $this->fileCache[$fileName][$fileType];
1237 * @param $code
1238 * @param $key
1239 * @return mixed
1241 public function getItem( $code, $key ) {
1242 unset( $this->mruLangs[$code] );
1243 $this->mruLangs[$code] = true;
1244 return parent::getItem( $code, $key );
1248 * @param $code
1249 * @param $key
1250 * @param $subkey
1251 * @return
1253 public function getSubitem( $code, $key, $subkey ) {
1254 unset( $this->mruLangs[$code] );
1255 $this->mruLangs[$code] = true;
1256 return parent::getSubitem( $code, $key, $subkey );
1260 * @param $code
1262 public function recache( $code ) {
1263 parent::recache( $code );
1264 unset( $this->mruLangs[$code] );
1265 $this->mruLangs[$code] = true;
1266 $this->trimCache();
1270 * @param $code
1272 public function unload( $code ) {
1273 unset( $this->mruLangs[$code] );
1274 parent::unload( $code );
1278 * Unload cached languages until there are less than $this->maxLoadedLangs
1280 protected function trimCache() {
1281 while ( count( $this->data ) > $this->maxLoadedLangs && count( $this->mruLangs ) ) {
1282 reset( $this->mruLangs );
1283 $code = key( $this->mruLangs );
1284 wfDebug( __METHOD__ . ": unloading $code\n" );
1285 $this->unload( $code );