Merge "Implement mediawiki.cookie module"
[lhc/web/wiklou.git] / includes / parser / CacheTime.php
index e1b3f28..91f404a 100644 (file)
  * @ingroup Parser
  */
 class CacheTime {
-       /** @var  array|bool ParserOptions which have been taken into account to
+       /** @var array|bool ParserOptions which have been taken into account to
         * produce output or false if not available.
         */
        public $mUsedOptions;
 
-       var     $mVersion = Parser::VERSION,  # Compatibility check
-               $mCacheTime = '',             # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
-               $mCacheExpiry = null,         # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
-               $mContainsOldMagic;           # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
+       /** @var string Compatibility check */
+       protected $mVersion = Parser::VERSION;
+
+       /** @var string Time when this object was generated, or -1 for uncacheable. Used in ParserCache. */
+       protected $mCacheTime = '';
+
+       /**
+        * @var int Seconds after which the object should expire, use 0 for uncachable.
+        *   Used in ParserCache.
+        */
+       protected $mCacheExpiry = null;
+
+       /** @var bool Boolean variable indicating if the input contained variables like {{CURRENTDAY}} */
+       protected $mContainsOldMagic;
+
+       /** @var int Revision ID that was parsed */
+       protected $mCacheRevisionId = null;
 
        /**
         * @return string TS_MW timestamp
@@ -52,7 +65,7 @@ class CacheTime {
        }
 
        /**
-        * @param $com bool
+        * @param bool $com
         * @return bool
         */
        function setContainsOldMagic( $com ) {
@@ -62,13 +75,29 @@ class CacheTime {
        /**
         * setCacheTime() sets the timestamp expressing when the page has been rendered.
         * This does not control expiry, see updateCacheExpiry() for that!
-        * @param $t string
+        * @param string $t
         * @return string
         */
        function setCacheTime( $t ) {
                return wfSetVar( $this->mCacheTime, $t );
        }
 
+       /**
+        * @since 1.23
+        * @return int|null Revision id, if any was set
+        */
+       function getCacheRevisionId() {
+               return $this->mCacheRevisionId;
+       }
+
+       /**
+        * @since 1.23
+        * @param int $id Revision id
+        */
+       function setCacheRevisionId( $id ) {
+               $this->mCacheRevisionId = $id;
+       }
+
        /**
         * Sets the number of seconds after which this object should expire.
         * This value is used with the ParserCache.
@@ -77,7 +106,7 @@ class CacheTime {
         * or equal to the smallest number that was provided as an argument to
         * updateCacheExpiry().
         *
-        * @param $seconds number
+        * @param int $seconds
         */
        function updateCacheExpiry( $seconds ) {
                $seconds = (int)$seconds;
@@ -139,17 +168,35 @@ class CacheTime {
         * per-article cache invalidation timestamps, or if it comes from
         * an incompatible older version.
         *
-        * @param string $touched the affected article's last touched timestamp
-        * @return Boolean
+        * @param string $touched The affected article's last touched timestamp
+        * @return bool
         */
        public function expired( $touched ) {
                global $wgCacheEpoch;
-               return !$this->isCacheable() || // parser says it's uncacheable
-                       $this->getCacheTime() < $touched ||
-                       $this->getCacheTime() <= $wgCacheEpoch ||
-                       $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
-                       !isset( $this->mVersion ) ||
-                       version_compare( $this->mVersion, Parser::VERSION, "lt" );
+
+               return !$this->isCacheable() // parser says it's uncacheable
+                       || $this->getCacheTime() < $touched
+                       || $this->getCacheTime() <= $wgCacheEpoch
+                       || $this->getCacheTime() <
+                               wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) // expiry period has passed
+                       || !isset( $this->mVersion )
+                       || version_compare( $this->mVersion, Parser::VERSION, "lt" );
        }
 
+       /**
+        * Return true if this cached output object is for a different revision of
+        * the page.
+        *
+        * @todo We always return false if $this->getCacheRevisionId() is null;
+        * this prevents invalidating the whole parser cache when this change is
+        * deployed. Someday that should probably be changed.
+        *
+        * @since 1.23
+        * @param int $id The affected article's current revision id
+        * @return bool
+        */
+       public function isDifferentRevision( $id ) {
+               $cached = $this->getCacheRevisionId();
+               return $cached !== null && $id !== $cached;
+       }
 }