MediaWiki  1.30.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:675
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: CacheHelper.php:30
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:705
SpecialCachedPage
Definition: SpecialCachedPage.php:40
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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:36
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:665
SpecialPage\$mName
$mName
Definition: SpecialPage.php:39
CacheHelper
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:105
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:63
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