merging latest master
[lhc/web/wiklou.git] / includes / parser / CacheTime.php
1 <?php
2
3 /**
4 * @todo document
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Parser
23 */
24 class CacheTime {
25
26 var $mVersion = Parser::VERSION, # Compatibility check
27 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
28 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
29 $mContainsOldMagic; # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
30
31 function getCacheTime() { return $this->mCacheTime; }
32
33 function containsOldMagic() { return $this->mContainsOldMagic; }
34 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
35
36 /**
37 * setCacheTime() sets the timestamp expressing when the page has been rendered.
38 * This doesn not control expiry, see updateCacheExpiry() for that!
39 * @param $t string
40 * @return string
41 */
42 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
43
44 /**
45 * Sets the number of seconds after which this object should expire.
46 * This value is used with the ParserCache.
47 * If called with a value greater than the value provided at any previous call,
48 * the new call has no effect. The value returned by getCacheExpiry is smaller
49 * or equal to the smallest number that was provided as an argument to
50 * updateCacheExpiry().
51 *
52 * @param $seconds number
53 */
54 function updateCacheExpiry( $seconds ) {
55 $seconds = (int)$seconds;
56
57 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
58 $this->mCacheExpiry = $seconds;
59 }
60
61 // hack: set old-style marker for uncacheable entries.
62 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
63 $this->mCacheTime = -1;
64 }
65 }
66
67 /**
68 * Returns the number of seconds after which this object should expire.
69 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
70 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
71 * The value returned by getCacheExpiry is smaller or equal to the smallest number
72 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
73 * value of $wgParserCacheExpireTime.
74 * @return int|mixed|null
75 */
76 function getCacheExpiry() {
77 global $wgParserCacheExpireTime;
78
79 if ( $this->mCacheTime < 0 ) {
80 return 0;
81 } // old-style marker for "not cachable"
82
83 $expire = $this->mCacheExpiry;
84
85 if ( $expire === null ) {
86 $expire = $wgParserCacheExpireTime;
87 } else {
88 $expire = min( $expire, $wgParserCacheExpireTime );
89 }
90
91 if( $this->containsOldMagic() ) { //compatibility hack
92 $expire = min( $expire, 3600 ); # 1 hour
93 }
94
95 if ( $expire <= 0 ) {
96 return 0; // not cachable
97 } else {
98 return $expire;
99 }
100 }
101
102 /**
103 * @return bool
104 */
105 function isCacheable() {
106 return $this->getCacheExpiry() > 0;
107 }
108
109 /**
110 * Return true if this cached output object predates the global or
111 * per-article cache invalidation timestamps, or if it comes from
112 * an incompatible older version.
113 *
114 * @param $touched String: the affected article's last touched timestamp
115 * @return Boolean
116 */
117 public function expired( $touched ) {
118 global $wgCacheEpoch;
119 return !$this->isCacheable() || // parser says it's uncacheable
120 $this->getCacheTime() < $touched ||
121 $this->getCacheTime() <= $wgCacheEpoch ||
122 $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
123 !isset( $this->mVersion ) ||
124 version_compare( $this->mVersion, Parser::VERSION, "lt" );
125 }
126
127 }