Support offsets in prefix searching
[mediawiki.git] / includes / parser / CacheTime.php
blob94abc2660940cdc3a0f21b436cda170dbbfe3163
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 uncachable. Used in ParserCache.
38 $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
39 $mCacheRevisionId = null; # Revision ID that was parsed
41 /**
42 * @return string TS_MW timestamp
44 public function getCacheTime() {
45 return wfTimestamp( TS_MW, $this->mCacheTime );
48 /**
49 * @return bool
51 public function containsOldMagic() {
52 return $this->mContainsOldMagic;
55 /**
56 * @param bool $com
57 * @return bool
59 public function setContainsOldMagic( $com ) {
60 return wfSetVar( $this->mContainsOldMagic, $com );
63 /**
64 * setCacheTime() sets the timestamp expressing when the page has been rendered.
65 * This does not control expiry, see updateCacheExpiry() for that!
66 * @param string $t
67 * @return string
69 public function setCacheTime( $t ) {
70 return wfSetVar( $this->mCacheTime, $t );
73 /**
74 * @since 1.23
75 * @return int|null Revision id, if any was set
77 public function getCacheRevisionId() {
78 return $this->mCacheRevisionId;
81 /**
82 * @since 1.23
83 * @param int $id Revision id
85 public function setCacheRevisionId( $id ) {
86 $this->mCacheRevisionId = $id;
89 /**
90 * Sets the number of seconds after which this object should expire.
91 * This value is used with the ParserCache.
92 * If called with a value greater than the value provided at any previous call,
93 * the new call has no effect. The value returned by getCacheExpiry is smaller
94 * or equal to the smallest number that was provided as an argument to
95 * updateCacheExpiry().
97 * @param int $seconds
99 public function updateCacheExpiry( $seconds ) {
100 $seconds = (int)$seconds;
102 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
103 $this->mCacheExpiry = $seconds;
106 // hack: set old-style marker for uncacheable entries.
107 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
108 $this->mCacheTime = -1;
113 * Returns the number of seconds after which this object should expire.
114 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
115 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
116 * The value returned by getCacheExpiry is smaller or equal to the smallest number
117 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
118 * value of $wgParserCacheExpireTime.
119 * @return int|mixed|null
121 public function getCacheExpiry() {
122 global $wgParserCacheExpireTime;
124 if ( $this->mCacheTime < 0 ) {
125 return 0;
126 } // old-style marker for "not cachable"
128 $expire = $this->mCacheExpiry;
130 if ( $expire === null ) {
131 $expire = $wgParserCacheExpireTime;
132 } else {
133 $expire = min( $expire, $wgParserCacheExpireTime );
136 if ( $this->containsOldMagic() ) { //compatibility hack
137 $expire = min( $expire, 3600 ); # 1 hour
140 if ( $expire <= 0 ) {
141 return 0; // not cachable
142 } else {
143 return $expire;
148 * @return bool
150 public function isCacheable() {
151 return $this->getCacheExpiry() > 0;
155 * Return true if this cached output object predates the global or
156 * per-article cache invalidation timestamps, or if it comes from
157 * an incompatible older version.
159 * @param string $touched The affected article's last touched timestamp
160 * @return bool
162 public function expired( $touched ) {
163 global $wgCacheEpoch;
165 return !$this->isCacheable() // parser says it's uncacheable
166 || $this->getCacheTime() < $touched
167 || $this->getCacheTime() <= $wgCacheEpoch
168 || $this->getCacheTime() <
169 wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) // expiry period has passed
170 || !isset( $this->mVersion )
171 || version_compare( $this->mVersion, Parser::VERSION, "lt" );
175 * Return true if this cached output object is for a different revision of
176 * the page.
178 * @todo We always return false if $this->getCacheRevisionId() is null;
179 * this prevents invalidating the whole parser cache when this change is
180 * deployed. Someday that should probably be changed.
182 * @since 1.23
183 * @param int $id The affected article's current revision id
184 * @return bool
186 public function isDifferentRevision( $id ) {
187 $cached = $this->getCacheRevisionId();
188 return $cached !== null && $id !== $cached;