MediaWiki  1.27.1
ParserCache.php
Go to the documentation of this file.
1 <?php
28 class ParserCache {
30  private $mMemc;
36  public static function singleton() {
37  static $instance;
38  if ( !isset( $instance ) ) {
40  $instance = new ParserCache( $parserMemc );
41  }
42  return $instance;
43  }
44 
54  protected function __construct( BagOStuff $memCached ) {
55  $this->mMemc = $memCached;
56  }
57 
63  protected function getParserOutputKey( $article, $hash ) {
65 
66  // idhash seem to mean 'page id' + 'rendering hash' (r3710)
67  $pageid = $article->getId();
68  $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
69 
70  $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
71  return $key;
72  }
73 
78  protected function getOptionsKey( $article ) {
79  $pageid = $article->getId();
80  return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
81  }
82 
97  public function getETag( $article, $popts ) {
98  return 'W/"' . $this->getParserOutputKey( $article,
99  $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
100  "--" . $article->getTouched() . '"';
101  }
102 
109  public function getDirty( $article, $popts ) {
110  $value = $this->get( $article, $popts, true );
111  return is_object( $value ) ? $value : false;
112  }
113 
133  public function getKey( $article, $popts, $useOutdated = true ) {
135 
136  if ( $popts instanceof User ) {
137  wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
138  $popts = ParserOptions::newFromUser( $popts );
139  }
140 
141  // Determine the options which affect this article
142  $casToken = null;
143  $optionsKey = $this->mMemc->get(
144  $this->getOptionsKey( $article ), $casToken, BagOStuff::READ_VERIFIED );
145  if ( $optionsKey instanceof CacheTime ) {
146  if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
147  wfIncrStats( "pcache.miss.expired" );
148  $cacheTime = $optionsKey->getCacheTime();
149  wfDebugLog( "ParserCache",
150  "Parser options key expired, touched " . $article->getTouched()
151  . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
152  return false;
153  } elseif ( !$useOutdated && $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
154  wfIncrStats( "pcache.miss.revid" );
155  $revId = $article->getLatest();
156  $cachedRevId = $optionsKey->getCacheRevisionId();
157  wfDebugLog( "ParserCache",
158  "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
159  );
160  return false;
161  }
162 
163  // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
164  $usedOptions = $optionsKey->mUsedOptions;
165  wfDebug( "Parser cache options found.\n" );
166  } else {
167  if ( !$useOutdated ) {
168  return false;
169  }
170  $usedOptions = ParserOptions::legacyOptions();
171  }
172 
173  return $this->getParserOutputKey(
174  $article,
175  $popts->optionsHash( $usedOptions, $article->getTitle() )
176  );
177  }
178 
189  public function get( $article, $popts, $useOutdated = false ) {
191 
192  $canCache = $article->checkTouched();
193  if ( !$canCache ) {
194  // It's a redirect now
195  return false;
196  }
197 
198  $touched = $article->getTouched();
199 
200  $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
201  if ( $parserOutputKey === false ) {
202  wfIncrStats( 'pcache.miss.absent' );
203  return false;
204  }
205 
206  $casToken = null;
208  $value = $this->mMemc->get( $parserOutputKey, $casToken, BagOStuff::READ_VERIFIED );
209  if ( !$value ) {
210  wfDebug( "ParserOutput cache miss.\n" );
211  wfIncrStats( "pcache.miss.absent" );
212  return false;
213  }
214 
215  wfDebug( "ParserOutput cache found.\n" );
216 
217  // The edit section preference may not be the appropiate one in
218  // the ParserOutput, as we are not storing it in the parsercache
219  // key. Force it here. See bug 31445.
220  $value->setEditSectionTokens( $popts->getEditSection() );
221 
222  $wikiPage = method_exists( $article, 'getPage' )
223  ? $article->getPage()
224  : $article;
225 
226  if ( !$useOutdated && $value->expired( $touched ) ) {
227  wfIncrStats( "pcache.miss.expired" );
228  $cacheTime = $value->getCacheTime();
229  wfDebugLog( "ParserCache",
230  "ParserOutput key expired, touched $touched, "
231  . "epoch $wgCacheEpoch, cached $cacheTime\n" );
232  $value = false;
233  } elseif ( !$useOutdated && $value->isDifferentRevision( $article->getLatest() ) ) {
234  wfIncrStats( "pcache.miss.revid" );
235  $revId = $article->getLatest();
236  $cachedRevId = $value->getCacheRevisionId();
237  wfDebugLog( "ParserCache",
238  "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
239  );
240  $value = false;
241  } elseif (
242  Hooks::run( 'RejectParserCacheValue', [ $value, $wikiPage, $popts ] ) === false
243  ) {
244  wfIncrStats( 'pcache.miss.rejected' );
245  wfDebugLog( "ParserCache",
246  "ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
247  );
248  $value = false;
249  } else {
250  wfIncrStats( "pcache.hit" );
251  }
252 
253  return $value;
254  }
255 
263  public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
264  $expire = $parserOutput->getCacheExpiry();
265  if ( $expire > 0 && !$this->mMemc instanceof EmptyBagOStuff ) {
266  $cacheTime = $cacheTime ?: wfTimestampNow();
267  if ( !$revId ) {
268  $revision = $page->getRevision();
269  $revId = $revision ? $revision->getId() : null;
270  }
271 
272  $optionsKey = new CacheTime;
273  $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
274  $optionsKey->updateCacheExpiry( $expire );
275 
276  $optionsKey->setCacheTime( $cacheTime );
277  $parserOutput->setCacheTime( $cacheTime );
278  $optionsKey->setCacheRevisionId( $revId );
279  $parserOutput->setCacheRevisionId( $revId );
280 
281  $parserOutputKey = $this->getParserOutputKey( $page,
282  $popts->optionsHash( $optionsKey->mUsedOptions, $page->getTitle() ) );
283 
284  // Save the timestamp so that we don't have to load the revision row on view
285  $parserOutput->setTimestamp( $page->getTimestamp() );
286 
287  $msg = "Saved in parser cache with key $parserOutputKey" .
288  " and timestamp $cacheTime" .
289  " and revision id $revId" .
290  "\n";
291 
292  $parserOutput->mText .= "\n<!-- $msg -->\n";
293  wfDebug( $msg );
294 
295  // Save the parser output
296  $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
297 
298  // ...and its pointer
299  $this->mMemc->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
300 
301  Hooks::run(
302  'ParserCacheSaveComplete',
303  [ $this, $parserOutput, $page->getTitle(), $popts, $revId ]
304  );
305  } elseif ( $expire <= 0 ) {
306  wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
307  }
308  }
309 }
static legacyOptions()
Returns the full array of options that would have been used by in 1.16.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
save($parserOutput, $page, $popts, $cacheTime=null, $revId=null)
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition: hooks.txt:78
$value
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context $revId
Definition: hooks.txt:1004
Parser cache specific expiry check.
Definition: CacheTime.php:29
getParserOutputKey($article, $hash)
Definition: ParserCache.php:63
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static newFromUser($user)
Get a ParserOptions object from a given user.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1798
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
getETag($article, $popts)
Provides an E-Tag suitable for the whole page.
Definition: ParserCache.php:97
getDirty($article, $popts)
Retrieve the ParserOutput from ParserCache, even if it's outdated.
const READ_VERIFIED
Definition: BagOStuff.php:81
wfWarn($msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
getKey($article, $popts, $useOutdated=true)
Generates a key for caching the given article considering the given parser options.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context $parserOutput
Definition: hooks.txt:1004
BagOStuff $mMemc
Definition: ParserCache.php:30
controlled by $wgMainCacheType * $parserMemc
Definition: memcached.txt:78
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
getOptionsKey($article)
Definition: ParserCache.php:78
wfIncrStats($key, $count=1)
Increment a statistics counter.
A BagOStuff object with no objects in it.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
__construct(BagOStuff $memCached)
Setup a cache pathway with a given back-end storage mechanism.
Definition: ParserCache.php:54
static singleton()
Get an instance of this object.
Definition: ParserCache.php:36
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
wfMemcKey()
Make a cache key for the local wiki.
$wgCacheEpoch
Set this to current time to invalidate all prior cached pages.
if(is_null($wgLocalTZoffset)) if(!$wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:657
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2338