Merge "(bug 37755) Set robot meta tags for 'view source' pages"
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
index b854a2e..9bf7436 100644 (file)
@@ -59,26 +59,6 @@ class MessageCache {
        /// Variable for tracking which variables are already loaded
        protected $mLoadedLanguages = array();
 
-       /**
-        * Used for automatic detection of most used messages.
-        */
-       protected $mRequestedMessages = array();
-
-       /**
-        * How long the message request counts are stored. Longer period gives
-        * better sample, but also takes longer to adapt changes. The counts
-        * are aggregrated per day, regardless of the value of this variable.
-        */
-       protected static $mAdaptiveDataAge = 604800; // Is 7*24*3600
-
-       /**
-        * Filter the tail of less used messages that are requested more seldom
-        * than this factor times the number of request of most requested message.
-        * These messages are not loaded in the default set, but are still cached
-        * individually on demand with the normal cache expiry time.
-        */
-       protected static $mAdaptiveInclusionThreshold = 0.05;
-
        /**
         * Singleton instance
         *
@@ -404,19 +384,20 @@ class MessageCache {
                );
 
                $mostused = array();
-               if ( $wgAdaptiveMessageCache ) {
-                       $mostused = $this->getMostUsedMessages();
-                       if ( $code !== $wgLanguageCode ) {
-                               foreach ( $mostused as $key => $value ) {
-                                       $mostused[$key] = "$value/$code";
-                               }
+               if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
+                       if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
+                               $this->load( $wgLanguageCode );
+                       }
+                       $mostused = array_keys( $this->mCache[$wgLanguageCode] );
+                       foreach ( $mostused as $key => $value ) {
+                               $mostused[$key] = "$value/$code";
                        }
                }
 
                if ( count( $mostused ) ) {
                        $conds['page_title'] = $mostused;
                } elseif ( $code !== $wgLanguageCode ) {
-                       $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
+                       $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
                } else {
                        # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
                        # other than language code.
@@ -459,12 +440,6 @@ class MessageCache {
                        $cache[$row->page_title] = $entry;
                }
 
-               foreach ( $mostused as $key ) {
-                       if ( !isset( $cache[$key] ) ) {
-                               $cache[$key] = '!NONEXISTENT';
-                       }
-               }
-
                $cache['VERSION'] = MSG_CACHE_VERSION;
                wfProfileOut( __METHOD__ );
                return $cache;
@@ -596,7 +571,7 @@ class MessageCache {
         * @param $key String: the message cache key
         * @param $useDB Boolean: get the message from the DB, false to use only
         *               the localisation
-        * @param $langcode String: code of the language to get the message for, if
+        * @param bool|string $langcode Code of the language to get the message for, if
         *                  it is a valid code create a language for that language,
         *                  if it is a string but not a valid code then make a basic
         *                  language object, if it is a false boolean then use the
@@ -607,6 +582,7 @@ class MessageCache {
         * @param $isFullKey Boolean: specifies whether $key is a two part key
         *                   "msg/lang".
         *
+        * @throws MWException
         * @return string|bool
         */
        function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
@@ -645,13 +621,6 @@ class MessageCache {
                        $uckey = $wgContLang->ucfirst( $lckey );
                }
 
-               /**
-                * Record each message request, but only once per request.
-                * This information is not used unless $wgAdaptiveMessageCache
-                * is enabled.
-                */
-               $this->mRequestedMessages[$uckey] = true;
-
                # Try the MediaWiki namespace
                if( !$this->mDisable && $useDB ) {
                        $title = $uckey;
@@ -718,8 +687,6 @@ class MessageCache {
         * @return string|bool False on failure
         */
        function getMsgFromNamespace( $title, $code ) {
-               global $wgAdaptiveMessageCache;
-
                $this->load( $code );
                if ( isset( $this->mCache[$code][$title] ) ) {
                        $entry = $this->mCache[$code][$title];
@@ -738,15 +705,7 @@ class MessageCache {
                                return $message;
                        }
 
-                       /**
-                        * If message cache is in normal mode, it is guaranteed
-                        * (except bugs) that there is always entry (or placeholder)
-                        * in the cache if message exists. Thus we can do minor
-                        * performance improvement and return false early.
-                        */
-                       if ( !$wgAdaptiveMessageCache ) {
-                               return false;
-                       }
+                       return false;
                }
 
                # Try the individual message cache
@@ -770,16 +729,32 @@ class MessageCache {
                        Title::makeTitle( NS_MEDIAWIKI, $title ), false, Revision::READ_LATEST
                );
                if ( $revision ) {
-                       $message = $revision->getText();
-                       if ($message === false) {
+                       $content = $revision->getContent();
+                       if ( !$content ) {
                                // A possibly temporary loading failure.
                                wfDebugLog( 'MessageCache', __METHOD__ . ": failed to load message page text for {$title} ($code)" );
+                               $message = null; // no negative caching
                        } else {
-                               $this->mCache[$code][$title] = ' ' . $message;
-                               $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
+                               // XXX: Is this the right way to turn a Content object into a message?
+                               // NOTE: $content is typically either WikitextContent, JavaScriptContent or CssContent.
+                               //       MessageContent is *not* used for storing messages, it's only used for wrapping them when needed.
+                               $message = $content->getWikitextForTransclusion();
+
+                               if ( $message === false || $message === null ) {
+                                       wfDebugLog( 'MessageCache', __METHOD__ . ": message content doesn't provide wikitext "
+                                                               . "(content model: " . $content->getContentHandler() . ")" );
+
+                                       $message = false; // negative caching
+                               } else {
+                                       $this->mCache[$code][$title] = ' ' . $message;
+                                       $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
+                               }
                        }
                } else {
-                       $message = false;
+                       $message = false; // negative caching
+               }
+
+               if ( $message === false ) { // negative caching
                        $this->mCache[$code][$title] = '!NONEXISTENT';
                        $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
                }
@@ -845,7 +820,7 @@ class MessageCache {
         * @param $linestart bool
         * @param $interface bool
         * @param $language
-        * @return ParserOutput
+        * @return ParserOutput|string
         */
        public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null  ) {
                if ( $this->mInParser ) {
@@ -890,7 +865,7 @@ class MessageCache {
         */
        function clear() {
                $langs = Language::fetchLanguageNames( null, 'mw' );
-               foreach ( array_keys($langs) as $code ) {
+               foreach ( array_keys( $langs ) as $code ) {
                        # Global cache
                        $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
                        # Invalidate all local caches
@@ -919,82 +894,6 @@ class MessageCache {
                return array( $message, $lang );
        }
 
-       public static function logMessages() {
-               wfProfileIn( __METHOD__ );
-               global $wgAdaptiveMessageCache;
-               if ( !$wgAdaptiveMessageCache || !self::$instance instanceof MessageCache ) {
-                       wfProfileOut( __METHOD__ );
-                       return;
-               }
-
-               $cachekey = wfMemckey( 'message-profiling' );
-               $cache = wfGetCache( CACHE_DB );
-               $data = $cache->get( $cachekey );
-
-               if ( !$data ) {
-                       $data = array();
-               }
-
-               $age = self::$mAdaptiveDataAge;
-               $filterDate = substr( wfTimestamp( TS_MW, time() - $age ), 0, 8 );
-               foreach ( array_keys( $data ) as $key ) {
-                       if ( $key < $filterDate ) {
-                               unset( $data[$key] );
-                       }
-               }
-
-               $index = substr( wfTimestampNow(), 0, 8 );
-               if ( !isset( $data[$index] ) ) {
-                       $data[$index] = array();
-               }
-
-               foreach ( self::$instance->mRequestedMessages as $message => $_ ) {
-                       if ( !isset( $data[$index][$message] ) ) {
-                               $data[$index][$message] = 0;
-                       }
-                       $data[$index][$message]++;
-               }
-
-               $cache->set( $cachekey, $data );
-               wfProfileOut( __METHOD__ );
-       }
-
-       /**
-        * @return array
-        */
-       public function getMostUsedMessages() {
-               wfProfileIn( __METHOD__ );
-               $cachekey = wfMemcKey( 'message-profiling' );
-               $cache = wfGetCache( CACHE_DB );
-               $data = $cache->get( $cachekey );
-               if ( !$data ) {
-                       wfProfileOut( __METHOD__ );
-                       return array();
-               }
-
-               $list = array();
-
-               foreach( $data as $messages ) {
-                       foreach( $messages as $message => $count ) {
-                               $key = $message;
-                               if ( !isset( $list[$key] ) ) {
-                                       $list[$key] = 0;
-                               }
-                               $list[$key] += $count;
-                       }
-               }
-
-               $max = max( $list );
-               foreach ( $list as $message => $count ) {
-                       if ( $count < intval( $max * self::$mAdaptiveInclusionThreshold ) ) {
-                               unset( $list[$message] );
-                       }
-               }
-
-               wfProfileOut( __METHOD__ );
-               return array_keys( $list );
-       }
-
        /**
         * Get all message keys stored in the message cache for a given language.
         * If $code is the content language code, this will return all message keys