MediaWiki  master
CachedAction.php
Go to the documentation of this file.
1 <?php
46 abstract class CachedAction extends FormlessAction implements ICacheHelper {
47 
55  protected $cacheHelper;
56 
63  protected $cacheEnabled = true;
64 
65  public function __construct(
66  Page $page,
68  ) {
69  parent::__construct( $page, $context );
70  wfDeprecated( __CLASS__, '1.36' );
71  }
72 
79  public function setCacheEnabled( $cacheEnabled ) {
80  $this->cacheHelper->setCacheEnabled( $cacheEnabled );
81  }
82 
92  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
93  $this->cacheHelper = new CacheHelper();
94 
95  $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
96  $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] );
97 
98  $keyArgs = $this->getCacheKey();
99 
100  if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
101  unset( $keyArgs['action'] );
102  }
103 
104  $this->cacheHelper->setCacheKey( $keyArgs );
105 
106  if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
107  $this->cacheHelper->rebuildOnDemand();
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  $html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
145  $this->getOutput()->addHTML( $html );
146  }
147 
154  public function saveCache() {
155  $this->cacheHelper->saveCache();
156  }
157 
166  public function setExpiry( $cacheExpiry ) {
167  $this->cacheHelper->setExpiry( $cacheExpiry );
168  }
169 
177  protected function getCacheKey() {
178  return [
179  get_class( $this->getArticle() ),
180  $this->getName(),
181  $this->getLanguage()->getCode()
182  ];
183  }
184 
192  public function onCacheInitialized( $hasCached ) {
193  if ( $hasCached ) {
194  $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
195  }
196  }
197 }
CachedAction\onCacheInitialized
onCacheInitialized( $hasCached)
Gets called after the cache got initialized.
Definition: CachedAction.php:192
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
CachedAction\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
Definition: CachedAction.php:154
CachedAction\getCachedValue
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
Definition: CachedAction.php:127
FormlessAction
An action which just does something, without showing a form first.
Definition: FormlessAction.php:30
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:229
CachedAction\getCacheKey
getCacheKey()
Returns the variables used to constructed the cache key in an array.
Definition: CachedAction.php:177
CachedAction\setCacheEnabled
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
Definition: CachedAction.php:79
Action\getName
getName()
Return the name of the action this object responds to.
ICacheHelper
Interface for all classes implementing CacheHelper functionality.
Definition: ICacheHelper.php:31
CachedAction\__construct
__construct(Page $page, IContextSource $context=null)
Only public since 1.21.
Definition: CachedAction.php:65
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1034
Action\getArticle
getArticle()
Get a Article object.
Definition: Action.php:289
$args
if( $line===false) $args
Definition: mcc.php:124
CachedAction
Abstract action class with scaffolding for caching HTML and other values in a single blob.
Definition: CachedAction.php:46
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
CachedAction\$cacheEnabled
bool $cacheEnabled
If the cache is enabled or not.
Definition: CachedAction.php:63
CachedAction\addCachedHTML
addCachedHTML( $computeFunction, $args=[], $key=null)
Add some HTML to be cached.
Definition: CachedAction.php:143
CachedAction\setExpiry
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
Definition: CachedAction.php:166
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
CacheHelper
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition: Action.php:53
CachedAction\$cacheHelper
CacheHelper $cacheHelper
CacheHelper object to which we forward the non-SpecialPage specific caching work.
Definition: CachedAction.php:55
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:268
CachedAction\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
Definition: CachedAction.php:92
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:239