25use CLDRPluralRuleParser\Evaluator;
26use CLDRPluralRuleParser\Error
as CLDRPluralRuleError;
112 'fallback',
'namespaceNames',
'bookstoreList',
113 'magicWords',
'messages',
'rtl',
'capitalizeAllNouns',
'digitTransformTable',
114 'separatorTransformTable',
'fallback8bitEncoding',
'linkPrefixExtension',
115 'linkTrail',
'linkPrefixCharset',
'namespaceAliases',
116 'dateFormats',
'datePreferences',
'datePreferenceMigrationMap',
117 'defaultDateFormat',
'extraUserToggles',
'specialPageAliases',
118 'imageFiles',
'preloadedMessages',
'namespaceGenderAliases',
119 'digitGroupingPattern',
'pluralRules',
'pluralRuleTypes',
'compiledPluralRules',
127 'namespaceAliases',
'dateFormats',
'imageFiles',
'preloadedMessages'
198 if ( !empty(
$conf[
'storeClass'] ) ) {
199 $storeClass =
$conf[
'storeClass'];
201 switch (
$conf[
'store'] ) {
204 $storeClass =
'LCStoreCDB';
207 $storeClass =
'LCStoreDB';
210 $storeClass =
'LCStoreStaticArray';
213 if ( !empty(
$conf[
'storeDirectory'] ) ) {
214 $storeClass =
'LCStoreCDB';
217 $storeClass =
'LCStoreCDB';
219 $storeClass =
'LCStoreDB';
224 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.'
229 wfDebugLog(
'caches', get_class( $this ) .
": using store $storeClass" );
230 if ( !empty(
$conf[
'storeDirectory'] ) ) {
231 $storeConf[
'directory'] =
$conf[
'storeDirectory'];
234 $this->
store =
new $storeClass( $storeConf );
235 foreach ( [
'manualRecache',
'forceRecache' ]
as $var ) {
236 if ( isset(
$conf[$var] ) ) {
237 $this->$var =
$conf[$var];
249 if ( $this->mergeableKeys ===
null ) {
250 $this->mergeableKeys = array_flip( array_merge(
251 self::$mergeableMapKeys,
252 self::$mergeableListKeys,
253 self::$mergeableAliasListKeys,
254 self::$optionalMergeKeys,
259 return isset( $this->mergeableKeys[$key] );
272 if ( !isset( $this->loadedItems[
$code][$key] ) ) {
276 if ( $key ===
'fallback' && isset( $this->shallowFallbacks[
$code] ) ) {
277 return $this->shallowFallbacks[
$code];
291 if ( !isset( $this->loadedSubitems[
$code][$key][$subkey] ) &&
292 !isset( $this->loadedItems[
$code][$key] )
297 if ( isset( $this->
data[
$code][$key][$subkey] ) ) {
298 return $this->
data[
$code][$key][$subkey];
317 if ( in_array( $key, self::$splitKeys ) ) {
321 if ( is_array( $item ) ) {
322 return array_keys( $item );
335 if ( !isset( $this->initialisedLangs[
$code] ) ) {
340 if ( isset( $this->loadedItems[
$code][$key] ) ) {
344 if ( isset( $this->shallowFallbacks[
$code] ) ) {
350 if ( in_array( $key, self::$splitKeys ) ) {
351 $subkeyList = $this->
getSubitem( $code,
'list', $key );
352 foreach ( $subkeyList
as $subkey ) {
353 if ( isset( $this->
data[
$code][$key][$subkey] ) ) {
362 $this->loadedItems[
$code][$key] =
true;
372 if ( !in_array( $key, self::$splitKeys ) ) {
378 if ( !isset( $this->initialisedLangs[
$code] ) ) {
383 if ( isset( $this->loadedItems[
$code][$key] ) ||
384 isset( $this->loadedSubitems[
$code][$key][$subkey] )
389 if ( isset( $this->shallowFallbacks[
$code] ) ) {
397 $this->loadedSubitems[
$code][$key][$subkey] =
true;
408 if ( $this->forceRecache && !isset( $this->recachedLangs[
$code] ) ) {
409 wfDebug( __METHOD__ .
"($code): forced reload\n" );
416 $preload = $this->
store->get(
$code,
'preload' );
418 if ( $deps ===
null ||
$keys ===
null || $preload ===
null ) {
419 wfDebug( __METHOD__ .
"($code): cache missing, need to make one\n" );
424 foreach ( $deps
as $dep ) {
430 wfDebug( __METHOD__ .
"($code): cache for $code expired due to " .
431 get_class( $dep ) .
"\n" );
446 if ( isset( $this->initialisedLangs[
$code] ) ) {
450 $this->initialisedLangs[
$code] =
true;
452 # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
453 if ( !Language::isValidBuiltInCode(
$code ) ) {
459 # Recache the data if necessary
460 if ( !$this->manualRecache && $this->
isExpired( $code ) ) {
461 if ( Language::isSupportedLanguage(
$code ) ) {
463 } elseif (
$code ===
'en' ) {
464 throw new MWException(
'MessagesEn.php is missing.' );
473 $preload = $this->
getItem( $code,
'preload' );
474 if ( $preload ===
null ) {
475 if ( $this->manualRecache ) {
477 if (
$code ===
'en' ) {
478 throw new MWException(
'No localisation cache found for English. ' .
479 'Please run maintenance/rebuildLocalisationCache.php.' );
485 throw new MWException(
'Invalid or missing localisation cache.' );
489 foreach ( $preload
as $key => $item ) {
490 if ( in_array( $key, self::$splitKeys ) ) {
491 foreach ( $item
as $subkey => $subitem ) {
492 $this->loadedSubitems[
$code][$key][$subkey] =
true;
495 $this->loadedItems[
$code][$key] =
true;
507 $this->
data[$primaryCode] =& $this->
data[$fallbackCode];
508 $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
509 $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
510 $this->shallowFallbacks[$primaryCode] = $fallbackCode;
522 MediaWiki\suppressWarnings();
523 $_apcEnabled = ini_set(
'apc.cache_by_default',
'0' );
524 MediaWiki\restoreWarnings();
528 MediaWiki\suppressWarnings();
529 ini_set(
'apc.cache_by_default', $_apcEnabled );
530 MediaWiki\restoreWarnings();
532 if ( $_fileType ==
'core' || $_fileType ==
'extension' ) {
533 $data = compact( self::$allKeys );
534 } elseif ( $_fileType ==
'aliases' ) {
535 $data = compact(
'aliases' );
537 throw new MWException( __METHOD__ .
": Invalid file type: $_fileType" );
551 if ( !is_readable( $fileName ) ) {
555 $json = file_get_contents( $fileName );
556 if ( $json ===
false ) {
560 $data = FormatJson::decode( $json,
true );
561 if (
$data ===
null ) {
563 throw new MWException( __METHOD__ .
": Invalid JSON file: $fileName" );
567 foreach (
$data as $key => $unused ) {
568 if ( $key ===
'' || $key[0] ===
'@' ) {
569 unset(
$data[$key] );
574 return [
'messages' =>
$data ];
585 if ( $rules ===
null ) {
589 $compiledRules = Evaluator::compile( $rules );
590 }
catch ( CLDRPluralRuleError
$e ) {
596 return $compiledRules;
607 if ( $this->pluralRules ===
null ) {
610 if ( !isset( $this->pluralRules[
$code] ) ) {
613 return $this->pluralRules[
$code];
625 if ( $this->pluralRuleTypes ===
null ) {
628 if ( !isset( $this->pluralRuleTypes[
$code] ) ) {
631 return $this->pluralRuleTypes[
$code];
640 $cldrPlural =
"$IP/languages/data/plurals.xml";
641 $mwPlural =
"$IP/languages/data/plurals-mediawiki.xml";
644 if ( file_exists( $mwPlural ) ) {
659 $xml = file_get_contents( $fileName );
661 throw new MWException(
"Unable to read plurals file $fileName" );
663 $doc =
new DOMDocument;
664 $doc->loadXML( $xml );
665 $rulesets = $doc->getElementsByTagName(
"pluralRules" );
666 foreach ( $rulesets
as $ruleset ) {
667 $codes = $ruleset->getAttribute(
'locales' );
670 $ruleElements = $ruleset->getElementsByTagName(
"pluralRule" );
671 foreach ( $ruleElements
as $elt ) {
672 $ruleType = $elt->getAttribute(
'count' );
673 if ( $ruleType ===
'other' ) {
677 $rules[] = $elt->nodeValue;
678 $ruleTypes[] = $ruleType;
680 foreach ( explode(
' ', $codes )
as $code ) {
681 $this->pluralRules[
$code] = $rules;
682 $this->pluralRuleTypes[
$code] = $ruleTypes;
700 $fileName = Language::getMessagesFileName(
$code );
701 if ( !file_exists( $fileName ) ) {
708 # Load CLDR plural rules for JavaScript
712 # Load plural rule types
715 $deps[
'plurals'] =
new FileDependency(
"$IP/languages/data/plurals.xml" );
716 $deps[
'plurals-mw'] =
new FileDependency(
"$IP/languages/data/plurals-mediawiki.xml" );
729 if ( !is_null(
$value ) ) {
730 if ( !is_null( $fallbackValue ) ) {
731 if ( in_array( $key, self::$mergeableMapKeys ) ) {
733 } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
734 $value = array_unique( array_merge( $fallbackValue,
$value ) );
735 } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
736 $value = array_merge_recursive(
$value, $fallbackValue );
737 } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
738 if ( !empty(
$value[
'inherit'] ) ) {
742 if ( isset(
$value[
'inherit'] ) ) {
743 unset(
$value[
'inherit'] );
745 } elseif ( in_array( $key, self::$magicWordKeys ) ) {
759 foreach ( $fallbackValue
as $magicName => $fallbackInfo ) {
760 if ( !isset(
$value[$magicName] ) ) {
761 $value[$magicName] = $fallbackInfo;
763 $oldSynonyms = array_slice( $fallbackInfo, 1 );
764 $newSynonyms = array_slice(
$value[$magicName], 1 );
765 $synonyms = array_values( array_unique( array_merge(
766 $newSynonyms, $oldSynonyms ) ) );
767 $value[$magicName] = array_merge( [ $fallbackInfo[0] ], $synonyms );
787 foreach ( $codeSequence
as $code ) {
788 if ( isset( $fallbackValue[
$code] ) ) {
807 $config = MediaWikiServices::getInstance()->getMainConfig();
808 $messagesDirs = $config->get(
'MessagesDirs' );
810 'core' =>
"$IP/languages/i18n",
811 'api' =>
"$IP/includes/api/i18n",
812 'oojs-ui' =>
"$IP/resources/lib/oojs-ui/i18n",
826 throw new MWException(
"Invalid language code requested" );
828 $this->recachedLangs[
$code] =
true;
831 $initialData = array_fill_keys( self::$allKeys,
null );
832 $coreData = $initialData;
835 # Load the primary localisation from the source file
837 if (
$data ===
false ) {
838 wfDebug( __METHOD__ .
": no localisation file for $code, using fallback to en\n" );
839 $coreData[
'fallback'] =
'en';
841 wfDebug( __METHOD__ .
": got localisation for $code from source\n" );
843 # Merge primary localisation
849 # Fill in the fallback if it's not there already
850 if ( is_null( $coreData[
'fallback'] ) ) {
851 $coreData[
'fallback'] =
$code ===
'en' ?
false :
'en';
853 if ( $coreData[
'fallback'] ===
false ) {
854 $coreData[
'fallbackSequence'] = [];
856 $coreData[
'fallbackSequence'] = array_map(
'trim', explode(
',', $coreData[
'fallback'] ) );
857 $len = count( $coreData[
'fallbackSequence'] );
859 # Ensure that the sequence ends at en
860 if ( $coreData[
'fallbackSequence'][$len - 1] !==
'en' ) {
861 $coreData[
'fallbackSequence'][] =
'en';
865 $codeSequence = array_merge( [
$code ], $coreData[
'fallbackSequence'] );
868 # Load non-JSON localisation data for extensions
869 $extensionData = array_fill_keys( $codeSequence, $initialData );
871 if ( isset( $messageDirs[$extension] ) ) {
872 # This extension has JSON message data; skip the PHP shim
879 foreach (
$data as $key => $item ) {
880 foreach ( $codeSequence
as $csCode ) {
881 if ( isset( $item[$csCode] ) ) {
882 $this->
mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] );
893 # Load the localisation data for each fallback, then merge it into the full array
894 $allData = $initialData;
895 foreach ( $codeSequence
as $csCode ) {
896 $csData = $initialData;
898 # Load core messages and the extension localisations.
899 foreach ( $messageDirs
as $dirs ) {
901 $fileName =
"$dir/$csCode.json";
904 foreach (
$data as $key => $item ) {
905 $this->
mergeItem( $key, $csData[$key], $item );
912 # Merge non-JSON extension data
913 if ( isset( $extensionData[$csCode] ) ) {
914 foreach ( $extensionData[$csCode]
as $key => $item ) {
915 $this->
mergeItem( $key, $csData[$key], $item );
919 if ( $csCode ===
$code ) {
920 # Merge core data into extension data
921 foreach ( $coreData
as $key => $item ) {
922 $this->
mergeItem( $key, $csData[$key], $item );
925 # Load the secondary localisation from the source file to
926 # avoid infinite cycles on cyclic fallbacks
928 if ( $fbData !==
false ) {
929 # Only merge the keys that make sense to merge
930 foreach ( self::$allKeys
as $key ) {
931 if ( !isset( $fbData[$key] ) ) {
935 if ( is_null( $coreData[$key] ) || $this->
isMergeableKey( $key ) ) {
936 $this->
mergeItem( $key, $csData[$key], $fbData[$key] );
942 # Allow extensions an opportunity to adjust the data for this
944 Hooks::run(
'LocalisationCacheRecacheFallback', [ $this, $csCode, &$csData ] );
946 # Merge the data for this fallback into the final array
947 if ( $csCode ===
$code ) {
950 foreach ( self::$allKeys
as $key ) {
951 if ( !isset( $csData[$key] ) ) {
955 if ( is_null( $allData[$key] ) || $this->
isMergeableKey( $key ) ) {
956 $this->
mergeItem( $key, $allData[$key], $csData[$key] );
962 # Add cache dependencies for any referenced globals
963 $deps[
'wgExtensionMessagesFiles'] =
new GlobalDependency(
'wgExtensionMessagesFiles' );
969 # Add dependencies to the cache entry
970 $allData[
'deps'] = $deps;
972 # Replace spaces with underscores in namespace names
973 $allData[
'namespaceNames'] = str_replace(
' ',
'_', $allData[
'namespaceNames'] );
975 # And do the same for special page aliases. $page is an array.
976 foreach ( $allData[
'specialPageAliases']
as &
$page ) {
979 # Decouple the reference to prevent accidental damage
982 # If there were no plural rules, return an empty array
983 if ( $allData[
'pluralRules'] ===
null ) {
984 $allData[
'pluralRules'] = [];
986 if ( $allData[
'compiledPluralRules'] ===
null ) {
987 $allData[
'compiledPluralRules'] = [];
989 # If there were no plural rule types, return an empty array
990 if ( $allData[
'pluralRuleTypes'] ===
null ) {
991 $allData[
'pluralRuleTypes'] = [];
995 $allData[
'list'] = [];
996 foreach ( self::$splitKeys
as $key ) {
997 $allData[
'list'][$key] = array_keys( $allData[$key] );
1001 Hooks::run(
'LocalisationCacheRecache', [ $this,
$code, &$allData, &$purgeBlobs ] );
1003 if ( is_null( $allData[
'namespaceNames'] ) ) {
1004 throw new MWException( __METHOD__ .
': Localisation data failed sanity check! ' .
1005 'Check that your languages/messages/MessagesEn.php file is intact.' );
1008 # Set the preload key
1011 # Save to the process cache and register the items loaded
1013 foreach ( $allData
as $key => $item ) {
1014 $this->loadedItems[
$code][$key] =
true;
1017 # Save to the persistent cache
1019 foreach ( $allData
as $key =>
$value ) {
1020 if ( in_array( $key, self::$splitKeys ) ) {
1021 foreach (
$value as $subkey => $subvalue ) {
1022 $this->
store->set(
"$key:$subkey", $subvalue );
1028 $this->
store->finishWrite();
1030 # Clear out the MessageBlobStore
1031 # HACK: If using a null (i.e. disabled) storage backend, we
1032 # can't write to the MessageBlobStore either
1035 $blobStore->clear();
1049 $preload = [
'messages' => [] ];
1050 foreach ( self::$preloadedKeys
as $key ) {
1051 $preload[$key] =
$data[$key];
1054 foreach (
$data[
'preloadedMessages']
as $subkey ) {
1055 if ( isset(
$data[
'messages'][$subkey] ) ) {
1056 $subitem =
$data[
'messages'][$subkey];
1060 $preload[
'messages'][$subkey] = $subitem;
1073 unset( $this->loadedItems[
$code] );
1074 unset( $this->loadedSubitems[
$code] );
1075 unset( $this->initialisedLangs[
$code] );
1076 unset( $this->shallowFallbacks[
$code] );
1078 foreach ( $this->shallowFallbacks
as $shallowCode => $fbCode ) {
1079 if ( $fbCode ===
$code ) {
1080 $this->
unload( $shallowCode );
1089 foreach ( $this->initialisedLangs
as $lang => $unused ) {
1099 $this->manualRecache =
false;
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgCacheDirectory
Directory for caching data in the local filesystem.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
isExpired()
Returns true if the dependency is expired, false otherwise.
Null store backend, used to avoid DB errors during install.
Class for caching the contents of localisation files, Messages*.php and *.i18n.php.
$manualRecache
True if recaching should only be done on an explicit call to recache().
static $magicWordKeys
Keys for items that are formatted like $magicWords.
buildPreload( $data)
Build the preload item from the given pre-cache data.
initLanguage( $code)
Initialise a language in this object.
static $mergeableMapKeys
Keys for items which consist of associative arrays, which may be merged by a fallback sequence.
readSourceFilesAndRegisterDeps( $code, &$deps)
Read the data from the source files for a given language, and register the relevant dependencies in t...
isMergeableKey( $key)
Returns true if the given key is mergeable, that is, if it is an associative array which can be merge...
loadPluralFile( $fileName)
Load a plural XML file with the given filename, compile the relevant rules, and save the compiled rul...
getPluralRules( $code)
Get the plural rules for a given language from the XML files.
$loadedItems
A 2-d associative array, code/key, where presence indicates that the item is loaded.
readPHPFile( $_fileName, $_fileType)
Read a PHP file containing localisation data.
$initialisedLangs
An array where presence of a key indicates that that language has been initialised.
readJSONFile( $fileName)
Read a JSON file containing localisation messages.
loadPluralFiles()
Load the plural XML files.
unload( $code)
Unload the data for a given language from the object cache.
unloadAll()
Unload all data.
disableBackend()
Disable the storage backend.
static $mergeableAliasListKeys
Keys for items which contain an array of arrays of equivalent aliases for each subitem.
getSubitemList( $code, $key)
Get the list of subitem keys for a given item.
$recachedLangs
An array where the keys are codes that have been recached by this instance.
$loadedSubitems
A 3-d associative array, code/key/subkey, where presence indicates that the subitem is loaded.
static $mergeableListKeys
Keys for items which are a numbered array.
recache( $code)
Load localisation data for a given language for both core and extensions and save it to the persisten...
isExpired( $code)
Returns true if the cache identified by $code is missing or expired.
mergeItem( $key, &$value, $fallbackValue)
Merge two localisation values, a primary and a fallback, overwriting the primary value in place.
getSubitem( $code, $key, $subkey)
Get a subitem, for instance a single message for a given language.
mergeMagicWords(&$value, $fallbackValue)
$shallowFallbacks
An array mapping non-existent pseudo-languages to fallback languages.
initShallowFallback( $primaryCode, $fallbackCode)
Create a fallback from one language to another, without creating a complete persistent cache.
static $allKeys
All item keys.
getCompiledPluralRules( $code)
Get the compiled plural rules for a given language from the XML files.
static $optionalMergeKeys
Keys for items which contain an associative array, and may be merged if the primary value contains th...
__construct( $conf)
Constructor.
getMessagesDirs()
Gets the combined list of messages dirs from core and extensions.
static $preloadedKeys
Keys which are loaded automatically by initLanguage()
getItem( $code, $key)
Get a cache item.
static $splitKeys
Keys for items where the subitems are stored in the backend separately.
loadItem( $code, $key)
Load an item into the cache.
$pluralRuleTypes
Associative array of cached plural rule types.
getPluralRuleTypes( $code)
Get the plural rule types for a given language from the XML files.
$forceRecache
True to treat all files as expired until they are regenerated by this object.
LCStore $store
The persistent store object.
loadSubitem( $code, $key, $subkey)
Load a subitem into the cache.
$pluralRules
Associative array of cached plural rules.
$conf
Configuration associative array.
mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue)
Given an array mapping language code to localisation value, such as is found in extension *....
This class generates message blobs for use by ResourceLoader modules.
when a variable name is used in a it is silently declared as a new local masking the global
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
processing should stop and the error should be shown to the user * false
returning false will NOT prevent logging $e
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
if(count( $args)==0) $dir
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Interface for the persistence layer of LocalisationCache.
$wgExtensionMessagesFiles['ExtensionNameMagic']
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN boolean columns are always mapped to as the code does not always treat the column as a and VARBINARY columns should simply be TEXT The only exception is when VARBINARY is used to store true binary data
MediaWiki s SiteStore can be cached and stored in a flat in a json format If the SiteStore is frequently the file cache may provide a performance benefit over a database store
if(!isset( $args[0])) $lang