Genderize Special:Preferences
[lhc/web/wiklou.git] / includes / 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 define( 'MW_LC_VERSION', 2 );
24
25 /**
26 * Class for caching the contents of localisation files, Messages*.php
27 * and *.i18n.php.
28 *
29 * An instance of this class is available using Language::getLocalisationCache().
30 *
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.
36 */
37 class LocalisationCache {
38 /** Configuration associative array */
39 var $conf;
40
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.
45 */
46 var $manualRecache = false;
47
48 /**
49 * True to treat all files as expired until they are regenerated by this object.
50 */
51 var $forceRecache = false;
52
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.
58 */
59 var $data = array();
60
61 /**
62 * The persistent store object. An instance of LCStore.
63 *
64 * @var LCStore
65 */
66 var $store;
67
68 /**
69 * A 2-d associative array, code/key, where presence indicates that the item
70 * is loaded. Value arbitrary.
71 *
72 * For split items, if set, this indicates that all of the subitems have been
73 * loaded.
74 */
75 var $loadedItems = array();
76
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.
80 */
81 var $loadedSubitems = array();
82
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.
87 */
88 var $initialisedLangs = array();
89
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.
94 */
95 var $shallowFallbacks = array();
96
97 /**
98 * An array where the keys are codes that have been recached by this instance.
99 */
100 var $recachedLangs = array();
101
102 /**
103 * All item keys
104 */
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',
114 );
115
116 /**
117 * Keys for items which consist of associative arrays, which may be merged
118 * by a fallback sequence.
119 */
120 static public $mergeableMapKeys = array( 'messages', 'namespaceNames',
121 'dateFormats', 'imageFiles', 'preloadedMessages'
122 );
123
124 /**
125 * Keys for items which are a numbered array.
126 */
127 static public $mergeableListKeys = array( 'extraUserToggles' );
128
129 /**
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.
132 */
133 static public $mergeableAliasListKeys = array( 'specialPageAliases' );
134
135 /**
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.
139 */
140 static public $optionalMergeKeys = array( 'bookstoreList' );
141
142 /**
143 * Keys for items that are formatted like $magicWords
144 */
145 static public $magicWordKeys = array( 'magicWords' );
146
147 /**
148 * Keys for items where the subitems are stored in the backend separately.
149 */
150 static public $splitKeys = array( 'messages' );
151
152 /**
153 * Keys which are loaded automatically by initLanguage()
154 */
155 static public $preloadedKeys = array( 'dateFormats', 'namespaceNames' );
156
157 /**
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.
160 */
161 var $pluralRules = null;
162
163 var $mergeableKeys = null;
164
165 /**
166 * Constructor.
167 * For constructor parameters, see the documentation in DefaultSettings.php
168 * for $wgLocalisationCacheConf.
169 *
170 * @param $conf Array
171 * @throws MWException
172 */
173 function __construct( $conf ) {
174 global $wgCacheDirectory;
175
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.' );
198 }
199 }
200
201 wfDebug( get_class( $this ) . ": using store $storeClass\n" );
202 if ( !empty( $conf['storeDirectory'] ) ) {
203 $storeConf['directory'] = $conf['storeDirectory'];
204 }
205
206 $this->store = new $storeClass( $storeConf );
207 foreach ( array( 'manualRecache', 'forceRecache' ) as $var ) {
208 if ( isset( $conf[$var] ) ) {
209 $this->$var = $conf[$var];
210 }
211 }
212 }
213
214 /**
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
219 */
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 ) );
229 }
230 return isset( $this->mergeableKeys[$key] );
231 }
232
233 /**
234 * Get a cache item.
235 *
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
241 */
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' );
247 }
248
249 if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
250 return $this->shallowFallbacks[$code];
251 }
252
253 return $this->data[$code][$key];
254 }
255
256 /**
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
262 */
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' );
269 }
270
271 if ( isset( $this->data[$code][$key][$subkey] ) ) {
272 return $this->data[$code][$key][$subkey];
273 } else {
274 return null;
275 }
276 }
277
278 /**
279 * Get the list of subitem keys for a given item.
280 *
281 * This is faster than array_keys($lc->getItem(...)) for the items listed in
282 * self::$splitKeys.
283 *
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
289 */
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;
299 }
300 }
301 }
302
303 /**
304 * Load an item into the cache.
305 * @param $code
306 * @param $key
307 */
308 protected function loadItem( $code, $key ) {
309 if ( !isset( $this->initialisedLangs[$code] ) ) {
310 $this->initLanguage( $code );
311 }
312
313 // Check to see if initLanguage() loaded it for us
314 if ( isset( $this->loadedItems[$code][$key] ) ) {
315 return;
316 }
317
318 if ( isset( $this->shallowFallbacks[$code] ) ) {
319 $this->loadItem( $this->shallowFallbacks[$code], $key );
320 return;
321 }
322
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;
328 }
329 $this->data[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
330 }
331 } else {
332 $this->data[$code][$key] = $this->store->get( $code, $key );
333 }
334
335 $this->loadedItems[$code][$key] = true;
336 }
337
338 /**
339 * Load a subitem into the cache
340 * @param $code
341 * @param $key
342 * @param $subkey
343 * @return
344 */
345 protected function loadSubitem( $code, $key, $subkey ) {
346 if ( !in_array( $key, self::$splitKeys ) ) {
347 $this->loadItem( $code, $key );
348 return;
349 }
350
351 if ( !isset( $this->initialisedLangs[$code] ) ) {
352 $this->initLanguage( $code );
353 }
354
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;
359 }
360
361 if ( isset( $this->shallowFallbacks[$code] ) ) {
362 $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
363 return;
364 }
365
366 $value = $this->store->get( $code, "$key:$subkey" );
367 $this->data[$code][$key][$subkey] = $value;
368 $this->loadedSubitems[$code][$key][$subkey] = true;
369 }
370
371 /**
372 * Returns true if the cache identified by $code is missing or expired.
373 * @return bool
374 */
375 public function isExpired( $code ) {
376 if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
377 wfDebug( __METHOD__ . "($code): forced reload\n" );
378 return true;
379 }
380
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;
388 }
389
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;
399 }
400 }
401
402 return false;
403 }
404
405 /**
406 * Initialise a language in this object. Rebuild the cache if necessary.
407 * @param $code
408 * @throws MWException
409 */
410 protected function initLanguage( $code ) {
411 if ( isset( $this->initialisedLangs[$code] ) ) {
412 return;
413 }
414
415 $this->initialisedLangs[$code] = true;
416
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;
421 }
422
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' );
431 }
432 return;
433 }
434
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.' );
443 }
444 $this->initShallowFallback( $code, 'en' );
445 return;
446 } else {
447 throw new MWException( 'Invalid or missing localisation cache.' );
448 }
449 }
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;
455 }
456 } else {
457 $this->loadedItems[$code][$key] = true;
458 }
459 }
460 }
461
462 /**
463 * Create a fallback from one language to another, without creating a
464 * complete persistent cache.
465 * @param $primaryCode
466 * @param $fallbackCode
467 */
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;
473 }
474
475 /**
476 * Read a PHP file containing localisation data.
477 * @param $_fileName
478 * @param $_fileType
479 * @throws MWException
480 * @return array
481 */
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 );
487
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" );
494 }
495 return $data;
496 }
497
498 /**
499 * Get the compiled plural rules for a given language from the XML files.
500 * @since 1.20
501 */
502 public function getCompiledPluralRules( $code ) {
503 $rules = $this->getPluralRules( $code );
504 if ( $rules === null ) {
505 return null;
506 }
507 try {
508 $compiledRules = CLDRPluralRuleEvaluator::compile( $rules );
509 } catch( CLDRPluralRuleError $e ) {
510 wfDebugLog( 'l10n', $e->getMessage() . "\n" );
511 return array();
512 }
513 return $compiledRules;
514 }
515
516 /**
517 * Get the plural rules for a given language from the XML files.
518 * Cached.
519 * @since 1.20
520 */
521 public function getPluralRules( $code ) {
522 if ( $this->pluralRules === null ) {
523 $cldrPlural = __DIR__ . "/../languages/data/plurals.xml";
524 $mwPlural = __DIR__ . "/../languages/data/plurals-mediawiki.xml";
525 // Load CLDR plural rules
526 $this->loadPluralFile( $cldrPlural );
527 if ( file_exists( $mwPlural ) ) {
528 // Override or extend
529 $this->loadPluralFile( $mwPlural );
530 }
531 }
532 if ( !isset( $this->pluralRules[$code] ) ) {
533 return null;
534 } else {
535 return $this->pluralRules[$code];
536 }
537 }
538
539
540 /**
541 * Load a plural XML file with the given filename, compile the relevant
542 * rules, and save the compiled rules in a process-local cache.
543 */
544 protected function loadPluralFile( $fileName ) {
545 $doc = new DOMDocument;
546 $doc->load( $fileName );
547 $rulesets = $doc->getElementsByTagName( "pluralRules" );
548 foreach ( $rulesets as $ruleset ) {
549 $codes = $ruleset->getAttribute( 'locales' );
550 $rules = array();
551 $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
552 foreach ( $ruleElements as $elt ) {
553 $rules[] = $elt->nodeValue;
554 }
555 foreach ( explode( ' ', $codes ) as $code ) {
556 $this->pluralRules[$code] = $rules;
557 }
558 }
559 }
560
561 /**
562 * Read the data from the source files for a given language, and register
563 * the relevant dependencies in the $deps array. If the localisation
564 * exists, the data array is returned, otherwise false is returned.
565 */
566 protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
567 $fileName = Language::getMessagesFileName( $code );
568 if ( !file_exists( $fileName ) ) {
569 return false;
570 }
571
572 $deps[] = new FileDependency( $fileName );
573 $data = $this->readPHPFile( $fileName, 'core' );
574
575 # Load CLDR plural rules for JavaScript
576 $data['pluralRules'] = $this->getPluralRules( $code );
577 # And for PHP
578 $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
579
580 $deps['plurals'] = new FileDependency( __DIR__ . "/../languages/data/plurals.xml" );
581 $deps['plurals-mw'] = new FileDependency( __DIR__ . "/../languages/data/plurals-mediawiki.xml" );
582 return $data;
583 }
584
585 /**
586 * Merge two localisation values, a primary and a fallback, overwriting the
587 * primary value in place.
588 * @param $key
589 * @param $value
590 * @param $fallbackValue
591 */
592 protected function mergeItem( $key, &$value, $fallbackValue ) {
593 if ( !is_null( $value ) ) {
594 if ( !is_null( $fallbackValue ) ) {
595 if ( in_array( $key, self::$mergeableMapKeys ) ) {
596 $value = $value + $fallbackValue;
597 } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
598 $value = array_unique( array_merge( $fallbackValue, $value ) );
599 } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
600 $value = array_merge_recursive( $value, $fallbackValue );
601 } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
602 if ( !empty( $value['inherit'] ) ) {
603 $value = array_merge( $fallbackValue, $value );
604 }
605
606 if ( isset( $value['inherit'] ) ) {
607 unset( $value['inherit'] );
608 }
609 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
610 $this->mergeMagicWords( $value, $fallbackValue );
611 }
612 }
613 } else {
614 $value = $fallbackValue;
615 }
616 }
617
618 /**
619 * @param $value
620 * @param $fallbackValue
621 */
622 protected function mergeMagicWords( &$value, $fallbackValue ) {
623 foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
624 if ( !isset( $value[$magicName] ) ) {
625 $value[$magicName] = $fallbackInfo;
626 } else {
627 $oldSynonyms = array_slice( $fallbackInfo, 1 );
628 $newSynonyms = array_slice( $value[$magicName], 1 );
629 $synonyms = array_values( array_unique( array_merge(
630 $newSynonyms, $oldSynonyms ) ) );
631 $value[$magicName] = array_merge( array( $fallbackInfo[0] ), $synonyms );
632 }
633 }
634 }
635
636 /**
637 * Given an array mapping language code to localisation value, such as is
638 * found in extension *.i18n.php files, iterate through a fallback sequence
639 * to merge the given data with an existing primary value.
640 *
641 * Returns true if any data from the extension array was used, false
642 * otherwise.
643 * @param $codeSequence
644 * @param $key
645 * @param $value
646 * @param $fallbackValue
647 * @return bool
648 */
649 protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
650 $used = false;
651 foreach ( $codeSequence as $code ) {
652 if ( isset( $fallbackValue[$code] ) ) {
653 $this->mergeItem( $key, $value, $fallbackValue[$code] );
654 $used = true;
655 }
656 }
657
658 return $used;
659 }
660
661 /**
662 * Load localisation data for a given language for both core and extensions
663 * and save it to the persistent cache store and the process cache
664 * @param $code
665 * @throws MWException
666 */
667 public function recache( $code ) {
668 global $wgExtensionMessagesFiles;
669 wfProfileIn( __METHOD__ );
670
671 if ( !$code ) {
672 throw new MWException( "Invalid language code requested" );
673 }
674 $this->recachedLangs[$code] = true;
675
676 # Initial values
677 $initialData = array_combine(
678 self::$allKeys,
679 array_fill( 0, count( self::$allKeys ), null ) );
680 $coreData = $initialData;
681 $deps = array();
682
683 # Load the primary localisation from the source file
684 $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
685 if ( $data === false ) {
686 wfDebug( __METHOD__ . ": no localisation file for $code, using fallback to en\n" );
687 $coreData['fallback'] = 'en';
688 } else {
689 wfDebug( __METHOD__ . ": got localisation for $code from source\n" );
690
691 # Merge primary localisation
692 foreach ( $data as $key => $value ) {
693 $this->mergeItem( $key, $coreData[$key], $value );
694 }
695
696 }
697
698 # Fill in the fallback if it's not there already
699 if ( is_null( $coreData['fallback'] ) ) {
700 $coreData['fallback'] = $code === 'en' ? false : 'en';
701 }
702 if ( $coreData['fallback'] === false ) {
703 $coreData['fallbackSequence'] = array();
704 } else {
705 $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
706 $len = count( $coreData['fallbackSequence'] );
707
708 # Ensure that the sequence ends at en
709 if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
710 $coreData['fallbackSequence'][] = 'en';
711 }
712
713 # Load the fallback localisation item by item and merge it
714 foreach ( $coreData['fallbackSequence'] as $fbCode ) {
715 # Load the secondary localisation from the source file to
716 # avoid infinite cycles on cyclic fallbacks
717 $fbData = $this->readSourceFilesAndRegisterDeps( $fbCode, $deps );
718 if ( $fbData === false ) {
719 continue;
720 }
721
722 foreach ( self::$allKeys as $key ) {
723 if ( !isset( $fbData[$key] ) ) {
724 continue;
725 }
726
727 if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
728 $this->mergeItem( $key, $coreData[$key], $fbData[$key] );
729 }
730 }
731 }
732 }
733
734 $codeSequence = array_merge( array( $code ), $coreData['fallbackSequence'] );
735
736 # Load the extension localisations
737 # This is done after the core because we know the fallback sequence now.
738 # But it has a higher precedence for merging so that we can support things
739 # like site-specific message overrides.
740 $allData = $initialData;
741 foreach ( $wgExtensionMessagesFiles as $fileName ) {
742 $data = $this->readPHPFile( $fileName, 'extension' );
743 $used = false;
744
745 foreach ( $data as $key => $item ) {
746 if ( $this->mergeExtensionItem( $codeSequence, $key, $allData[$key], $item ) ) {
747 $used = true;
748 }
749 }
750
751 if ( $used ) {
752 $deps[] = new FileDependency( $fileName );
753 }
754 }
755
756 # Merge core data into extension data
757 foreach ( $coreData as $key => $item ) {
758 $this->mergeItem( $key, $allData[$key], $item );
759 }
760
761 # Add cache dependencies for any referenced globals
762 $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
763 $deps['version'] = new ConstantDependency( 'MW_LC_VERSION' );
764
765 # Add dependencies to the cache entry
766 $allData['deps'] = $deps;
767
768 # Replace spaces with underscores in namespace names
769 $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
770
771 # And do the same for special page aliases. $page is an array.
772 foreach ( $allData['specialPageAliases'] as &$page ) {
773 $page = str_replace( ' ', '_', $page );
774 }
775 # Decouple the reference to prevent accidental damage
776 unset( $page );
777
778 # If there were no plural rules, return an empty array
779 if ( $allData['pluralRules'] === null ) {
780 $allData['pluralRules'] = array();
781 }
782 if ( $allData['compiledPluralRules'] === null ) {
783 $allData['compiledPluralRules'] = array();
784 }
785
786 # Set the list keys
787 $allData['list'] = array();
788 foreach ( self::$splitKeys as $key ) {
789 $allData['list'][$key] = array_keys( $allData[$key] );
790 }
791 # Run hooks
792 wfRunHooks( 'LocalisationCacheRecache', array( $this, $code, &$allData ) );
793
794 if ( is_null( $allData['namespaceNames'] ) ) {
795 throw new MWException( __METHOD__ . ': Localisation data failed sanity check! ' .
796 'Check that your languages/messages/MessagesEn.php file is intact.' );
797 }
798
799 # Set the preload key
800 $allData['preload'] = $this->buildPreload( $allData );
801
802 # Save to the process cache and register the items loaded
803 $this->data[$code] = $allData;
804 foreach ( $allData as $key => $item ) {
805 $this->loadedItems[$code][$key] = true;
806 }
807
808 # Save to the persistent cache
809 $this->store->startWrite( $code );
810 foreach ( $allData as $key => $value ) {
811 if ( in_array( $key, self::$splitKeys ) ) {
812 foreach ( $value as $subkey => $subvalue ) {
813 $this->store->set( "$key:$subkey", $subvalue );
814 }
815 } else {
816 $this->store->set( $key, $value );
817 }
818 }
819 $this->store->finishWrite();
820
821 # Clear out the MessageBlobStore
822 # HACK: If using a null (i.e. disabled) storage backend, we
823 # can't write to the MessageBlobStore either
824 if ( !$this->store instanceof LCStore_Null ) {
825 MessageBlobStore::clear();
826 }
827
828 wfProfileOut( __METHOD__ );
829 }
830
831 /**
832 * Build the preload item from the given pre-cache data.
833 *
834 * The preload item will be loaded automatically, improving performance
835 * for the commonly-requested items it contains.
836 * @param $data
837 * @return array
838 */
839 protected function buildPreload( $data ) {
840 $preload = array( 'messages' => array() );
841 foreach ( self::$preloadedKeys as $key ) {
842 $preload[$key] = $data[$key];
843 }
844
845 foreach ( $data['preloadedMessages'] as $subkey ) {
846 if ( isset( $data['messages'][$subkey] ) ) {
847 $subitem = $data['messages'][$subkey];
848 } else {
849 $subitem = null;
850 }
851 $preload['messages'][$subkey] = $subitem;
852 }
853
854 return $preload;
855 }
856
857 /**
858 * Unload the data for a given language from the object cache.
859 * Reduces memory usage.
860 * @param $code
861 */
862 public function unload( $code ) {
863 unset( $this->data[$code] );
864 unset( $this->loadedItems[$code] );
865 unset( $this->loadedSubitems[$code] );
866 unset( $this->initialisedLangs[$code] );
867
868 foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
869 if ( $fbCode === $code ) {
870 $this->unload( $shallowCode );
871 }
872 }
873 }
874
875 /**
876 * Unload all data
877 */
878 public function unloadAll() {
879 foreach ( $this->initialisedLangs as $lang => $unused ) {
880 $this->unload( $lang );
881 }
882 }
883
884 /**
885 * Disable the storage backend
886 */
887 public function disableBackend() {
888 $this->store = new LCStore_Null;
889 $this->manualRecache = false;
890 }
891 }
892
893 /**
894 * Interface for the persistence layer of LocalisationCache.
895 *
896 * The persistence layer is two-level hierarchical cache. The first level
897 * is the language, the second level is the item or subitem.
898 *
899 * Since the data for a whole language is rebuilt in one operation, it needs
900 * to have a fast and atomic method for deleting or replacing all of the
901 * current data for a given language. The interface reflects this bulk update
902 * operation. Callers writing to the cache must first call startWrite(), then
903 * will call set() a couple of thousand times, then will call finishWrite()
904 * to commit the operation. When finishWrite() is called, the cache is
905 * expected to delete all data previously stored for that language.
906 *
907 * The values stored are PHP variables suitable for serialize(). Implementations
908 * of LCStore are responsible for serializing and unserializing.
909 */
910 interface LCStore {
911 /**
912 * Get a value.
913 * @param $code string Language code
914 * @param $key string Cache key
915 */
916 function get( $code, $key );
917
918 /**
919 * Start a write transaction.
920 * @param $code Language code
921 */
922 function startWrite( $code );
923
924 /**
925 * Finish a write transaction.
926 */
927 function finishWrite();
928
929 /**
930 * Set a key to a given value. startWrite() must be called before this
931 * is called, and finishWrite() must be called afterwards.
932 * @param $key
933 * @param $value
934 */
935 function set( $key, $value );
936 }
937
938 /**
939 * LCStore implementation which uses PHP accelerator to store data.
940 * This will work if one of XCache, WinCache or APC cacher is configured.
941 * (See ObjectCache.php)
942 */
943 class LCStore_Accel implements LCStore {
944 var $currentLang;
945 var $keys;
946
947 public function __construct() {
948 $this->cache = wfGetCache( CACHE_ACCEL );
949 }
950
951 public function get( $code, $key ) {
952 $k = wfMemcKey( 'l10n', $code, 'k', $key );
953 $r = $this->cache->get( $k );
954 return $r === false ? null : $r;
955 }
956
957 public function startWrite( $code ) {
958 $k = wfMemcKey( 'l10n', $code, 'l' );
959 $keys = $this->cache->get( $k );
960 if ( $keys ) {
961 foreach ( $keys as $k ) {
962 $this->cache->delete( $k );
963 }
964 }
965 $this->currentLang = $code;
966 $this->keys = array();
967 }
968
969 public function finishWrite() {
970 if ( $this->currentLang ) {
971 $k = wfMemcKey( 'l10n', $this->currentLang, 'l' );
972 $this->cache->set( $k, array_keys( $this->keys ) );
973 }
974 $this->currentLang = null;
975 $this->keys = array();
976 }
977
978 public function set( $key, $value ) {
979 if ( $this->currentLang ) {
980 $k = wfMemcKey( 'l10n', $this->currentLang, 'k', $key );
981 $this->keys[$k] = true;
982 $this->cache->set( $k, $value );
983 }
984 }
985 }
986
987 /**
988 * LCStore implementation which uses the standard DB functions to store data.
989 * This will work on any MediaWiki installation.
990 */
991 class LCStore_DB implements LCStore {
992 var $currentLang;
993 var $writesDone = false;
994
995 /**
996 * @var DatabaseBase
997 */
998 var $dbw;
999 var $batch;
1000 var $readOnly = false;
1001
1002 public function get( $code, $key ) {
1003 if ( $this->writesDone ) {
1004 $db = wfGetDB( DB_MASTER );
1005 } else {
1006 $db = wfGetDB( DB_SLAVE );
1007 }
1008 $row = $db->selectRow( 'l10n_cache', array( 'lc_value' ),
1009 array( 'lc_lang' => $code, 'lc_key' => $key ), __METHOD__ );
1010 if ( $row ) {
1011 return unserialize( $row->lc_value );
1012 } else {
1013 return null;
1014 }
1015 }
1016
1017 public function startWrite( $code ) {
1018 if ( $this->readOnly ) {
1019 return;
1020 }
1021
1022 if ( !$code ) {
1023 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
1024 }
1025
1026 $this->dbw = wfGetDB( DB_MASTER );
1027 try {
1028 $this->dbw->begin( __METHOD__ );
1029 $this->dbw->delete( 'l10n_cache', array( 'lc_lang' => $code ), __METHOD__ );
1030 } catch ( DBQueryError $e ) {
1031 if ( $this->dbw->wasReadOnlyError() ) {
1032 $this->readOnly = true;
1033 $this->dbw->rollback( __METHOD__ );
1034 $this->dbw->ignoreErrors( false );
1035 return;
1036 } else {
1037 throw $e;
1038 }
1039 }
1040
1041 $this->currentLang = $code;
1042 $this->batch = array();
1043 }
1044
1045 public function finishWrite() {
1046 if ( $this->readOnly ) {
1047 return;
1048 }
1049
1050 if ( $this->batch ) {
1051 $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
1052 }
1053
1054 $this->dbw->commit( __METHOD__ );
1055 $this->currentLang = null;
1056 $this->dbw = null;
1057 $this->batch = array();
1058 $this->writesDone = true;
1059 }
1060
1061 public function set( $key, $value ) {
1062 if ( $this->readOnly ) {
1063 return;
1064 }
1065
1066 if ( is_null( $this->currentLang ) ) {
1067 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
1068 }
1069
1070 $this->batch[] = array(
1071 'lc_lang' => $this->currentLang,
1072 'lc_key' => $key,
1073 'lc_value' => serialize( $value ) );
1074
1075 if ( count( $this->batch ) >= 100 ) {
1076 $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
1077 $this->batch = array();
1078 }
1079 }
1080 }
1081
1082 /**
1083 * LCStore implementation which stores data as a collection of CDB files in the
1084 * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
1085 * will throw an exception.
1086 *
1087 * Profiling indicates that on Linux, this implementation outperforms MySQL if
1088 * the directory is on a local filesystem and there is ample kernel cache
1089 * space. The performance advantage is greater when the DBA extension is
1090 * available than it is with the PHP port.
1091 *
1092 * See Cdb.php and http://cr.yp.to/cdb.html
1093 */
1094 class LCStore_CDB implements LCStore {
1095 var $readers, $writer, $currentLang, $directory;
1096
1097 function __construct( $conf = array() ) {
1098 global $wgCacheDirectory;
1099
1100 if ( isset( $conf['directory'] ) ) {
1101 $this->directory = $conf['directory'];
1102 } else {
1103 $this->directory = $wgCacheDirectory;
1104 }
1105 }
1106
1107 public function get( $code, $key ) {
1108 if ( !isset( $this->readers[$code] ) ) {
1109 $fileName = $this->getFileName( $code );
1110
1111 if ( !file_exists( $fileName ) ) {
1112 $this->readers[$code] = false;
1113 } else {
1114 $this->readers[$code] = CdbReader::open( $fileName );
1115 }
1116 }
1117
1118 if ( !$this->readers[$code] ) {
1119 return null;
1120 } else {
1121 $value = $this->readers[$code]->get( $key );
1122
1123 if ( $value === false ) {
1124 return null;
1125 }
1126 return unserialize( $value );
1127 }
1128 }
1129
1130 public function startWrite( $code ) {
1131 if ( !file_exists( $this->directory ) ) {
1132 if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
1133 throw new MWException( "Unable to create the localisation store " .
1134 "directory \"{$this->directory}\"" );
1135 }
1136 }
1137
1138 // Close reader to stop permission errors on write
1139 if ( !empty( $this->readers[$code] ) ) {
1140 $this->readers[$code]->close();
1141 }
1142
1143 $this->writer = CdbWriter::open( $this->getFileName( $code ) );
1144 $this->currentLang = $code;
1145 }
1146
1147 public function finishWrite() {
1148 // Close the writer
1149 $this->writer->close();
1150 $this->writer = null;
1151 unset( $this->readers[$this->currentLang] );
1152 $this->currentLang = null;
1153 }
1154
1155 public function set( $key, $value ) {
1156 if ( is_null( $this->writer ) ) {
1157 throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
1158 }
1159 $this->writer->set( $key, serialize( $value ) );
1160 }
1161
1162 protected function getFileName( $code ) {
1163 if ( !$code || strpos( $code, '/' ) !== false ) {
1164 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
1165 }
1166 return "{$this->directory}/l10n_cache-$code.cdb";
1167 }
1168 }
1169
1170 /**
1171 * Null store backend, used to avoid DB errors during install
1172 */
1173 class LCStore_Null implements LCStore {
1174 public function get( $code, $key ) {
1175 return null;
1176 }
1177
1178 public function startWrite( $code ) {}
1179 public function finishWrite() {}
1180 public function set( $key, $value ) {}
1181 }
1182
1183 /**
1184 * A localisation cache optimised for loading large amounts of data for many
1185 * languages. Used by rebuildLocalisationCache.php.
1186 */
1187 class LocalisationCache_BulkLoad extends LocalisationCache {
1188 /**
1189 * A cache of the contents of data files.
1190 * Core files are serialized to avoid using ~1GB of RAM during a recache.
1191 */
1192 var $fileCache = array();
1193
1194 /**
1195 * Most recently used languages. Uses the linked-list aspect of PHP hashtables
1196 * to keep the most recently used language codes at the end of the array, and
1197 * the language codes that are ready to be deleted at the beginning.
1198 */
1199 var $mruLangs = array();
1200
1201 /**
1202 * Maximum number of languages that may be loaded into $this->data
1203 */
1204 var $maxLoadedLangs = 10;
1205
1206 /**
1207 * @param $fileName
1208 * @param $fileType
1209 * @return array|mixed
1210 */
1211 protected function readPHPFile( $fileName, $fileType ) {
1212 $serialize = $fileType === 'core';
1213 if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
1214 $data = parent::readPHPFile( $fileName, $fileType );
1215
1216 if ( $serialize ) {
1217 $encData = serialize( $data );
1218 } else {
1219 $encData = $data;
1220 }
1221
1222 $this->fileCache[$fileName][$fileType] = $encData;
1223
1224 return $data;
1225 } elseif ( $serialize ) {
1226 return unserialize( $this->fileCache[$fileName][$fileType] );
1227 } else {
1228 return $this->fileCache[$fileName][$fileType];
1229 }
1230 }
1231
1232 /**
1233 * @param $code
1234 * @param $key
1235 * @return mixed
1236 */
1237 public function getItem( $code, $key ) {
1238 unset( $this->mruLangs[$code] );
1239 $this->mruLangs[$code] = true;
1240 return parent::getItem( $code, $key );
1241 }
1242
1243 /**
1244 * @param $code
1245 * @param $key
1246 * @param $subkey
1247 * @return
1248 */
1249 public function getSubitem( $code, $key, $subkey ) {
1250 unset( $this->mruLangs[$code] );
1251 $this->mruLangs[$code] = true;
1252 return parent::getSubitem( $code, $key, $subkey );
1253 }
1254
1255 /**
1256 * @param $code
1257 */
1258 public function recache( $code ) {
1259 parent::recache( $code );
1260 unset( $this->mruLangs[$code] );
1261 $this->mruLangs[$code] = true;
1262 $this->trimCache();
1263 }
1264
1265 /**
1266 * @param $code
1267 */
1268 public function unload( $code ) {
1269 unset( $this->mruLangs[$code] );
1270 parent::unload( $code );
1271 }
1272
1273 /**
1274 * Unload cached languages until there are less than $this->maxLoadedLangs
1275 */
1276 protected function trimCache() {
1277 while ( count( $this->data ) > $this->maxLoadedLangs && count( $this->mruLangs ) ) {
1278 reset( $this->mruLangs );
1279 $code = key( $this->mruLangs );
1280 wfDebug( __METHOD__ . ": unloading $code\n" );
1281 $this->unload( $code );
1282 }
1283 }
1284
1285 }