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