Merge "Fixed wrong EnqueueJob comment"
[mediawiki.git] / includes / parser / CacheTime.php
blob950c0d46df76fa781377035d690fed089c2c1beb
1 <?php
2 /**
3 * Parser cache specific expiry check.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Parser
24 /**
25 * Parser cache specific expiry check.
27 * @ingroup Parser
29 class CacheTime {
30 /** @var array|bool ParserOptions which have been taken into account to
31 * produce output or false if not available.
33 public $mUsedOptions;
35 public $mVersion = Parser::VERSION, # Compatibility check
36 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
37 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncacheable. Used in ParserCache.
38 $mCacheRevisionId = null; # Revision ID that was parsed
40 /**
41 * @return string TS_MW timestamp
43 public function getCacheTime() {
44 return wfTimestamp( TS_MW, $this->mCacheTime );
47 /**
48 * setCacheTime() sets the timestamp expressing when the page has been rendered.
49 * This does not control expiry, see updateCacheExpiry() for that!
50 * @param string $t
51 * @return string
53 public function setCacheTime( $t ) {
54 return wfSetVar( $this->mCacheTime, $t );
57 /**
58 * @since 1.23
59 * @return int|null Revision id, if any was set
61 public function getCacheRevisionId() {
62 return $this->mCacheRevisionId;
65 /**
66 * @since 1.23
67 * @param int $id Revision id
69 public function setCacheRevisionId( $id ) {
70 $this->mCacheRevisionId = $id;
73 /**
74 * Sets the number of seconds after which this object should expire.
75 * This value is used with the ParserCache.
76 * If called with a value greater than the value provided at any previous call,
77 * the new call has no effect. The value returned by getCacheExpiry is smaller
78 * or equal to the smallest number that was provided as an argument to
79 * updateCacheExpiry().
81 * @param int $seconds
83 public function updateCacheExpiry( $seconds ) {
84 $seconds = (int)$seconds;
86 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
87 $this->mCacheExpiry = $seconds;
90 // hack: set old-style marker for uncacheable entries.
91 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
92 $this->mCacheTime = -1;
96 /**
97 * Returns the number of seconds after which this object should expire.
98 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
99 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
100 * The value returned by getCacheExpiry is smaller or equal to the smallest number
101 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
102 * value of $wgParserCacheExpireTime.
103 * @return int|mixed|null
105 public function getCacheExpiry() {
106 global $wgParserCacheExpireTime;
108 if ( $this->mCacheTime < 0 ) {
109 return 0;
110 } // old-style marker for "not cacheable"
112 $expire = $this->mCacheExpiry;
114 if ( $expire === null ) {
115 $expire = $wgParserCacheExpireTime;
116 } else {
117 $expire = min( $expire, $wgParserCacheExpireTime );
120 if ( $expire <= 0 ) {
121 return 0; // not cacheable
122 } else {
123 return $expire;
128 * @return bool
130 public function isCacheable() {
131 return $this->getCacheExpiry() > 0;
135 * Return true if this cached output object predates the global or
136 * per-article cache invalidation timestamps, or if it comes from
137 * an incompatible older version.
139 * @param string $touched The affected article's last touched timestamp
140 * @return bool
142 public function expired( $touched ) {
143 global $wgCacheEpoch;
145 return !$this->isCacheable() // parser says it's uncacheable
146 || $this->getCacheTime() < $touched
147 || $this->getCacheTime() <= $wgCacheEpoch
148 || $this->getCacheTime() <
149 wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) // expiry period has passed
150 || !isset( $this->mVersion )
151 || version_compare( $this->mVersion, Parser::VERSION, "lt" );
155 * Return true if this cached output object is for a different revision of
156 * the page.
158 * @todo We always return false if $this->getCacheRevisionId() is null;
159 * this prevents invalidating the whole parser cache when this change is
160 * deployed. Someday that should probably be changed.
162 * @since 1.23
163 * @param int $id The affected article's current revision id
164 * @return bool
166 public function isDifferentRevision( $id ) {
167 $cached = $this->getCacheRevisionId();
168 return $cached !== null && $id !== $cached;