Merge "set tidy = true for action=purge&forcelinkupdate="
[mediawiki.git] / includes / parser / ParserCache.php
blobbb990397d3f7d231fe5c446a07021fa1cfa9bde1
1 <?php
2 /**
3 * Cache for outputs of the PHP parser
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.
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.
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
20 * @file
21 * @ingroup Cache Parser
24 /**
25 * @ingroup Cache Parser
26 * @todo document
28 class ParserCache {
29 private $mMemc;
30 const try116cache = false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
32 /**
33 * Get an instance of this object
35 * @return ParserCache
37 public static function singleton() {
38 static $instance;
39 if ( !isset( $instance ) ) {
40 global $parserMemc;
41 $instance = new ParserCache( $parserMemc );
43 return $instance;
46 /**
47 * Setup a cache pathway with a given back-end storage mechanism.
48 * May be a memcached client or a BagOStuff derivative.
50 * @param $memCached Object
52 protected function __construct( $memCached ) {
53 if ( !$memCached ) {
54 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
56 $this->mMemc = $memCached;
59 /**
60 * @param $article Article
61 * @param $hash string
62 * @return mixed|string
64 protected function getParserOutputKey( $article, $hash ) {
65 global $wgRequest;
67 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
68 $pageid = $article->getID();
69 $renderkey = (int)($wgRequest->getVal('action') == 'render');
71 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
72 return $key;
75 /**
76 * @param $article Article
77 * @return mixed|string
79 protected function getOptionsKey( $article ) {
80 $pageid = $article->getID();
81 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
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)
94 * @param $article Article
95 * @param $popts ParserOptions
96 * @return string
98 function getETag( $article, $popts ) {
99 return 'W/"' . $this->getParserOutputKey( $article,
100 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
101 "--" . $article->getTouched() . '"';
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
110 public function getDirty( $article, $popts ) {
111 $value = $this->get( $article, $popts, true );
112 return is_object( $value ) ? $value : false;
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.
120 * @todo Document parameter $useOutdated
122 * @param $article Article
123 * @param $popts ParserOptions
124 * @return bool|mixed|string
126 public function getKey( $article, $popts, $useOutdated = true ) {
127 global $wgCacheEpoch;
129 if( $popts instanceof User ) {
130 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
131 $popts = ParserOptions::newFromUser( $popts );
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;
144 $usedOptions = $optionsKey->mUsedOptions;
145 wfDebug( "Parser cache options found.\n" );
146 } else {
147 if ( !$useOutdated && !self::try116cache ) {
148 return false;
150 $usedOptions = ParserOptions::legacyOptions();
153 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
157 * Retrieve the ParserOutput from ParserCache.
158 * false if not found or outdated.
160 * @param $article Article
161 * @param $popts ParserOptions
162 * @param $useOutdated
164 * @return ParserOutput|bool False on failure
166 public function get( $article, $popts, $useOutdated = false ) {
167 global $wgCacheEpoch;
168 wfProfileIn( __METHOD__ );
170 $canCache = $article->checkTouched();
171 if ( !$canCache ) {
172 // It's a redirect now
173 wfProfileOut( __METHOD__ );
174 return false;
177 $touched = $article->getTouched();
179 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
180 if ( $parserOutputKey === false ) {
181 wfIncrStats( 'pcache_miss_absent' );
182 wfProfileOut( __METHOD__ );
183 return false;
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 );
193 if ( !$value ) {
194 wfDebug( "ParserOutput cache miss.\n" );
195 wfIncrStats( "pcache_miss_absent" );
196 wfProfileOut( __METHOD__ );
197 return false;
200 wfDebug( "ParserOutput cache found.\n" );
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() );
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" );
216 wfProfileOut( __METHOD__ );
217 return $value;
221 * @param $parserOutput ParserOutput
222 * @param $article Article
223 * @param $popts ParserOptions
225 public function save( $parserOutput, $article, $popts ) {
226 $expire = $parserOutput->getCacheExpiry();
228 if( $expire > 0 ) {
229 $now = wfTimestampNow();
231 $optionsKey = new CacheTime;
232 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
233 $optionsKey->updateCacheExpiry( $expire );
235 $optionsKey->setCacheTime( $now );
236 $parserOutput->setCacheTime( $now );
238 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
240 $parserOutputKey = $this->getParserOutputKey( $article,
241 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
243 // Save the timestamp so that we don't have to load the revision row on view
244 $parserOutput->setTimestamp( $article->getTimestamp() );
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" );
249 // Save the parser output
250 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
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" );