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