Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / parser / ParserCache.php
blob7c5eeb4a8fdbb0725e27dd9c2c84ce0f763b9aa6
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
51 * @throws MWException
53 protected function __construct( $memCached ) {
54 if ( !$memCached ) {
55 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
57 $this->mMemc = $memCached;
60 /**
61 * @param $article Article
62 * @param $hash string
63 * @return mixed|string
65 protected function getParserOutputKey( $article, $hash ) {
66 global $wgRequest;
68 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
69 $pageid = $article->getID();
70 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
72 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
73 return $key;
76 /**
77 * @param $article Article
78 * @return mixed|string
80 protected function getOptionsKey( $article ) {
81 $pageid = $article->getID();
82 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
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)
95 * @param $article Article
96 * @param $popts ParserOptions
97 * @return string
99 function getETag( $article, $popts ) {
100 return 'W/"' . $this->getParserOutputKey( $article,
101 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
102 "--" . $article->getTouched() . '"';
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
111 public function getDirty( $article, $popts ) {
112 $value = $this->get( $article, $popts, true );
113 return is_object( $value ) ? $value : false;
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.
121 * @todo Document parameter $useOutdated
123 * @param $article Article
124 * @param $popts ParserOptions
125 * @param $useOutdated Boolean (default true)
126 * @return bool|mixed|string
128 public function getKey( $article, $popts, $useOutdated = true ) {
129 global $wgCacheEpoch;
131 if ( $popts instanceof User ) {
132 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
133 $popts = ParserOptions::newFromUser( $popts );
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;
146 $usedOptions = $optionsKey->mUsedOptions;
147 wfDebug( "Parser cache options found.\n" );
148 } else {
149 if ( !$useOutdated && !self::try116cache ) {
150 return false;
152 $usedOptions = ParserOptions::legacyOptions();
155 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
159 * Retrieve the ParserOutput from ParserCache.
160 * false if not found or outdated.
162 * @param $article Article
163 * @param $popts ParserOptions
164 * @param $useOutdated Boolean (default false)
166 * @return ParserOutput|bool False on failure
168 public function get( $article, $popts, $useOutdated = false ) {
169 global $wgCacheEpoch;
170 wfProfileIn( __METHOD__ );
172 $canCache = $article->checkTouched();
173 if ( !$canCache ) {
174 // It's a redirect now
175 wfProfileOut( __METHOD__ );
176 return false;
179 $touched = $article->getTouched();
181 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
182 if ( $parserOutputKey === false ) {
183 wfIncrStats( 'pcache_miss_absent' );
184 wfProfileOut( __METHOD__ );
185 return false;
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 );
195 if ( !$value ) {
196 wfDebug( "ParserOutput cache miss.\n" );
197 wfIncrStats( "pcache_miss_absent" );
198 wfProfileOut( __METHOD__ );
199 return false;
202 wfDebug( "ParserOutput cache found.\n" );
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() );
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" );
218 wfProfileOut( __METHOD__ );
219 return $value;
223 * @param $parserOutput ParserOutput
224 * @param $article Article
225 * @param $popts ParserOptions
227 public function save( $parserOutput, $article, $popts ) {
228 $expire = $parserOutput->getCacheExpiry();
230 if ( $expire > 0 ) {
231 $now = wfTimestampNow();
233 $optionsKey = new CacheTime;
234 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
235 $optionsKey->updateCacheExpiry( $expire );
237 $optionsKey->setCacheTime( $now );
238 $parserOutput->setCacheTime( $now );
240 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
242 $parserOutputKey = $this->getParserOutputKey( $article,
243 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
245 // Save the timestamp so that we don't have to load the revision row on view
246 $parserOutput->setTimestamp( $article->getTimestamp() );
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" );
251 // Save the parser output
252 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
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" );