Add PLURAL support to youhavenewmessages*
[mediawiki.git] / includes / parser / CacheTime.php
blob7b8935a646edf85a99524a0293f03292a0ff3f4a
1 <?php
2 /**
3 * Parser cache specific expiry check.
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.
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.
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
20 * @file
21 * @ingroup Parser
24 /**
25 * Parser cache specific expiry check.
27 * @ingroup Parser
29 class CacheTime {
30 /** @var array|bool ParserOptions which have been taken into account to
31 * produce output or false if not available.
33 public $mUsedOptions;
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}}
40 /**
41 * @return string TS_MW timestamp
43 function getCacheTime() { return wfTimestamp( TS_MW, $this->mCacheTime ); }
45 function containsOldMagic() { return $this->mContainsOldMagic; }
46 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
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
54 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
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().
64 * @param $seconds number
66 function updateCacheExpiry( $seconds ) {
67 $seconds = (int)$seconds;
69 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
70 $this->mCacheExpiry = $seconds;
73 // hack: set old-style marker for uncacheable entries.
74 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
75 $this->mCacheTime = -1;
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
88 function getCacheExpiry() {
89 global $wgParserCacheExpireTime;
91 if ( $this->mCacheTime < 0 ) {
92 return 0;
93 } // old-style marker for "not cachable"
95 $expire = $this->mCacheExpiry;
97 if ( $expire === null ) {
98 $expire = $wgParserCacheExpireTime;
99 } else {
100 $expire = min( $expire, $wgParserCacheExpireTime );
103 if ( $this->containsOldMagic() ) { //compatibility hack
104 $expire = min( $expire, 3600 ); # 1 hour
107 if ( $expire <= 0 ) {
108 return 0; // not cachable
109 } else {
110 return $expire;
115 * @return bool
117 function isCacheable() {
118 return $this->getCacheExpiry() > 0;
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.
126 * @param string $touched the affected article's last touched timestamp
127 * @return Boolean
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" );