Merge "Adding wildcard support to $wgCopyUploadsDomains"
[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 * @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 && !self::try116cache ) {
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 ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
189 wfDebug( "New format parser cache miss.\n" );
190 $parserOutputKey = $this->getParserOutputKey( $article,
191 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) );
192 $value = $this->mMemc->get( $parserOutputKey );
193 }
194 if ( !$value ) {
195 wfDebug( "ParserOutput cache miss.\n" );
196 wfIncrStats( "pcache_miss_absent" );
197 wfProfileOut( __METHOD__ );
198 return false;
199 }
200
201 wfDebug( "ParserOutput cache found.\n" );
202
203 // The edit section preference may not be the appropiate one in
204 // the ParserOutput, as we are not storing it in the parsercache
205 // key. Force it here. See bug 31445.
206 $value->setEditSectionTokens( $popts->getEditSection() );
207
208 if ( !$useOutdated && $value->expired( $touched ) ) {
209 wfIncrStats( "pcache_miss_expired" );
210 $cacheTime = $value->getCacheTime();
211 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
212 $value = false;
213 } else {
214 wfIncrStats( "pcache_hit" );
215 }
216
217 wfProfileOut( __METHOD__ );
218 return $value;
219 }
220
221 /**
222 * @param $parserOutput ParserOutput
223 * @param $article Article
224 * @param $popts ParserOptions
225 */
226 public function save( $parserOutput, $article, $popts ) {
227 $expire = $parserOutput->getCacheExpiry();
228
229 if( $expire > 0 ) {
230 $now = wfTimestampNow();
231
232 $optionsKey = new CacheTime;
233 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
234 $optionsKey->updateCacheExpiry( $expire );
235
236 $optionsKey->setCacheTime( $now );
237 $parserOutput->setCacheTime( $now );
238
239 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
240
241 $parserOutputKey = $this->getParserOutputKey( $article,
242 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
243
244 // Save the timestamp so that we don't have to load the revision row on view
245 $parserOutput->setTimestamp( $article->getTimestamp() );
246
247 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
248 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
249
250 // Save the parser output
251 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
252
253 // ...and its pointer
254 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
255 } else {
256 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
257 }
258 }
259 }