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