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
25 * @ingroup Cache Parser
32 * Get an instance of this object
36 public static function singleton() {
38 if ( !isset( $instance ) ) {
40 $instance = new ParserCache( $parserMemc );
46 * Setup a cache pathway with a given back-end storage mechanism.
48 * This class use an invalidation strategy that is compatible with
49 * MultiWriteBagOStuff in async replication mode.
51 * @param BagOStuff $memCached
54 protected function __construct( BagOStuff
$memCached ) {
55 $this->mMemc
= $memCached;
59 * @param WikiPage $article
61 * @return mixed|string
63 protected function getParserOutputKey( $article, $hash ) {
66 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
67 $pageid = $article->getID();
68 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
70 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
75 * @param WikiPage $article
76 * @return mixed|string
78 protected function getOptionsKey( $article ) {
79 $pageid = $article->getID();
80 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
84 * Provides an E-Tag suitable for the whole page. Note that $article
85 * is just the main wikitext. The E-Tag has to be unique to the whole
86 * page, even if the article itself is the same, so it uses the
87 * complete set of user options. We don't want to use the preference
88 * of a different user on a message just because it wasn't used in
89 * $article. For example give a Chinese interface to a user with
90 * English preferences. That's why we take into account *all* user
91 * options. (r70809 CR)
93 * @param WikiPage $article
94 * @param ParserOptions $popts
97 public function getETag( $article, $popts ) {
98 return 'W/"' . $this->getParserOutputKey( $article,
99 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) ) .
100 "--" . $article->getTouched() . '"';
104 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
105 * @param WikiPage $article
106 * @param ParserOptions $popts
107 * @return ParserOutput|bool False on failure
109 public function getDirty( $article, $popts ) {
110 $value = $this->get( $article, $popts, true );
111 return is_object( $value ) ?
$value : false;
115 * Generates a key for caching the given article considering
116 * the given parser options.
118 * @note Which parser options influence the cache key
119 * is controlled via ParserOutput::recordOption() or
120 * ParserOptions::addExtraKey().
122 * @note Used by Article to provide a unique id for the PoolCounter.
123 * It would be preferable to have this code in get()
124 * instead of having Article looking in our internals.
126 * @todo Document parameter $useOutdated
128 * @param WikiPage $article
129 * @param ParserOptions $popts
130 * @param bool $useOutdated (default true)
131 * @return bool|mixed|string
133 public function getKey( $article, $popts, $useOutdated = true ) {
134 global $wgCacheEpoch;
136 if ( $popts instanceof User
) {
137 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
138 $popts = ParserOptions
::newFromUser( $popts );
141 // Determine the options which affect this article
142 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
143 if ( $optionsKey instanceof CacheTime
) {
144 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
145 wfIncrStats( "pcache.miss.expired" );
146 $cacheTime = $optionsKey->getCacheTime();
147 wfDebug( "Parser options key expired, touched " . $article->getTouched()
148 . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
150 } elseif ( $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
151 wfIncrStats( "pcache.miss.revid" );
152 $revId = $article->getLatest();
153 $cachedRevId = $optionsKey->getCacheRevisionId();
154 wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
158 // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
159 $usedOptions = $optionsKey->mUsedOptions
;
160 wfDebug( "Parser cache options found.\n" );
162 if ( !$useOutdated ) {
165 $usedOptions = ParserOptions
::legacyOptions();
168 return $this->getParserOutputKey(
170 $popts->optionsHash( $usedOptions, $article->getTitle() )
175 * Retrieve the ParserOutput from ParserCache.
176 * false if not found or outdated.
178 * @param WikiPage|Article $article
179 * @param ParserOptions $popts
180 * @param bool $useOutdated (default false)
182 * @return ParserOutput|bool False on failure
184 public function get( $article, $popts, $useOutdated = false ) {
185 global $wgCacheEpoch;
187 $canCache = $article->checkTouched();
189 // It's a redirect now
193 $touched = $article->getTouched();
195 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
196 if ( $parserOutputKey === false ) {
197 wfIncrStats( 'pcache.miss.absent' );
201 $value = $this->mMemc
->get( $parserOutputKey );
203 wfDebug( "ParserOutput cache miss.\n" );
204 wfIncrStats( "pcache.miss.absent" );
208 wfDebug( "ParserOutput cache found.\n" );
210 // The edit section preference may not be the appropiate one in
211 // the ParserOutput, as we are not storing it in the parsercache
212 // key. Force it here. See bug 31445.
213 $value->setEditSectionTokens( $popts->getEditSection() );
215 $wikiPage = method_exists( $article, 'getPage' )
216 ?
$article->getPage()
219 if ( !$useOutdated && $value->expired( $touched ) ) {
220 wfIncrStats( "pcache.miss.expired" );
221 $cacheTime = $value->getCacheTime();
222 wfDebug( "ParserOutput key expired, touched $touched, "
223 . "epoch $wgCacheEpoch, cached $cacheTime\n" );
225 } elseif ( $value->isDifferentRevision( $article->getLatest() ) ) {
226 wfIncrStats( "pcache.miss.revid" );
227 $revId = $article->getLatest();
228 $cachedRevId = $value->getCacheRevisionId();
230 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
234 Hooks
::run( 'RejectParserCacheValue', array( $value, $wikiPage, $popts ) ) === false
236 wfIncrStats( 'pcache.miss.rejected' );
238 "ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
242 wfIncrStats( "pcache.hit" );
249 * @param ParserOutput $parserOutput
250 * @param WikiPage $page
251 * @param ParserOptions $popts
252 * @param string $cacheTime Time when the cache was generated
253 * @param int $revId Revision ID that was parsed
255 public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
256 $expire = $parserOutput->getCacheExpiry();
258 $cacheTime = $cacheTime ?
: wfTimestampNow();
260 $revision = $page->getRevision();
261 $revId = $revision ?
$revision->getId() : null;
264 $optionsKey = new CacheTime
;
265 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
266 $optionsKey->updateCacheExpiry( $expire );
268 $optionsKey->setCacheTime( $cacheTime );
269 $parserOutput->setCacheTime( $cacheTime );
270 $optionsKey->setCacheRevisionId( $revId );
271 $parserOutput->setCacheRevisionId( $revId );
273 $parserOutputKey = $this->getParserOutputKey( $page,
274 $popts->optionsHash( $optionsKey->mUsedOptions
, $page->getTitle() ) );
276 // Save the timestamp so that we don't have to load the revision row on view
277 $parserOutput->setTimestamp( $page->getTimestamp() );
279 $msg = "Saved in parser cache with key $parserOutputKey" .
280 " and timestamp $cacheTime" .
281 " and revision id $revId" .
284 $parserOutput->mText
.= "\n<!-- $msg -->\n";
287 // Save the parser output
288 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
290 // ...and its pointer
291 $this->mMemc
->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
294 'ParserCacheSaveComplete',
295 array( $this, $parserOutput, $page->getTitle(), $popts, $revId )
298 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );