No more undefined usage of rev{Start,End}Id warnings
[mediawiki.git] / includes / parser / ParserCache.php
bloba8899cbc2bb4ef216624bec05110e5f51c005d41
1 <?php
2 /**
3 * Cache for outputs of the PHP parser
5 * @file
6 */
8 /**
9 * @ingroup Cache Parser
10 * @todo document
12 class ParserCache {
13 private $mMemc;
14 const try116cache = false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
16 /**
17 * Get an instance of this object
19 * @return ParserCache
21 public static function singleton() {
22 static $instance;
23 if ( !isset( $instance ) ) {
24 global $parserMemc;
25 $instance = new ParserCache( $parserMemc );
27 return $instance;
30 /**
31 * Setup a cache pathway with a given back-end storage mechanism.
32 * May be a memcached client or a BagOStuff derivative.
34 * @param $memCached Object
36 protected function __construct( $memCached ) {
37 if ( !$memCached ) {
38 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
40 $this->mMemc = $memCached;
43 /**
44 * @param $article Article
45 * @param $hash string
46 * @return mixed|string
48 protected function getParserOutputKey( $article, $hash ) {
49 global $wgRequest;
51 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
52 $pageid = $article->getID();
53 $renderkey = (int)($wgRequest->getVal('action') == 'render');
55 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
56 return $key;
59 /**
60 * @param $article Article
61 * @return mixed|string
63 protected function getOptionsKey( $article ) {
64 $pageid = $article->getID();
65 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
68 /**
69 * Provides an E-Tag suitable for the whole page. Note that $article
70 * is just the main wikitext. The E-Tag has to be unique to the whole
71 * page, even if the article itself is the same, so it uses the
72 * complete set of user options. We don't want to use the preference
73 * of a different user on a message just because it wasn't used in
74 * $article. For example give a Chinese interface to a user with
75 * English preferences. That's why we take into account *all* user
76 * options. (r70809 CR)
78 * @param $article Article
79 * @param $popts ParserOptions
80 * @return string
82 function getETag( $article, $popts ) {
83 return 'W/"' . $this->getParserOutputKey( $article,
84 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
85 "--" . $article->getTouched() . '"';
88 /**
89 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
90 * @param $article Article
91 * @param $popts ParserOptions
92 * @return ParserOutput|bool False on failure
94 public function getDirty( $article, $popts ) {
95 $value = $this->get( $article, $popts, true );
96 return is_object( $value ) ? $value : false;
99 /**
100 * Used to provide a unique id for the PoolCounter.
101 * It would be preferable to have this code in get()
102 * instead of having Article looking in our internals.
104 * @todo Document parameter $useOutdated
106 * @param $article Article
107 * @param $popts ParserOptions
108 * @return bool|mixed|string
110 public function getKey( $article, $popts, $useOutdated = true ) {
111 global $wgCacheEpoch;
113 if( $popts instanceof User ) {
114 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
115 $popts = ParserOptions::newFromUser( $popts );
118 // Determine the options which affect this article
119 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
120 if ( $optionsKey != false ) {
121 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
122 wfIncrStats( "pcache_miss_expired" );
123 $cacheTime = $optionsKey->getCacheTime();
124 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
125 return false;
128 $usedOptions = $optionsKey->mUsedOptions;
129 wfDebug( "Parser cache options found.\n" );
130 } else {
131 if ( !$useOutdated && !self::try116cache ) {
132 return false;
134 $usedOptions = ParserOptions::legacyOptions();
137 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
141 * Retrieve the ParserOutput from ParserCache.
142 * false if not found or outdated.
144 * @param $article Article
145 * @param $popts ParserOptions
146 * @param $useOutdated
148 * @return ParserOutput|bool False on failure
150 public function get( $article, $popts, $useOutdated = false ) {
151 global $wgCacheEpoch;
152 wfProfileIn( __METHOD__ );
154 $canCache = $article->checkTouched();
155 if ( !$canCache ) {
156 // It's a redirect now
157 wfProfileOut( __METHOD__ );
158 return false;
161 $touched = $article->getTouched();
163 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
164 if ( $parserOutputKey === false ) {
165 wfIncrStats( 'pcache_miss_absent' );
166 wfProfileOut( __METHOD__ );
167 return false;
170 $value = $this->mMemc->get( $parserOutputKey );
171 if ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
172 wfDebug( "New format parser cache miss.\n" );
173 $parserOutputKey = $this->getParserOutputKey( $article,
174 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) );
175 $value = $this->mMemc->get( $parserOutputKey );
177 if ( !$value ) {
178 wfDebug( "ParserOutput cache miss.\n" );
179 wfIncrStats( "pcache_miss_absent" );
180 wfProfileOut( __METHOD__ );
181 return false;
184 wfDebug( "ParserOutput cache found.\n" );
186 // The edit section preference may not be the appropiate one in
187 // the ParserOutput, as we are not storing it in the parsercache
188 // key. Force it here. See bug 31445.
189 $value->setEditSectionTokens( $popts->getEditSection() );
191 if ( !$useOutdated && $value->expired( $touched ) ) {
192 wfIncrStats( "pcache_miss_expired" );
193 $cacheTime = $value->getCacheTime();
194 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
195 $value = false;
196 } else {
197 wfIncrStats( "pcache_hit" );
200 wfProfileOut( __METHOD__ );
201 return $value;
205 * @param $parserOutput ParserOutput
206 * @param $article Article
207 * @param $popts ParserOptions
209 public function save( $parserOutput, $article, $popts ) {
210 $expire = $parserOutput->getCacheExpiry();
212 if( $expire > 0 ) {
213 $now = wfTimestampNow();
215 $optionsKey = new CacheTime;
216 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
217 $optionsKey->updateCacheExpiry( $expire );
219 $optionsKey->setCacheTime( $now );
220 $parserOutput->setCacheTime( $now );
222 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
224 $parserOutputKey = $this->getParserOutputKey( $article,
225 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
227 // Save the timestamp so that we don't have to load the revision row on view
228 $parserOutput->setTimestamp( $article->getTimestamp() );
230 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
231 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
233 // Save the parser output
234 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
236 // ...and its pointer
237 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
238 } else {
239 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );