MediaWiki  master
CacheTime.php
Go to the documentation of this file.
1 <?php
28 use Wikimedia\Reflection\GhostFieldAccessTrait;
29 
36  use GhostFieldAccessTrait;
38 
43  protected $mParseUsedOptions = [];
44 
49  public $mCacheTime = '';
50 
55  public $mCacheExpiry = null;
56 
60  public $mCacheRevisionId = null;
61 
65  public function getCacheTime() {
66  // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
67  if ( $this->mCacheTime === '' ) {
68  $this->mCacheTime = MWTimestamp::now();
69  }
70  return $this->mCacheTime;
71  }
72 
79  public function setCacheTime( $t ) {
80  // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
81  if ( is_string( $t ) && $t !== '-1' ) {
82  $t = MWTimestamp::convert( TS_MW, $t );
83  }
84 
85  if ( $t === -1 || $t === '-1' ) {
86  wfDeprecatedMsg( __METHOD__ . ' called with -1 as an argument', '1.36' );
87  }
88 
89  return wfSetVar( $this->mCacheTime, $t );
90  }
91 
96  public function getCacheRevisionId(): ?int {
98  }
99 
104  public function setCacheRevisionId( $id ) {
105  $this->mCacheRevisionId = $id;
106  }
107 
121  public function updateCacheExpiry( $seconds ) {
122  $seconds = (int)$seconds;
123 
124  if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
125  $this->mCacheExpiry = $seconds;
126  }
127  }
128 
138  public function getCacheExpiry(): int {
140 
141  // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
142  if ( $this->mCacheTime !== '' && $this->mCacheTime < 0 ) {
143  return 0;
144  }
145 
146  $expire = $this->mCacheExpiry;
147 
148  if ( $expire === null ) {
149  $expire = $wgParserCacheExpireTime;
150  } else {
151  $expire = min( $expire, $wgParserCacheExpireTime );
152  }
153 
154  if ( $expire <= 0 ) {
155  return 0; // not cacheable
156  } else {
157  return $expire;
158  }
159  }
160 
164  public function isCacheable() {
165  return $this->getCacheExpiry() > 0;
166  }
167 
176  public function expired( $touched ) {
177  global $wgCacheEpoch;
178 
179  $expiry = MWTimestamp::convert( TS_MW, MWTimestamp::time() - $this->getCacheExpiry() );
180 
181  return !$this->isCacheable() // parser says it's not cacheable
182  || $this->getCacheTime() < $touched
183  || $this->getCacheTime() <= $wgCacheEpoch
184  || $this->getCacheTime() < $expiry; // expiry period has passed
185  }
186 
199  public function isDifferentRevision( $id ) {
200  $cached = $this->getCacheRevisionId();
201  return $cached !== null && $id !== $cached;
202  }
203 
210  public function getUsedOptions(): array {
211  return array_keys( $this->mParseUsedOptions );
212  }
213 
226  public function recordOption( string $option ) {
227  $this->mParseUsedOptions[$option] = true;
228  }
229 
236  public function recordOptions( array $options ) {
237  $this->mParseUsedOptions = array_merge(
238  $this->mParseUsedOptions,
239  array_fill_keys( $options, true )
240  );
241  }
242 
249  protected function toJsonArray(): array {
250  return [
251  'ParseUsedOptions' => $this->mParseUsedOptions,
252  'CacheExpiry' => $this->mCacheExpiry,
253  'CacheTime' => $this->mCacheTime,
254  'CacheRevisionId' => $this->mCacheRevisionId,
255  ];
256  }
257 
258  public static function newFromJsonArray( JsonUnserializer $unserializer, array $json ) {
259  $cacheTime = new CacheTime();
260  $cacheTime->initFromJson( $unserializer, $json );
261  return $cacheTime;
262  }
263 
269  protected function initFromJson( JsonUnserializer $unserializer, array $jsonData ) {
270  if ( array_key_exists( 'AccessedOptions', $jsonData ) ) {
271  // Backwards compatibility for ParserOutput
272  $this->mParseUsedOptions = $jsonData['AccessedOptions'] ?: [];
273  } elseif ( array_key_exists( 'UsedOptions', $jsonData ) ) {
274  // Backwards compatibility
275  $this->recordOptions( $jsonData['UsedOptions'] ?: [] );
276  } else {
277  $this->mParseUsedOptions = $jsonData['ParseUsedOptions'] ?: [];
278  }
279  $this->mCacheExpiry = $jsonData['CacheExpiry'];
280  $this->mCacheTime = $jsonData['CacheTime'];
281  $this->mCacheRevisionId = $jsonData['CacheRevisionId'];
282  }
283 
284  public function __wakeup() {
285  // Backwards compatibility, pre 1.36
286  $priorOptions = $this->getGhostFieldValue( 'mUsedOptions' );
287  if ( $priorOptions ) {
288  $this->recordOptions( $priorOptions );
289  }
290  }
291 }
MediaWiki\Json\JsonUnserializer
Definition: JsonUnserializer.php:33
CacheTime\getCacheExpiry
getCacheExpiry()
Returns the number of seconds after which this object should expire.
Definition: CacheTime.php:138
MediaWiki\Json\JsonUnserializableTrait
trait JsonUnserializableTrait
Definition: JsonUnserializableTrait.php:24
CacheTime
Parser cache specific expiry check.
Definition: CacheTime.php:35
MediaWiki\Json\JsonUnserializable
Definition: JsonUnserializable.php:38
wfSetVar
wfSetVar(&$dest, $source, $force=false)
Sets dest to source and returns the original value of dest If source is NULL, it just returns the val...
Definition: GlobalFunctions.php:1561
CacheTime\getUsedOptions
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
Definition: CacheTime.php:210
CacheTime\$mCacheRevisionId
int null $mCacheRevisionId
Revision ID that was parsed.
Definition: CacheTime.php:60
CacheTime\recordOptions
recordOptions(array $options)
Tags a list of parser option names for use in the cache key for this parser output.
Definition: CacheTime.php:236
CacheTime\getCacheRevisionId
getCacheRevisionId()
Definition: CacheTime.php:96
CacheTime\newFromJsonArray
static newFromJsonArray(JsonUnserializer $unserializer, array $json)
Creates a new instance of the class and initialized it from the $json array.
Definition: CacheTime.php:258
CacheTime\setCacheTime
setCacheTime( $t)
setCacheTime() sets the timestamp expressing when the page has been rendered.
Definition: CacheTime.php:79
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1066
CacheTime\$mParseUsedOptions
true[] $mParseUsedOptions
ParserOptions which have been taken into account to produce output, option names stored in array keys...
Definition: CacheTime.php:43
CacheTime\isDifferentRevision
isDifferentRevision( $id)
Return true if this cached output object is for a different revision of the page.
Definition: CacheTime.php:199
$wgParserCacheExpireTime
$wgParserCacheExpireTime
The expiry time for the parser cache, in seconds.
Definition: DefaultSettings.php:2735
CacheTime\$mCacheExpiry
int null $mCacheExpiry
Seconds after which the object should expire, use 0 for not cacheable.
Definition: CacheTime.php:55
CacheTime\$mCacheTime
string int $mCacheTime
TS_MW timestamp when this object was generated, or -1 for not cacheable.
Definition: CacheTime.php:49
CacheTime\__wakeup
__wakeup()
Definition: CacheTime.php:284
$wgCacheEpoch
$wgCacheEpoch
Set this to current time to invalidate all prior cached pages.
Definition: DefaultSettings.php:2873
CacheTime\expired
expired( $touched)
Return true if this cached output object predates the global or per-article cache invalidation timest...
Definition: CacheTime.php:176
CacheTime\setCacheRevisionId
setCacheRevisionId( $id)
Definition: CacheTime.php:104
CacheTime\recordOption
recordOption(string $option)
Tags a parser option for use in the cache key for this parser output.
Definition: CacheTime.php:226
CacheTime\updateCacheExpiry
updateCacheExpiry( $seconds)
Sets the number of seconds after which this object should expire.
Definition: CacheTime.php:121
CacheTime\isCacheable
isCacheable()
Definition: CacheTime.php:164
CacheTime\initFromJson
initFromJson(JsonUnserializer $unserializer, array $jsonData)
Initialize member fields from an array returned by jsonSerialize().
Definition: CacheTime.php:269
$t
$t
Definition: testCompression.php:74
CacheTime\toJsonArray
toJsonArray()
Returns a JSON serializable structure representing this CacheTime instance.
Definition: CacheTime.php:249
CacheTime\getCacheTime
getCacheTime()
Definition: CacheTime.php:65
Parser\ParserCacheMetadata
Read-only interface for metadata about a ParserCache entry.
Definition: ParserCacheMetadata.php:9