3 * Cache for outputs of the PHP parser
9 * @ingroup Cache Parser
14 const try116cache
= false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
17 * Get an instance of this object
21 public static function singleton() {
23 if ( !isset( $instance ) ) {
25 $instance = new ParserCache( $parserMemc );
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 ) {
38 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
40 $this->mMemc
= $memCached;
44 * @param $article Article
46 * @return mixed|string
48 protected function getParserOutputKey( $article, $hash ) {
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}" );
60 * @param $article Article
61 * @return mixed|string
63 protected function getOptionsKey( $article ) {
64 $pageid = $article->getID();
65 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
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
81 function getETag( $article, $popts ) {
82 return 'W/"' . $this->getParserOutputKey( $article,
83 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) ) .
84 "--" . $article->getTouched() . '"';
88 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
89 * @param $article Article
90 * @param $popts ParserOptions
91 * @return ParserOutput|false
93 public function getDirty( $article, $popts ) {
94 $value = $this->get( $article, $popts, true );
95 return is_object( $value ) ?
$value : false;
99 * Used to provide a unique id for the PoolCounter.
100 * It would be preferable to have this code in get()
101 * instead of having Article looking in our internals.
103 * @todo Document parameter $useOutdated
105 * @param $article Article
106 * @param $popts ParserOptions
108 public function getKey( $article, $popts, $useOutdated = true ) {
109 global $wgCacheEpoch;
111 if( $popts instanceof User
) {
112 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
113 $popts = ParserOptions
::newFromUser( $popts );
116 // Determine the options which affect this article
117 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
118 if ( $optionsKey != false ) {
119 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
120 wfIncrStats( "pcache_miss_expired" );
121 $cacheTime = $optionsKey->getCacheTime();
122 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
126 $usedOptions = $optionsKey->mUsedOptions
;
127 wfDebug( "Parser cache options found.\n" );
129 if ( !$useOutdated && !self
::try116cache
) {
132 $usedOptions = ParserOptions
::legacyOptions();
135 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
139 * Retrieve the ParserOutput from ParserCache.
140 * false if not found or outdated.
142 * @param $article Article
143 * @param $popts ParserOptions
144 * @param $useOutdated
146 * @return ParserOutput|false
148 public function get( $article, $popts, $useOutdated = false ) {
149 global $wgCacheEpoch;
150 wfProfileIn( __METHOD__
);
152 $canCache = $article->checkTouched();
154 // It's a redirect now
155 wfProfileOut( __METHOD__
);
159 $touched = $article->getTouched();
161 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
162 if ( $parserOutputKey === false ) {
163 wfIncrStats( 'pcache_miss_absent' );
164 wfProfileOut( __METHOD__
);
168 $value = $this->mMemc
->get( $parserOutputKey );
169 if ( self
::try116cache
&& !$value && strpos( $value, '*' ) !== -1 ) {
170 wfDebug( "New format parser cache miss.\n" );
171 $parserOutputKey = $this->getParserOutputKey( $article,
172 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) );
173 $value = $this->mMemc
->get( $parserOutputKey );
176 wfDebug( "ParserOutput cache miss.\n" );
177 wfIncrStats( "pcache_miss_absent" );
178 wfProfileOut( __METHOD__
);
182 wfDebug( "ParserOutput cache found.\n" );
184 // The edit section preference may not be the appropiate one in
185 // the ParserOutput, as we are not storing it in the parsercache
186 // key. Force it here. See bug 31445.
187 $value->setEditSectionTokens( $popts->getEditSection() );
189 if ( !$useOutdated && $value->expired( $touched ) ) {
190 wfIncrStats( "pcache_miss_expired" );
191 $cacheTime = $value->getCacheTime();
192 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
195 wfIncrStats( "pcache_hit" );
198 wfProfileOut( __METHOD__
);
203 * @param $parserOutput ParserOutput
204 * @param $article Article
205 * @param $popts ParserOptions
207 public function save( $parserOutput, $article, $popts ) {
208 $expire = $parserOutput->getCacheExpiry();
211 $now = wfTimestampNow();
213 $optionsKey = new CacheTime
;
214 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
215 $optionsKey->updateCacheExpiry( $expire );
217 $optionsKey->setCacheTime( $now );
218 $parserOutput->setCacheTime( $now );
220 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
222 $parserOutputKey = $this->getParserOutputKey( $article,
223 $popts->optionsHash( $optionsKey->mUsedOptions
, $article->getTitle() ) );
225 // Save the timestamp so that we don't have to load the revision row on view
226 $parserOutput->setTimestamp( $article->getTimestamp() );
228 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
229 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
231 // Save the parser output
232 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
234 // ...and its pointer
235 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
237 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );