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