MediaWiki  1.30.1
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 = [], $key = null );
65 
72  function saveCache();
73 
82  function setExpiry( $cacheExpiry );
83 }
84 
86 
105 class CacheHelper implements ICacheHelper {
112  protected $cacheExpiry = 3600;
113 
122  protected $cachedChunks;
123 
131  protected $hasCached = null;
132 
139  protected $cacheEnabled = true;
140 
147  protected $onInitHandler = false;
148 
155  protected $cacheKey = [];
156 
163  public function setCacheEnabled( $cacheEnabled ) {
164  $this->cacheEnabled = $cacheEnabled;
165  }
166 
176  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
177  if ( is_null( $this->hasCached ) ) {
178  if ( !is_null( $cacheExpiry ) ) {
179  $this->cacheExpiry = $cacheExpiry;
180  }
181 
182  if ( !is_null( $cacheEnabled ) ) {
183  $this->setCacheEnabled( $cacheEnabled );
184  }
185 
186  $this->initCaching();
187  }
188  }
189 
201  public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
202  if ( $this->cacheExpiry < 86400 * 3650 ) {
203  $message = $context->msg(
204  'cachedspecial-viewing-cached-ttl',
205  $context->getLanguage()->formatDuration( $this->cacheExpiry )
206  )->escaped();
207  } else {
208  $message = $context->msg(
209  'cachedspecial-viewing-cached-ts'
210  )->escaped();
211  }
212 
213  if ( $includePurgeLink ) {
214  $refreshArgs = $context->getRequest()->getQueryValues();
215  unset( $refreshArgs['title'] );
216  $refreshArgs['action'] = 'purge';
217 
218  $subPage = $context->getTitle()->getFullText();
219  $subPage = explode( '/', $subPage, 2 );
220  $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
221 
222  $message .= ' ' . MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
223  $context->getTitle( $subPage ),
224  $context->msg( 'cachedspecial-refresh-now' )->text(),
225  [],
226  $refreshArgs
227  );
228  }
229 
230  return $message;
231  }
232 
239  protected function initCaching() {
240  if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
242 
243  $this->hasCached = is_array( $cachedChunks );
244  $this->cachedChunks = $this->hasCached ? $cachedChunks : [];
245 
246  if ( $this->onInitHandler !== false ) {
247  call_user_func( $this->onInitHandler, $this->hasCached );
248  }
249  }
250  }
251 
266  public function getCachedValue( $computeFunction, $args = [], $key = null ) {
267  $this->initCaching();
268 
269  if ( $this->cacheEnabled && $this->hasCached ) {
270  $value = null;
271 
272  if ( is_null( $key ) ) {
273  $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
274  $itemKey = array_shift( $itemKey );
275 
276  if ( !is_integer( $itemKey ) ) {
277  wfWarn( "Attempted to get item with non-numeric key while " .
278  "the next item in the queue has a key ($itemKey) in " . __METHOD__ );
279  } elseif ( is_null( $itemKey ) ) {
280  wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
281  } else {
282  $value = array_shift( $this->cachedChunks );
283  }
284  } else {
285  if ( array_key_exists( $key, $this->cachedChunks ) ) {
286  $value = $this->cachedChunks[$key];
287  unset( $this->cachedChunks[$key] );
288  } else {
289  wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
290  }
291  }
292  } else {
293  if ( !is_array( $args ) ) {
294  $args = [ $args ];
295  }
296 
297  $value = call_user_func_array( $computeFunction, $args );
298 
299  if ( $this->cacheEnabled ) {
300  if ( is_null( $key ) ) {
301  $this->cachedChunks[] = $value;
302  } else {
303  $this->cachedChunks[$key] = $value;
304  }
305  }
306  }
307 
308  return $value;
309  }
310 
317  public function saveCache() {
318  if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
319  wfGetCache( CACHE_ANYTHING )->set(
320  $this->getCacheKeyString(),
321  $this->cachedChunks,
322  $this->cacheExpiry
323  );
324  }
325  }
326 
335  public function setExpiry( $cacheExpiry ) {
336  $this->cacheExpiry = $cacheExpiry;
337  }
338 
348  protected function getCacheKeyString() {
349  if ( $this->cacheKey === [] ) {
350  throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' );
351  }
352 
353  return call_user_func_array( 'wfMemcKey', $this->cacheKey );
354  }
355 
363  public function setCacheKey( array $cacheKey ) {
364  $this->cacheKey = $cacheKey;
365  }
366 
374  public function rebuildOnDemand() {
375  $this->hasCached = false;
376  }
377 
385  public function setOnInitializedHandler( $handlerFunction ) {
386  $this->onInitHandler = $handlerFunction;
387  }
388 }
CacheHelper\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
Definition: CacheHelper.php:176
ICacheHelper\startCache
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2581
captcha-old.count
count
Definition: captcha-old.py:249
MediaWiki\getTitle
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
Definition: MediaWiki.php:137
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:201
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
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:335
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:163
php
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
CacheHelper\$cachedChunks
array $cachedChunks
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
Definition: CacheHelper.php:122
CacheHelper\$cacheExpiry
int $cacheExpiry
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
Definition: CacheHelper.php:112
MWException
MediaWiki exception.
Definition: MWException.php:26
CacheHelper\$hasCached
bool null $hasCached
Indicates if the to be cached content was already cached.
Definition: CacheHelper.php:131
CacheHelper\initCaching
initCaching()
Initializes the caching if not already done so.
Definition: CacheHelper.php:239
wfGetCache
wfGetCache( $cacheType)
Get a specific cache object.
Definition: GlobalFunctions.php:3195
CacheHelper\setCacheKey
setCacheKey(array $cacheKey)
Sets the cache key that should be used.
Definition: CacheHelper.php:363
CacheHelper\$onInitHandler
callable $onInitHandler
Function that gets called when initialization is done.
Definition: CacheHelper.php:147
$value
$value
Definition: styleTest.css.php:45
CacheHelper\getCachedValue
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
Definition: CacheHelper.php:266
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:102
CacheHelper\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
Definition: CacheHelper.php:317
CacheHelper\rebuildOnDemand
rebuildOnDemand()
Rebuild the content, even if it's already cached.
Definition: CacheHelper.php:374
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
CacheHelper
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:105
CacheHelper\$cacheEnabled
bool $cacheEnabled
If the cache is enabled or not.
Definition: CacheHelper.php:139
ICacheHelper\saveCache
saveCache()
Saves the HTML to the cache in case it got recomputed.
$args
if( $line===false) $args
Definition: cdb.php:63
CacheHelper\$cacheKey
array $cacheKey
Elements to build a cache key with.
Definition: CacheHelper.php:155
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:1190
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
ICacheHelper\setCacheEnabled
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
ICacheHelper\getCachedValue
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
CacheHelper\setOnInitializedHandler
setOnInitializedHandler( $handlerFunction)
Sets a function that gets called when initialization of the cache is done.
Definition: CacheHelper.php:385
array
the array() calling protocol came about after MediaWiki 1.4rc1.
CacheHelper\getCacheKeyString
getCacheKeyString()
Returns the cache key to use to cache this page's HTML output.
Definition: CacheHelper.php:348