Merge "Make setUp and tearDown protected in tests"
[lhc/web/wiklou.git] / includes / cache / LocalisationCache.php
index c5a153a..f02e0a1 100644 (file)
@@ -36,19 +36,19 @@ define( 'MW_LC_VERSION', 2 );
  */
 class LocalisationCache {
        /** Configuration associative array */
-       var $conf;
+       private $conf;
 
        /**
         * True if recaching should only be done on an explicit call to recache().
         * Setting this reduces the overhead of cache freshness checking, which
         * requires doing a stat() for every extension i18n file.
         */
-       var $manualRecache = false;
+       private $manualRecache = false;
 
        /**
         * True to treat all files as expired until they are regenerated by this object.
         */
-       var $forceRecache = false;
+       private $forceRecache = false;
 
        /**
         * The cache data. 3-d array, where the first key is the language code,
@@ -56,14 +56,14 @@ class LocalisationCache {
         * an item specific subkey index. Some items are not arrays and so for those
         * items, there are no subkeys.
         */
-       var $data = array();
+       protected $data = array();
 
        /**
         * The persistent store object. An instance of LCStore.
         *
         * @var LCStore
         */
-       var $store;
+       private $store;
 
        /**
         * A 2-d associative array, code/key, where presence indicates that the item
@@ -72,32 +72,32 @@ class LocalisationCache {
         * For split items, if set, this indicates that all of the subitems have been
         * loaded.
         */
-       var $loadedItems = array();
+       private $loadedItems = array();
 
        /**
         * A 3-d associative array, code/key/subkey, where presence indicates that
         * the subitem is loaded. Only used for the split items, i.e. messages.
         */
-       var $loadedSubitems = array();
+       private $loadedSubitems = array();
 
        /**
         * An array where presence of a key indicates that that language has been
         * initialised. Initialisation includes checking for cache expiry and doing
         * any necessary updates.
         */
-       var $initialisedLangs = array();
+       private $initialisedLangs = array();
 
        /**
         * An array mapping non-existent pseudo-languages to fallback languages. This
         * is filled by initShallowFallback() when data is requested from a language
         * that lacks a Messages*.php file.
         */
-       var $shallowFallbacks = array();
+       private $shallowFallbacks = array();
 
        /**
         * An array where the keys are codes that have been recached by this instance.
         */
-       var $recachedLangs = array();
+       private $recachedLangs = array();
 
        /**
         * All item keys
@@ -158,7 +158,7 @@ class LocalisationCache {
         * Associative array of cached plural rules. The key is the language code,
         * the value is an array of plural rules for that language.
         */
-       var $pluralRules = null;
+       private $pluralRules = null;
 
        /**
         * Associative array of cached plural rule types. The key is the language
@@ -172,9 +172,9 @@ class LocalisationCache {
         * example, {{plural:count|wordform1|wordform2|wordform3}}, rather than
         * {{plural:count|one=wordform1|two=wordform2|many=wordform3}}.
         */
-       var $pluralRuleTypes = null;
+       private $pluralRuleTypes = null;
 
-       var $mergeableKeys = null;
+       private $mergeableKeys = null;
 
        /**
         * Constructor.
@@ -195,16 +195,16 @@ class LocalisationCache {
                        switch ( $conf['store'] ) {
                                case 'files':
                                case 'file':
-                                       $storeClass = 'LCStore_CDB';
+                                       $storeClass = 'LCStoreCDB';
                                        break;
                                case 'db':
-                                       $storeClass = 'LCStore_DB';
+                                       $storeClass = 'LCStoreDB';
                                        break;
                                case 'accel':
-                                       $storeClass = 'LCStore_Accel';
+                                       $storeClass = 'LCStoreAccel';
                                        break;
                                case 'detect':
-                                       $storeClass = $wgCacheDirectory ? 'LCStore_CDB' : 'LCStore_DB';
+                                       $storeClass = $wgCacheDirectory ? 'LCStoreCDB' : 'LCStoreDB';
                                        break;
                                default:
                                        throw new MWException(
@@ -241,6 +241,7 @@ class LocalisationCache {
                                self::$magicWordKeys
                        ) );
                }
+
                return isset( $this->mergeableKeys[$key] );
        }
 
@@ -276,7 +277,8 @@ class LocalisationCache {
         */
        public function getSubitem( $code, $key, $subkey ) {
                if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) &&
-                        !isset( $this->loadedItems[$code][$key] ) ) {
+                       !isset( $this->loadedItems[$code][$key] )
+               ) {
                        wfProfileIn( __METHOD__ . '-load' );
                        $this->loadSubitem( $code, $key, $subkey );
                        wfProfileOut( __METHOD__ . '-load' );
@@ -331,6 +333,7 @@ class LocalisationCache {
 
                if ( isset( $this->shallowFallbacks[$code] ) ) {
                        $this->loadItem( $this->shallowFallbacks[$code], $key );
+
                        return;
                }
 
@@ -358,6 +361,7 @@ class LocalisationCache {
        protected function loadSubitem( $code, $key, $subkey ) {
                if ( !in_array( $key, self::$splitKeys ) ) {
                        $this->loadItem( $code, $key );
+
                        return;
                }
 
@@ -367,12 +371,14 @@ class LocalisationCache {
 
                // Check to see if initLanguage() loaded it for us
                if ( isset( $this->loadedItems[$code][$key] ) ||
-                        isset( $this->loadedSubitems[$code][$key][$subkey] ) ) {
+                       isset( $this->loadedSubitems[$code][$key][$subkey] )
+               ) {
                        return;
                }
 
                if ( isset( $this->shallowFallbacks[$code] ) ) {
                        $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
+
                        return;
                }
 
@@ -391,15 +397,17 @@ class LocalisationCache {
        public function isExpired( $code ) {
                if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
                        wfDebug( __METHOD__ . "($code): forced reload\n" );
+
                        return true;
                }
 
                $deps = $this->store->get( $code, 'deps' );
                $keys = $this->store->get( $code, 'list' );
                $preload = $this->store->get( $code, 'preload' );
-               // Different keys may expire separately, at least in LCStore_Accel
+               // Different keys may expire separately, at least in LCStoreAccel
                if ( $deps === null || $keys === null || $preload === null ) {
                        wfDebug( __METHOD__ . "($code): cache missing, need to make one\n" );
+
                        return true;
                }
 
@@ -411,6 +419,7 @@ class LocalisationCache {
                        if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
                                wfDebug( __METHOD__ . "($code): cache for $code expired due to " .
                                        get_class( $dep ) . "\n" );
+
                                return true;
                        }
                }
@@ -433,6 +442,7 @@ class LocalisationCache {
                # 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;
                }
 
@@ -445,6 +455,7 @@ class LocalisationCache {
                        } else {
                                $this->initShallowFallback( $code, 'en' );
                        }
+
                        return;
                }
 
@@ -458,6 +469,7 @@ class LocalisationCache {
                                                'Please run maintenance/rebuildLocalisationCache.php.' );
                                }
                                $this->initShallowFallback( $code, 'en' );
+
                                return;
                        } else {
                                throw new MWException( 'Invalid or missing localisation cache.' );
@@ -511,6 +523,7 @@ class LocalisationCache {
                        throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" );
                }
                wfProfileOut( __METHOD__ );
+
                return $data;
        }
 
@@ -527,8 +540,10 @@ class LocalisationCache {
                        $compiledRules = CLDRPluralRuleEvaluator::compile( $rules );
                } catch ( CLDRPluralRuleError $e ) {
                        wfDebugLog( 'l10n', $e->getMessage() . "\n" );
+
                        return array();
                }
+
                return $compiledRules;
        }
 
@@ -616,6 +631,7 @@ class LocalisationCache {
                $fileName = Language::getMessagesFileName( $code );
                if ( !file_exists( $fileName ) ) {
                        wfProfileOut( __METHOD__ );
+
                        return false;
                }
 
@@ -633,6 +649,7 @@ class LocalisationCache {
                $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
 
                wfProfileOut( __METHOD__ );
+
                return $data;
        }
 
@@ -747,7 +764,6 @@ class LocalisationCache {
                        foreach ( $data as $key => $value ) {
                                $this->mergeItem( $key, $coreData[$key], $value );
                        }
-
                }
 
                # Fill in the fallback if it's not there already
@@ -885,7 +901,7 @@ class LocalisationCache {
                # 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 ) {
+               if ( !$this->store instanceof LCStoreNull ) {
                        MessageBlobStore::clear();
                }
 
@@ -950,7 +966,7 @@ class LocalisationCache {
         * Disable the storage backend
         */
        public function disableBackend() {
-               $this->store = new LCStore_Null;
+               $this->store = new LCStoreNull;
                $this->manualRecache = false;
        }
 }
@@ -1005,9 +1021,9 @@ interface LCStore {
  * This will work if one of XCache, WinCache or APC cacher is configured.
  * (See ObjectCache.php)
  */
-class LCStore_Accel implements LCStore {
-       var $currentLang;
-       var $keys;
+class LCStoreAccel implements LCStore {
+       private $currentLang;
+       private $keys;
 
        public function __construct() {
                $this->cache = wfGetCache( CACHE_ACCEL );
@@ -1016,6 +1032,7 @@ class LCStore_Accel implements LCStore {
        public function get( $code, $key ) {
                $k = wfMemcKey( 'l10n', $code, 'k', $key );
                $r = $this->cache->get( $k );
+
                return $r === false ? null : $r;
        }
 
@@ -1053,16 +1070,16 @@ class LCStore_Accel implements LCStore {
  * LCStore implementation which uses the standard DB functions to store data.
  * This will work on any MediaWiki installation.
  */
-class LCStore_DB implements LCStore {
-       var $currentLang;
-       var $writesDone = false;
+class LCStoreDB implements LCStore {
+       private $currentLang;
+       private $writesDone = false;
 
        /**
         * @var DatabaseBase
         */
-       var $dbw;
-       var $batch;
-       var $readOnly = false;
+       private $dbw;
+       private $batch;
+       private $readOnly = false;
 
        public function get( $code, $key ) {
                if ( $this->writesDone ) {
@@ -1096,6 +1113,7 @@ class LCStore_DB implements LCStore {
                        if ( $this->dbw->wasReadOnlyError() ) {
                                $this->readOnly = true;
                                $this->dbw->rollback( __METHOD__ );
+
                                return;
                        } else {
                                throw $e;
@@ -1155,8 +1173,11 @@ class LCStore_DB implements LCStore {
  *
  * See Cdb.php and http://cr.yp.to/cdb.html
  */
-class LCStore_CDB implements LCStore {
-       var $readers, $writer, $currentLang, $directory;
+class LCStoreCDB implements LCStore {
+       private $readers;
+       private $writer;
+       private $currentLang;
+       private $directory;
 
        function __construct( $conf = array() ) {
                global $wgCacheDirectory;
@@ -1172,21 +1193,30 @@ class LCStore_CDB implements LCStore {
                if ( !isset( $this->readers[$code] ) ) {
                        $fileName = $this->getFileName( $code );
 
-                       if ( !file_exists( $fileName ) ) {
-                               $this->readers[$code] = false;
-                       } else {
-                               $this->readers[$code] = CdbReader::open( $fileName );
+                       $this->readers[$code] = false;
+                       if ( file_exists( $fileName ) ) {
+                               try {
+                                       $this->readers[$code] = CdbReader::open( $fileName );
+                               } catch( CdbException $e ) {
+                                       wfDebug( __METHOD__ . ": unable to open cdb file for reading" );
+                               }
                        }
                }
 
                if ( !$this->readers[$code] ) {
                        return null;
                } else {
-                       $value = $this->readers[$code]->get( $key );
-
+                       $value = false;
+                       try {
+                               $value = $this->readers[$code]->get( $key );
+                       } catch ( CdbException $e ) {
+                               wfDebug( __METHOD__ . ": CdbException caught, error message was "
+                                       . $e->getMessage() );
+                       }
                        if ( $value === false ) {
                                return null;
                        }
+
                        return unserialize( $value );
                }
        }
@@ -1204,13 +1234,21 @@ class LCStore_CDB implements LCStore {
                        $this->readers[$code]->close();
                }
 
-               $this->writer = CdbWriter::open( $this->getFileName( $code ) );
+               try {
+                       $this->writer = CdbWriter::open( $this->getFileName( $code ) );
+               } catch ( CdbException $e ) {
+                       throw new MWException( $e->getMessage() );
+               }
                $this->currentLang = $code;
        }
 
        public function finishWrite() {
                // Close the writer
-               $this->writer->close();
+               try {
+                       $this->writer->close();
+               } catch ( CdbException $e ) {
+                       throw new MWException( $e->getMessage() );
+               }
                $this->writer = null;
                unset( $this->readers[$this->currentLang] );
                $this->currentLang = null;
@@ -1220,13 +1258,18 @@ class LCStore_CDB implements LCStore {
                if ( is_null( $this->writer ) ) {
                        throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
                }
-               $this->writer->set( $key, serialize( $value ) );
+               try {
+                       $this->writer->set( $key, serialize( $value ) );
+               } catch ( CdbException $e ) {
+                       throw new MWException( $e->getMessage() );
+               }
        }
 
        protected function getFileName( $code ) {
                if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
                        throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
                }
+
                return "{$this->directory}/l10n_cache-$code.cdb";
        }
 }
@@ -1234,38 +1277,43 @@ class LCStore_CDB implements LCStore {
 /**
  * Null store backend, used to avoid DB errors during install
  */
-class LCStore_Null implements LCStore {
+class LCStoreNull implements LCStore {
        public function get( $code, $key ) {
                return null;
        }
 
-       public function startWrite( $code ) {}
-       public function finishWrite() {}
-       public function set( $key, $value ) {}
+       public function startWrite( $code ) {
+       }
+
+       public function finishWrite() {
+       }
+
+       public function set( $key, $value ) {
+       }
 }
 
 /**
  * A localisation cache optimised for loading large amounts of data for many
  * languages. Used by rebuildLocalisationCache.php.
  */
-class LocalisationCache_BulkLoad extends LocalisationCache {
+class LocalisationCacheBulkLoad extends LocalisationCache {
        /**
         * A cache of the contents of data files.
         * Core files are serialized to avoid using ~1GB of RAM during a recache.
         */
-       var $fileCache = array();
+       private $fileCache = array();
 
        /**
         * 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
         * the language codes that are ready to be deleted at the beginning.
         */
-       var $mruLangs = array();
+       private $mruLangs = array();
 
        /**
         * Maximum number of languages that may be loaded into $this->data
         */
-       var $maxLoadedLangs = 10;
+       private $maxLoadedLangs = 10;
 
        /**
         * @param $fileName
@@ -1301,6 +1349,7 @@ class LocalisationCache_BulkLoad extends LocalisationCache {
        public function getItem( $code, $key ) {
                unset( $this->mruLangs[$code] );
                $this->mruLangs[$code] = true;
+
                return parent::getItem( $code, $key );
        }
 
@@ -1313,6 +1362,7 @@ class LocalisationCache_BulkLoad extends LocalisationCache {
        public function getSubitem( $code, $key, $subkey ) {
                unset( $this->mruLangs[$code] );
                $this->mruLangs[$code] = true;
+
                return parent::getSubitem( $code, $key, $subkey );
        }