Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / parser / CacheTime.php
blob8190a8a09a7740b21d5e098bc68f32e59ad93b2f
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 {
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}}
36 function getCacheTime() { return $this->mCacheTime; }
38 function containsOldMagic() { return $this->mContainsOldMagic; }
39 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
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
47 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
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().
57 * @param $seconds number
59 function updateCacheExpiry( $seconds ) {
60 $seconds = (int)$seconds;
62 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
63 $this->mCacheExpiry = $seconds;
66 // hack: set old-style marker for uncacheable entries.
67 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
68 $this->mCacheTime = -1;
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
81 function getCacheExpiry() {
82 global $wgParserCacheExpireTime;
84 if ( $this->mCacheTime < 0 ) {
85 return 0;
86 } // old-style marker for "not cachable"
88 $expire = $this->mCacheExpiry;
90 if ( $expire === null ) {
91 $expire = $wgParserCacheExpireTime;
92 } else {
93 $expire = min( $expire, $wgParserCacheExpireTime );
96 if ( $this->containsOldMagic() ) { //compatibility hack
97 $expire = min( $expire, 3600 ); # 1 hour
100 if ( $expire <= 0 ) {
101 return 0; // not cachable
102 } else {
103 return $expire;
108 * @return bool
110 function isCacheable() {
111 return $this->getCacheExpiry() > 0;
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.
119 * @param string $touched the affected article's last touched timestamp
120 * @return Boolean
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" );