MediaWiki  master
Go to the documentation of this file.
1 <?php
46 abstract class CachedAction extends FormlessAction implements ICacheHelper {
55  protected $cacheHelper;
63  protected $cacheEnabled = true;
71  public function setCacheEnabled( $cacheEnabled ) {
72  $this->cacheHelper->setCacheEnabled( $cacheEnabled );
73  }
84  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
85  $this->cacheHelper = new CacheHelper();
87  $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
88  $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] );
90  $keyArgs = $this->getCacheKey();
92  if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
93  unset( $keyArgs['action'] );
94  }
96  $this->cacheHelper->setCacheKey( $keyArgs );
98  if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
99  $this->cacheHelper->rebuildOnDemand();
100  }
102  $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
103  }
119  public function getCachedValue( $computeFunction, $args = [], $key = null ) {
120  return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
121  }
135  public function addCachedHTML( $computeFunction, $args = [], $key = null ) {
136  $html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
137  $this->getOutput()->addHTML( $html );
138  }
146  public function saveCache() {
147  $this->cacheHelper->saveCache();
148  }
158  public function setExpiry( $cacheExpiry ) {
159  $this->cacheHelper->setExpiry( $cacheExpiry );
160  }
169  protected function getCacheKey() {
170  return [
171  get_class( $this->page ),
172  $this->getName(),
173  $this->getLanguage()->getCode()
174  ];
175  }
184  public function onCacheInitialized( $hasCached ) {
185  if ( $hasCached ) {
186  $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
187  }
188  }
189 }
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1982
Get the OutputPage being used for this instance.
Definition: Action.php:208
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
Shortcut to get the user Language being used for this instance.
Definition: Action.php:237
target page
CacheHelper $cacheHelper
CacheHelper object to which we forward the non-SpecialPage specific caching work. ...
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
if( $line===false) $args
Definition: cdb.php:64
Returns the variables used to constructed the cache key in an array.
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...
Return the name of the action this object responds to.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
onCacheInitialized( $hasCached)
Gets called after the cache got initialized.
Saves the HTML to the cache in case it got recomputed.
An action which just does something, without showing a form first.
Abstract action class with scaffolding for caching HTML and other values in a single blob...
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
addCachedHTML( $computeFunction, $args=[], $key=null)
Add some HTML to be cached.
Get the WebRequest being used for this instance.
Definition: Action.php:198
Interface for all classes implementing CacheHelper functionality.
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.