2004-02-26 13:37:26 +00:00
|
|
|
<?php
|
2010-08-22 14:31:05 +00:00
|
|
|
/**
|
|
|
|
|
* Cache for outputs of the PHP parser
|
|
|
|
|
*
|
2012-04-30 09:22:16 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-22 14:31:05 +00:00
|
|
|
* @file
|
2012-04-30 09:22:16 +00:00
|
|
|
* @ingroup Cache Parser
|
2010-08-22 14:31:05 +00:00
|
|
|
*/
|
|
|
|
|
|
2017-05-30 00:10:16 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Cache Parser
|
2007-04-04 05:22:37 +00:00
|
|
|
* @todo document
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-11-22 01:16:44 +00:00
|
|
|
class ParserCache {
|
2017-06-21 16:21:45 +00:00
|
|
|
/**
|
|
|
|
|
* Constants for self::getKey()
|
|
|
|
|
* @since 1.30
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** Use only current data */
|
|
|
|
|
const USE_CURRENT_ONLY = 0;
|
|
|
|
|
|
|
|
|
|
/** Use expired data if current data is unavailable */
|
|
|
|
|
const USE_EXPIRED = 1;
|
|
|
|
|
|
|
|
|
|
/** Use expired data or data from different revisions if current data is unavailable */
|
|
|
|
|
const USE_OUTDATED = 2;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use expired data and data from different revisions, and if all else
|
|
|
|
|
* fails vary on all variable options
|
|
|
|
|
*/
|
|
|
|
|
const USE_ANYTHING = 3;
|
|
|
|
|
|
2015-08-21 00:41:27 +00:00
|
|
|
/** @var BagOStuff */
|
2019-06-18 15:35:27 +00:00
|
|
|
private $cache;
|
2017-05-30 00:10:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Anything cached prior to this is invalidated
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $cacheEpoch;
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2006-01-05 04:26:52 +00:00
|
|
|
/**
|
|
|
|
|
* Get an instance of this object
|
2011-07-24 21:36:04 +00:00
|
|
|
*
|
2017-05-30 00:10:16 +00:00
|
|
|
* @deprecated since 1.30, use MediaWikiServices instead
|
2011-07-24 21:36:04 +00:00
|
|
|
* @return ParserCache
|
2006-01-05 04:26:52 +00:00
|
|
|
*/
|
Implemented the PoolCounter feature and did some general refactoring in the areas that it touched.
* Renamed Article::outputFromWikitext() to Article::getOutputFromWikitext()
* Factored out cascade protection updates
* Removed recently-added Article::tryParserCache(): misnamed, can be done in one line of code in the caller. Deprecated OutputPage::tryParserCache().
* Made some functions public instead of protected when they could be useful from hooks.
* In ParserCache, removed PHP 4-style ampersands
In Article::view():
* Factored out robot policy logic, "redirected from" header, patrol footer, diff page, revdelete header, CSS/JS formatting, footer, namespace header, missing article error
* Removed some variables, renamed some others, fixed incorrect use of empty()
* Used the refactored footer section to do a couple of early returns and unindent a massive if(!$outputDone) block
* Removed fantasy interpretation of $this->getContent()===false in comment
* Don't try the parser cache when ArticleViewHeader specified $outputDone=true
* Move timing hack to getOutputFromWikitext()
* Stop using $wgOut->parserOptions() with save/restore nonsense every time you want to change something in it. This is meant to be OOP.
* Don't overwrite the article text with an error message and then pretend to write it to the cache, that's confusing
2009-07-08 08:12:35 +00:00
|
|
|
public static function singleton() {
|
2017-05-30 00:10:16 +00:00
|
|
|
return MediaWikiServices::getInstance()->getParserCache();
|
2006-01-05 04:26:52 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-11-22 01:16:44 +00:00
|
|
|
/**
|
|
|
|
|
* Setup a cache pathway with a given back-end storage mechanism.
|
2015-08-21 06:53:52 +00:00
|
|
|
*
|
|
|
|
|
* This class use an invalidation strategy that is compatible with
|
|
|
|
|
* MultiWriteBagOStuff in async replication mode.
|
2004-11-22 01:16:44 +00:00
|
|
|
*
|
2017-05-30 00:10:16 +00:00
|
|
|
* @param BagOStuff $cache
|
|
|
|
|
* @param string $cacheEpoch Anything before this timestamp is invalidated
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2004-11-22 01:16:44 +00:00
|
|
|
*/
|
2017-05-30 00:10:16 +00:00
|
|
|
public function __construct( BagOStuff $cache, $cacheEpoch = '20030516000000' ) {
|
2019-06-18 15:35:27 +00:00
|
|
|
$this->cache = $cache;
|
2017-05-30 00:10:16 +00:00
|
|
|
$this->cacheEpoch = $cacheEpoch;
|
2004-11-22 01:16:44 +00:00
|
|
|
}
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2011-02-19 01:02:56 +00:00
|
|
|
/**
|
2015-05-02 06:07:18 +00:00
|
|
|
* @param WikiPage $article
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param string $hash
|
2011-02-19 01:02:56 +00:00
|
|
|
* @return mixed|string
|
|
|
|
|
*/
|
2010-08-09 21:53:21 +00:00
|
|
|
protected function getParserOutputKey( $article, $hash ) {
|
2009-03-18 23:27:48 +00:00
|
|
|
global $wgRequest;
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
// idhash seem to mean 'page id' + 'rendering hash' (r3710)
|
2016-03-18 13:55:54 +00:00
|
|
|
$pageid = $article->getId();
|
2013-04-20 15:38:24 +00:00
|
|
|
$renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2019-06-18 15:35:27 +00:00
|
|
|
$key = $this->cache->makeKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
|
2004-05-27 15:24:04 +00:00
|
|
|
return $key;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2005-07-01 00:03:31 +00:00
|
|
|
|
2011-02-19 01:02:56 +00:00
|
|
|
/**
|
2016-09-11 18:49:42 +00:00
|
|
|
* @param WikiPage $page
|
2011-02-19 01:02:56 +00:00
|
|
|
* @return mixed|string
|
|
|
|
|
*/
|
2016-09-11 18:49:42 +00:00
|
|
|
protected function getOptionsKey( $page ) {
|
2019-06-18 15:35:27 +00:00
|
|
|
return $this->cache->makeKey( 'pcache', 'idoptions', $page->getId() );
|
2016-09-11 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param WikiPage $page
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public function deleteOptionsKey( $page ) {
|
2019-06-18 15:35:27 +00:00
|
|
|
$this->cache->delete( $this->getOptionsKey( $page ) );
|
2010-08-09 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-10 12:10:49 +00:00
|
|
|
/**
|
2010-09-12 19:26:01 +00:00
|
|
|
* Provides an E-Tag suitable for the whole page. Note that $article
|
|
|
|
|
* is just the main wikitext. The E-Tag has to be unique to the whole
|
|
|
|
|
* page, even if the article itself is the same, so it uses the
|
|
|
|
|
* complete set of user options. We don't want to use the preference
|
|
|
|
|
* of a different user on a message just because it wasn't used in
|
|
|
|
|
* $article. For example give a Chinese interface to a user with
|
|
|
|
|
* English preferences. That's why we take into account *all* user
|
2010-08-23 21:54:45 +00:00
|
|
|
* options. (r70809 CR)
|
2011-02-19 01:02:56 +00:00
|
|
|
*
|
2015-05-02 06:07:18 +00:00
|
|
|
* @param WikiPage $article
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param ParserOptions $popts
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2010-08-10 12:10:49 +00:00
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function getETag( $article, $popts ) {
|
2010-09-12 19:26:01 +00:00
|
|
|
return 'W/"' . $this->getParserOutputKey( $article,
|
2017-06-21 16:21:45 +00:00
|
|
|
$popts->optionsHash( ParserOptions::allCacheVaryingOptions(), $article->getTitle() ) ) .
|
2010-12-14 17:49:01 +00:00
|
|
|
"--" . $article->getTouched() . '"';
|
2005-07-01 00:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
/**
|
|
|
|
|
* Retrieve the ParserOutput from ParserCache, even if it's outdated.
|
2015-05-02 06:07:18 +00:00
|
|
|
* @param WikiPage $article
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param ParserOptions $popts
|
2012-02-10 15:37:33 +00:00
|
|
|
* @return ParserOutput|bool False on failure
|
2010-08-09 21:53:21 +00:00
|
|
|
*/
|
|
|
|
|
public function getDirty( $article, $popts ) {
|
2010-09-15 16:58:44 +00:00
|
|
|
$value = $this->get( $article, $popts, true );
|
Implemented the PoolCounter feature and did some general refactoring in the areas that it touched.
* Renamed Article::outputFromWikitext() to Article::getOutputFromWikitext()
* Factored out cascade protection updates
* Removed recently-added Article::tryParserCache(): misnamed, can be done in one line of code in the caller. Deprecated OutputPage::tryParserCache().
* Made some functions public instead of protected when they could be useful from hooks.
* In ParserCache, removed PHP 4-style ampersands
In Article::view():
* Factored out robot policy logic, "redirected from" header, patrol footer, diff page, revdelete header, CSS/JS formatting, footer, namespace header, missing article error
* Removed some variables, renamed some others, fixed incorrect use of empty()
* Used the refactored footer section to do a couple of early returns and unindent a massive if(!$outputDone) block
* Removed fantasy interpretation of $this->getContent()===false in comment
* Don't try the parser cache when ArticleViewHeader specified $outputDone=true
* Move timing hack to getOutputFromWikitext()
* Stop using $wgOut->parserOptions() with save/restore nonsense every time you want to change something in it. This is meant to be OOP.
* Don't overwrite the article text with an error message and then pretend to write it to the cache, that's confusing
2009-07-08 08:12:35 +00:00
|
|
|
return is_object( $value ) ? $value : false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 16:58:04 +00:00
|
|
|
/**
|
|
|
|
|
* @param WikiPage $article
|
|
|
|
|
* @param string $metricSuffix
|
|
|
|
|
*/
|
|
|
|
|
private function incrementStats( $article, $metricSuffix ) {
|
|
|
|
|
// old style global metric (can be removed once no longer used)
|
|
|
|
|
wfIncrStats( 'pcache.' . $metricSuffix );
|
|
|
|
|
// new per content model metric
|
|
|
|
|
$contentModel = str_replace( '.', '_', $article->getContentModel() );
|
|
|
|
|
$metricSuffix = str_replace( '.', '_', $metricSuffix );
|
|
|
|
|
wfIncrStats( 'pcache.' . $contentModel . '.' . $metricSuffix );
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
/**
|
2013-12-02 13:20:26 +00:00
|
|
|
* Generates a key for caching the given article considering
|
|
|
|
|
* the given parser options.
|
|
|
|
|
*
|
|
|
|
|
* @note Which parser options influence the cache key
|
|
|
|
|
* is controlled via ParserOutput::recordOption() or
|
|
|
|
|
* ParserOptions::addExtraKey().
|
|
|
|
|
*
|
|
|
|
|
* @note Used by Article to provide a unique id for the PoolCounter.
|
2010-09-12 19:26:01 +00:00
|
|
|
* It would be preferable to have this code in get()
|
2010-08-09 21:53:21 +00:00
|
|
|
* instead of having Article looking in our internals.
|
2011-02-19 01:02:56 +00:00
|
|
|
*
|
2015-05-02 06:07:18 +00:00
|
|
|
* @param WikiPage $article
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param ParserOptions $popts
|
2017-06-21 16:21:45 +00:00
|
|
|
* @param int|bool $useOutdated One of the USE constants. For backwards
|
|
|
|
|
* compatibility, boolean false is treated as USE_CURRENT_ONLY and
|
|
|
|
|
* boolean true is treated as USE_ANYTHING.
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return bool|mixed|string
|
2017-06-21 16:21:45 +00:00
|
|
|
* @since 1.30 Changed $useOutdated to an int and added the non-boolean values
|
2010-08-09 21:53:21 +00:00
|
|
|
*/
|
2017-06-21 16:21:45 +00:00
|
|
|
public function getKey( $article, $popts, $useOutdated = self::USE_ANYTHING ) {
|
|
|
|
|
if ( is_bool( $useOutdated ) ) {
|
|
|
|
|
$useOutdated = $useOutdated ? self::USE_ANYTHING : self::USE_CURRENT_ONLY;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( $popts instanceof User ) {
|
2010-08-10 19:32:49 +00:00
|
|
|
wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
|
2010-08-10 14:11:51 +00:00
|
|
|
$popts = ParserOptions::newFromUser( $popts );
|
|
|
|
|
}
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
// Determine the options which affect this article
|
2019-06-18 15:35:27 +00:00
|
|
|
$optionsKey = $this->cache->get(
|
2019-03-12 06:39:29 +00:00
|
|
|
$this->getOptionsKey( $article ), BagOStuff::READ_VERIFIED );
|
2015-06-10 21:09:20 +00:00
|
|
|
if ( $optionsKey instanceof CacheTime ) {
|
2017-06-21 16:21:45 +00:00
|
|
|
if ( $useOutdated < self::USE_EXPIRED && $optionsKey->expired( $article->getTouched() ) ) {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, "miss.expired" );
|
2010-08-09 21:53:21 +00:00
|
|
|
$cacheTime = $optionsKey->getCacheTime();
|
2016-04-27 03:25:38 +00:00
|
|
|
wfDebugLog( "ParserCache",
|
|
|
|
|
"Parser options key expired, touched " . $article->getTouched()
|
2017-05-30 00:10:16 +00:00
|
|
|
. ", epoch {$this->cacheEpoch}, cached $cacheTime\n" );
|
2010-08-09 21:53:21 +00:00
|
|
|
return false;
|
2017-06-21 16:21:45 +00:00
|
|
|
} elseif ( $useOutdated < self::USE_OUTDATED &&
|
|
|
|
|
$optionsKey->isDifferentRevision( $article->getLatest() )
|
|
|
|
|
) {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, "miss.revid" );
|
2014-04-01 14:52:24 +00:00
|
|
|
$revId = $article->getLatest();
|
|
|
|
|
$cachedRevId = $optionsKey->getCacheRevisionId();
|
2016-04-27 03:25:38 +00:00
|
|
|
wfDebugLog( "ParserCache",
|
|
|
|
|
"ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
|
|
|
|
|
);
|
2014-04-01 14:52:24 +00:00
|
|
|
return false;
|
2010-08-09 21:53:21 +00:00
|
|
|
}
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2013-12-02 13:20:26 +00:00
|
|
|
// $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
|
2010-08-09 21:53:21 +00:00
|
|
|
$usedOptions = $optionsKey->mUsedOptions;
|
|
|
|
|
wfDebug( "Parser cache options found.\n" );
|
|
|
|
|
} else {
|
2017-06-21 16:21:45 +00:00
|
|
|
if ( $useOutdated < self::USE_ANYTHING ) {
|
2011-03-07 19:13:19 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2017-06-21 16:21:45 +00:00
|
|
|
$usedOptions = ParserOptions::allCacheVaryingOptions();
|
2010-08-09 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-10 23:03:45 +00:00
|
|
|
return $this->getParserOutputKey(
|
|
|
|
|
$article,
|
|
|
|
|
$popts->optionsHash( $usedOptions, $article->getTitle() )
|
|
|
|
|
);
|
2010-08-09 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the ParserOutput from ParserCache.
|
|
|
|
|
* false if not found or outdated.
|
2011-05-28 18:59:42 +00:00
|
|
|
*
|
2015-06-23 21:57:52 +00:00
|
|
|
* @param WikiPage|Article $article
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param ParserOptions $popts
|
|
|
|
|
* @param bool $useOutdated (default false)
|
2011-05-28 18:59:42 +00:00
|
|
|
*
|
2012-02-10 15:37:33 +00:00
|
|
|
* @return ParserOutput|bool False on failure
|
2010-08-09 21:53:21 +00:00
|
|
|
*/
|
|
|
|
|
public function get( $article, $popts, $useOutdated = false ) {
|
|
|
|
|
$canCache = $article->checkTouched();
|
|
|
|
|
if ( !$canCache ) {
|
|
|
|
|
// It's a redirect now
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-14 17:49:01 +00:00
|
|
|
$touched = $article->getTouched();
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2017-06-21 16:21:45 +00:00
|
|
|
$parserOutputKey = $this->getKey( $article, $popts,
|
|
|
|
|
$useOutdated ? self::USE_OUTDATED : self::USE_CURRENT_ONLY
|
|
|
|
|
);
|
2010-08-09 21:53:21 +00:00
|
|
|
if ( $parserOutputKey === false ) {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, 'miss.absent' );
|
2010-08-09 21:53:21 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 07:43:40 +00:00
|
|
|
$casToken = null;
|
2016-04-27 22:43:38 +00:00
|
|
|
/** @var ParserOutput $value */
|
2019-06-18 15:35:27 +00:00
|
|
|
$value = $this->cache->get( $parserOutputKey, BagOStuff::READ_VERIFIED );
|
Implemented the PoolCounter feature and did some general refactoring in the areas that it touched.
* Renamed Article::outputFromWikitext() to Article::getOutputFromWikitext()
* Factored out cascade protection updates
* Removed recently-added Article::tryParserCache(): misnamed, can be done in one line of code in the caller. Deprecated OutputPage::tryParserCache().
* Made some functions public instead of protected when they could be useful from hooks.
* In ParserCache, removed PHP 4-style ampersands
In Article::view():
* Factored out robot policy logic, "redirected from" header, patrol footer, diff page, revdelete header, CSS/JS formatting, footer, namespace header, missing article error
* Removed some variables, renamed some others, fixed incorrect use of empty()
* Used the refactored footer section to do a couple of early returns and unindent a massive if(!$outputDone) block
* Removed fantasy interpretation of $this->getContent()===false in comment
* Don't try the parser cache when ArticleViewHeader specified $outputDone=true
* Move timing hack to getOutputFromWikitext()
* Stop using $wgOut->parserOptions() with save/restore nonsense every time you want to change something in it. This is meant to be OOP.
* Don't overwrite the article text with an error message and then pretend to write it to the cache, that's confusing
2009-07-08 08:12:35 +00:00
|
|
|
if ( !$value ) {
|
2011-11-08 09:08:15 +00:00
|
|
|
wfDebug( "ParserOutput cache miss.\n" );
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, "miss.absent" );
|
Implemented the PoolCounter feature and did some general refactoring in the areas that it touched.
* Renamed Article::outputFromWikitext() to Article::getOutputFromWikitext()
* Factored out cascade protection updates
* Removed recently-added Article::tryParserCache(): misnamed, can be done in one line of code in the caller. Deprecated OutputPage::tryParserCache().
* Made some functions public instead of protected when they could be useful from hooks.
* In ParserCache, removed PHP 4-style ampersands
In Article::view():
* Factored out robot policy logic, "redirected from" header, patrol footer, diff page, revdelete header, CSS/JS formatting, footer, namespace header, missing article error
* Removed some variables, renamed some others, fixed incorrect use of empty()
* Used the refactored footer section to do a couple of early returns and unindent a massive if(!$outputDone) block
* Removed fantasy interpretation of $this->getContent()===false in comment
* Don't try the parser cache when ArticleViewHeader specified $outputDone=true
* Move timing hack to getOutputFromWikitext()
* Stop using $wgOut->parserOptions() with save/restore nonsense every time you want to change something in it. This is meant to be OOP.
* Don't overwrite the article text with an error message and then pretend to write it to the cache, that's confusing
2009-07-08 08:12:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 09:08:15 +00:00
|
|
|
wfDebug( "ParserOutput cache found.\n" );
|
|
|
|
|
|
2015-06-23 21:57:52 +00:00
|
|
|
$wikiPage = method_exists( $article, 'getPage' )
|
|
|
|
|
? $article->getPage()
|
|
|
|
|
: $article;
|
|
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
if ( !$useOutdated && $value->expired( $touched ) ) {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, "miss.expired" );
|
2010-08-10 14:11:51 +00:00
|
|
|
$cacheTime = $value->getCacheTime();
|
2016-04-27 03:25:38 +00:00
|
|
|
wfDebugLog( "ParserCache",
|
|
|
|
|
"ParserOutput key expired, touched $touched, "
|
2017-05-30 00:10:16 +00:00
|
|
|
. "epoch {$this->cacheEpoch}, cached $cacheTime\n" );
|
2004-05-27 15:24:04 +00:00
|
|
|
$value = false;
|
2016-04-26 06:28:38 +00:00
|
|
|
} elseif ( !$useOutdated && $value->isDifferentRevision( $article->getLatest() ) ) {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, "miss.revid" );
|
2014-04-01 14:52:24 +00:00
|
|
|
$revId = $article->getLatest();
|
|
|
|
|
$cachedRevId = $value->getCacheRevisionId();
|
2016-04-27 03:25:38 +00:00
|
|
|
wfDebugLog( "ParserCache",
|
2015-09-26 17:17:49 +00:00
|
|
|
"ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
|
|
|
|
|
);
|
2014-04-01 14:52:24 +00:00
|
|
|
$value = false;
|
2015-09-26 17:17:49 +00:00
|
|
|
} elseif (
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'RejectParserCacheValue', [ $value, $wikiPage, $popts ] ) === false
|
2015-09-26 17:17:49 +00:00
|
|
|
) {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, 'miss.rejected' );
|
2016-04-27 03:25:38 +00:00
|
|
|
wfDebugLog( "ParserCache",
|
2015-09-26 17:17:49 +00:00
|
|
|
"ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
|
|
|
|
|
);
|
2015-06-23 05:09:53 +00:00
|
|
|
$value = false;
|
Implemented the PoolCounter feature and did some general refactoring in the areas that it touched.
* Renamed Article::outputFromWikitext() to Article::getOutputFromWikitext()
* Factored out cascade protection updates
* Removed recently-added Article::tryParserCache(): misnamed, can be done in one line of code in the caller. Deprecated OutputPage::tryParserCache().
* Made some functions public instead of protected when they could be useful from hooks.
* In ParserCache, removed PHP 4-style ampersands
In Article::view():
* Factored out robot policy logic, "redirected from" header, patrol footer, diff page, revdelete header, CSS/JS formatting, footer, namespace header, missing article error
* Removed some variables, renamed some others, fixed incorrect use of empty()
* Used the refactored footer section to do a couple of early returns and unindent a massive if(!$outputDone) block
* Removed fantasy interpretation of $this->getContent()===false in comment
* Don't try the parser cache when ArticleViewHeader specified $outputDone=true
* Move timing hack to getOutputFromWikitext()
* Stop using $wgOut->parserOptions() with save/restore nonsense every time you want to change something in it. This is meant to be OOP.
* Don't overwrite the article text with an error message and then pretend to write it to the cache, that's confusing
2009-07-08 08:12:35 +00:00
|
|
|
} else {
|
2019-01-24 16:58:04 +00:00
|
|
|
$this->incrementStats( $article, "hit" );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-05-28 05:45:13 +00:00
|
|
|
|
2004-05-27 15:24:04 +00:00
|
|
|
return $value;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2005-07-01 00:03:31 +00:00
|
|
|
|
2011-02-19 01:02:56 +00:00
|
|
|
/**
|
2013-11-16 21:11:49 +00:00
|
|
|
* @param ParserOutput $parserOutput
|
2013-12-30 16:11:52 +00:00
|
|
|
* @param WikiPage $page
|
2013-11-16 21:11:49 +00:00
|
|
|
* @param ParserOptions $popts
|
2018-04-18 12:31:13 +00:00
|
|
|
* @param string|null $cacheTime TS_MW timestamp when the cache was generated
|
|
|
|
|
* @param int|null $revId Revision ID that was parsed
|
2011-02-19 01:02:56 +00:00
|
|
|
*/
|
2018-07-27 20:20:39 +00:00
|
|
|
public function save(
|
|
|
|
|
ParserOutput $parserOutput,
|
|
|
|
|
$page,
|
|
|
|
|
$popts,
|
|
|
|
|
$cacheTime = null,
|
|
|
|
|
$revId = null
|
|
|
|
|
) {
|
2018-08-13 20:33:31 +00:00
|
|
|
if ( !$parserOutput->hasText() ) {
|
|
|
|
|
throw new InvalidArgumentException( 'Attempt to cache a ParserOutput with no text set!' );
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-01 14:28:51 +00:00
|
|
|
$expire = $parserOutput->getCacheExpiry();
|
2019-06-18 15:35:27 +00:00
|
|
|
if ( $expire > 0 && !$this->cache instanceof EmptyBagOStuff ) {
|
2013-09-24 20:55:29 +00:00
|
|
|
$cacheTime = $cacheTime ?: wfTimestampNow();
|
2014-04-01 14:52:24 +00:00
|
|
|
if ( !$revId ) {
|
|
|
|
|
$revision = $page->getRevision();
|
|
|
|
|
$revId = $revision ? $revision->getId() : null;
|
|
|
|
|
}
|
2010-08-09 21:53:21 +00:00
|
|
|
|
2010-09-12 19:26:01 +00:00
|
|
|
$optionsKey = new CacheTime;
|
2010-12-26 19:21:45 +00:00
|
|
|
$optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
|
2010-08-09 21:53:21 +00:00
|
|
|
$optionsKey->updateCacheExpiry( $expire );
|
2010-09-12 19:26:01 +00:00
|
|
|
|
2013-09-24 20:55:29 +00:00
|
|
|
$optionsKey->setCacheTime( $cacheTime );
|
|
|
|
|
$parserOutput->setCacheTime( $cacheTime );
|
2014-04-01 14:52:24 +00:00
|
|
|
$optionsKey->setCacheRevisionId( $revId );
|
|
|
|
|
$parserOutput->setCacheRevisionId( $revId );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2013-12-30 16:11:52 +00:00
|
|
|
$parserOutputKey = $this->getParserOutputKey( $page,
|
|
|
|
|
$popts->optionsHash( $optionsKey->mUsedOptions, $page->getTitle() ) );
|
2010-08-09 21:53:21 +00:00
|
|
|
|
2006-05-13 17:40:59 +00:00
|
|
|
// Save the timestamp so that we don't have to load the revision row on view
|
2013-12-30 16:11:52 +00:00
|
|
|
$parserOutput->setTimestamp( $page->getTimestamp() );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2014-04-01 14:52:24 +00:00
|
|
|
$msg = "Saved in parser cache with key $parserOutputKey" .
|
|
|
|
|
" and timestamp $cacheTime" .
|
|
|
|
|
" and revision id $revId" .
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
$parserOutput->mText .= "\n<!-- $msg -->\n";
|
|
|
|
|
wfDebug( $msg );
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
// Save the parser output
|
2019-06-18 15:35:27 +00:00
|
|
|
$this->cache->set(
|
|
|
|
|
$parserOutputKey,
|
|
|
|
|
$parserOutput,
|
|
|
|
|
$expire,
|
|
|
|
|
BagOStuff::WRITE_ALLOW_SEGMENTS
|
|
|
|
|
);
|
2007-04-21 21:35:21 +00:00
|
|
|
|
2010-08-09 21:53:21 +00:00
|
|
|
// ...and its pointer
|
2019-06-18 15:35:27 +00:00
|
|
|
$this->cache->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
|
2015-05-20 12:15:17 +00:00
|
|
|
|
2015-09-26 17:17:49 +00:00
|
|
|
Hooks::run(
|
|
|
|
|
'ParserCacheSaveComplete',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, $parserOutput, $page->getTitle(), $popts, $revId ]
|
2015-09-26 17:17:49 +00:00
|
|
|
);
|
2016-04-27 00:45:52 +00:00
|
|
|
} elseif ( $expire <= 0 ) {
|
2006-05-13 17:40:59 +00:00
|
|
|
wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-30 00:10:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the backend BagOStuff instance that
|
|
|
|
|
* powers the parser cache
|
|
|
|
|
*
|
|
|
|
|
* @since 1.30
|
|
|
|
|
* @return BagOStuff
|
|
|
|
|
*/
|
|
|
|
|
public function getCacheStorage() {
|
2019-06-18 15:35:27 +00:00
|
|
|
return $this->cache;
|
2017-05-30 00:10:16 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|