Followup r77748, throw NEW MWException
[mediawiki.git] / includes / parser / ParserCache.php
blobea0292b851d0e4cc5b029f8476f07bd9034ec2c3
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;
15 /**
16 * Get an instance of this object
18 public static function singleton() {
19 static $instance;
20 if ( !isset( $instance ) ) {
21 global $parserMemc;
22 $instance = new ParserCache( $parserMemc );
24 return $instance;
27 /**
28 * Setup a cache pathway with a given back-end storage mechanism.
29 * May be a memcached client or a BagOStuff derivative.
31 * @param $memCached Object
33 function __construct( $memCached ) {
34 if ( !$memCached ) {
35 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
37 $this->mMemc = $memCached;
40 protected function getParserOutputKey( $article, $hash ) {
41 global $wgRequest;
43 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
44 $pageid = $article->getID();
45 $renderkey = (int)($wgRequest->getVal('action') == 'render');
47 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
48 return $key;
51 protected function getOptionsKey( $article ) {
52 $pageid = $article->getID();
53 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
56 /**
57 * Provides an E-Tag suitable for the whole page. Note that $article
58 * is just the main wikitext. The E-Tag has to be unique to the whole
59 * page, even if the article itself is the same, so it uses the
60 * complete set of user options. We don't want to use the preference
61 * of a different user on a message just because it wasn't used in
62 * $article. For example give a Chinese interface to a user with
63 * English preferences. That's why we take into account *all* user
64 * options. (r70809 CR)
66 function getETag( $article, $popts ) {
67 return 'W/"' . $this->getParserOutputKey( $article,
68 $popts->optionsHash( ParserOptions::legacyOptions() ) ) .
69 "--" . $article->mTouched . '"';
72 /**
73 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
75 public function getDirty( $article, $popts ) {
76 $value = $this->get( $article, $popts, true );
77 return is_object( $value ) ? $value : false;
80 /**
81 * Used to provide a unique id for the PoolCounter.
82 * It would be preferable to have this code in get()
83 * instead of having Article looking in our internals.
85 * Precondition: $article->checkTouched() has been called.
87 public function getKey( $article, $popts, $useOutdated = true ) {
88 global $wgCacheEpoch;
90 if( $popts instanceof User ) {
91 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
92 $popts = ParserOptions::newFromUser( $popts );
95 // Determine the options which affect this article
96 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
97 if ( $optionsKey != false ) {
98 if ( !$useOutdated && $optionsKey->expired( $article->mTouched ) ) {
99 wfIncrStats( "pcache_miss_expired" );
100 $cacheTime = $optionsKey->getCacheTime();
101 wfDebug( "Parser options key expired, touched {$article->mTouched}, epoch $wgCacheEpoch, cached $cacheTime\n" );
102 return false;
105 $usedOptions = $optionsKey->mUsedOptions;
106 wfDebug( "Parser cache options found.\n" );
107 } else {
108 # TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
110 $usedOptions = ParserOptions::legacyOptions();
113 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
117 * Retrieve the ParserOutput from ParserCache.
118 * false if not found or outdated.
120 public function get( $article, $popts, $useOutdated = false ) {
121 global $wgCacheEpoch;
122 wfProfileIn( __METHOD__ );
124 $canCache = $article->checkTouched();
125 if ( !$canCache ) {
126 // It's a redirect now
127 wfProfileOut( __METHOD__ );
128 return false;
131 // Having called checkTouched() ensures this will be loaded
132 $touched = $article->mTouched;
134 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
135 if ( $parserOutputKey === false ) {
136 wfProfileOut( __METHOD__ );
137 return false;
140 $value = $this->mMemc->get( $parserOutputKey );
141 if ( !$value ) {
142 wfDebug( "Parser cache miss.\n" );
143 wfIncrStats( "pcache_miss_absent" );
144 wfProfileOut( __METHOD__ );
145 return false;
148 wfDebug( "Found.\n" );
150 if ( !$useOutdated && $value->expired( $touched ) ) {
151 wfIncrStats( "pcache_miss_expired" );
152 $cacheTime = $value->getCacheTime();
153 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
154 $value = false;
155 } else {
156 if ( isset( $value->mTimestamp ) ) {
157 $article->mTimestamp = $value->mTimestamp;
159 wfIncrStats( "pcache_hit" );
162 wfProfileOut( __METHOD__ );
163 return $value;
167 public function save( $parserOutput, $article, $popts ) {
168 $expire = $parserOutput->getCacheExpiry();
170 if( $expire > 0 ) {
171 $now = wfTimestampNow();
173 $optionsKey = new CacheTime;
174 $optionsKey->mUsedOptions = $popts->usedOptions();
175 $optionsKey->updateCacheExpiry( $expire );
177 $optionsKey->setCacheTime( $now );
178 $parserOutput->setCacheTime( $now );
180 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
182 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions ) );
184 // Save the timestamp so that we don't have to load the revision row on view
185 $parserOutput->mTimestamp = $article->getTimestamp();
187 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
188 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
190 // Save the parser output
191 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
193 // ...and its pointer
194 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
195 } else {
196 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );