Followup r94995: add hooks.txt entries for the new hooks
[lhc/web/wiklou.git] / includes / LocalisationCache.php
index 9d00681..b20db32 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-define( 'MW_LC_VERSION', 1 );
+define( 'MW_LC_VERSION', 2 );
 
 /**
  * Class for caching the contents of localisation files, Messages*.php
@@ -8,8 +8,8 @@ define( 'MW_LC_VERSION', 1 );
  *
  * An instance of this class is available using Language::getLocalisationCache().
  *
- * The values retrieved from here are merged, containing items from extension 
- * files, core messages files and the language fallback sequence (e.g. zh-cn -> 
+ * The values retrieved from here are merged, containing items from extension
+ * files, core messages files and the language fallback sequence (e.g. zh-cn ->
  * zh-hans -> en ). Some common errors are corrected, for example namespace
  * names with spaces instead of underscores, but heavyweight processing, such
  * as grammatical transformation, is done by the caller.
@@ -84,7 +84,7 @@ class LocalisationCache {
                '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',
@@ -95,8 +95,7 @@ class LocalisationCache {
         * by a fallback sequence.
         */
        static public $mergeableMapKeys = array( 'messages', 'namespaceNames',
-               'dateFormats', 'defaultUserOptionOverrides', 'imageFiles',
-               'preloadedMessages',
+               'dateFormats', 'imageFiles', 'preloadedMessages',
        );
 
        /**
@@ -130,13 +129,14 @@ class LocalisationCache {
        /**
         * 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 constructor parameters, see the documentation in DefaultSettings.php
         * for $wgLocalisationCacheConf.
+        *
+        * @param $conf Array
         */
        function __construct( $conf ) {
                global $wgCacheDirectory;
@@ -158,7 +158,7 @@ class LocalisationCache {
                                        $storeClass = $wgCacheDirectory ? 'LCStore_CDB' : 'LCStore_DB';
                                        break;
                                default:
-                                       throw new MWException( 
+                                       throw new MWException(
                                                'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' );
                        }
                }
@@ -215,8 +215,8 @@ class LocalisationCache {
         * Get a subitem, for instance a single message for a given language.
         */
        public function getSubitem( $code, $key, $subkey ) {
-               if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) 
-                       && !isset( $this->loadedItems[$code][$key] ) ) 
+               if ( !isset( $this->loadedSubitems[$code][$key][$subkey] )
+                       && !isset( $this->loadedItems[$code][$key] ) )
                {
                        wfProfileIn( __METHOD__.'-load' );
                        $this->loadSubitem( $code, $key, $subkey );
@@ -232,10 +232,10 @@ class LocalisationCache {
        /**
         * Get the list of subitem keys for a given item.
         *
-        * This is faster than array_keys($lc->getItem(...)) for the items listed in 
+        * This is faster than array_keys($lc->getItem(...)) for the items listed in
         * self::$splitKeys.
         *
-        * Will return null if the item is not found, or false if the item is not an 
+        * Will return null if the item is not found, or false if the item is not an
         * array.
         */
        public function getSubitemList( $code, $key ) {
@@ -326,7 +326,7 @@ class LocalisationCache {
                        // anymore (e.g. uninstalled extensions)
                        // When this happens, always expire the cache
                        if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
-                               wfDebug( __METHOD__."($code): cache for $code expired due to " . 
+                               wfDebug( __METHOD__."($code): cache for $code expired due to " .
                                        get_class( $dep ) . "\n" );
                                return true;
                        }
@@ -367,7 +367,7 @@ class LocalisationCache {
                        if ( $this->manualRecache ) {
                                // No Messages*.php file. Do shallow fallback to en.
                                if ( $code === 'en' ) {
-                                       throw new MWException( 'No localisation cache found for English. ' . 
+                                       throw new MWException( 'No localisation cache found for English. ' .
                                                'Please run maintenance/rebuildLocalisationCache.php.' );
                                }
                                $this->initShallowFallback( $code, 'en' );
@@ -389,7 +389,7 @@ class LocalisationCache {
        }
 
        /**
-        * Create a fallback from one language to another, without creating a 
+        * Create a fallback from one language to another, without creating a
         * complete persistent cache.
         */
        public function initShallowFallback( $primaryCode, $fallbackCode ) {
@@ -419,7 +419,7 @@ class LocalisationCache {
        }
 
        /**
-        * Merge two localisation values, a primary and a fallback, overwriting the 
+        * Merge two localisation values, a primary and a fallback, overwriting the
         * primary value in place.
         */
        protected function mergeItem( $key, &$value, $fallbackValue ) {
@@ -454,7 +454,7 @@ class LocalisationCache {
                        } else {
                                $oldSynonyms = array_slice( $fallbackInfo, 1 );
                                $newSynonyms = array_slice( $value[$magicName], 1 );
-                               $synonyms = array_values( array_unique( array_merge( 
+                               $synonyms = array_values( array_unique( array_merge(
                                        $newSynonyms, $oldSynonyms ) ) );
                                $value[$magicName] = array_merge( array( $fallbackInfo[0] ), $synonyms );
                        }
@@ -466,7 +466,7 @@ class LocalisationCache {
         * found in extension *.i18n.php files, iterate through a fallback sequence
         * to merge the given data with an existing primary value.
         *
-        * Returns true if any data from the extension array was used, false 
+        * Returns true if any data from the extension array was used, false
         * otherwise.
         */
        protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
@@ -485,7 +485,6 @@ class LocalisationCache {
         * and save it to the persistent cache store and the process cache
         */
        public function recache( $code ) {
-               static $recursionGuard = array();
                global $wgExtensionMessagesFiles, $wgExtensionAliasesFiles;
                wfProfileIn( __METHOD__ );
 
@@ -496,7 +495,7 @@ class LocalisationCache {
 
                # Initial values
                $initialData = array_combine(
-                       self::$allKeys, 
+                       self::$allKeys,
                        array_fill( 0, count( self::$allKeys ), null ) );
                $coreData = $initialData;
                $deps = array();
@@ -522,33 +521,33 @@ class LocalisationCache {
                        $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 $fallback ) {
+                               $deps = array_merge( $deps, $this->getItem( $fallback, 'deps' ) );
+                               foreach ( self::$allKeys as $key ) {
+                                       if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
+                                               $fallbackValue = $this->getItem( $fallback, $key );
+                                               $this->mergeItem( $key, $coreData[$key], $fallbackValue );
+                                       }
                                }
                        }
-                       $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
                # This is done after the core because we know the fallback sequence now.
-               # But it has a higher precedence for merging so that we can support things 
+               # But it has a higher precedence for merging so that we can support things
                # like site-specific message overrides.
                $allData = $initialData;
                foreach ( $wgExtensionMessagesFiles as $fileName ) {
@@ -599,11 +598,6 @@ class LocalisationCache {
                }
                # 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();
@@ -614,11 +608,6 @@ class LocalisationCache {
                # Run hooks
                wfRunHooks( 'LocalisationCacheRecache', array( $this, $code, &$allData ) );
 
-               if ( is_null( $allData['defaultUserOptionOverrides'] ) ) {
-                       throw new MWException( __METHOD__.': Localisation data failed sanity check! ' . 
-                               'Check that your languages/messages/MessagesEn.php file is intact.' );
-               }
-
                # Set the preload key
                $allData['preload'] = $this->buildPreload( $allData );
 
@@ -640,7 +629,7 @@ class LocalisationCache {
                        }
                }
                $this->store->finishWrite();
-               
+
                # Clear out the MessageBlobStore
                # HACK: If using a null (i.e. disabled) storage backend, we
                # can't write to the MessageBlobStore either
@@ -674,7 +663,7 @@ class LocalisationCache {
        }
 
        /**
-        * Unload the data for a given language from the object cache. 
+        * Unload the data for a given language from the object cache.
         * Reduces memory usage.
         */
        public function unload( $code ) {
@@ -713,15 +702,15 @@ class LocalisationCache {
  * The persistence layer is two-level hierarchical cache. The first level
  * is the language, the second level is the item or subitem.
  *
- * Since the data for a whole language is rebuilt in one operation, it needs 
- * to have a fast and atomic method for deleting or replacing all of the 
+ * Since the data for a whole language is rebuilt in one operation, it needs
+ * to have a fast and atomic method for deleting or replacing all of the
  * current data for a given language. The interface reflects this bulk update
- * operation. Callers writing to the cache must first call startWrite(), then 
- * will call set() a couple of thousand times, then will call finishWrite() 
- * to commit the operation. When finishWrite() is called, the cache is 
+ * operation. Callers writing to the cache must first call startWrite(), then
+ * will call set() a couple of thousand times, then will call finishWrite()
+ * to commit the operation. When finishWrite() is called, the cache is
  * expected to delete all data previously stored for that language.
  *
- * The values stored are PHP variables suitable for serialize(). Implementations 
+ * The values stored are PHP variables suitable for serialize(). Implementations
  * of LCStore are responsible for serializing and unserializing.
  */
 interface LCStore {
@@ -752,7 +741,7 @@ interface LCStore {
 }
 
 /**
- * LCStore implementation which uses the standard DB functions to store data. 
+ * LCStore implementation which uses the standard DB functions to store data.
  * This will work on any MediaWiki installation.
  */
 class LCStore_DB implements LCStore {
@@ -838,9 +827,9 @@ class LCStore_DB implements LCStore {
  * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
  * will throw an exception.
  *
- * Profiling indicates that on Linux, this implementation outperforms MySQL if 
- * the directory is on a local filesystem and there is ample kernel cache 
- * space. The performance advantage is greater when the DBA extension is 
+ * Profiling indicates that on Linux, this implementation outperforms MySQL if
+ * the directory is on a local filesystem and there is ample kernel cache
+ * space. The performance advantage is greater when the DBA extension is
  * available than it is with the PHP port.
  *
  * See Cdb.php and http://cr.yp.to/cdb.html
@@ -879,8 +868,8 @@ class LCStore_CDB implements LCStore {
 
        public function startWrite( $code ) {
                if ( !file_exists( $this->directory ) ) {
-                       if ( !wfMkdirParents( $this->directory ) ) {
-                               throw new MWException( "Unable to create the localisation store " . 
+                       if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
+                               throw new MWException( "Unable to create the localisation store " .
                                        "directory \"{$this->directory}\"" );
                        }
                }
@@ -929,7 +918,7 @@ class LCStore_Null implements LCStore {
 }
 
 /**
- * A localisation cache optimised for loading large amounts of data for many 
+ * A localisation cache optimised for loading large amounts of data for many
  * languages. Used by rebuildLocalisationCache.php.
  */
 class LocalisationCache_BulkLoad extends LocalisationCache {
@@ -941,7 +930,7 @@ class LocalisationCache_BulkLoad extends LocalisationCache {
 
        /**
         * Most recently used languages. Uses the linked-list aspect of PHP hashtables
-        * to keep the most recently used language codes at the end of the array, and 
+        * to keep the most recently used language codes at the end of the array, and
         * the language codes that are ready to be deleted at the beginning.
         */
        var $mruLangs = array();