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