Document parser cache key control.
[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 const try116cache = false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
31
32 /**
33 * Get an instance of this object
34 *
35 * @return ParserCache
36 */
37 public static function singleton() {
38 static $instance;
39 if ( !isset( $instance ) ) {
40 global $parserMemc;
41 $instance = new ParserCache( $parserMemc );
42 }
43 return $instance;
44 }
45
46 /**
47 * Setup a cache pathway with a given back-end storage mechanism.
48 * May be a memcached client or a BagOStuff derivative.
49 *
50 * @param $memCached Object
51 * @throws MWException
52 */
53 protected function __construct( $memCached ) {
54 if ( !$memCached ) {
55 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
56 }
57 $this->mMemc = $memCached;
58 }
59
60 /**
61 * @param $article Article
62 * @param $hash string
63 * @return mixed|string
64 */
65 protected function getParserOutputKey( $article, $hash ) {
66 global $wgRequest;
67
68 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
69 $pageid = $article->getID();
70 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
71
72 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
73 return $key;
74 }
75
76 /**
77 * @param $article Article
78 * @return mixed|string
79 */
80 protected function getOptionsKey( $article ) {
81 $pageid = $article->getID();
82 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
83 }
84
85 /**
86 * Provides an E-Tag suitable for the whole page. Note that $article
87 * is just the main wikitext. The E-Tag has to be unique to the whole
88 * page, even if the article itself is the same, so it uses the
89 * complete set of user options. We don't want to use the preference
90 * of a different user on a message just because it wasn't used in
91 * $article. For example give a Chinese interface to a user with
92 * English preferences. That's why we take into account *all* user
93 * options. (r70809 CR)
94 *
95 * @param $article Article
96 * @param $popts ParserOptions
97 * @return string
98 */
99 function getETag( $article, $popts ) {
100 return 'W/"' . $this->getParserOutputKey( $article,
101 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
102 "--" . $article->getTouched() . '"';
103 }
104
105 /**
106 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
107 * @param $article Article
108 * @param $popts ParserOptions
109 * @return ParserOutput|bool False on failure
110 */
111 public function getDirty( $article, $popts ) {
112 $value = $this->get( $article, $popts, true );
113 return is_object( $value ) ? $value : false;
114 }
115
116 /**
117 * Generates a key for caching the given article considering
118 * the given parser options.
119 *
120 * @note Which parser options influence the cache key
121 * is controlled via ParserOutput::recordOption() or
122 * ParserOptions::addExtraKey().
123 *
124 * @note Used by Article to provide a unique id for the PoolCounter.
125 * It would be preferable to have this code in get()
126 * instead of having Article looking in our internals.
127 *
128 * @todo Document parameter $useOutdated
129 *
130 * @param $article Article
131 * @param $popts ParserOptions
132 * @param $useOutdated Boolean (default true)
133 * @return bool|mixed|string
134 */
135 public function getKey( $article, $popts, $useOutdated = true ) {
136 global $wgCacheEpoch;
137
138 if ( $popts instanceof User ) {
139 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
140 $popts = ParserOptions::newFromUser( $popts );
141 }
142
143 // Determine the options which affect this article
144 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
145 if ( $optionsKey != false ) {
146 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
147 wfIncrStats( "pcache_miss_expired" );
148 $cacheTime = $optionsKey->getCacheTime();
149 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
150 return false;
151 }
152
153 // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
154 $usedOptions = $optionsKey->mUsedOptions;
155 wfDebug( "Parser cache options found.\n" );
156 } else {
157 if ( !$useOutdated && !self::try116cache ) {
158 return false;
159 }
160 $usedOptions = ParserOptions::legacyOptions();
161 }
162
163 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
164 }
165
166 /**
167 * Retrieve the ParserOutput from ParserCache.
168 * false if not found or outdated.
169 *
170 * @param $article Article
171 * @param $popts ParserOptions
172 * @param $useOutdated Boolean (default false)
173 *
174 * @return ParserOutput|bool False on failure
175 */
176 public function get( $article, $popts, $useOutdated = false ) {
177 global $wgCacheEpoch;
178 wfProfileIn( __METHOD__ );
179
180 $canCache = $article->checkTouched();
181 if ( !$canCache ) {
182 // It's a redirect now
183 wfProfileOut( __METHOD__ );
184 return false;
185 }
186
187 $touched = $article->getTouched();
188
189 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
190 if ( $parserOutputKey === false ) {
191 wfIncrStats( 'pcache_miss_absent' );
192 wfProfileOut( __METHOD__ );
193 return false;
194 }
195
196 $value = $this->mMemc->get( $parserOutputKey );
197 if ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
198 wfDebug( "New format parser cache miss.\n" );
199 $parserOutputKey = $this->getParserOutputKey( $article,
200 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) );
201 $value = $this->mMemc->get( $parserOutputKey );
202 }
203 if ( !$value ) {
204 wfDebug( "ParserOutput cache miss.\n" );
205 wfIncrStats( "pcache_miss_absent" );
206 wfProfileOut( __METHOD__ );
207 return false;
208 }
209
210 wfDebug( "ParserOutput cache found.\n" );
211
212 // The edit section preference may not be the appropiate one in
213 // the ParserOutput, as we are not storing it in the parsercache
214 // key. Force it here. See bug 31445.
215 $value->setEditSectionTokens( $popts->getEditSection() );
216
217 if ( !$useOutdated && $value->expired( $touched ) ) {
218 wfIncrStats( "pcache_miss_expired" );
219 $cacheTime = $value->getCacheTime();
220 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
221 $value = false;
222 } else {
223 wfIncrStats( "pcache_hit" );
224 }
225
226 wfProfileOut( __METHOD__ );
227 return $value;
228 }
229
230 /**
231 * @param ParserOutput $parserOutput
232 * @param Article $article
233 * @param ParserOptions $popts
234 * @param string $cacheTime Time when the cache was generated
235 */
236 public function save( $parserOutput, $article, $popts, $cacheTime = null ) {
237 $expire = $parserOutput->getCacheExpiry();
238 if ( $expire > 0 ) {
239 $cacheTime = $cacheTime ?: wfTimestampNow();
240
241 $optionsKey = new CacheTime;
242 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
243 $optionsKey->updateCacheExpiry( $expire );
244
245 $optionsKey->setCacheTime( $cacheTime );
246 $parserOutput->setCacheTime( $cacheTime );
247
248 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
249
250 $parserOutputKey = $this->getParserOutputKey( $article,
251 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
252
253 // Save the timestamp so that we don't have to load the revision row on view
254 $parserOutput->setTimestamp( $article->getTimestamp() );
255
256 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $cacheTime\n -->\n";
257 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $cacheTime\n" );
258
259 // Save the parser output
260 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
261
262 // ...and its pointer
263 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
264 } else {
265 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
266 }
267 }
268 }