Make sure to not unpack an associative array into parameter list
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
index 93959db..7a172be 100644 (file)
@@ -55,7 +55,7 @@ class MessageCache {
        /**
         * @var bool[] Map of (language code => boolean)
         */
-       protected $mCacheVolatile = [];
+       protected $cacheVolatile = [];
 
        /**
         * Should mean that database cannot be used, but check
@@ -239,7 +239,7 @@ class MessageCache {
         * is disabled.
         *
         * @param string $code Language to which load messages
-        * @param int $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
+        * @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache [optional]
         * @throws InvalidArgumentException
         * @return bool
         */
@@ -272,7 +272,7 @@ class MessageCache {
                # 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)
                list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
-               $this->mCacheVolatile[$code] = $hashVolatile;
+               $this->cacheVolatile[$code] = $hashVolatile;
 
                # Try the local cache and check against the cluster hash key...
                $cache = $this->getLocalCache( $code );
@@ -383,7 +383,7 @@ class MessageCache {
        /**
         * @param string $code
         * @param array &$where List of wfDebug() comments
-        * @param int $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
+        * @param int|null $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
         * @return bool|string True on success or one of ("cantacquire", "disabled")
         */
        protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
@@ -445,7 +445,7 @@ class MessageCache {
         * on-demand from the database later.
         *
         * @param string $code Language code
-        * @param int $mode Use MessageCache::FOR_UPDATE to skip process cache
+        * @param int|null $mode Use MessageCache::FOR_UPDATE to skip process cache
         * @return array Loaded messages for storing in caches
         */
        protected function loadFromDB( $code, $mode = null ) {
@@ -592,13 +592,15 @@ class MessageCache {
                                                [ 'title' => $title, 'code' => $code ] );
                                        return;
                                }
-                               // Load the messages from the master DB to avoid race conditions
+                               // Reload messages from the database and pre-populate dc-local caches
+                               // as optimisation. Use the master DB to avoid race conditions.
                                $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
                                // Check if an individual cache key should exist and update cache accordingly
                                $page = WikiPage::factory( Title::makeTitle( NS_MEDIAWIKI, $title ) );
                                $page->loadPageData( $page::READ_LATEST );
                                $text = $this->getMessageTextFromContent( $page->getContent() );
                                if ( is_string( $text ) && strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
+                                       // Match logic of loadCachedMessagePageEntry()
                                        $this->wanCache->set(
                                                $this->bigMessageCacheKey( $cache['HASH'], $title ),
                                                ' ' . $text,
@@ -966,10 +968,13 @@ class MessageCache {
         * @return string|bool The message, or false if it does not exist or on error
         */
        public function getMsgFromNamespace( $title, $code ) {
+               // Load all MediaWiki page definitions into cache. Note that individual keys
+               // already loaded into cache during this request remain in the cache, which
+               // includes the value of hook-defined messages.
                $this->load( $code );
 
-               if ( $this->cache->hasField( $code, $title ) ) {
-                       $entry = $this->cache->getField( $code, $title );
+               $entry = $this->cache->getField( $code, $title );
+               if ( $entry !== null ) {
                        if ( substr( $entry, 0, 1 ) === ' ' ) {
                                // The message exists and is not '!TOO BIG'
                                return (string)substr( $entry, 1 );
@@ -978,6 +983,7 @@ class MessageCache {
                        }
                        // Fall through and try invididual message cache below
                } else {
+                       // Message does not have a MediaWiki page definition
                        $message = false;
                        Hooks::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
                        if ( $message !== false ) {
@@ -989,82 +995,89 @@ class MessageCache {
                        return $message;
                }
 
-               // Individual message cache key
-               $bigKey = $this->bigMessageCacheKey( $this->cache->getField( $code, 'HASH' ), $title );
-
-               if ( $this->mCacheVolatile[$code] ) {
+               if ( $this->cacheVolatile[$code] ) {
                        $entry = false;
                        // Make sure that individual keys respect the WAN cache holdoff period too
                        LoggerFactory::getInstance( 'MessageCache' )->debug(
                                __METHOD__ . ': loading volatile key \'{titleKey}\'',
-                               [ 'titleKey' => $bigKey, 'code' => $code ] );
+                               [ 'titleKey' => $title, 'code' => $code ] );
                } else {
                        // Try the individual message cache
-                       $entry = $this->wanCache->get( $bigKey );
-               }
-
-               if ( $entry !== false ) {
-                       if ( substr( $entry, 0, 1 ) === ' ' ) {
-                               $this->cache->setField( $code, $title, $entry );
-                               // The message exists, so make sure a string is returned
-                               return (string)substr( $entry, 1 );
-                       } elseif ( $entry === '!NONEXISTENT' ) {
-                               $this->cache->setField( $code, $title, '!NONEXISTENT' );
-
-                               return false;
-                       } else {
-                               // Corrupt/obsolete entry, delete it
-                               $this->wanCache->delete( $bigKey );
-                       }
-               }
-
-               // Try loading the message from the database
-               $dbr = wfGetDB( DB_REPLICA );
-               $cacheOpts = Database::getCacheSetOptions( $dbr );
-               // Use newKnownCurrent() to avoid querying revision/user tables
-               $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
-               if ( $titleObj->getLatestRevID() ) {
-                       $revision = Revision::newKnownCurrent(
-                               $dbr,
-                               $titleObj
+                       $entry = $this->loadCachedMessagePageEntry(
+                               $title,
+                               $code,
+                               $this->cache->getField( $code, 'HASH' )
                        );
-               } else {
-                       $revision = false;
                }
 
-               if ( $revision ) {
-                       $content = $revision->getContent();
-                       if ( $content ) {
-                               $message = $this->getMessageTextFromContent( $content );
-                               if ( is_string( $message ) ) {
-                                       $this->cache->setField( $code, $title, ' ' . $message );
-                                       $this->wanCache->set( $bigKey, ' ' . $message, $this->mExpiry, $cacheOpts );
-                               }
-                       } else {
-                               // A possibly temporary loading failure
-                               LoggerFactory::getInstance( 'MessageCache' )->warning(
-                                       __METHOD__ . ': failed to load message page text for \'{titleKey}\'',
-                                       [ 'titleKey' => $bigKey, 'code' => $code ] );
-                               $message = null; // no negative caching
-                       }
-               } else {
-                       $message = false; // negative caching
+               if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) {
+                       $this->cache->setField( $code, $title, $entry );
+                       // The message exists, so make sure a string is returned
+                       return (string)substr( $entry, 1 );
                }
 
-               if ( $message === false ) {
-                       // Negative caching in case a "too big" message is no longer available (deleted)
-                       $this->cache->setField( $code, $title, '!NONEXISTENT' );
-                       $this->wanCache->set( $bigKey, '!NONEXISTENT', $this->mExpiry, $cacheOpts );
-               }
+               $this->cache->setField( $code, $title, '!NONEXISTENT' );
 
-               return $message;
+               return false;
+       }
+
+       /**
+        * @param string $dbKey
+        * @param string $code
+        * @param string $hash
+        * @return string Either " <MESSAGE>" or "!NONEXISTANT"
+        */
+       private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) {
+               return $this->srvCache->getWithSetCallback(
+                       $this->srvCache->makeKey( 'messages-big', $hash, $dbKey ),
+                       IExpiringStore::TTL_MINUTE,
+                       function () use ( $code, $dbKey, $hash ) {
+                               return $this->wanCache->getWithSetCallback(
+                                       $this->bigMessageCacheKey( $hash, $dbKey ),
+                                       $this->mExpiry,
+                                       function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code ) {
+                                               // Try loading the message from the database
+                                               $dbr = wfGetDB( DB_REPLICA );
+                                               $setOpts += Database::getCacheSetOptions( $dbr );
+                                               // Use newKnownCurrent() to avoid querying revision/user tables
+                                               $title = Title::makeTitle( NS_MEDIAWIKI, $dbKey );
+                                               $revision = Revision::newKnownCurrent( $dbr, $title );
+                                               if ( !$revision ) {
+                                                       // The wiki doesn't have a local override page. Cache absence with normal TTL.
+                                                       // When overrides are created, self::replace() takes care of the cache.
+                                                       return '!NONEXISTENT';
+                                               }
+                                               $content = $revision->getContent();
+                                               if ( $content ) {
+                                                       $message = $this->getMessageTextFromContent( $content );
+                                               } else {
+                                                       LoggerFactory::getInstance( 'MessageCache' )->warning(
+                                                               __METHOD__ . ': failed to load page text for \'{titleKey}\'',
+                                                               [ 'titleKey' => $dbKey, 'code' => $code ]
+                                                       );
+                                                       $message = null;
+                                               }
+
+                                               if ( !is_string( $message ) ) {
+                                                       // Revision failed to load Content, or Content is incompatible with wikitext.
+                                                       // Possibly a temporary loading failure.
+                                                       $ttl = 5;
+
+                                                       return '!NONEXISTENT';
+                                               }
+
+                                               return ' ' . $message;
+                                       }
+                               );
+                       }
+               );
        }
 
        /**
         * @param string $message
         * @param bool $interface
-        * @param Language $language
-        * @param Title $title
+        * @param Language|null $language
+        * @param Title|null $title
         * @return string
         */
        public function transform( $message, $interface = false, $language = null, $title = null ) {
@@ -1117,10 +1130,10 @@ class MessageCache {
 
        /**
         * @param string $text
-        * @param Title $title
+        * @param Title|null $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 Language|string $language Language code
+        * @param Language|string|null $language Language code
         * @return ParserOutput|string
         */
        public function parse( $text, $title = null, $linestart = true,