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
25 * Parser cache specific expiry check.
30 /** @var array|bool ParserOptions which have been taken into account to
31 * produce output or false if not available.
36 public $mVersion = Parser
::VERSION
;
38 # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
39 public $mCacheTime = '';
41 # Seconds after which the object should expire, use 0 for uncacheable. Used in ParserCache.
42 public $mCacheExpiry = null;
44 # Revision ID that was parsed
45 public $mCacheRevisionId = null;
48 * @return string TS_MW timestamp
50 public function getCacheTime() {
51 return wfTimestamp( TS_MW
, $this->mCacheTime
);
55 * setCacheTime() sets the timestamp expressing when the page has been rendered.
56 * This does not control expiry, see updateCacheExpiry() for that!
57 * @param string $t TS_MW timestamp
60 public function setCacheTime( $t ) {
61 return wfSetVar( $this->mCacheTime
, $t );
66 * @return int|null Revision id, if any was set
68 public function getCacheRevisionId() {
69 return $this->mCacheRevisionId
;
74 * @param int $id Revision id
76 public function setCacheRevisionId( $id ) {
77 $this->mCacheRevisionId
= $id;
81 * Sets the number of seconds after which this object should expire.
83 * This value is used with the ParserCache.
84 * If called with a value greater than the value provided at any previous call,
85 * the new call has no effect. The value returned by getCacheExpiry is smaller
86 * or equal to the smallest number that was provided as an argument to
87 * updateCacheExpiry().
89 * Avoid using 0 if at all possible. Consider JavaScript for highly dynamic content.
93 public function updateCacheExpiry( $seconds ) {
94 $seconds = (int)$seconds;
96 if ( $this->mCacheExpiry
=== null ||
$this->mCacheExpiry
> $seconds ) {
97 $this->mCacheExpiry
= $seconds;
102 * Returns the number of seconds after which this object should expire.
103 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
104 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
105 * The value returned by getCacheExpiry is smaller or equal to the smallest number
106 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
107 * value of $wgParserCacheExpireTime.
108 * @return int|mixed|null
110 public function getCacheExpiry() {
111 global $wgParserCacheExpireTime;
113 if ( $this->mCacheTime
< 0 ) {
115 } // old-style marker for "not cacheable"
117 $expire = $this->mCacheExpiry
;
119 if ( $expire === null ) {
120 $expire = $wgParserCacheExpireTime;
122 $expire = min( $expire, $wgParserCacheExpireTime );
125 if ( $expire <= 0 ) {
126 return 0; // not cacheable
135 public function isCacheable() {
136 return $this->getCacheExpiry() > 0;
140 * Return true if this cached output object predates the global or
141 * per-article cache invalidation timestamps, or if it comes from
142 * an incompatible older version.
144 * @param string $touched The affected article's last touched timestamp
147 public function expired( $touched ) {
148 global $wgCacheEpoch;
150 return !$this->isCacheable() // parser says it's uncacheable
151 ||
$this->getCacheTime() < $touched
152 ||
$this->getCacheTime() <= $wgCacheEpoch
153 ||
$this->getCacheTime() <
154 wfTimestamp( TS_MW
, time() - $this->getCacheExpiry() ) // expiry period has passed
155 ||
!isset( $this->mVersion
)
156 ||
version_compare( $this->mVersion
, Parser
::VERSION
, "lt" );
160 * Return true if this cached output object is for a different revision of
163 * @todo We always return false if $this->getCacheRevisionId() is null;
164 * this prevents invalidating the whole parser cache when this change is
165 * deployed. Someday that should probably be changed.
168 * @param int $id The affected article's current revision id
171 public function isDifferentRevision( $id ) {
172 $cached = $this->getCacheRevisionId();
173 return $cached !== null && $id !== $cached;