MediaWiki  1.34.0
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 }
SpecialCachedPage\getCachedValue
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
Definition: SpecialCachedPage.php:127
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialCachedPage\$cacheEnabled
bool $cacheEnabled
If the cache is enabled or not.
Definition: SpecialCachedPage.php:56
SpecialCachedPage\onCacheInitialized
onCacheInitialized( $hasCached)
Gets called after the cache got initialized.
Definition: SpecialCachedPage.php:196
ICacheHelper
Interface for all classes implementing CacheHelper functionality.
Definition: ICacheHelper.php:30
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:749
SpecialCachedPage
Definition: SpecialCachedPage.php:40
SpecialCachedPage\setCacheEnabled
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
Definition: SpecialCachedPage.php:77
SpecialCachedPage\setExpiry
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
Definition: SpecialCachedPage.php:171
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:37
SpecialCachedPage\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
Definition: SpecialCachedPage.php:157
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
SpecialPage\$mName
$mName
Definition: SpecialPage.php:40
CacheHelper
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
SpecialCachedPage\addCachedHTML
addCachedHTML( $computeFunction, $args=[], $key=null)
Add some HTML to be cached.
Definition: SpecialCachedPage.php:143
$args
if( $line===false) $args
Definition: cdb.php:64
SpecialCachedPage\afterExecute
afterExecute( $subPage)
Gets called after.
Definition: SpecialCachedPage.php:65
SpecialCachedPage\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
Definition: SpecialCachedPage.php:90
SpecialCachedPage\getCacheKey
getCacheKey()
Returns the variables used to constructed the cache key in an array.
Definition: SpecialCachedPage.php:182
SpecialCachedPage\$cacheHelper
CacheHelper $cacheHelper
CacheHelper object to which we forward the non-SpecialPage specific caching work.
Definition: SpecialCachedPage.php:48