Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[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 * Used to provide a unique id for the PoolCounter.
118 * It would be preferable to have this code in get()
119 * instead of having Article looking in our internals.
120 *
121 * @todo Document parameter $useOutdated
122 *
123 * @param $article Article
124 * @param $popts ParserOptions
125 * @param $useOutdated Boolean (default true)
126 * @return bool|mixed|string
127 */
128 public function getKey( $article, $popts, $useOutdated = true ) {
129 global $wgCacheEpoch;
130
131 if ( $popts instanceof User ) {
132 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
133 $popts = ParserOptions::newFromUser( $popts );
134 }
135
136 // Determine the options which affect this article
137 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
138 if ( $optionsKey != false ) {
139 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
140 wfIncrStats( "pcache_miss_expired" );
141 $cacheTime = $optionsKey->getCacheTime();
142 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
143 return false;
144 }
145
146 $usedOptions = $optionsKey->mUsedOptions;
147 wfDebug( "Parser cache options found.\n" );
148 } else {
149 if ( !$useOutdated && !self::try116cache ) {
150 return false;
151 }
152 $usedOptions = ParserOptions::legacyOptions();
153 }
154
155 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
156 }
157
158 /**
159 * Retrieve the ParserOutput from ParserCache.
160 * false if not found or outdated.
161 *
162 * @param $article Article
163 * @param $popts ParserOptions
164 * @param $useOutdated Boolean (default false)
165 *
166 * @return ParserOutput|bool False on failure
167 */
168 public function get( $article, $popts, $useOutdated = false ) {
169 global $wgCacheEpoch;
170 wfProfileIn( __METHOD__ );
171
172 $canCache = $article->checkTouched();
173 if ( !$canCache ) {
174 // It's a redirect now
175 wfProfileOut( __METHOD__ );
176 return false;
177 }
178
179 $touched = $article->getTouched();
180
181 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
182 if ( $parserOutputKey === false ) {
183 wfIncrStats( 'pcache_miss_absent' );
184 wfProfileOut( __METHOD__ );
185 return false;
186 }
187
188 $value = $this->mMemc->get( $parserOutputKey );
189 if ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
190 wfDebug( "New format parser cache miss.\n" );
191 $parserOutputKey = $this->getParserOutputKey( $article,
192 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) );
193 $value = $this->mMemc->get( $parserOutputKey );
194 }
195 if ( !$value ) {
196 wfDebug( "ParserOutput cache miss.\n" );
197 wfIncrStats( "pcache_miss_absent" );
198 wfProfileOut( __METHOD__ );
199 return false;
200 }
201
202 wfDebug( "ParserOutput cache found.\n" );
203
204 // The edit section preference may not be the appropiate one in
205 // the ParserOutput, as we are not storing it in the parsercache
206 // key. Force it here. See bug 31445.
207 $value->setEditSectionTokens( $popts->getEditSection() );
208
209 if ( !$useOutdated && $value->expired( $touched ) ) {
210 wfIncrStats( "pcache_miss_expired" );
211 $cacheTime = $value->getCacheTime();
212 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
213 $value = false;
214 } else {
215 wfIncrStats( "pcache_hit" );
216 }
217
218 wfProfileOut( __METHOD__ );
219 return $value;
220 }
221
222 /**
223 * @param $parserOutput ParserOutput
224 * @param $article Article
225 * @param $popts ParserOptions
226 */
227 public function save( $parserOutput, $article, $popts ) {
228 $expire = $parserOutput->getCacheExpiry();
229
230 if ( $expire > 0 ) {
231 $now = wfTimestampNow();
232
233 $optionsKey = new CacheTime;
234 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
235 $optionsKey->updateCacheExpiry( $expire );
236
237 $optionsKey->setCacheTime( $now );
238 $parserOutput->setCacheTime( $now );
239
240 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
241
242 $parserOutputKey = $this->getParserOutputKey( $article,
243 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
244
245 // Save the timestamp so that we don't have to load the revision row on view
246 $parserOutput->setTimestamp( $article->getTimestamp() );
247
248 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
249 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
250
251 // Save the parser output
252 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
253
254 // ...and its pointer
255 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
256 } else {
257 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
258 }
259 }
260 }