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