Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
index 09e9077..b26dc8d 100644 (file)
@@ -72,7 +72,7 @@ class MessageCache {
         * Variable for tracking which variables are already loaded
         * @var array $mLoadedLanguages
         */
-       protected $mLoadedLanguages = array();
+       protected $mLoadedLanguages = [];
 
        /**
         * @var bool $mInParser
@@ -98,7 +98,7 @@ class MessageCache {
         * @return MessageCache
         */
        public static function singleton() {
-               if ( is_null( self::$instance ) ) {
+               if ( self::$instance === null ) {
                        global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
                        self::$instance = new self(
                                wfGetMessageCacheStorage(),
@@ -154,7 +154,7 @@ class MessageCache {
                $this->mExpiry = $expiry;
 
                if ( $wgUseLocalMessageCache ) {
-                       $this->localCache = ObjectCache::newAccelerator( CACHE_NONE );
+                       $this->localCache = ObjectCache::getLocalServerInstance( CACHE_NONE );
                } else {
                        $this->localCache = wfGetCache( CACHE_NONE );
                }
@@ -168,7 +168,18 @@ class MessageCache {
         * @return ParserOptions
         */
        function getParserOptions() {
+               global $wgUser;
+
                if ( !$this->mParserOptions ) {
+                       if ( !$wgUser->isSafeToLoad() ) {
+                               // $wgUser isn't unstubbable yet, so don't try to get a
+                               // ParserOptions for it. And don't cache this ParserOptions
+                               // either.
+                               $po = ParserOptions::newFromAnon();
+                               $po->setEditSection( false );
+                               return $po;
+                       }
+
                        $this->mParserOptions = new ParserOptions;
                        $this->mParserOptions->setEditSection( false );
                }
@@ -247,7 +258,7 @@ class MessageCache {
                # Loading code starts
                $success = false; # Keep track of success
                $staleCache = false; # a cache array with expired data, or false if none has been loaded
-               $where = array(); # Debug info, delayed to avoid spamming debug log too much
+               $where = []; # Debug info, delayed to avoid spamming debug log too much
 
                # Hash of the contents is stored in memcache, to detect if data-center cache
                # or local cache goes out of date (e.g. due to replace() on some other server)
@@ -432,15 +443,15 @@ class MessageCache {
 
                $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_SLAVE );
 
-               $cache = array();
+               $cache = [];
 
                # Common conditions
-               $conds = array(
+               $conds = [
                        'page_is_redirect' => 0,
                        'page_namespace' => NS_MEDIAWIKI,
-               );
+               ];
 
-               $mostused = array();
+               $mostused = [];
                if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
                        if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
                                $this->load( $wgLanguageCode );
@@ -478,8 +489,8 @@ class MessageCache {
                $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
 
                $res = $dbr->select(
-                       array( 'page', 'revision', 'text' ),
-                       array( 'page_title', 'old_text', 'old_flags' ),
+                       [ 'page', 'revision', 'text' ],
+                       [ 'page_title', 'old_text', 'old_flags' ],
                        $smallConds,
                        __METHOD__ . "($code)-small"
                );
@@ -565,7 +576,7 @@ class MessageCache {
                $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
 
                // Also delete cached sidebar... just in case it is affected
-               $codes = array( $code );
+               $codes = [ $code ];
                if ( $code === 'en' ) {
                        // Delete all sidebars, like for example on action=purge on the
                        // sidebar messages
@@ -574,14 +585,15 @@ class MessageCache {
 
                foreach ( $codes as $code ) {
                        $sidebarKey = wfMemcKey( 'sidebar', $code );
-                       $this->wanCache->delete( $sidebarKey, 5 );
+                       $this->wanCache->delete( $sidebarKey );
                }
 
                // Update the message in the message blob store
-               $blobStore = new MessageBlobStore();
+               $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
+               $blobStore = $resourceloader->getMessageBlobStore();
                $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
 
-               Hooks::run( 'MessageCacheReplace', array( $title, $text ) );
+               Hooks::run( 'MessageCacheReplace', [ $title, $text ] );
        }
 
        /**
@@ -638,7 +650,7 @@ class MessageCache {
                $value = $this->wanCache->get(
                        wfMemcKey( 'messages', $code, 'hash', 'v1' ),
                        $curTTL,
-                       array( wfMemcKey( 'messages', $code ) )
+                       [ wfMemcKey( 'messages', $code ) ]
                );
 
                if ( !$value ) {
@@ -656,7 +668,7 @@ class MessageCache {
                        }
                }
 
-               return array( $hash, $expired );
+               return [ $hash, $expired ];
        }
 
        /**
@@ -671,10 +683,10 @@ class MessageCache {
        protected function setValidationHash( $code, array $cache ) {
                $this->wanCache->set(
                        wfMemcKey( 'messages', $code, 'hash', 'v1' ),
-                       array(
+                       [
                                'hash' => $cache['HASH'],
                                'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
-                       ),
+                       ],
                        WANObjectCache::TTL_INDEFINITE
                );
        }
@@ -723,8 +735,6 @@ class MessageCache {
         *   message (which can be empty)
         */
        function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
-               global $wgContLang;
-
                if ( is_int( $key ) ) {
                        // Fix numerical strings that somehow become ints
                        // on their way here
@@ -746,20 +756,13 @@ class MessageCache {
                // Normalise title-case input (with some inlining)
                $lckey = MessageCache::normalizeKey( $key );
 
-               Hooks::run( 'MessageCache::get', array( &$lckey ) );
-
-               if ( ord( $lckey ) < 128 ) {
-                       $uckey = ucfirst( $lckey );
-               } else {
-                       $uckey = $wgContLang->ucfirst( $lckey );
-               }
+               Hooks::run( 'MessageCache::get', [ &$lckey ] );
 
                // Loop through each language in the fallback list until we find something useful
                $lang = wfGetLangObj( $langcode );
                $message = $this->getMessageFromFallbackChain(
                        $lang,
                        $lckey,
-                       $uckey,
                        !$this->mDisable && $useDB
                );
 
@@ -781,18 +784,20 @@ class MessageCache {
                if ( $message !== false ) {
                        // Fix whitespace
                        $message = str_replace(
-                               array(
+                               [
                                        # Fix for trailing whitespace, removed by textarea
                                        '&#32;',
                                        # Fix for NBSP, converted to space by firefox
                                        '&nbsp;',
                                        '&#160;',
-                               ),
-                               array(
+                                       '&shy;'
+                               ],
+                               [
                                        ' ',
                                        "\xc2\xa0",
-                                       "\xc2\xa0"
-                               ),
+                                       "\xc2\xa0",
+                                       "\xc2\xad"
+                               ],
                                $message
                        );
                }
@@ -801,21 +806,21 @@ class MessageCache {
        }
 
        /**
-        * Given a language, try and fetch a message from that language, then the
-        * fallbacks of that language, then the site language, then the fallbacks for the
-        * site language.
+        * Given a language, try and fetch messages from that language.
         *
-        * @param Language $lang Requested language
-        * @param string $lckey Lowercase key for the message
-        * @param string $uckey Uppercase key for the message
-        * @param bool $useDB Whether to use the database
+        * Will also consider fallbacks of that language, the site language, and fallbacks for
+        * the site language.
         *
         * @see MessageCache::get
+        * @param Language|StubObject $lang Preferred language
+        * @param string $lckey Lowercase key for the message (as for localisation cache)
+        * @param bool $useDB Whether to include messages from the wiki database
         * @return string|bool The message, or false if not found
         */
-       protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
+       protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
                global $wgLanguageCode, $wgContLang;
 
+               $uckey = $wgContLang->ucfirst( $lckey );
                $langcode = $lang->getCode();
                $message = false;
 
@@ -903,7 +908,7 @@ class MessageCache {
         * @param string $code Code denoting the language to try.
         * @return string|bool The message, or false if it does not exist or on error
         */
-       function getMsgFromNamespace( $title, $code ) {
+       public function getMsgFromNamespace( $title, $code ) {
                $this->load( $code );
                if ( isset( $this->mCache[$code][$title] ) ) {
                        $entry = $this->mCache[$code][$title];
@@ -919,7 +924,7 @@ class MessageCache {
                } else {
                        // XXX: This is not cached in process cache, should it?
                        $message = false;
-                       Hooks::run( 'MessagesPreLoad', array( $title, &$message ) );
+                       Hooks::run( 'MessagesPreLoad', [ $title, &$message ] );
                        if ( $message !== false ) {
                                return $message;
                        }
@@ -1049,7 +1054,7 @@ class MessageCache {
         * @param Title $title
         * @param bool $linestart Whether or not this is at the start of a line
         * @param bool $interface Whether this is an interface message
-        * @param string $language Language code
+        * @param Language|string $language Language code
         * @return ParserOutput|string
         */
        public function parse( $text, $title = null, $linestart = true,
@@ -1062,6 +1067,10 @@ class MessageCache {
                $parser = $this->getParser();
                $popts = $this->getParserOptions();
                $popts->setInterfaceMessage( $interface );
+
+               if ( is_string( $language ) ) {
+                       $language = Language::factory( $language );
+               }
                $popts->setTargetLanguage( $language );
 
                if ( !$title || !$title instanceof Title ) {
@@ -1102,7 +1111,7 @@ class MessageCache {
                        $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
                }
 
-               $this->mLoadedLanguages = array();
+               $this->mLoadedLanguages = [];
        }
 
        /**
@@ -1114,17 +1123,17 @@ class MessageCache {
 
                $pieces = explode( '/', $key );
                if ( count( $pieces ) < 2 ) {
-                       return array( $key, $wgLanguageCode );
+                       return [ $key, $wgLanguageCode ];
                }
 
                $lang = array_pop( $pieces );
                if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
-                       return array( $key, $wgLanguageCode );
+                       return [ $key, $wgLanguageCode ];
                }
 
                $message = implode( '/', $pieces );
 
-               return array( $message, $lang );
+               return [ $message, $lang ];
        }
 
        /**
@@ -1147,9 +1156,9 @@ class MessageCache {
                unset( $cache['VERSION'] );
                unset( $cache['EXPIRY'] );
                // Remove any !NONEXISTENT keys
-               $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
+               $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
 
                // Keys may appear with a capital first letter. lcfirst them.
-               return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );
+               return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
        }
 }