Merge "CSS style for thumbs on special pages"
[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 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
40
41 /**
42 * @return string TS_MW timestamp
43 */
44 public function getCacheTime() {
45 return wfTimestamp( TS_MW, $this->mCacheTime );
46 }
47
48 /**
49 * @return bool
50 */
51 public function containsOldMagic() {
52 return $this->mContainsOldMagic;
53 }
54
55 /**
56 * @param bool $com
57 * @return bool
58 */
59 public function setContainsOldMagic( $com ) {
60 return wfSetVar( $this->mContainsOldMagic, $com );
61 }
62
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
68 */
69 public function setCacheTime( $t ) {
70 return wfSetVar( $this->mCacheTime, $t );
71 }
72
73 /**
74 * @since 1.23
75 * @return int|null Revision id, if any was set
76 */
77 public function getCacheRevisionId() {
78 return $this->mCacheRevisionId;
79 }
80
81 /**
82 * @since 1.23
83 * @param int $id Revision id
84 */
85 public function setCacheRevisionId( $id ) {
86 $this->mCacheRevisionId = $id;
87 }
88
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().
96 *
97 * @param int $seconds
98 */
99 public function updateCacheExpiry( $seconds ) {
100 $seconds = (int)$seconds;
101
102 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
103 $this->mCacheExpiry = $seconds;
104 }
105
106 // hack: set old-style marker for uncacheable entries.
107 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
108 $this->mCacheTime = -1;
109 }
110 }
111
112 /**
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
120 */
121 public function getCacheExpiry() {
122 global $wgParserCacheExpireTime;
123
124 if ( $this->mCacheTime < 0 ) {
125 return 0;
126 } // old-style marker for "not cachable"
127
128 $expire = $this->mCacheExpiry;
129
130 if ( $expire === null ) {
131 $expire = $wgParserCacheExpireTime;
132 } else {
133 $expire = min( $expire, $wgParserCacheExpireTime );
134 }
135
136 if ( $this->containsOldMagic() ) { //compatibility hack
137 $expire = min( $expire, 3600 ); # 1 hour
138 }
139
140 if ( $expire <= 0 ) {
141 return 0; // not cachable
142 } else {
143 return $expire;
144 }
145 }
146
147 /**
148 * @return bool
149 */
150 public function isCacheable() {
151 return $this->getCacheExpiry() > 0;
152 }
153
154 /**
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.
158 *
159 * @param string $touched The affected article's last touched timestamp
160 * @return bool
161 */
162 public function expired( $touched ) {
163 global $wgCacheEpoch;
164
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" );
172 }
173
174 /**
175 * Return true if this cached output object is for a different revision of
176 * the page.
177 *
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.
181 *
182 * @since 1.23
183 * @param int $id The affected article's current revision id
184 * @return bool
185 */
186 public function isDifferentRevision( $id ) {
187 $cached = $this->getCacheRevisionId();
188 return $cached !== null && $id !== $cached;
189 }
190 }