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  protected $mCacheTime = '';
50 
55  protected $mCacheExpiry = null;
56 
60  protected $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 
125  public function updateCacheExpiry( $seconds ) {
126  $seconds = (int)$seconds;
127 
128  if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
129  $this->mCacheExpiry = $seconds;
130  }
131  }
132 
142  public function getCacheExpiry(): int {
144 
145  // NOTE: keep support for undocumented used of -1 to mean "not cacheable".
146  if ( $this->mCacheTime !== '' && $this->mCacheTime < 0 ) {
147  return 0;
148  }
149 
150  $expire = $this->mCacheExpiry;
151 
152  if ( $expire === null ) {
153  $expire = $wgParserCacheExpireTime;
154  } else {
155  $expire = min( $expire, $wgParserCacheExpireTime );
156  }
157 
158  if ( $expire <= 0 ) {
159  return 0; // not cacheable
160  } else {
161  return $expire;
162  }
163  }
164 
168  public function isCacheable() {
169  return $this->getCacheExpiry() > 0;
170  }
171 
180  public function expired( $touched ) {
181  global $wgCacheEpoch;
182 
183  $expiry = MWTimestamp::convert( TS_MW, MWTimestamp::time() - $this->getCacheExpiry() );
184 
185  return !$this->isCacheable() // parser says it's not cacheable
186  || $this->getCacheTime() < $touched
187  || $this->getCacheTime() <= $wgCacheEpoch
188  || $this->getCacheTime() < $expiry; // expiry period has passed
189  }
190 
203  public function isDifferentRevision( $id ) {
204  $cached = $this->getCacheRevisionId();
205  return $cached !== null && $id !== $cached;
206  }
207 
214  public function getUsedOptions(): array {
215  return array_keys( $this->mParseUsedOptions );
216  }
217 
230  public function recordOption( string $option ) {
231  $this->mParseUsedOptions[$option] = true;
232  }
233 
240  public function recordOptions( array $options ) {
241  $this->mParseUsedOptions = array_merge(
242  $this->mParseUsedOptions,
243  array_fill_keys( $options, true )
244  );
245  }
246 
253  protected function toJsonArray(): array {
254  return [
255  'ParseUsedOptions' => $this->mParseUsedOptions,
256  'CacheExpiry' => $this->mCacheExpiry,
257  'CacheTime' => $this->mCacheTime,
258  'CacheRevisionId' => $this->mCacheRevisionId,
259  ];
260  }
261 
262  public static function newFromJsonArray( JsonUnserializer $unserializer, array $json ) {
263  $cacheTime = new CacheTime();
264  $cacheTime->initFromJson( $unserializer, $json );
265  return $cacheTime;
266  }
267 
273  protected function initFromJson( JsonUnserializer $unserializer, array $jsonData ) {
274  if ( array_key_exists( 'AccessedOptions', $jsonData ) ) {
275  // Backwards compatibility for ParserOutput
276  $this->mParseUsedOptions = $jsonData['AccessedOptions'] ?: [];
277  } elseif ( array_key_exists( 'UsedOptions', $jsonData ) ) {
278  // Backwards compatibility
279  $this->recordOptions( $jsonData['UsedOptions'] ?: [] );
280  } else {
281  $this->mParseUsedOptions = $jsonData['ParseUsedOptions'] ?: [];
282  }
283  $this->mCacheExpiry = $jsonData['CacheExpiry'];
284  $this->mCacheTime = $jsonData['CacheTime'];
285  $this->mCacheRevisionId = $jsonData['CacheRevisionId'];
286  }
287 
288  public function __wakeup() {
289  // Backwards compatibility, pre 1.36
290  $priorOptions = $this->getGhostFieldValue( 'mUsedOptions' );
291  if ( $priorOptions ) {
292  $this->recordOptions( $priorOptions );
293  }
294  }
295 
296  public function __get( $name ) {
297  if ( property_exists( get_called_class(), $name ) ) {
298  // Direct access to a public property, deprecated.
299  wfDeprecatedMsg( "CacheTime::{$name} public read access deprecated", '1.38' );
300  return $this->$name;
301  } elseif ( property_exists( $this, $name ) ) {
302  // Dynamic property access, deprecated.
303  wfDeprecatedMsg( "CacheTime::{$name} dynamic property read access deprecated", '1.38' );
304  return $this->$name;
305  } else {
306  trigger_error( "Inaccessible property via __set(): $name" );
307  return null;
308  }
309  }
310 
311  public function __set( $name, $value ) {
312  if ( property_exists( get_called_class(), $name ) ) {
313  // Direct access to a public property, deprecated.
314  wfDeprecatedMsg( "CacheTime::$name public write access deprecated", '1.38' );
315  $this->$name = $value;
316  } else {
317  // Dynamic property access, deprecated.
318  wfDeprecatedMsg( "CacheTime::$name dynamic property write access deprecated", '1.38' );
319  $this->$name = $value;
320  }
321  }
322 }
MediaWiki\Json\JsonUnserializer
Definition: JsonUnserializer.php:33
CacheTime\getCacheExpiry
getCacheExpiry()
Returns the number of seconds after which this object should expire.
Definition: CacheTime.php:142
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:1512
CacheTime\getUsedOptions
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
Definition: CacheTime.php:214
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:240
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:262
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:1028
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:203
$wgParserCacheExpireTime
$wgParserCacheExpireTime
The expiry time for the parser cache, in seconds.
Definition: DefaultSettings.php:2970
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\__set
__set( $name, $value)
Definition: CacheTime.php:311
CacheTime\__wakeup
__wakeup()
Definition: CacheTime.php:288
$wgCacheEpoch
$wgCacheEpoch
Set this to current time to invalidate all prior cached pages.
Definition: DefaultSettings.php:3108
CacheTime\expired
expired( $touched)
Return true if this cached output object predates the global or per-article cache invalidation timest...
Definition: CacheTime.php:180
CacheTime\setCacheRevisionId
setCacheRevisionId( $id)
Definition: CacheTime.php:104
CacheTime\__get
__get( $name)
Definition: CacheTime.php:296
CacheTime\recordOption
recordOption(string $option)
Tags a parser option for use in the cache key for this parser output.
Definition: CacheTime.php:230
CacheTime\updateCacheExpiry
updateCacheExpiry( $seconds)
Sets the number of seconds after which this object should expire.
Definition: CacheTime.php:125
CacheTime\isCacheable
isCacheable()
Definition: CacheTime.php:168
CacheTime\initFromJson
initFromJson(JsonUnserializer $unserializer, array $jsonData)
Initialize member fields from an array returned by jsonSerialize().
Definition: CacheTime.php:273
$t
$t
Definition: testCompression.php:74
CacheTime\toJsonArray
toJsonArray()
Returns a JSON serializable structure representing this CacheTime instance.
Definition: CacheTime.php:253
CacheTime\getCacheTime
getCacheTime()
Definition: CacheTime.php:65
Parser\ParserCacheMetadata
Read-only interface for metadata about a ParserCache entry.
Definition: ParserCacheMetadata.php:9