Merge "Include parsed revision ID in parser cache"
[lhc/web/wiklou.git] / includes / parser / ParserCache.php
1 <?php
2 /**
3 * Cache for outputs of the PHP parser
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache Parser
22 */
23
24 /**
25 * @ingroup Cache Parser
26 * @todo document
27 */
28 class ParserCache {
29 private $mMemc;
30 /**
31 * Get an instance of this object
32 *
33 * @return ParserCache
34 */
35 public static function singleton() {
36 static $instance;
37 if ( !isset( $instance ) ) {
38 global $parserMemc;
39 $instance = new ParserCache( $parserMemc );
40 }
41 return $instance;
42 }
43
44 /**
45 * Setup a cache pathway with a given back-end storage mechanism.
46 * May be a memcached client or a BagOStuff derivative.
47 *
48 * @param $memCached Object
49 * @throws MWException
50 */
51 protected function __construct( $memCached ) {
52 if ( !$memCached ) {
53 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
54 }
55 $this->mMemc = $memCached;
56 }
57
58 /**
59 * @param $article Article
60 * @param $hash string
61 * @return mixed|string
62 */
63 protected function getParserOutputKey( $article, $hash ) {
64 global $wgRequest;
65
66 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
67 $pageid = $article->getID();
68 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
69
70 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
71 return $key;
72 }
73
74 /**
75 * @param $article Article
76 * @return mixed|string
77 */
78 protected function getOptionsKey( $article ) {
79 $pageid = $article->getID();
80 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
81 }
82
83 /**
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)
92 *
93 * @param $article Article
94 * @param $popts ParserOptions
95 * @return string
96 */
97 function getETag( $article, $popts ) {
98 return 'W/"' . $this->getParserOutputKey( $article,
99 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
100 "--" . $article->getTouched() . '"';
101 }
102
103 /**
104 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
105 * @param $article Article
106 * @param $popts ParserOptions
107 * @return ParserOutput|bool False on failure
108 */
109 public function getDirty( $article, $popts ) {
110 $value = $this->get( $article, $popts, true );
111 return is_object( $value ) ? $value : false;
112 }
113
114 /**
115 * Generates a key for caching the given article considering
116 * the given parser options.
117 *
118 * @note Which parser options influence the cache key
119 * is controlled via ParserOutput::recordOption() or
120 * ParserOptions::addExtraKey().
121 *
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.
125 *
126 * @todo Document parameter $useOutdated
127 *
128 * @param $article Article
129 * @param $popts ParserOptions
130 * @param $useOutdated Boolean (default true)
131 * @return bool|mixed|string
132 */
133 public function getKey( $article, $popts, $useOutdated = true ) {
134 global $wgCacheEpoch;
135
136 if ( $popts instanceof User ) {
137 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
138 $popts = ParserOptions::newFromUser( $popts );
139 }
140
141 // Determine the options which affect this article
142 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
143 if ( $optionsKey != false ) {
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() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
148 return false;
149 } elseif ( $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
150 wfIncrStats( "pcache_miss_revid" );
151 $revId = $article->getLatest();
152 $cachedRevId = $optionsKey->getCacheRevisionId();
153 wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
154 return false;
155 }
156
157 // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
158 $usedOptions = $optionsKey->mUsedOptions;
159 wfDebug( "Parser cache options found.\n" );
160 } else {
161 if ( !$useOutdated ) {
162 return false;
163 }
164 $usedOptions = ParserOptions::legacyOptions();
165 }
166
167 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
168 }
169
170 /**
171 * Retrieve the ParserOutput from ParserCache.
172 * false if not found or outdated.
173 *
174 * @param $article Article
175 * @param $popts ParserOptions
176 * @param $useOutdated Boolean (default false)
177 *
178 * @return ParserOutput|bool False on failure
179 */
180 public function get( $article, $popts, $useOutdated = false ) {
181 global $wgCacheEpoch;
182 wfProfileIn( __METHOD__ );
183
184 $canCache = $article->checkTouched();
185 if ( !$canCache ) {
186 // It's a redirect now
187 wfProfileOut( __METHOD__ );
188 return false;
189 }
190
191 $touched = $article->getTouched();
192
193 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
194 if ( $parserOutputKey === false ) {
195 wfIncrStats( 'pcache_miss_absent' );
196 wfProfileOut( __METHOD__ );
197 return false;
198 }
199
200 $value = $this->mMemc->get( $parserOutputKey );
201 if ( !$value ) {
202 wfDebug( "ParserOutput cache miss.\n" );
203 wfIncrStats( "pcache_miss_absent" );
204 wfProfileOut( __METHOD__ );
205 return false;
206 }
207
208 wfDebug( "ParserOutput cache found.\n" );
209
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() );
214
215 if ( !$useOutdated && $value->expired( $touched ) ) {
216 wfIncrStats( "pcache_miss_expired" );
217 $cacheTime = $value->getCacheTime();
218 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
219 $value = false;
220 } elseif ( $value->isDifferentRevision( $article->getLatest() ) ) {
221 wfIncrStats( "pcache_miss_revid" );
222 $revId = $article->getLatest();
223 $cachedRevId = $value->getCacheRevisionId();
224 wfDebug( "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n" );
225 $value = false;
226 } else {
227 wfIncrStats( "pcache_hit" );
228 }
229
230 wfProfileOut( __METHOD__ );
231 return $value;
232 }
233
234 /**
235 * @param ParserOutput $parserOutput
236 * @param WikiPage $page
237 * @param ParserOptions $popts
238 * @param string $cacheTime Time when the cache was generated
239 * @param int $revId Revision ID that was parsed
240 */
241 public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
242 $expire = $parserOutput->getCacheExpiry();
243 if ( $expire > 0 ) {
244 $cacheTime = $cacheTime ?: wfTimestampNow();
245 if ( !$revId ) {
246 $revision = $page->getRevision();
247 $revId = $revision ? $revision->getId() : null;
248 }
249
250 $optionsKey = new CacheTime;
251 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
252 $optionsKey->updateCacheExpiry( $expire );
253
254 $optionsKey->setCacheTime( $cacheTime );
255 $parserOutput->setCacheTime( $cacheTime );
256 $optionsKey->setCacheRevisionId( $revId );
257 $parserOutput->setCacheRevisionId( $revId );
258
259 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
260
261 $parserOutputKey = $this->getParserOutputKey( $page,
262 $popts->optionsHash( $optionsKey->mUsedOptions, $page->getTitle() ) );
263
264 // Save the timestamp so that we don't have to load the revision row on view
265 $parserOutput->setTimestamp( $page->getTimestamp() );
266
267 $msg = "Saved in parser cache with key $parserOutputKey" .
268 " and timestamp $cacheTime" .
269 " and revision id $revId" .
270 "\n";
271
272 $parserOutput->mText .= "\n<!-- $msg -->\n";
273 wfDebug( $msg );
274
275 // Save the parser output
276 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
277
278 // ...and its pointer
279 $this->mMemc->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
280 } else {
281 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
282 }
283 }
284 }