Documentation and type hinting.
[lhc/web/wiklou.git] / includes / LocalisationCache.php
index fe22079..9d00681 100644 (file)
@@ -77,31 +77,25 @@ class LocalisationCache {
         */
        var $recachedLangs = array();
 
-       /**
-        * Data added by extensions using the deprecated $wgMessageCache->addMessages() 
-        * interface.
-        */
-       var $legacyData = array();
-
        /**
         * All item keys
         */
        static public $allKeys = array(
-               'fallback', 'namespaceNames', 'mathNames', 'bookstoreList',
+               'fallback', 'namespaceNames', 'bookstoreList',
                'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable',
                'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
                'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases',
                'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
                'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
-               'imageFiles', 'preloadedMessages',
+               'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
        );
 
        /**
         * Keys for items which consist of associative arrays, which may be merged
         * by a fallback sequence.
         */
-       static public $mergeableMapKeys = array( 'messages', 'namespaceNames', 'mathNames',
-               'dateFormats', 'defaultUserOptionOverrides', 'magicWords', 'imageFiles',
+       static public $mergeableMapKeys = array( 'messages', 'namespaceNames',
+               'dateFormats', 'defaultUserOptionOverrides', 'imageFiles',
                'preloadedMessages',
        );
 
@@ -123,6 +117,11 @@ class LocalisationCache {
         */
        static public $optionalMergeKeys = array( 'bookstoreList' );
 
+       /**
+        * Keys for items that are formatted like $magicWords
+        */
+       static public $magicWordKeys = array( 'magicWords' );
+
        /**
         * Keys for items where the subitems are stored in the backend separately.
         */
@@ -160,7 +159,7 @@ class LocalisationCache {
                                        break;
                                default:
                                        throw new MWException( 
-                                               'Please set $wgLocalisationConf[\'store\'] to something sensible.' );
+                                               'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' );
                        }
                }
 
@@ -187,7 +186,8 @@ class LocalisationCache {
                                self::$mergeableMapKeys,
                                self::$mergeableListKeys,
                                self::$mergeableAliasListKeys,
-                               self::$optionalMergeKeys
+                               self::$optionalMergeKeys,
+                               self::$magicWordKeys
                        ) );
                }
                return isset( $this->mergeableKeys[$key] );
@@ -215,23 +215,40 @@ class LocalisationCache {
         * Get a subitem, for instance a single message for a given language.
         */
        public function getSubitem( $code, $key, $subkey ) {
-               if ( isset( $this->legacyData[$code][$key][$subkey] ) ) {
-                       return $this->legacyData[$code][$key][$subkey];
+               if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) 
+                       && !isset( $this->loadedItems[$code][$key] ) ) 
+               {
+                       wfProfileIn( __METHOD__.'-load' );
+                       $this->loadSubitem( $code, $key, $subkey );
+                       wfProfileOut( __METHOD__.'-load' );
                }
-               if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) ) {
-                       if ( isset( $this->loadedItems[$code][$key] ) ) {
-                               if ( isset( $this->data[$code][$key][$subkey] ) ) {
-                                       return $this->data[$code][$key][$subkey];
-                               } else {
-                                       return null;
-                               }
+               if ( isset( $this->data[$code][$key][$subkey] ) ) {
+                       return $this->data[$code][$key][$subkey];
+               } else {
+                       return null;
+               }
+       }
+
+       /**
+        * Get the list of subitem keys for a given item.
+        *
+        * 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 
+        * array.
+        */
+       public function getSubitemList( $code, $key ) {
+               if ( in_array( $key, self::$splitKeys ) ) {
+                       return $this->getSubitem( $code, 'list', $key );
+               } else {
+                       $item = $this->getItem( $code, $key );
+                       if ( is_array( $item ) ) {
+                               return array_keys( $item );
                        } else {
-                               wfProfileIn( __METHOD__.'-load' );
-                               $this->loadSubitem( $code, $key, $subkey );
-                               wfProfileOut( __METHOD__.'-load' );
+                               return false;
                        }
                }
-               return $this->data[$code][$key][$subkey];
        }
 
        /**
@@ -275,7 +292,9 @@ class LocalisationCache {
                        $this->initLanguage( $code );
                }
                // Check to see if initLanguage() loaded it for us
-               if ( isset( $this->loadedSubitems[$code][$key][$subkey] ) ) {
+               if ( isset( $this->loadedItems[$code][$key] )
+                       || isset( $this->loadedSubitems[$code][$key][$subkey] ) )
+               {
                        return;
                }
                if ( isset( $this->shallowFallbacks[$code] ) ) {
@@ -302,7 +321,11 @@ class LocalisationCache {
                        return true;
                }
                foreach ( $deps as $dep ) {
-                       if ( $dep->isExpired() ) {
+                       // Because we're unserializing stuff from cache, we
+                       // could receive objects of classes that don't exist
+                       // 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 " . 
                                        get_class( $dep ) . "\n" );
                                return true;
@@ -320,6 +343,12 @@ class LocalisationCache {
                }
                $this->initialisedLangs[$code] = true;
 
+               # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
+               if ( !Language::isValidBuiltInCode( $code ) ) {
+                       $this->initShallowFallback( $code, 'en' );
+                       return;
+               }
+
                # Recache the data if necessary
                if ( !$this->manualRecache && $this->isExpired( $code ) ) {
                        if ( file_exists( Language::getMessagesFileName( $code ) ) ) {
@@ -375,9 +404,9 @@ class LocalisationCache {
         */
        protected function readPHPFile( $_fileName, $_fileType ) {
                // Disable APC caching
-               $_apcEnabled = ini_set( 'apc.enabled', '0' );
+               $_apcEnabled = ini_set( 'apc.cache_by_default', '0' );
                include( $_fileName );
-               ini_set( 'apc.enabled', $_apcEnabled );
+               ini_set( 'apc.cache_by_default', $_apcEnabled );
 
                if ( $_fileType == 'core' || $_fileType == 'extension' ) {
                        $data = compact( self::$allKeys );
@@ -409,6 +438,8 @@ class LocalisationCache {
                                        if ( isset( $value['inherit'] ) ) {
                                                unset( $value['inherit'] );
                                        }
+                               } elseif ( in_array( $key, self::$magicWordKeys ) ) {
+                                       $this->mergeMagicWords( $value, $fallbackValue );
                                }
                        }
                } else {
@@ -416,6 +447,20 @@ class LocalisationCache {
                }
        }
 
+       protected function mergeMagicWords( &$value, $fallbackValue ) {
+               foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
+                       if ( !isset( $value[$magicName] ) ) {
+                               $value[$magicName] = $fallbackInfo;
+                       } else {
+                               $oldSynonyms = array_slice( $fallbackInfo, 1 );
+                               $newSynonyms = array_slice( $value[$magicName], 1 );
+                               $synonyms = array_values( array_unique( array_merge( 
+                                       $newSynonyms, $oldSynonyms ) ) );
+                               $value[$magicName] = array_merge( array( $fallbackInfo[0] ), $synonyms );
+                       }
+               }
+       }
+
        /**
         * Given an array mapping language code to localisation value, such as is
         * found in extension *.i18n.php files, iterate through a fallback sequence
@@ -510,8 +555,9 @@ class LocalisationCache {
                        $data = $this->readPHPFile( $fileName, 'extension' );
                        $used = false;
                        foreach ( $data as $key => $item ) {
-                               $used = $used |
-                                       $this->mergeExtensionItem( $codeSequence, $key, $allData[$key], $item );
+                               if( $this->mergeExtensionItem( $codeSequence, $key, $allData[$key], $item ) ) {
+                                       $used = true;
+                               }
                        }
                        if ( $used ) {
                                $deps[] = new FileDependency( $fileName );
@@ -559,9 +605,6 @@ class LocalisationCache {
                        $allData['defaultUserOptionOverrides'] = array();
                }
 
-               # Set the preload key
-               $allData['preload'] = $this->buildPreload( $allData );
-
                # Set the list keys
                $allData['list'] = array();
                foreach ( self::$splitKeys as $key ) {
@@ -576,6 +619,9 @@ class LocalisationCache {
                                'Check that your languages/messages/MessagesEn.php file is intact.' );
                }
 
+               # Set the preload key
+               $allData['preload'] = $this->buildPreload( $allData );
+
                # Save to the process cache and register the items loaded
                $this->data[$code] = $allData;
                foreach ( $allData as $key => $item ) {
@@ -594,6 +640,13 @@ 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
+               if ( !$this->store instanceof LCStore_Null ) {
+                       MessageBlobStore::clear();
+               }
 
                wfProfileOut( __METHOD__ );
        }
@@ -629,8 +682,6 @@ class LocalisationCache {
                unset( $this->loadedItems[$code] );
                unset( $this->loadedSubitems[$code] );
                unset( $this->initialisedLangs[$code] );
-               // We don't unload legacyData because there's no way to get it back 
-               // again, it's not really a cache
                foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
                        if ( $fbCode === $code ) {
                                $this->unload( $shallowCode );
@@ -647,27 +698,12 @@ class LocalisationCache {
                }
        }
 
-       /**
-        * Add messages to the cache, from an extension that has not yet been 
-        * migrated to $wgExtensionMessages or the LocalisationCacheRecache hook. 
-        * Called by deprecated function $wgMessageCache->addMessages(). 
-        */
-       public function addLegacyMessages( $messages ) {
-               foreach ( $messages as $lang => $langMessages ) {
-                       if ( isset( $this->legacyData[$lang]['messages'] ) ) {
-                               $this->legacyData[$lang]['messages'] = 
-                                       $langMessages + $this->legacyData[$lang]['messages'];
-                       } else {
-                               $this->legacyData[$lang]['messages'] = $langMessages;
-                       }
-               }
-       }
-
        /**
         * Disable the storage backend
         */
        public function disableBackend() {
                $this->store = new LCStore_Null;
+               $this->manualRecache = false;
        }
 }
 
@@ -694,24 +730,24 @@ interface LCStore {
         * @param $code Language code
         * @param $key Cache key
         */
-       public function get( $code, $key );
+       function get( $code, $key );
 
        /**
         * Start a write transaction.
         * @param $code Language code
         */
-       public function startWrite( $code );
+       function startWrite( $code );
 
        /**
         * Finish a write transaction.
         */
-       public function finishWrite();
+       function finishWrite();
 
        /**
         * Set a key to a given value. startWrite() must be called before this
         * is called, and finishWrite() must be called afterwards.
         */
-       public function set( $key, $value );
+       function set( $key, $value );
 
 }
 
@@ -723,6 +759,7 @@ class LCStore_DB implements LCStore {
        var $currentLang;
        var $writesDone = false;
        var $dbw, $batch;
+       var $readOnly = false;
 
        public function get( $code, $key ) {
                if ( $this->writesDone ) {
@@ -740,17 +777,34 @@ class LCStore_DB implements LCStore {
        }
 
        public function startWrite( $code ) {
+               if ( $this->readOnly ) {
+                       return;
+               }
                if ( !$code ) {
                        throw new MWException( __METHOD__.": Invalid language \"$code\"" );
                }
                $this->dbw = wfGetDB( DB_MASTER );
-               $this->dbw->begin();
-               $this->dbw->delete( 'l10n_cache', array( 'lc_lang' => $code ), __METHOD__ );
+               try {
+                       $this->dbw->begin();
+                       $this->dbw->delete( 'l10n_cache', array( 'lc_lang' => $code ), __METHOD__ );
+               } catch ( DBQueryError $e ) {
+                       if ( $this->dbw->wasReadOnlyError() ) {
+                               $this->readOnly = true;
+                               $this->dbw->rollback();
+                               $this->dbw->ignoreErrors( false );
+                               return;
+                       } else {
+                               throw $e;
+                       }
+               }
                $this->currentLang = $code;
                $this->batch = array();
        }
 
        public function finishWrite() {
+               if ( $this->readOnly ) {
+                       return;
+               }
                if ( $this->batch ) {
                        $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
                }
@@ -762,6 +816,9 @@ class LCStore_DB implements LCStore {
        }
 
        public function set( $key, $value ) {
+               if ( $this->readOnly ) {
+                       return;
+               }
                if ( is_null( $this->currentLang ) ) {
                        throw new MWException( __CLASS__.': must call startWrite() before calling set()' );
                }
@@ -839,11 +896,6 @@ class LCStore_CDB implements LCStore {
                // Close the writer
                $this->writer->close();
                $this->writer = null;
-
-               // Close and remove the reader
-               if ( !empty( $this->readers[$this->currentLang] ) ) {
-                       $this->readers[$this->currentLang]->close();
-               }
                unset( $this->readers[$this->currentLang] );
                $this->currentLang = null;
        }