3 * Cache for outputs of the PHP parser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Cache Parser
24 use MediaWiki\MediaWikiServices
;
27 * @ingroup Cache Parser
32 * Constants for self::getKey()
36 /** Use only current data */
37 const USE_CURRENT_ONLY
= 0;
39 /** Use expired data if current data is unavailable */
40 const USE_EXPIRED
= 1;
42 /** Use expired data or data from different revisions if current data is unavailable */
43 const USE_OUTDATED
= 2;
46 * Use expired data and data from different revisions, and if all else
47 * fails vary on all variable options
49 const USE_ANYTHING
= 3;
55 * Anything cached prior to this is invalidated
61 * Get an instance of this object
63 * @deprecated since 1.30, use MediaWikiServices instead
66 public static function singleton() {
67 return MediaWikiServices
::getInstance()->getParserCache();
71 * Setup a cache pathway with a given back-end storage mechanism.
73 * This class use an invalidation strategy that is compatible with
74 * MultiWriteBagOStuff in async replication mode.
76 * @param BagOStuff $cache
77 * @param string $cacheEpoch Anything before this timestamp is invalidated
80 public function __construct( BagOStuff
$cache, $cacheEpoch = '20030516000000' ) {
81 $this->mMemc
= $cache;
82 $this->cacheEpoch
= $cacheEpoch;
86 * @param WikiPage $article
88 * @return mixed|string
90 protected function getParserOutputKey( $article, $hash ) {
93 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
94 $pageid = $article->getId();
95 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
97 $key = $this->mMemc
->makeKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
102 * @param WikiPage $page
103 * @return mixed|string
105 protected function getOptionsKey( $page ) {
106 return $this->mMemc
->makeKey( 'pcache', 'idoptions', $page->getId() );
110 * @param WikiPage $page
113 public function deleteOptionsKey( $page ) {
114 $this->mMemc
->delete( $this->getOptionsKey( $page ) );
118 * Provides an E-Tag suitable for the whole page. Note that $article
119 * is just the main wikitext. The E-Tag has to be unique to the whole
120 * page, even if the article itself is the same, so it uses the
121 * complete set of user options. We don't want to use the preference
122 * of a different user on a message just because it wasn't used in
123 * $article. For example give a Chinese interface to a user with
124 * English preferences. That's why we take into account *all* user
125 * options. (r70809 CR)
127 * @param WikiPage $article
128 * @param ParserOptions $popts
131 public function getETag( $article, $popts ) {
132 return 'W/"' . $this->getParserOutputKey( $article,
133 $popts->optionsHash( ParserOptions
::allCacheVaryingOptions(), $article->getTitle() ) ) .
134 "--" . $article->getTouched() . '"';
138 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
139 * @param WikiPage $article
140 * @param ParserOptions $popts
141 * @return ParserOutput|bool False on failure
143 public function getDirty( $article, $popts ) {
144 $value = $this->get( $article, $popts, true );
145 return is_object( $value ) ?
$value : false;
149 * Generates a key for caching the given article considering
150 * the given parser options.
152 * @note Which parser options influence the cache key
153 * is controlled via ParserOutput::recordOption() or
154 * ParserOptions::addExtraKey().
156 * @note Used by Article to provide a unique id for the PoolCounter.
157 * It would be preferable to have this code in get()
158 * instead of having Article looking in our internals.
160 * @param WikiPage $article
161 * @param ParserOptions $popts
162 * @param int|bool $useOutdated One of the USE constants. For backwards
163 * compatibility, boolean false is treated as USE_CURRENT_ONLY and
164 * boolean true is treated as USE_ANYTHING.
165 * @return bool|mixed|string
166 * @since 1.30 Changed $useOutdated to an int and added the non-boolean values
168 public function getKey( $article, $popts, $useOutdated = self
::USE_ANYTHING
) {
169 if ( is_bool( $useOutdated ) ) {
170 $useOutdated = $useOutdated ? self
::USE_ANYTHING
: self
::USE_CURRENT_ONLY
;
173 if ( $popts instanceof User
) {
174 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
175 $popts = ParserOptions
::newFromUser( $popts );
178 // Determine the options which affect this article
180 $optionsKey = $this->mMemc
->get(
181 $this->getOptionsKey( $article ), $casToken, BagOStuff
::READ_VERIFIED
);
182 if ( $optionsKey instanceof CacheTime
) {
183 if ( $useOutdated < self
::USE_EXPIRED
&& $optionsKey->expired( $article->getTouched() ) ) {
184 wfIncrStats( "pcache.miss.expired" );
185 $cacheTime = $optionsKey->getCacheTime();
186 wfDebugLog( "ParserCache",
187 "Parser options key expired, touched " . $article->getTouched()
188 . ", epoch {$this->cacheEpoch}, cached $cacheTime\n" );
190 } elseif ( $useOutdated < self
::USE_OUTDATED
&&
191 $optionsKey->isDifferentRevision( $article->getLatest() )
193 wfIncrStats( "pcache.miss.revid" );
194 $revId = $article->getLatest();
195 $cachedRevId = $optionsKey->getCacheRevisionId();
196 wfDebugLog( "ParserCache",
197 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
202 // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
203 $usedOptions = $optionsKey->mUsedOptions
;
204 wfDebug( "Parser cache options found.\n" );
206 if ( $useOutdated < self
::USE_ANYTHING
) {
209 $usedOptions = ParserOptions
::allCacheVaryingOptions();
212 return $this->getParserOutputKey(
214 $popts->optionsHash( $usedOptions, $article->getTitle() )
219 * Retrieve the ParserOutput from ParserCache.
220 * false if not found or outdated.
222 * @param WikiPage|Article $article
223 * @param ParserOptions $popts
224 * @param bool $useOutdated (default false)
226 * @return ParserOutput|bool False on failure
228 public function get( $article, $popts, $useOutdated = false ) {
229 $canCache = $article->checkTouched();
231 // It's a redirect now
235 $touched = $article->getTouched();
237 $parserOutputKey = $this->getKey( $article, $popts,
238 $useOutdated ? self
::USE_OUTDATED
: self
::USE_CURRENT_ONLY
240 if ( $parserOutputKey === false ) {
241 wfIncrStats( 'pcache.miss.absent' );
246 /** @var ParserOutput $value */
247 $value = $this->mMemc
->get( $parserOutputKey, $casToken, BagOStuff
::READ_VERIFIED
);
249 wfDebug( "ParserOutput cache miss.\n" );
250 wfIncrStats( "pcache.miss.absent" );
254 wfDebug( "ParserOutput cache found.\n" );
256 // The edit section preference may not be the appropiate one in
257 // the ParserOutput, as we are not storing it in the parsercache
258 // key. Force it here. See T33445.
259 $value->setEditSectionTokens( $popts->getEditSection() );
261 $wikiPage = method_exists( $article, 'getPage' )
262 ?
$article->getPage()
265 if ( !$useOutdated && $value->expired( $touched ) ) {
266 wfIncrStats( "pcache.miss.expired" );
267 $cacheTime = $value->getCacheTime();
268 wfDebugLog( "ParserCache",
269 "ParserOutput key expired, touched $touched, "
270 . "epoch {$this->cacheEpoch}, cached $cacheTime\n" );
272 } elseif ( !$useOutdated && $value->isDifferentRevision( $article->getLatest() ) ) {
273 wfIncrStats( "pcache.miss.revid" );
274 $revId = $article->getLatest();
275 $cachedRevId = $value->getCacheRevisionId();
276 wfDebugLog( "ParserCache",
277 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
281 Hooks
::run( 'RejectParserCacheValue', [ $value, $wikiPage, $popts ] ) === false
283 wfIncrStats( 'pcache.miss.rejected' );
284 wfDebugLog( "ParserCache",
285 "ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
289 wfIncrStats( "pcache.hit" );
296 * @param ParserOutput $parserOutput
297 * @param WikiPage $page
298 * @param ParserOptions $popts
299 * @param string $cacheTime Time when the cache was generated
300 * @param int $revId Revision ID that was parsed
302 public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
303 $expire = $parserOutput->getCacheExpiry();
304 if ( $expire > 0 && !$this->mMemc
instanceof EmptyBagOStuff
) {
305 $cacheTime = $cacheTime ?
: wfTimestampNow();
307 $revision = $page->getRevision();
308 $revId = $revision ?
$revision->getId() : null;
311 $optionsKey = new CacheTime
;
312 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
313 $optionsKey->updateCacheExpiry( $expire );
315 $optionsKey->setCacheTime( $cacheTime );
316 $parserOutput->setCacheTime( $cacheTime );
317 $optionsKey->setCacheRevisionId( $revId );
318 $parserOutput->setCacheRevisionId( $revId );
320 $parserOutputKey = $this->getParserOutputKey( $page,
321 $popts->optionsHash( $optionsKey->mUsedOptions
, $page->getTitle() ) );
323 // Save the timestamp so that we don't have to load the revision row on view
324 $parserOutput->setTimestamp( $page->getTimestamp() );
326 $msg = "Saved in parser cache with key $parserOutputKey" .
327 " and timestamp $cacheTime" .
328 " and revision id $revId" .
331 $parserOutput->mText
.= "\n<!-- $msg -->\n";
334 // Save the parser output
335 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
337 // ...and its pointer
338 $this->mMemc
->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
341 'ParserCacheSaveComplete',
342 [ $this, $parserOutput, $page->getTitle(), $popts, $revId ]
344 } elseif ( $expire <= 0 ) {
345 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
350 * Get the backend BagOStuff instance that
351 * powers the parser cache
356 public function getCacheStorage() {