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