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