Remove underscore from classes WebInstaller_*
[mediawiki.git] / includes / parser / CacheTime.php
blob91f404acc24ce392f46126d369159560e8afa60a
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 /** @var string Compatibility check */
36 protected $mVersion = Parser::VERSION;
38 /** @var string Time when this object was generated, or -1 for uncacheable. Used in ParserCache. */
39 protected $mCacheTime = '';
41 /**
42 * @var int Seconds after which the object should expire, use 0 for uncachable.
43 * Used in ParserCache.
45 protected $mCacheExpiry = null;
47 /** @var bool Boolean variable indicating if the input contained variables like {{CURRENTDAY}} */
48 protected $mContainsOldMagic;
50 /** @var int Revision ID that was parsed */
51 protected $mCacheRevisionId = null;
53 /**
54 * @return string TS_MW timestamp
56 function getCacheTime() {
57 return wfTimestamp( TS_MW, $this->mCacheTime );
60 /**
61 * @return bool
63 function containsOldMagic() {
64 return $this->mContainsOldMagic;
67 /**
68 * @param bool $com
69 * @return bool
71 function setContainsOldMagic( $com ) {
72 return wfSetVar( $this->mContainsOldMagic, $com );
75 /**
76 * setCacheTime() sets the timestamp expressing when the page has been rendered.
77 * This does not control expiry, see updateCacheExpiry() for that!
78 * @param string $t
79 * @return string
81 function setCacheTime( $t ) {
82 return wfSetVar( $this->mCacheTime, $t );
85 /**
86 * @since 1.23
87 * @return int|null Revision id, if any was set
89 function getCacheRevisionId() {
90 return $this->mCacheRevisionId;
93 /**
94 * @since 1.23
95 * @param int $id Revision id
97 function setCacheRevisionId( $id ) {
98 $this->mCacheRevisionId = $id;
102 * Sets the number of seconds after which this object should expire.
103 * This value is used with the ParserCache.
104 * If called with a value greater than the value provided at any previous call,
105 * the new call has no effect. The value returned by getCacheExpiry is smaller
106 * or equal to the smallest number that was provided as an argument to
107 * updateCacheExpiry().
109 * @param int $seconds
111 function updateCacheExpiry( $seconds ) {
112 $seconds = (int)$seconds;
114 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
115 $this->mCacheExpiry = $seconds;
118 // hack: set old-style marker for uncacheable entries.
119 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
120 $this->mCacheTime = -1;
125 * Returns the number of seconds after which this object should expire.
126 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
127 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
128 * The value returned by getCacheExpiry is smaller or equal to the smallest number
129 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
130 * value of $wgParserCacheExpireTime.
131 * @return int|mixed|null
133 function getCacheExpiry() {
134 global $wgParserCacheExpireTime;
136 if ( $this->mCacheTime < 0 ) {
137 return 0;
138 } // old-style marker for "not cachable"
140 $expire = $this->mCacheExpiry;
142 if ( $expire === null ) {
143 $expire = $wgParserCacheExpireTime;
144 } else {
145 $expire = min( $expire, $wgParserCacheExpireTime );
148 if ( $this->containsOldMagic() ) { //compatibility hack
149 $expire = min( $expire, 3600 ); # 1 hour
152 if ( $expire <= 0 ) {
153 return 0; // not cachable
154 } else {
155 return $expire;
160 * @return bool
162 function isCacheable() {
163 return $this->getCacheExpiry() > 0;
167 * Return true if this cached output object predates the global or
168 * per-article cache invalidation timestamps, or if it comes from
169 * an incompatible older version.
171 * @param string $touched The affected article's last touched timestamp
172 * @return bool
174 public function expired( $touched ) {
175 global $wgCacheEpoch;
177 return !$this->isCacheable() // parser says it's uncacheable
178 || $this->getCacheTime() < $touched
179 || $this->getCacheTime() <= $wgCacheEpoch
180 || $this->getCacheTime() <
181 wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) // expiry period has passed
182 || !isset( $this->mVersion )
183 || version_compare( $this->mVersion, Parser::VERSION, "lt" );
187 * Return true if this cached output object is for a different revision of
188 * the page.
190 * @todo We always return false if $this->getCacheRevisionId() is null;
191 * this prevents invalidating the whole parser cache when this change is
192 * deployed. Someday that should probably be changed.
194 * @since 1.23
195 * @param int $id The affected article's current revision id
196 * @return bool
198 public function isDifferentRevision( $id ) {
199 $cached = $this->getCacheRevisionId();
200 return $cached !== null && $id !== $cached;