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