f91f26a2ffadbbd01d4837f876d338a16955869f
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 * @param $article Article
104 * @param $popts ParserOptions
106 public function getKey( $article, $popts, $useOutdated = true ) {
107 global $wgCacheEpoch;
109 if( $popts instanceof User
) {
110 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
111 $popts = ParserOptions
::newFromUser( $popts );
114 // Determine the options which affect this article
115 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
116 if ( $optionsKey != false ) {
117 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
118 wfIncrStats( "pcache_miss_expired" );
119 $cacheTime = $optionsKey->getCacheTime();
120 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
124 $usedOptions = $optionsKey->mUsedOptions
;
125 wfDebug( "Parser cache options found.\n" );
127 if ( !$useOutdated && !self
::try116cache
) {
130 $usedOptions = ParserOptions
::legacyOptions();
133 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
137 * Retrieve the ParserOutput from ParserCache.
138 * false if not found or outdated.
140 * @param $article Article
141 * @param $popts ParserOptions
142 * @param $useOutdated
144 * @return ParserOutput|false
146 public function get( $article, $popts, $useOutdated = false ) {
147 global $wgCacheEpoch;
148 wfProfileIn( __METHOD__
);
150 $canCache = $article->checkTouched();
152 // It's a redirect now
153 wfProfileOut( __METHOD__
);
157 $touched = $article->getTouched();
159 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
160 if ( $parserOutputKey === false ) {
161 wfProfileOut( __METHOD__
);
165 $value = $this->mMemc
->get( $parserOutputKey );
166 if ( self
::try116cache
&& !$value && strpos( $value, '*' ) !== -1 ) {
167 wfDebug( "New format parser cache miss.\n" );
168 $parserOutputKey = $this->getParserOutputKey( $article,
169 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) );
170 $value = $this->mMemc
->get( $parserOutputKey );
173 wfDebug( "ParserOutput cache miss.\n" );
174 wfIncrStats( "pcache_miss_absent" );
175 wfProfileOut( __METHOD__
);
179 wfDebug( "ParserOutput cache found.\n" );
181 // The edit section preference may not be the appropiate one in
182 // the ParserOutput, as we are not storing it in the parsercache
183 // key. Force it here. See bug 31445.
184 $value->setEditSectionTokens( $popts->getEditSection() );
186 if ( !$useOutdated && $value->expired( $touched ) ) {
187 wfIncrStats( "pcache_miss_expired" );
188 $cacheTime = $value->getCacheTime();
189 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
192 wfIncrStats( "pcache_hit" );
195 wfProfileOut( __METHOD__
);
200 * @param $parserOutput ParserOutput
201 * @param $article Article
202 * @param $popts ParserOptions
204 public function save( $parserOutput, $article, $popts ) {
205 $expire = $parserOutput->getCacheExpiry();
208 $now = wfTimestampNow();
210 $optionsKey = new CacheTime
;
211 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
212 $optionsKey->updateCacheExpiry( $expire );
214 $optionsKey->setCacheTime( $now );
215 $parserOutput->setCacheTime( $now );
217 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
219 $parserOutputKey = $this->getParserOutputKey( $article,
220 $popts->optionsHash( $optionsKey->mUsedOptions
, $article->getTitle() ) );
222 // Save the timestamp so that we don't have to load the revision row on view
223 $parserOutput->setTimestamp( $article->getTimestamp() );
225 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
226 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
228 // Save the parser output
229 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
231 // ...and its pointer
232 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
234 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );