Declare visibility on class properties of includes/parser/
[lhc/web/wiklou.git] / includes / parser / CacheTime.php
1 <?php
2 /**
3 * Parser cache specific expiry check.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * Parser cache specific expiry check.
26 *
27 * @ingroup Parser
28 */
29 class CacheTime {
30 /** @var array|bool ParserOptions which have been taken into account to
31 * produce output or false if not available.
32 */
33 public $mUsedOptions;
34
35 /** @var string Compatibility check */
36 protected $mVersion = Parser::VERSION;
37
38 /** @var string Time when this object was generated, or -1 for uncacheable. Used in ParserCache. */
39 protected $mCacheTime = '';
40
41 /**
42 * @var int Seconds after which the object should expire, use 0 for uncachable.
43 * Used in ParserCache.
44 */
45 protected $mCacheExpiry = null;
46
47 /** @var bool Boolean variable indicating if the input contained variables like {{CURRENTDAY}} */
48 protected $mContainsOldMagic;
49
50 /** @var int Revision ID that was parsed */
51 protected $mCacheRevisionId = null;
52
53 /**
54 * @return string TS_MW timestamp
55 */
56 function getCacheTime() {
57 return wfTimestamp( TS_MW, $this->mCacheTime );
58 }
59
60 /**
61 * @return bool
62 */
63 function containsOldMagic() {
64 return $this->mContainsOldMagic;
65 }
66
67 /**
68 * @param bool $com
69 * @return bool
70 */
71 function setContainsOldMagic( $com ) {
72 return wfSetVar( $this->mContainsOldMagic, $com );
73 }
74
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
80 */
81 function setCacheTime( $t ) {
82 return wfSetVar( $this->mCacheTime, $t );
83 }
84
85 /**
86 * @since 1.23
87 * @return int|null Revision id, if any was set
88 */
89 function getCacheRevisionId() {
90 return $this->mCacheRevisionId;
91 }
92
93 /**
94 * @since 1.23
95 * @param int $id Revision id
96 */
97 function setCacheRevisionId( $id ) {
98 $this->mCacheRevisionId = $id;
99 }
100
101 /**
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().
108 *
109 * @param int $seconds
110 */
111 function updateCacheExpiry( $seconds ) {
112 $seconds = (int)$seconds;
113
114 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
115 $this->mCacheExpiry = $seconds;
116 }
117
118 // hack: set old-style marker for uncacheable entries.
119 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
120 $this->mCacheTime = -1;
121 }
122 }
123
124 /**
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
132 */
133 function getCacheExpiry() {
134 global $wgParserCacheExpireTime;
135
136 if ( $this->mCacheTime < 0 ) {
137 return 0;
138 } // old-style marker for "not cachable"
139
140 $expire = $this->mCacheExpiry;
141
142 if ( $expire === null ) {
143 $expire = $wgParserCacheExpireTime;
144 } else {
145 $expire = min( $expire, $wgParserCacheExpireTime );
146 }
147
148 if ( $this->containsOldMagic() ) { //compatibility hack
149 $expire = min( $expire, 3600 ); # 1 hour
150 }
151
152 if ( $expire <= 0 ) {
153 return 0; // not cachable
154 } else {
155 return $expire;
156 }
157 }
158
159 /**
160 * @return bool
161 */
162 function isCacheable() {
163 return $this->getCacheExpiry() > 0;
164 }
165
166 /**
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.
170 *
171 * @param string $touched The affected article's last touched timestamp
172 * @return bool
173 */
174 public function expired( $touched ) {
175 global $wgCacheEpoch;
176 return !$this->isCacheable() || // parser says it's uncacheable
177 $this->getCacheTime() < $touched ||
178 $this->getCacheTime() <= $wgCacheEpoch ||
179 $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
180 !isset( $this->mVersion ) ||
181 version_compare( $this->mVersion, Parser::VERSION, "lt" );
182 }
183
184 /**
185 * Return true if this cached output object is for a different revision of
186 * the page.
187 *
188 * @todo We always return false if $this->getCacheRevisionId() is null;
189 * this prevents invalidating the whole parser cache when this change is
190 * deployed. Someday that should probably be changed.
191 *
192 * @since 1.23
193 * @param int $id The affected article's current revision id
194 * @return bool
195 */
196 public function isDifferentRevision( $id ) {
197 $cached = $this->getCacheRevisionId();
198 return $cached !== null && $id !== $cached;
199 }
200 }