MediaWiki  master
CacheHelper.php
Go to the documentation of this file.
1 <?php
26 
45 class CacheHelper implements ICacheHelper {
52  protected $cacheExpiry = 3600;
53 
62  protected $cachedChunks;
63 
71  protected $hasCached = null;
72 
79  protected $cacheEnabled = true;
80 
87  protected $onInitHandler = false;
88 
95  protected $cacheKey = [];
96 
103  public function setCacheEnabled( $cacheEnabled ) {
104  $this->cacheEnabled = $cacheEnabled;
105  }
106 
116  public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
117  if ( is_null( $this->hasCached ) ) {
118  if ( !is_null( $cacheExpiry ) ) {
119  $this->cacheExpiry = $cacheExpiry;
120  }
121 
122  if ( !is_null( $cacheEnabled ) ) {
123  $this->setCacheEnabled( $cacheEnabled );
124  }
125 
126  $this->initCaching();
127  }
128  }
129 
141  public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
142  if ( $this->cacheExpiry < 86400 * 3650 ) {
143  $message = $context->msg(
144  'cachedspecial-viewing-cached-ttl',
145  $context->getLanguage()->formatDuration( $this->cacheExpiry )
146  )->escaped();
147  } else {
148  $message = $context->msg(
149  'cachedspecial-viewing-cached-ts'
150  )->escaped();
151  }
152 
153  if ( $includePurgeLink ) {
154  $refreshArgs = $context->getRequest()->getQueryValues();
155  unset( $refreshArgs['title'] );
156  $refreshArgs['action'] = 'purge';
157 
158  $subPage = $context->getTitle()->getFullText();
159  $subPage = explode( '/', $subPage, 2 );
160  $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
161 
162  $message .= ' ' . MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
163  $context->getTitle( $subPage ),
164  $context->msg( 'cachedspecial-refresh-now' )->text(),
165  [],
166  $refreshArgs
167  );
168  }
169 
170  return $message;
171  }
172 
179  protected function initCaching() {
180  if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
182 
183  $this->hasCached = is_array( $cachedChunks );
184  $this->cachedChunks = $this->hasCached ? $cachedChunks : [];
185 
186  if ( $this->onInitHandler !== false ) {
187  call_user_func( $this->onInitHandler, $this->hasCached );
188  }
189  }
190  }
191 
206  public function getCachedValue( $computeFunction, $args = [], $key = null ) {
207  $this->initCaching();
208 
209  if ( $this->cacheEnabled && $this->hasCached ) {
210  $value = null;
211 
212  if ( is_null( $key ) ) {
213  reset( $this->cachedChunks );
214  $itemKey = key( $this->cachedChunks );
215 
216  if ( !is_int( $itemKey ) ) {
217  wfWarn( "Attempted to get item with non-numeric key while " .
218  "the next item in the queue has a key ($itemKey) in " . __METHOD__ );
219  } elseif ( is_null( $itemKey ) ) {
220  wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
221  } else {
222  $value = array_shift( $this->cachedChunks );
223  }
224  } elseif ( array_key_exists( $key, $this->cachedChunks ) ) {
225  $value = $this->cachedChunks[$key];
226  unset( $this->cachedChunks[$key] );
227  } else {
228  wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
229  }
230  } else {
231  if ( !is_array( $args ) ) {
232  $args = [ $args ];
233  }
234 
235  $value = $computeFunction( ...$args );
236 
237  if ( $this->cacheEnabled ) {
238  if ( is_null( $key ) ) {
239  $this->cachedChunks[] = $value;
240  } else {
241  $this->cachedChunks[$key] = $value;
242  }
243  }
244  }
245 
246  return $value;
247  }
248 
255  public function saveCache() {
256  if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
257  wfGetCache( CACHE_ANYTHING )->set(
258  $this->getCacheKeyString(),
259  $this->cachedChunks,
260  $this->cacheExpiry
261  );
262  }
263  }
264 
273  public function setExpiry( $cacheExpiry ) {
274  $this->cacheExpiry = $cacheExpiry;
275  }
276 
286  protected function getCacheKeyString() {
287  if ( $this->cacheKey === [] ) {
288  throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' );
289  }
290 
291  return ObjectCache::getLocalClusterInstance()->makeKey(
292  ...array_values( $this->cacheKey )
293  );
294  }
295 
303  public function setCacheKey( array $cacheKey ) {
304  $this->cacheKey = $cacheKey;
305  }
306 
314  public function rebuildOnDemand() {
315  $this->hasCached = false;
316  }
317 
325  public function setOnInitializedHandler( $handlerFunction ) {
326  $this->onInitHandler = $handlerFunction;
327  }
328 }
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
setCacheEnabled( $cacheEnabled)
Sets if the cache should be enabled or not.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetCache( $cacheType)
Get a specific cache object.
$value
int $cacheExpiry
The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
Definition: CacheHelper.php:52
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2151
static getLocalClusterInstance()
Get the main cluster-local cache object.
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
Helper class for caching various elements in a single cache entry.
Definition: CacheHelper.php:45
setExpiry( $cacheExpiry)
Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry...
bool null $hasCached
Indicates if the to be cached content was already cached.
Definition: CacheHelper.php:71
getCachedNotice(IContextSource $context, $includePurgeLink=true)
Returns a message that notifies the user he/she is looking at a cached version of the page...
if( $line===false) $args
Definition: cdb.php:64
initCaching()
Initializes the caching if not already done so.
callable $onInitHandler
Function that gets called when initialization is done.
Definition: CacheHelper.php:87
array $cachedChunks
List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
Definition: CacheHelper.php:62
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
setCacheKey(array $cacheKey)
Sets the cache key that should be used.
msg( $key)
This is the method for getting translated interface messages.
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
getCachedValue( $computeFunction, $args=[], $key=null)
Get a cached value if available or compute it if not and then cache it if possible.
rebuildOnDemand()
Rebuild the content, even if it&#39;s already cached.
const CACHE_ANYTHING
Definition: Defines.php:97
bool $cacheEnabled
If the cache is enabled or not.
Definition: CacheHelper.php:79
setOnInitializedHandler( $handlerFunction)
Sets a function that gets called when initialization of the cache is done.
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:2633
saveCache()
Saves the HTML to the cache in case it got recomputed.
getCacheKeyString()
Returns the cache key to use to cache this page&#39;s HTML output.
array $cacheKey
Elements to build a cache key with.
Definition: CacheHelper.php:95
Interface for all classes implementing CacheHelper functionality.
startCache( $cacheExpiry=null, $cacheEnabled=null)
Initializes the caching.