<?php
-define( 'MW_LC_VERSION', 1 );
+define( 'MW_LC_VERSION', 2 );
/**
* Class for caching the contents of localisation files, Messages*.php
'fallback', 'namespaceNames', 'bookstoreList',
'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable',
'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
- 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
+ 'linkTrail', 'namespaceAliases',
'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
+ 'digitGroupingPattern'
);
/**
* by a fallback sequence.
*/
static public $mergeableMapKeys = array( 'messages', 'namespaceNames',
- 'dateFormats', 'defaultUserOptionOverrides', 'imageFiles',
- 'preloadedMessages',
+ 'dateFormats', 'imageFiles', 'preloadedMessages',
);
/**
/**
* Keys which are loaded automatically by initLanguage()
*/
- static public $preloadedKeys = array( 'dateFormats', 'namespaceNames',
- 'defaultUserOptionOverrides' );
+ static public $preloadedKeys = array( 'dateFormats', 'namespaceNames' );
/**
* Constructor.
* For constructor parameters, see the documentation in DefaultSettings.php
* for $wgLocalisationCacheConf.
+ *
+ * @param $conf Array
*/
function __construct( $conf ) {
global $wgCacheDirectory;
$this->loadItem( $code, $key );
wfProfileOut( __METHOD__.'-load' );
}
+
if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
return $this->shallowFallbacks[$code];
}
+
return $this->data[$code][$key];
}
$this->loadSubitem( $code, $key, $subkey );
wfProfileOut( __METHOD__.'-load' );
}
+
if ( isset( $this->data[$code][$key][$subkey] ) ) {
return $this->data[$code][$key][$subkey];
} else {
if ( !isset( $this->initialisedLangs[$code] ) ) {
$this->initLanguage( $code );
}
+
// Check to see if initLanguage() loaded it for us
if ( isset( $this->loadedItems[$code][$key] ) ) {
return;
}
+
if ( isset( $this->shallowFallbacks[$code] ) ) {
$this->loadItem( $this->shallowFallbacks[$code], $key );
return;
}
+
if ( in_array( $key, self::$splitKeys ) ) {
$subkeyList = $this->getSubitem( $code, 'list', $key );
foreach ( $subkeyList as $subkey ) {
} else {
$this->data[$code][$key] = $this->store->get( $code, $key );
}
+
$this->loadedItems[$code][$key] = true;
}
$this->loadItem( $code, $key );
return;
}
+
if ( !isset( $this->initialisedLangs[$code] ) ) {
$this->initLanguage( $code );
}
+
// Check to see if initLanguage() loaded it for us
if ( isset( $this->loadedItems[$code][$key] )
|| isset( $this->loadedSubitems[$code][$key][$subkey] ) )
{
return;
}
+
if ( isset( $this->shallowFallbacks[$code] ) ) {
$this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
return;
}
+
$value = $this->store->get( $code, "$key:$subkey" );
$this->data[$code][$key][$subkey] = $value;
$this->loadedSubitems[$code][$key][$subkey] = true;
wfDebug( __METHOD__."($code): cache missing, need to make one\n" );
return true;
}
+
foreach ( $deps as $dep ) {
// Because we're unserializing stuff from cache, we
// could receive objects of classes that don't exist
return true;
}
}
+
return false;
}
if ( isset( $this->initialisedLangs[$code] ) ) {
return;
}
+
$this->initialisedLangs[$code] = true;
# If the code is of the wrong form for a Messages*.php file, do a shallow fallback
} else {
throw new MWException( __METHOD__.": Invalid file type: $_fileType" );
}
+
return $data;
}
if ( !empty( $value['inherit'] ) ) {
$value = array_merge( $fallbackValue, $value );
}
+
if ( isset( $value['inherit'] ) ) {
unset( $value['inherit'] );
}
$used = true;
}
}
+
return $used;
}
* and save it to the persistent cache store and the process cache
*/
public function recache( $code ) {
- static $recursionGuard = array();
global $wgExtensionMessagesFiles, $wgExtensionAliasesFiles;
wfProfileIn( __METHOD__ );
foreach ( $data as $key => $value ) {
$this->mergeItem( $key, $coreData[$key], $value );
}
+
}
# Fill in the fallback if it's not there already
$coreData['fallback'] = $code === 'en' ? false : 'en';
}
- if ( $coreData['fallback'] !== false ) {
- # Guard against circular references
- if ( isset( $recursionGuard[$code] ) ) {
- throw new MWException( "Error: Circular fallback reference in language code $code" );
+ if ( $coreData['fallback'] === false ) {
+ $coreData['fallbackSequence'] = array();
+ } else {
+ $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
+ $len = count( $coreData['fallbackSequence'] );
+
+ # Ensure that the sequence ends at en
+ if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
+ $coreData['fallbackSequence'][] = 'en';
}
- $recursionGuard[$code] = true;
# Load the fallback localisation item by item and merge it
- $deps = array_merge( $deps, $this->getItem( $coreData['fallback'], 'deps' ) );
- foreach ( self::$allKeys as $key ) {
- if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
- $fallbackValue = $this->getItem( $coreData['fallback'], $key );
- $this->mergeItem( $key, $coreData[$key], $fallbackValue );
+ foreach ( $coreData['fallbackSequence'] as $fbCode ) {
+ # Load the secondary localisation from the source file to
+ # avoid infinite cycles on cyclic fallbacks
+ $fbFilename = Language::getMessagesFileName( $fbCode );
+
+ if ( !file_exists( $fbFilename ) ) {
+ continue;
+ }
+
+ $deps[] = new FileDependency( $fbFilename );
+ $fbData = $this->readPHPFile( $fbFilename, 'core' );
+
+ foreach ( self::$allKeys as $key ) {
+ if ( !isset( $fbData[$key] ) ) {
+ continue;
+ }
+
+ if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
+ $this->mergeItem( $key, $coreData[$key], $fbData[$key] );
+ }
}
}
- $fallbackSequence = $this->getItem( $coreData['fallback'], 'fallbackSequence' );
- array_unshift( $fallbackSequence, $coreData['fallback'] );
- $coreData['fallbackSequence'] = $fallbackSequence;
- unset( $recursionGuard[$code] );
- } else {
- $coreData['fallbackSequence'] = array();
}
+
$codeSequence = array_merge( array( $code ), $coreData['fallbackSequence'] );
# Load the extension localisations
foreach ( $wgExtensionMessagesFiles as $fileName ) {
$data = $this->readPHPFile( $fileName, 'extension' );
$used = false;
+
foreach ( $data as $key => $item ) {
if( $this->mergeExtensionItem( $codeSequence, $key, $allData[$key], $item ) ) {
$used = true;
}
}
+
if ( $used ) {
$deps[] = new FileDependency( $fileName );
}
# Load deprecated $wgExtensionAliasesFiles
foreach ( $wgExtensionAliasesFiles as $fileName ) {
$data = $this->readPHPFile( $fileName, 'aliases' );
+
if ( !isset( $data['aliases'] ) ) {
continue;
}
+
$used = $this->mergeExtensionItem( $codeSequence, 'specialPageAliases',
$allData['specialPageAliases'], $data['aliases'] );
+
if ( $used ) {
$deps[] = new FileDependency( $fileName );
}
# Decouple the reference to prevent accidental damage
unset($page);
- # Fix broken defaultUserOptionOverrides
- if ( !is_array( $allData['defaultUserOptionOverrides'] ) ) {
- $allData['defaultUserOptionOverrides'] = array();
- }
-
# Set the list keys
$allData['list'] = array();
foreach ( self::$splitKeys as $key ) {
# Run hooks
wfRunHooks( 'LocalisationCacheRecache', array( $this, $code, &$allData ) );
- if ( is_null( $allData['defaultUserOptionOverrides'] ) ) {
+ if ( is_null( $allData['namespaceNames'] ) ) {
throw new MWException( __METHOD__.': Localisation data failed sanity check! ' .
'Check that your languages/messages/MessagesEn.php file is intact.' );
}
foreach ( self::$preloadedKeys as $key ) {
$preload[$key] = $data[$key];
}
+
foreach ( $data['preloadedMessages'] as $subkey ) {
if ( isset( $data['messages'][$subkey] ) ) {
$subitem = $data['messages'][$subkey];
}
$preload['messages'][$subkey] = $subitem;
}
+
return $preload;
}
unset( $this->loadedItems[$code] );
unset( $this->loadedSubitems[$code] );
unset( $this->initialisedLangs[$code] );
+
foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
if ( $fbCode === $code ) {
$this->unload( $shallowCode );
* is called, and finishWrite() must be called afterwards.
*/
function set( $key, $value );
-
}
/**
if ( $this->readOnly ) {
return;
}
+
if ( !$code ) {
throw new MWException( __METHOD__.": Invalid language \"$code\"" );
}
+
$this->dbw = wfGetDB( DB_MASTER );
try {
$this->dbw->begin();
throw $e;
}
}
+
$this->currentLang = $code;
$this->batch = array();
}
if ( $this->readOnly ) {
return;
}
+
if ( $this->batch ) {
$this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
}
+
$this->dbw->commit();
$this->currentLang = null;
$this->dbw = null;
if ( $this->readOnly ) {
return;
}
+
if ( is_null( $this->currentLang ) ) {
throw new MWException( __CLASS__.': must call startWrite() before calling set()' );
}
+
$this->batch[] = array(
'lc_lang' => $this->currentLang,
'lc_key' => $key,
'lc_value' => serialize( $value ) );
+
if ( count( $this->batch ) >= 100 ) {
$this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
$this->batch = array();
function __construct( $conf = array() ) {
global $wgCacheDirectory;
+
if ( isset( $conf['directory'] ) ) {
$this->directory = $conf['directory'];
} else {
public function get( $code, $key ) {
if ( !isset( $this->readers[$code] ) ) {
$fileName = $this->getFileName( $code );
+
if ( !file_exists( $fileName ) ) {
$this->readers[$code] = false;
} else {
$this->readers[$code] = CdbReader::open( $fileName );
}
}
+
if ( !$this->readers[$code] ) {
return null;
} else {
$value = $this->readers[$code]->get( $key );
+
if ( $value === false ) {
return null;
}
public function startWrite( $code ) {
if ( !file_exists( $this->directory ) ) {
- if ( !wfMkdirParents( $this->directory ) ) {
+ if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
throw new MWException( "Unable to create the localisation store " .
"directory \"{$this->directory}\"" );
}
}
+
// Close reader to stop permission errors on write
if( !empty($this->readers[$code]) ) {
$this->readers[$code]->close();
}
+
$this->writer = CdbWriter::open( $this->getFileName( $code ) );
$this->currentLang = $code;
}
$serialize = $fileType === 'core';
if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
$data = parent::readPHPFile( $fileName, $fileType );
+
if ( $serialize ) {
$encData = serialize( $data );
} else {
$encData = $data;
}
+
$this->fileCache[$fileName][$fileType] = $encData;
+
return $data;
} elseif ( $serialize ) {
return unserialize( $this->fileCache[$fileName][$fileType] );