68 return MediaWikiServices::getInstance()->getParserCache();
95 $pageid = $article->getId();
96 $renderkey = (int)(
$wgRequest->getVal(
'action' ) ==
'render' );
98 $key = $this->cache->makeKey(
'pcache',
'idhash',
"{$pageid}-{$renderkey}!{$hash}" );
107 return $this->cache->makeKey(
'pcache',
'idoptions', $page->getId() );
134 $popts->optionsHash( ParserOptions::allCacheVaryingOptions(), $article->getTitle() ) ) .
135 "--" . $article->getTouched() .
'"';
145 $value = $this->
get( $article, $popts,
true );
146 return is_object( $value ) ? $value :
false;
157 $contentModel = str_replace(
'.',
'_', $article->getContentModel() );
158 $metricSuffix = str_replace(
'.',
'_', $metricSuffix );
159 wfIncrStats(
'pcache.' . $contentModel .
'.' . $metricSuffix );
182 public function getKey( $article, $popts, $useOutdated = self::USE_ANYTHING ) {
183 if ( is_bool( $useOutdated ) ) {
184 $useOutdated = $useOutdated ? self::USE_ANYTHING : self::USE_CURRENT_ONLY;
187 if ( $popts instanceof
User ) {
188 wfWarn(
"Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
189 $popts = ParserOptions::newFromUser( $popts );
193 $optionsKey = $this->cache->get(
194 $this->
getOptionsKey( $article ), BagOStuff::READ_VERIFIED );
195 if ( $optionsKey instanceof
CacheTime ) {
196 if ( $useOutdated < self::USE_EXPIRED && $optionsKey->expired( $article->getTouched() ) ) {
198 $cacheTime = $optionsKey->getCacheTime();
200 "Parser options key expired, touched " . $article->getTouched()
201 .
", epoch {$this->cacheEpoch}, cached $cacheTime\n" );
203 } elseif ( $useOutdated < self::USE_OUTDATED &&
204 $optionsKey->isDifferentRevision( $article->getLatest() )
207 $revId = $article->getLatest();
208 $cachedRevId = $optionsKey->getCacheRevisionId();
210 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
216 $usedOptions = $optionsKey->mUsedOptions;
217 wfDebug(
"Parser cache options found.\n" );
219 if ( $useOutdated < self::USE_ANYTHING ) {
222 $usedOptions = ParserOptions::allCacheVaryingOptions();
227 $popts->optionsHash( $usedOptions, $article->getTitle() )
241 public function get( $article, $popts, $useOutdated = false ) {
242 $canCache = $article->checkTouched();
248 $touched = $article->getTouched();
250 $parserOutputKey = $this->
getKey( $article, $popts,
251 $useOutdated ? self::USE_OUTDATED : self::USE_CURRENT_ONLY
253 if ( $parserOutputKey ===
false ) {
260 $value = $this->cache->get( $parserOutputKey, BagOStuff::READ_VERIFIED );
262 wfDebug(
"ParserOutput cache miss.\n" );
267 wfDebug(
"ParserOutput cache found.\n" );
269 $wikiPage = method_exists( $article,
'getPage' )
270 ? $article->getPage()
273 if ( !$useOutdated && $value->expired( $touched ) ) {
275 $cacheTime = $value->getCacheTime();
277 "ParserOutput key expired, touched $touched, "
278 .
"epoch {$this->cacheEpoch}, cached $cacheTime\n" );
280 } elseif ( !$useOutdated && $value->isDifferentRevision( $article->getLatest() ) ) {
282 $revId = $article->getLatest();
283 $cachedRevId = $value->getCacheRevisionId();
285 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
289 Hooks::run(
'RejectParserCacheValue', [ $value, $wikiPage, $popts ] ) ===
false
293 "ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
317 if ( !$parserOutput->
hasText() ) {
318 throw new InvalidArgumentException(
'Attempt to cache a ParserOutput with no text set!' );
325 $revision = $page->getRevision();
326 $revId = $revision ? $revision->getId() :
null;
331 $optionsKey->updateCacheExpiry( $expire );
333 $optionsKey->setCacheTime( $cacheTime );
335 $optionsKey->setCacheRevisionId( $revId );
339 $popts->optionsHash( $optionsKey->mUsedOptions, $page->getTitle() ) );
344 $msg =
"Saved in parser cache with key $parserOutputKey" .
345 " and timestamp $cacheTime" .
346 " and revision id $revId" .
349 $parserOutput->mText .=
"\n<!-- $msg -->\n";
357 BagOStuff::WRITE_ALLOW_SEGMENTS
361 $this->cache->set( $this->
getOptionsKey( $page ), $optionsKey, $expire );
364 'ParserCacheSaveComplete',
365 [ $this, $parserOutput, $page->getTitle(), $popts, $revId ]
367 } elseif ( $expire <= 0 ) {
368 wfDebug(
"Parser output was marked as uncacheable and has not been saved.\n" );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfIncrStats( $key, $count=1)
Increment a statistics counter.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
if(! $wgDBerrorLogTZ) $wgRequest
Class representing a cache/ephemeral data store.
Parser cache specific expiry check.
setCacheTime( $t)
setCacheTime() sets the timestamp expressing when the page has been rendered.
getCacheExpiry()
Returns the number of seconds after which this object should expire.
A BagOStuff object with no objects in it.
save(ParserOutput $parserOutput, $page, $popts, $cacheTime=null, $revId=null)
getDirty( $article, $popts)
Retrieve the ParserOutput from ParserCache, even if it's outdated.
getParserOutputKey( $article, $hash)
const USE_EXPIRED
Use expired data if current data is unavailable.
__construct(BagOStuff $cache, $cacheEpoch='20030516000000')
Setup a cache pathway with a given back-end storage mechanism.
static singleton()
Get an instance of this object.
getCacheStorage()
Get the backend BagOStuff instance that powers the parser cache.
const USE_ANYTHING
Use expired data and data from different revisions, and if all else fails vary on all variable option...
string $cacheEpoch
Anything cached prior to this is invalidated.
const USE_CURRENT_ONLY
Constants for self::getKey()
getKey( $article, $popts, $useOutdated=self::USE_ANYTHING)
Generates a key for caching the given article considering the given parser options.
incrementStats( $article, $metricSuffix)
const USE_OUTDATED
Use expired data or data from different revisions if current data is unavailable.
getETag( $article, $popts)
Provides an E-Tag suitable for the whole page.
hasText()
Returns true if text was passed to the constructor, or set using setText().
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce this output.
setTimestamp( $timestamp)
The User object encapsulates all of the user-specific settings (user_id, name, rights,...