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