MediaWiki  1.23.0
CacheHelper.php
Go to the documentation of this file.
1 <?php
30 interface ICacheHelper {
37  function setCacheEnabled( $cacheEnabled );
38 
48  function startCache( $cacheExpiry = null, $cacheEnabled = null );
49 
64  function getCachedValue( $computeFunction, $args = array(), $key = null );
65 
72  function saveCache();
73 
82  function setExpiry( $cacheExpiry );
83 }
84 
103 class CacheHelper implements ICacheHelper {
110  protected $cacheExpiry = 3600;
111 
120  protected $cachedChunks;
121 
129  protected $hasCached = null;
130 
137  protected $cacheEnabled = true;
138 
145  protected $onInitHandler = false;
146 
153  protected $cacheKey = array();
154 
161  public function setCacheEnabled( $cacheEnabled ) {
162  $this->cacheEnabled = $cacheEnabled;
163  }
164 
174  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
175  if ( is_null( $this->hasCached ) ) {
176  if ( !is_null( $cacheExpiry ) ) {
177  $this->cacheExpiry = $cacheExpiry;
178  }
179 
180  if ( !is_null( $cacheEnabled ) ) {
181  $this->setCacheEnabled( $cacheEnabled );
182  }
183 
184  $this->initCaching();
185  }
186  }
187 
199  public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
200  if ( $this->cacheExpiry < 86400 * 3650 ) {
201  $message = $context->msg(
202  'cachedspecial-viewing-cached-ttl',
203  $context->getLanguage()->formatDuration( $this->cacheExpiry )
204  )->escaped();
205  } else {
206  $message = $context->msg(
207  'cachedspecial-viewing-cached-ts'
208  )->escaped();
209  }
210 
211  if ( $includePurgeLink ) {
212  $refreshArgs = $context->getRequest()->getQueryValues();
213  unset( $refreshArgs['title'] );
214  $refreshArgs['action'] = 'purge';
215 
216  $subPage = $context->getTitle()->getFullText();
217  $subPage = explode( '/', $subPage, 2 );
218  $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
219 
220  $message .= ' ' . Linker::link(
221  $context->getTitle( $subPage ),
222  $context->msg( 'cachedspecial-refresh-now' )->escaped(),
223  array(),
224  $refreshArgs
225  );
226  }
227 
228  return $message;
229  }
230 
237  protected function initCaching() {
238  if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
240 
241  $this->hasCached = is_array( $cachedChunks );
242  $this->cachedChunks = $this->hasCached ? $cachedChunks : array();
243 
244  if ( $this->onInitHandler !== false ) {
245  call_user_func( $this->onInitHandler, $this->hasCached );
246  }
247  }
248  }
249 
264  public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
265  $this->initCaching();
266 
267  if ( $this->cacheEnabled && $this->hasCached ) {
268  $value = null;
269 
270  if ( is_null( $key ) ) {
271  $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
272  $itemKey = array_shift( $itemKey );
273 
274  if ( !is_integer( $itemKey ) ) {
275  wfWarn( "Attempted to get item with non-numeric key while " .
276  "the next item in the queue has a key ($itemKey) in " . __METHOD__ );
277  } elseif ( is_null( $itemKey ) ) {
278  wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
279  } else {
280  $value = array_shift( $this->cachedChunks );
281  }
282  } else {
283  if ( array_key_exists( $key, $this->cachedChunks ) ) {
284  $value = $this->cachedChunks[$key];
285  unset( $this->cachedChunks[$key] );
286  } else {
287  wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
288  }
289  }
290  } else {
291  if ( !is_array( $args ) ) {
292  $args = array( $args );
293  }
294 
295  $value = call_user_func_array( $computeFunction, $args );
296 
297  if ( $this->cacheEnabled ) {
298  if ( is_null( $key ) ) {
299  $this->cachedChunks[] = $value;
300  } else {
301  $this->cachedChunks[$key] = $value;
302  }
303  }
304  }
305 
306  return $value;
307  }
308 
315  public function saveCache() {
316  if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
317  wfGetCache( CACHE_ANYTHING )->set(
318  $this->getCacheKeyString(),
319  $this->cachedChunks,
320  $this->cacheExpiry
321  );
322  }
323  }
324 
333  public function setExpiry( $cacheExpiry ) {
334  $this->cacheExpiry = $cacheExpiry;
335  }
336 
346  protected function getCacheKeyString() {
347  if ( $this->cacheKey === array() ) {
348  throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' );
349  }
350 
351  return call_user_func_array( 'wfMemcKey', $this->cacheKey );
352  }
353 
361  public function setCacheKey( array $cacheKey ) {
362  $this->cacheKey = $cacheKey;
363  }
364 
372  public function rebuildOnDemand() {
373  $this->hasCached = false;
374  }
375 
383  public function setOnInitializedHandler( $handlerFunction ) {
384  $this->onInitHandler = $handlerFunction;
385  }
386 }
CacheHelper\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
Definition: CacheHelper.php:168
ICacheHelper\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
CacheHelper\getCachedNotice
getCachedNotice(IContextSource $context, $includePurgeLink=true)
Returns a message that notifies the user he/she is looking at a cached version of the page,...
Definition: CacheHelper.php:193
ICacheHelper\setExpiry
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry....
CacheHelper\setExpiry
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry....
Definition: CacheHelper.php:327
IContextSource\msg
msg()
Get a Message object with context set.
wfGetCache
wfGetCache( $inputType)
Get a cache object.
Definition: GlobalFunctions.php:3948
ICacheHelper
Interface for all classes implementing CacheHelper functionality.
Definition: CacheHelper.php:30
CacheHelper\setCacheEnabled
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
Definition: CacheHelper.php:155
Linker\link
static link( $target, $html=null, $customAttribs=array(), $query=array(), $options=array())
This function returns an HTML link to the given target.
Definition: Linker.php:192
CacheHelper\$cachedChunks
array $cachedChunks
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
Definition: CacheHelper.php:118
MWException
MediaWiki exception.
Definition: MWException.php:26
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
CacheHelper\initCaching
initCaching()
Initializes the caching if not already done so.
Definition: CacheHelper.php:231
CacheHelper\setCacheKey
setCacheKey(array $cacheKey)
Sets the cache key that should be used.
Definition: CacheHelper.php:355
CacheHelper\$onInitHandler
callable $onInitHandler
Function that gets called when initialization is done.
Definition: CacheHelper.php:140
CacheHelper\$cacheExpiry
integer $cacheExpiry
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
Definition: CacheHelper.php:109
CacheHelper\$cacheEnabled
boolean $cacheEnabled
If the cache is enabled or not.
Definition: CacheHelper.php:133
$value
$value
Definition: styleTest.css.php:45
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:111
CacheHelper\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
Definition: CacheHelper.php:309
CacheHelper\rebuildOnDemand
rebuildOnDemand()
Rebuild the content, even if it's already cached.
Definition: CacheHelper.php:366
IContextSource\getTitle
getTitle()
Get the Title object.
IContextSource
Interface for objects which can provide a context on request.
Definition: IContextSource.php:29
CacheHelper
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:103
ICacheHelper\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
$args
if( $line===false) $args
Definition: cdb.php:62
ICacheHelper\getCachedValue
getCachedValue( $computeFunction, $args=array(), $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
CacheHelper\$cacheKey
array $cacheKey
Elements to build a cache key with.
Definition: CacheHelper.php:147
IContextSource\getRequest
getRequest()
Get the WebRequest object.
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1141
ICacheHelper\setCacheEnabled
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
CacheHelper\getCachedValue
getCachedValue( $computeFunction, $args=array(), $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
Definition: CacheHelper.php:258
CacheHelper\setOnInitializedHandler
setOnInitializedHandler( $handlerFunction)
Sets a function that gets called when initialization of the cache is done.
Definition: CacheHelper.php:377
IContextSource\getLanguage
getLanguage()
Get the Language object.
CacheHelper\$hasCached
boolean null $hasCached
Indicates if the to be cached content was already cached.
Definition: CacheHelper.php:126
CacheHelper\getCacheKeyString
getCacheKeyString()
Returns the cache key to use to cache this page's HTML output.
Definition: CacheHelper.php:340