MediaWiki REL1_36
CachedAction.php
Go to the documentation of this file.
1<?php
46abstract 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}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
getName()
Return the name of the action this object responds to.
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition Action.php:53
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:228
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition Action.php:66
getArticle()
Get a Article object.
Definition Action.php:278
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:257
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:218
Helper class for caching various elements in a single cache entry.
Abstract action class with scaffolding for caching HTML and other values in a single blob.
CacheHelper $cacheHelper
CacheHelper object to which we forward the non-SpecialPage specific caching work.
addCachedHTML( $computeFunction, $args=[], $key=null)
Add some HTML to be cached.
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
saveCache()
Saves the HTML to the cache in case it got recomputed.
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
bool $cacheEnabled
If the cache is enabled or not.
onCacheInitialized( $hasCached)
Gets called after the cache got initialized.
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
__construct(Page $page, IContextSource $context=null)
Only public since 1.21.
getCacheKey()
Returns the variables used to constructed the cache key in an array.
An action which just does something, without showing a form first.
Interface for all classes implementing CacheHelper functionality.
Interface for objects which can provide a MediaWiki context on request.
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition Page.php:29
if( $line===false) $args
Definition mcc.php:124