MediaWiki  master
SpecialCachedPage.php
Go to the documentation of this file.
1 <?php
2 
40 abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
48  protected $cacheHelper;
49 
56  protected $cacheEnabled = true;
57 
65  protected function afterExecute( $subPage ) {
66  $this->saveCache();
67 
68  parent::afterExecute( $subPage );
69  }
70 
77  public function setCacheEnabled( $cacheEnabled ) {
78  $this->cacheHelper->setCacheEnabled( $cacheEnabled );
79  }
80 
90  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
91  if ( !isset( $this->cacheHelper ) ) {
92  $this->cacheHelper = new CacheHelper();
93 
94  $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
95  $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] );
96 
97  $keyArgs = $this->getCacheKey();
98 
99  if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
100  unset( $keyArgs['action'] );
101  }
102 
103  $this->cacheHelper->setCacheKey( $keyArgs );
104 
105  if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
106  $this->cacheHelper->rebuildOnDemand();
107  }
108  }
109 
110  $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
111  }
112 
127  public function getCachedValue( $computeFunction, $args = [], $key = null ) {
128  return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
129  }
130 
143  public function addCachedHTML( $computeFunction, $args = [], $key = null ) {
144  $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue(
145  $computeFunction,
146  $args,
147  $key
148  ) );
149  }
150 
157  public function saveCache() {
158  if ( isset( $this->cacheHelper ) ) {
159  $this->cacheHelper->saveCache();
160  }
161  }
162 
171  public function setExpiry( $cacheExpiry ) {
172  $this->cacheHelper->setExpiry( $cacheExpiry );
173  }
174 
182  protected function getCacheKey() {
183  return [
184  $this->mName,
185  $this->getLanguage()->getCode()
186  ];
187  }
188 
196  public function onCacheInitialized( $hasCached ) {
197  if ( $hasCached ) {
198  $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
199  }
200  }
201 }
getCacheKey()
Returns the variables used to constructed the cache key in an array.
afterExecute( $subPage)
Gets called after.
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
getOutput()
Get the OutputPage being used for this instance.
if( $line===false) $args
Definition: mcc.php:124
saveCache()
Saves the HTML to the cache in case it got recomputed.
bool $cacheEnabled
If the cache is enabled or not.
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
onCacheInitialized( $hasCached)
Gets called after the cache got initialized.
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
CacheHelper $cacheHelper
CacheHelper object to which we forward the non-SpecialPage specific caching work. ...
getLanguage()
Shortcut to get user&#39;s language.
getRequest()
Get the WebRequest being used for this instance.
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
Interface for all classes implementing CacheHelper functionality.
addCachedHTML( $computeFunction, $args=[], $key=null)
Add some HTML to be cached.