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