Merge "ResourceLoaderModule: Add @since annotations to new methods"
[lhc/web/wiklou.git] / includes / parser / ParserCache.php
index 040c74f..e374361 100644 (file)
@@ -45,7 +45,7 @@ class ParserCache {
         * Setup a cache pathway with a given back-end storage mechanism.
         * May be a memcached client or a BagOStuff derivative.
         *
-        * @param $memCached Object
+        * @param MWMemcached $memCached
         * @throws MWException
         */
        protected function __construct( $memCached ) {
@@ -56,8 +56,8 @@ class ParserCache {
        }
 
        /**
-        * @param $article Article
-        * @param $hash string
+        * @param Article $article
+        * @param string $hash
         * @return mixed|string
         */
        protected function getParserOutputKey( $article, $hash ) {
@@ -72,7 +72,7 @@ class ParserCache {
        }
 
        /**
-        * @param $article Article
+        * @param Article $article
         * @return mixed|string
         */
        protected function getOptionsKey( $article ) {
@@ -90,8 +90,8 @@ class ParserCache {
         * English preferences. That's why we take into account *all* user
         * options. (r70809 CR)
         *
-        * @param $article Article
-        * @param $popts ParserOptions
+        * @param Article $article
+        * @param ParserOptions $popts
         * @return string
         */
        function getETag( $article, $popts ) {
@@ -102,8 +102,8 @@ class ParserCache {
 
        /**
         * Retrieve the ParserOutput from ParserCache, even if it's outdated.
-        * @param $article Article
-        * @param $popts ParserOptions
+        * @param Article $article
+        * @param ParserOptions $popts
         * @return ParserOutput|bool False on failure
         */
        public function getDirty( $article, $popts ) {
@@ -125,9 +125,9 @@ class ParserCache {
         *
         * @todo Document parameter $useOutdated
         *
-        * @param $article     Article
-        * @param $popts       ParserOptions
-        * @param $useOutdated Boolean (default true)
+        * @param Article $article
+        * @param ParserOptions $popts
+        * @param bool $useOutdated (default true)
         * @return bool|mixed|string
         */
        public function getKey( $article, $popts, $useOutdated = true ) {
@@ -146,6 +146,12 @@ class ParserCache {
                                $cacheTime = $optionsKey->getCacheTime();
                                wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
                                return false;
+                       } elseif ( $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
+                               wfIncrStats( "pcache_miss_revid" );
+                               $revId = $article->getLatest();
+                               $cachedRevId = $optionsKey->getCacheRevisionId();
+                               wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
+                               return false;
                        }
 
                        // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
@@ -165,9 +171,9 @@ class ParserCache {
         * Retrieve the ParserOutput from ParserCache.
         * false if not found or outdated.
         *
-        * @param $article     Article
-        * @param $popts       ParserOptions
-        * @param $useOutdated Boolean (default false)
+        * @param Article $article
+        * @param ParserOptions $popts
+        * @param bool $useOutdated (default false)
         *
         * @return ParserOutput|bool False on failure
         */
@@ -211,6 +217,12 @@ class ParserCache {
                        $cacheTime = $value->getCacheTime();
                        wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
                        $value = false;
+               } elseif ( $value->isDifferentRevision( $article->getLatest() ) ) {
+                       wfIncrStats( "pcache_miss_revid" );
+                       $revId = $article->getLatest();
+                       $cachedRevId = $value->getCacheRevisionId();
+                       wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
+                       $value = false;
                } else {
                        wfIncrStats( "pcache_hit" );
                }
@@ -224,11 +236,16 @@ class ParserCache {
         * @param WikiPage $page
         * @param ParserOptions $popts
         * @param string $cacheTime Time when the cache was generated
+        * @param int $revId Revision ID that was parsed
         */
-       public function save( $parserOutput, $page, $popts, $cacheTime = null ) {
+       public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
                $expire = $parserOutput->getCacheExpiry();
                if ( $expire > 0 ) {
                        $cacheTime = $cacheTime ?: wfTimestampNow();
+                       if ( !$revId ) {
+                               $revision = $page->getRevision();
+                               $revId = $revision ? $revision->getId() : null;
+                       }
 
                        $optionsKey = new CacheTime;
                        $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
@@ -236,6 +253,8 @@ class ParserCache {
 
                        $optionsKey->setCacheTime( $cacheTime );
                        $parserOutput->setCacheTime( $cacheTime );
+                       $optionsKey->setCacheRevisionId( $revId );
+                       $parserOutput->setCacheRevisionId( $revId );
 
                        $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
 
@@ -245,8 +264,13 @@ class ParserCache {
                        // Save the timestamp so that we don't have to load the revision row on view
                        $parserOutput->setTimestamp( $page->getTimestamp() );
 
-                       $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $cacheTime\n -->\n";
-                       wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $cacheTime\n" );
+                       $msg = "Saved in parser cache with key $parserOutputKey" .
+                               " and timestamp $cacheTime" .
+                               " and revision id $revId" .
+                               "\n";
+
+                       $parserOutput->mText .= "\n<!-- $msg -->\n";
+                       wfDebug( $msg );
 
                        // Save the parser output
                        $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );