Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / parser / ParserCache.php
blobad131f4a926e29fca5b054af7b16e76b507b1d98
1 <?php
2 /**
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
20 * @file
21 * @ingroup Cache Parser
24 /**
25 * @ingroup Cache Parser
26 * @todo document
28 class ParserCache {
29 /** @var MWMemcached */
30 private $mMemc;
31 /**
32 * Get an instance of this object
34 * @return ParserCache
36 public static function singleton() {
37 static $instance;
38 if ( !isset( $instance ) ) {
39 global $parserMemc;
40 $instance = new ParserCache( $parserMemc );
42 return $instance;
45 /**
46 * Setup a cache pathway with a given back-end storage mechanism.
47 * May be a memcached client or a BagOStuff derivative.
49 * @param MWMemcached $memCached
50 * @throws MWException
52 protected function __construct( $memCached ) {
53 if ( !$memCached ) {
54 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
56 $this->mMemc = $memCached;
59 /**
60 * @param Article $article
61 * @param string $hash
62 * @return mixed|string
64 protected function getParserOutputKey( $article, $hash ) {
65 global $wgRequest;
67 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
68 $pageid = $article->getID();
69 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
71 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
72 return $key;
75 /**
76 * @param Article $article
77 * @return mixed|string
79 protected function getOptionsKey( $article ) {
80 $pageid = $article->getID();
81 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
84 /**
85 * Provides an E-Tag suitable for the whole page. Note that $article
86 * is just the main wikitext. The E-Tag has to be unique to the whole
87 * page, even if the article itself is the same, so it uses the
88 * complete set of user options. We don't want to use the preference
89 * of a different user on a message just because it wasn't used in
90 * $article. For example give a Chinese interface to a user with
91 * English preferences. That's why we take into account *all* user
92 * options. (r70809 CR)
94 * @param Article $article
95 * @param ParserOptions $popts
96 * @return string
98 public function getETag( $article, $popts ) {
99 return 'W/"' . $this->getParserOutputKey( $article,
100 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
101 "--" . $article->getTouched() . '"';
105 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
106 * @param Article $article
107 * @param ParserOptions $popts
108 * @return ParserOutput|bool False on failure
110 public function getDirty( $article, $popts ) {
111 $value = $this->get( $article, $popts, true );
112 return is_object( $value ) ? $value : false;
116 * Generates a key for caching the given article considering
117 * the given parser options.
119 * @note Which parser options influence the cache key
120 * is controlled via ParserOutput::recordOption() or
121 * ParserOptions::addExtraKey().
123 * @note Used by Article to provide a unique id for the PoolCounter.
124 * It would be preferable to have this code in get()
125 * instead of having Article looking in our internals.
127 * @todo Document parameter $useOutdated
129 * @param Article $article
130 * @param ParserOptions $popts
131 * @param bool $useOutdated (default true)
132 * @return bool|mixed|string
134 public function getKey( $article, $popts, $useOutdated = true ) {
135 global $wgCacheEpoch;
137 if ( $popts instanceof User ) {
138 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
139 $popts = ParserOptions::newFromUser( $popts );
142 // Determine the options which affect this article
143 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
144 if ( $optionsKey != false ) {
145 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
146 wfIncrStats( "pcache_miss_expired" );
147 $cacheTime = $optionsKey->getCacheTime();
148 wfDebug( "Parser options key expired, touched " . $article->getTouched()
149 . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
150 return false;
151 } elseif ( $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
152 wfIncrStats( "pcache_miss_revid" );
153 $revId = $article->getLatest();
154 $cachedRevId = $optionsKey->getCacheRevisionId();
155 wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
156 return false;
159 // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
160 $usedOptions = $optionsKey->mUsedOptions;
161 wfDebug( "Parser cache options found.\n" );
162 } else {
163 if ( !$useOutdated ) {
164 return false;
166 $usedOptions = ParserOptions::legacyOptions();
169 return $this->getParserOutputKey(
170 $article,
171 $popts->optionsHash( $usedOptions, $article->getTitle() )
176 * Retrieve the ParserOutput from ParserCache.
177 * false if not found or outdated.
179 * @param Article $article
180 * @param ParserOptions $popts
181 * @param bool $useOutdated (default false)
183 * @return ParserOutput|bool False on failure
185 public function get( $article, $popts, $useOutdated = false ) {
186 global $wgCacheEpoch;
188 $canCache = $article->checkTouched();
189 if ( !$canCache ) {
190 // It's a redirect now
191 return false;
194 $touched = $article->getTouched();
196 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
197 if ( $parserOutputKey === false ) {
198 wfIncrStats( 'pcache_miss_absent' );
199 return false;
202 $value = $this->mMemc->get( $parserOutputKey );
203 if ( !$value ) {
204 wfDebug( "ParserOutput cache miss.\n" );
205 wfIncrStats( "pcache_miss_absent" );
206 return false;
209 wfDebug( "ParserOutput cache found.\n" );
211 // The edit section preference may not be the appropiate one in
212 // the ParserOutput, as we are not storing it in the parsercache
213 // key. Force it here. See bug 31445.
214 $value->setEditSectionTokens( $popts->getEditSection() );
216 if ( !$useOutdated && $value->expired( $touched ) ) {
217 wfIncrStats( "pcache_miss_expired" );
218 $cacheTime = $value->getCacheTime();
219 wfDebug( "ParserOutput key expired, touched $touched, "
220 . "epoch $wgCacheEpoch, cached $cacheTime\n" );
221 $value = false;
222 } elseif ( $value->isDifferentRevision( $article->getLatest() ) ) {
223 wfIncrStats( "pcache_miss_revid" );
224 $revId = $article->getLatest();
225 $cachedRevId = $value->getCacheRevisionId();
226 wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
227 $value = false;
228 } else {
229 wfIncrStats( "pcache_hit" );
232 return $value;
236 * @param ParserOutput $parserOutput
237 * @param WikiPage $page
238 * @param ParserOptions $popts
239 * @param string $cacheTime Time when the cache was generated
240 * @param int $revId Revision ID that was parsed
242 public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
243 $expire = $parserOutput->getCacheExpiry();
244 if ( $expire > 0 ) {
245 $cacheTime = $cacheTime ?: wfTimestampNow();
246 if ( !$revId ) {
247 $revision = $page->getRevision();
248 $revId = $revision ? $revision->getId() : null;
251 $optionsKey = new CacheTime;
252 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
253 $optionsKey->updateCacheExpiry( $expire );
255 $optionsKey->setCacheTime( $cacheTime );
256 $parserOutput->setCacheTime( $cacheTime );
257 $optionsKey->setCacheRevisionId( $revId );
258 $parserOutput->setCacheRevisionId( $revId );
260 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
262 $parserOutputKey = $this->getParserOutputKey( $page,
263 $popts->optionsHash( $optionsKey->mUsedOptions, $page->getTitle() ) );
265 // Save the timestamp so that we don't have to load the revision row on view
266 $parserOutput->setTimestamp( $page->getTimestamp() );
268 $msg = "Saved in parser cache with key $parserOutputKey" .
269 " and timestamp $cacheTime" .
270 " and revision id $revId" .
271 "\n";
273 $parserOutput->mText .= "\n<!-- $msg -->\n";
274 wfDebug( $msg );
276 // Save the parser output
277 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
279 // ...and its pointer
280 $this->mMemc->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
281 } else {
282 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );