SECURITY: rate-limit and prevent blocked users from changing email
[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 /**
31 * @var string[] ParserOptions which have been taken into account to produce output.
32 */
33 public $mUsedOptions;
34
35 /**
36 * @var string|null Compatibility check
37 */
38 public $mVersion = Parser::VERSION;
39
40 /**
41 * @var string|int TS_MW timestamp when this object was generated, or -1 for not cacheable. Used
42 * in ParserCache.
43 */
44 public $mCacheTime = '';
45
46 /**
47 * @var int|null Seconds after which the object should expire, use 0 for not cacheable. Used in
48 * ParserCache.
49 */
50 public $mCacheExpiry = null;
51
52 /**
53 * @var int|null Revision ID that was parsed
54 */
55 public $mCacheRevisionId = null;
56
57 /**
58 * @return string TS_MW timestamp
59 */
60 public function getCacheTime() {
61 // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
62 if ( $this->mCacheTime === '' ) {
63 $this->mCacheTime = MWTimestamp::now();
64 }
65 return $this->mCacheTime;
66 }
67
68 /**
69 * setCacheTime() sets the timestamp expressing when the page has been rendered.
70 * This does not control expiry, see updateCacheExpiry() for that!
71 * @param string $t TS_MW timestamp
72 * @return string
73 */
74 public function setCacheTime( $t ) {
75 // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
76 if ( is_string( $t ) && $t !== '-1' ) {
77 $t = MWTimestamp::convert( TS_MW, $t );
78 }
79
80 return wfSetVar( $this->mCacheTime, $t );
81 }
82
83 /**
84 * @since 1.23
85 * @return int|null Revision id, if any was set
86 */
87 public function getCacheRevisionId() {
88 return $this->mCacheRevisionId;
89 }
90
91 /**
92 * @since 1.23
93 * @param int|null $id Revision ID
94 */
95 public function setCacheRevisionId( $id ) {
96 $this->mCacheRevisionId = $id;
97 }
98
99 /**
100 * Sets the number of seconds after which this object should expire.
101 *
102 * This value is used with the ParserCache.
103 * If called with a value greater than the value provided at any previous call,
104 * the new call has no effect. The value returned by getCacheExpiry is smaller
105 * or equal to the smallest number that was provided as an argument to
106 * updateCacheExpiry().
107 *
108 * Avoid using 0 if at all possible. Consider JavaScript for highly dynamic content.
109 *
110 * @param int $seconds
111 */
112 public function updateCacheExpiry( $seconds ) {
113 $seconds = (int)$seconds;
114
115 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
116 $this->mCacheExpiry = $seconds;
117 }
118 }
119
120 /**
121 * Returns the number of seconds after which this object should expire.
122 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
123 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
124 * The value returned by getCacheExpiry is smaller or equal to the smallest number
125 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
126 * value of $wgParserCacheExpireTime.
127 * @return int
128 */
129 public function getCacheExpiry() {
130 global $wgParserCacheExpireTime;
131
132 // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
133 if ( $this->mCacheTime < 0 ) {
134 return 0;
135 }
136
137 $expire = $this->mCacheExpiry;
138
139 if ( $expire === null ) {
140 $expire = $wgParserCacheExpireTime;
141 } else {
142 $expire = min( $expire, $wgParserCacheExpireTime );
143 }
144
145 if ( $expire <= 0 ) {
146 return 0; // not cacheable
147 } else {
148 return $expire;
149 }
150 }
151
152 /**
153 * @return bool
154 */
155 public function isCacheable() {
156 return $this->getCacheExpiry() > 0;
157 }
158
159 /**
160 * Return true if this cached output object predates the global or
161 * per-article cache invalidation timestamps, or if it comes from
162 * an incompatible older version.
163 *
164 * @param string $touched The affected article's last touched timestamp
165 * @return bool
166 */
167 public function expired( $touched ) {
168 global $wgCacheEpoch;
169
170 $expiry = MWTimestamp::convert( TS_MW, MWTimestamp::time() - $this->getCacheExpiry() );
171
172 return !$this->isCacheable() // parser says it's not cacheable
173 || $this->getCacheTime() < $touched
174 || $this->getCacheTime() <= $wgCacheEpoch
175 || $this->getCacheTime() < $expiry // expiry period has passed
176 || !isset( $this->mVersion )
177 || version_compare( $this->mVersion, Parser::VERSION, "lt" );
178 }
179
180 /**
181 * Return true if this cached output object is for a different revision of
182 * the page.
183 *
184 * @todo We always return false if $this->getCacheRevisionId() is null;
185 * this prevents invalidating the whole parser cache when this change is
186 * deployed. Someday that should probably be changed.
187 *
188 * @since 1.23
189 * @param int $id The affected article's current revision id
190 * @return bool
191 */
192 public function isDifferentRevision( $id ) {
193 $cached = $this->getCacheRevisionId();
194 return $cached !== null && $id !== $cached;
195 }
196 }