4 * Abstract special page class with scaffolding for caching the HTML output.
6 * To add HTML that should be cached, use addCachedHTML like this:
7 * $this->addCachedHTML( $callback );
9 * The callback function is only called when needed, so do all your expensive
10 * computations here. This function should returns the HTML to be cached.
11 * It should not add anything to the PageOutput object!
13 * Before the first addCachedHTML call, you should call $this->startCache();
14 * After adding the last HTML that should be cached, call $this->saveCache();
18 * @file SpecialCachedPage.php
19 * @ingroup SpecialPage
21 * @licence GNU GPL v2 or later
22 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
24 abstract class SpecialCachedPage
extends SpecialPage
{
27 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
32 protected $cacheExpiry = 3600;
35 * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached).
36 * If no cached already, then the newly computed chunks are added here,
37 * if it as cached already, chunks are removed from this list as they are needed.
42 protected $cachedChunks;
45 * Indicates if the to be cached content was already cached.
46 * Null if this information is not available yet.
51 protected $hasCached = null;
54 * If the cache is enabled or not.
59 protected $cacheEnabled = true;
62 * Sets if the cache should be enabled or not.
65 * @param boolean $cacheEnabled
67 public function setCacheEnabled( $cacheEnabled ) {
68 $this->cacheEnabled
= $cacheEnabled;
72 * Initializes the caching.
73 * Should be called before the first time anything is added via addCachedHTML.
77 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
78 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
80 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
81 if ( is_null( $this->hasCached
) ) {
82 if ( !is_null( $cacheExpiry ) ) {
83 $this->cacheExpiry
= $cacheExpiry;
86 if ( !is_null( $cacheEnabled ) ) {
87 $this->setCacheEnabled( $cacheEnabled );
90 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
91 $this->hasCached
= false;
99 * Returns a message that notifies the user he/she is looking at
100 * a cached version of the page, including a refresh link.
106 protected function getCachedNotice() {
107 $refreshArgs = $this->getRequest()->getQueryValues();
108 unset( $refreshArgs['title'] );
109 $refreshArgs['action'] = 'purge';
111 $refreshLink = Linker
::link(
112 $this->getTitle( $this->getTitle()->getSubpageText() ),
113 $this->msg( 'cachedspecial-refresh-now' )->escaped(),
118 if ( $this->cacheExpiry
< 86400 * 3650 ) {
119 $message = $this->msg(
120 'cachedspecial-viewing-cached-ttl',
121 $this->getLanguage()->formatDuration( $this->cacheExpiry
)
125 $message = $this->msg(
126 'cachedspecial-viewing-cached-ts'
130 return $message . ' ' . $refreshLink;
134 * Initializes the caching if not already done so.
135 * Should be called before any of the caching functionality is used.
139 protected function initCaching() {
140 if ( $this->cacheEnabled
&& is_null( $this->hasCached
) ) {
141 $cachedChunks = wfGetCache( CACHE_ANYTHING
)->get( $this->getCacheKeyString() );
143 $this->hasCached
= is_array( $cachedChunks );
144 $this->cachedChunks
= $this->hasCached ?
$cachedChunks : array();
146 $this->onCacheInitialized();
151 * Gets called after the cache got initialized.
155 protected function onCacheInitialized() {
156 if ( $this->hasCached
) {
157 $this->getOutput()->setSubtitle( $this->getCachedNotice() );
162 * Add some HTML to be cached.
163 * This is done by providing a callback function that should
164 * return the HTML to be added. It will only be called if the
165 * item is not in the cache yet or when the cache has been invalidated.
169 * @param {function} $computeFunction
171 * @param string|null $key
173 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
174 $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
178 * Get a cached value if available or compute it if not and then cache it if possible.
179 * The provided $computeFunction is only called when the computation needs to happen
180 * and should return a result value. $args are arguments that will be passed to the
181 * compute function when called.
185 * @param {function} $computeFunction
186 * @param array|mixed $args
187 * @param string|null $key
191 protected function getCachedValue( $computeFunction, $args = array(), $key = null ) {
192 $this->initCaching();
194 if ( $this->cacheEnabled
&& $this->hasCached
) {
197 if ( is_null( $key ) ) {
198 $itemKey = array_keys( array_slice( $this->cachedChunks
, 0, 1 ) );
199 $itemKey = array_shift( $itemKey );
201 if ( !is_integer( $itemKey ) ) {
202 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__
);
204 elseif ( is_null( $itemKey ) ) {
205 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__
);
208 $value = array_shift( $this->cachedChunks
);
212 if ( array_key_exists( $key, $this->cachedChunks
) ) {
213 $value = $this->cachedChunks
[$key];
214 unset( $this->cachedChunks
[$key] );
217 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__
);
222 if ( !is_array( $args ) ) {
223 $args = array( $args );
226 $value = call_user_func_array( $computeFunction, $args );
228 if ( $this->cacheEnabled
) {
229 if ( is_null( $key ) ) {
230 $this->cachedChunks
[] = $value;
233 $this->cachedChunks
[$key] = $value;
242 * Saves the HTML to the cache in case it got recomputed.
243 * Should be called after the last time anything is added via addCachedHTML.
247 public function saveCache() {
248 if ( $this->cacheEnabled
&& $this->hasCached
=== false && !empty( $this->cachedChunks
) ) {
249 wfGetCache( CACHE_ANYTHING
)->set( $this->getCacheKeyString(), $this->cachedChunks
, $this->cacheExpiry
);
254 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
258 * @param integer $cacheExpiry
260 protected function setExpirey( $cacheExpiry ) {
261 $this->cacheExpiry
= $cacheExpiry;
265 * Returns the cache key to use to cache this page's HTML output.
266 * Is constructed from the special page name and language code.
272 protected function getCacheKeyString() {
273 $keyArgs = $this->getCacheKey();
275 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
276 unset( $keyArgs['action'] );
279 return call_user_func_array( 'wfMemcKey', $keyArgs );
283 * Returns the variables used to constructed the cache key in an array.
289 protected function getCacheKey() {
292 $this->getLanguage()->getCode()