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
21 * @ingroup Cache Parser
25 * @ingroup Cache Parser
30 const try116cache
= false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
33 * Get an instance of this object
37 public static function singleton() {
39 if ( !isset( $instance ) ) {
41 $instance = new ParserCache( $parserMemc );
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
52 protected function __construct( $memCached ) {
54 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
56 $this->mMemc
= $memCached;
60 * @param $article Article
62 * @return mixed|string
64 protected function getParserOutputKey( $article, $hash ) {
67 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
68 $pageid = $article->getID();
69 $renderkey = (int)($wgRequest->getVal('action') == 'render');
71 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
76 * @param $article Article
77 * @return mixed|string
79 protected function getOptionsKey( $article ) {
80 $pageid = $article->getID();
81 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
85 * Provides an E-Tag suitable for the whole page. Note that $article
86 * is just the main wikitext. The E-Tag has to be unique to the whole
87 * page, even if the article itself is the same, so it uses the
88 * complete set of user options. We don't want to use the preference
89 * of a different user on a message just because it wasn't used in
90 * $article. For example give a Chinese interface to a user with
91 * English preferences. That's why we take into account *all* user
92 * options. (r70809 CR)
94 * @param $article Article
95 * @param $popts ParserOptions
98 function getETag( $article, $popts ) {
99 return 'W/"' . $this->getParserOutputKey( $article,
100 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) ) .
101 "--" . $article->getTouched() . '"';
105 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
106 * @param $article Article
107 * @param $popts ParserOptions
108 * @return ParserOutput|bool False on failure
110 public function getDirty( $article, $popts ) {
111 $value = $this->get( $article, $popts, true );
112 return is_object( $value ) ?
$value : false;
116 * Used to provide a unique id for the PoolCounter.
117 * It would be preferable to have this code in get()
118 * instead of having Article looking in our internals.
120 * @todo Document parameter $useOutdated
122 * @param $article Article
123 * @param $popts ParserOptions
124 * @param $useOutdated Boolean (default true)
125 * @return bool|mixed|string
127 public function getKey( $article, $popts, $useOutdated = true ) {
128 global $wgCacheEpoch;
130 if( $popts instanceof User
) {
131 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
132 $popts = ParserOptions
::newFromUser( $popts );
135 // Determine the options which affect this article
136 $optionsKey = $this->mMemc
->get( $this->getOptionsKey( $article ) );
137 if ( $optionsKey != false ) {
138 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
139 wfIncrStats( "pcache_miss_expired" );
140 $cacheTime = $optionsKey->getCacheTime();
141 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
145 $usedOptions = $optionsKey->mUsedOptions
;
146 wfDebug( "Parser cache options found.\n" );
148 if ( !$useOutdated && !self
::try116cache
) {
151 $usedOptions = ParserOptions
::legacyOptions();
154 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
158 * Retrieve the ParserOutput from ParserCache.
159 * false if not found or outdated.
161 * @param $article Article
162 * @param $popts ParserOptions
163 * @param $useOutdated Boolean (default false)
165 * @return ParserOutput|bool False on failure
167 public function get( $article, $popts, $useOutdated = false ) {
168 global $wgCacheEpoch;
169 wfProfileIn( __METHOD__
);
171 $canCache = $article->checkTouched();
173 // It's a redirect now
174 wfProfileOut( __METHOD__
);
178 $touched = $article->getTouched();
180 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
181 if ( $parserOutputKey === false ) {
182 wfIncrStats( 'pcache_miss_absent' );
183 wfProfileOut( __METHOD__
);
187 $value = $this->mMemc
->get( $parserOutputKey );
188 if ( self
::try116cache
&& !$value && strpos( $value, '*' ) !== -1 ) {
189 wfDebug( "New format parser cache miss.\n" );
190 $parserOutputKey = $this->getParserOutputKey( $article,
191 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) );
192 $value = $this->mMemc
->get( $parserOutputKey );
195 wfDebug( "ParserOutput cache miss.\n" );
196 wfIncrStats( "pcache_miss_absent" );
197 wfProfileOut( __METHOD__
);
201 wfDebug( "ParserOutput cache found.\n" );
203 // The edit section preference may not be the appropiate one in
204 // the ParserOutput, as we are not storing it in the parsercache
205 // key. Force it here. See bug 31445.
206 $value->setEditSectionTokens( $popts->getEditSection() );
208 if ( !$useOutdated && $value->expired( $touched ) ) {
209 wfIncrStats( "pcache_miss_expired" );
210 $cacheTime = $value->getCacheTime();
211 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
214 wfIncrStats( "pcache_hit" );
217 wfProfileOut( __METHOD__
);
222 * @param $parserOutput ParserOutput
223 * @param $article Article
224 * @param $popts ParserOptions
226 public function save( $parserOutput, $article, $popts ) {
227 $expire = $parserOutput->getCacheExpiry();
230 $now = wfTimestampNow();
232 $optionsKey = new CacheTime
;
233 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
234 $optionsKey->updateCacheExpiry( $expire );
236 $optionsKey->setCacheTime( $now );
237 $parserOutput->setCacheTime( $now );
239 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
241 $parserOutputKey = $this->getParserOutputKey( $article,
242 $popts->optionsHash( $optionsKey->mUsedOptions
, $article->getTitle() ) );
244 // Save the timestamp so that we don't have to load the revision row on view
245 $parserOutput->setTimestamp( $article->getTimestamp() );
247 $parserOutput->mText
.= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
248 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
250 // Save the parser output
251 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
253 // ...and its pointer
254 $this->mMemc
->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
256 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );