Fix typos in MessageCache
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
index b669fcd..b0716b1 100644 (file)
@@ -48,7 +48,7 @@ class MessageCache {
        /**
         * Process cache of loaded messages that are defined in MediaWiki namespace
         *
-        * @var MapCacheLRU Map of (language code => key => " <MESSAGE>" or "!TOO BIG")
+        * @var MapCacheLRU Map of (language code => key => " <MESSAGE>" or "!TOO BIG" or "!ERROR")
         */
        protected $cache;
 
@@ -217,7 +217,7 @@ class MessageCache {
        /**
         * Try to load the cache from APC.
         *
-        * @param string $code Optional language code, see documenation of load().
+        * @param string $code Optional language code, see documentation of load().
         * @return array|bool The cache array, or false if not in cache.
         */
        protected function getLocalCache( $code ) {
@@ -342,7 +342,7 @@ class MessageCache {
                                        } elseif ( $hashVolatile ) {
                                                # DB results are replica DB lag prone until the holdoff TTL passes.
                                                # By then, updates should be reflected in loadFromDBWithLock().
-                                               # One thread renerates the cache while others use old values.
+                                               # One thread regenerates the cache while others use old values.
                                                $where[] = 'global cache is expired/volatile';
                                                $staleCache = $cache;
                                        } else {
@@ -379,7 +379,7 @@ class MessageCache {
                                        break;
                                } elseif ( $loadStatus === 'cantacquire' ) {
                                        # Wait for the other thread to finish, then retry. Normally,
-                                       # the memcached get() will then yeild the other thread's result.
+                                       # the memcached get() will then yield the other thread's result.
                                        $where[] = 'waited for other thread to complete';
                                        $this->getReentrantScopedLock( $cacheKey );
                                } else {
@@ -535,27 +535,33 @@ class MessageCache {
                }
 
                // Set the text for small software-defined messages in the main cache map
+               $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
+               $revQuery = $revisionStore->getQueryInfo( [ 'page', 'user' ] );
                $res = $dbr->select(
-                       [ 'page', 'revision', 'text' ],
-                       [ 'page_title', 'page_latest', 'old_id', 'old_text', 'old_flags' ],
-                       array_merge( $conds, [ 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ] ),
+                       $revQuery['tables'],
+                       $revQuery['fields'],
+                       array_merge( $conds, [
+                               'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ),
+                               'page_latest = rev_id' // get the latest revision only
+                       ] ),
                        __METHOD__ . "($code)-small",
                        [],
-                       [
-                               'revision' => [ 'JOIN', 'page_latest=rev_id' ],
-                               'text' => [ 'JOIN', 'rev_text_id=old_id' ],
-                       ]
+                       $revQuery['joins']
                );
                foreach ( $res as $row ) {
                        $name = $this->contLang->lcfirst( $row->page_title );
                        // Include entries/stubs for all keys in $mostused in adaptive mode
                        if ( $wgAdaptiveMessageCache || $this->isMainCacheable( $name, $overridable ) ) {
-                               $text = Revision::getRevisionText( $row );
-                               if ( $text === false ) {
-                                       // Failed to fetch data; possible ES errors?
-                                       // Store a marker to fetch on-demand as a workaround...
-                                       // TODO Use a differnt marker
-                                       $entry = '!TOO BIG';
+                               try {
+                                       $rev = $revisionStore->newRevisionFromRow( $row );
+                                       $content = $rev->getContent( MediaWiki\Revision\SlotRecord::MAIN );
+                                       $text = $this->getMessageTextFromContent( $content );
+                               } catch ( Exception $ex ) {
+                                       $text = false;
+                               }
+
+                               if ( !is_string( $text ) ) {
+                                       $entry = '!ERROR';
                                        wfDebugLog(
                                                'MessageCache',
                                                __METHOD__
@@ -712,8 +718,7 @@ class MessageCache {
                $this->wanCache->touchCheckKey( $this->getCheckKey( $code ) );
 
                // Purge the messages in the message blob store and fire any hook handlers
-               $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
-               $blobStore = $resourceloader->getMessageBlobStore();
+               $blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore();
                foreach ( $replacements as list( $title, $msg ) ) {
                        $blobStore->updateMessage( $this->contLang->lcfirst( $msg ) );
                        Hooks::run( 'MessageCacheReplace', [ $title, $newTextByTitle[$title] ] );
@@ -1049,7 +1054,7 @@ class MessageCache {
                if ( $entry !== null ) {
                        // Message page exists as an override of a software messages
                        if ( substr( $entry, 0, 1 ) === ' ' ) {
-                               // The message exists and is not '!TOO BIG'
+                               // The message exists and is not '!TOO BIG' or '!ERROR'
                                return (string)substr( $entry, 1 );
                        } elseif ( $entry === '!NONEXISTENT' ) {
                                // The text might be '-' or missing due to some data loss
@@ -1197,18 +1202,18 @@ class MessageCache {
         * @return Parser
         */
        public function getParser() {
-               global $wgParser, $wgParserConf;
-
-               if ( !$this->mParser && isset( $wgParser ) ) {
+               global $wgParserConf;
+               if ( !$this->mParser ) {
+                       $parser = MediaWikiServices::getInstance()->getParser();
                        # Do some initialisation so that we don't have to do it twice
-                       $wgParser->firstCallInit();
+                       $parser->firstCallInit();
                        # Clone it and store it
                        $class = $wgParserConf['class'];
                        if ( $class == ParserDiffTest::class ) {
                                # Uncloneable
                                $this->mParser = new $class( $wgParserConf );
                        } else {
-                               $this->mParser = clone $wgParser;
+                               $this->mParser = clone $parser;
                        }
                }