]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/parser/ParserCache.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / parser / ParserCache.php
1 <?php
2 /**
3  * Cache for outputs of the PHP parser
4  *
5  * @file
6  */
7
8 /**
9  * @ingroup Cache Parser
10  * @todo document
11  */
12 class ParserCache {
13         private $mMemc;
14         const try116cache = false; /* Only useful $wgParserCacheExpireTime after updating to 1.17 */
15
16         /**
17          * Get an instance of this object
18          */
19         public static function singleton() {
20                 static $instance;
21                 if ( !isset( $instance ) ) {
22                         global $parserMemc;
23                         $instance = new ParserCache( $parserMemc );
24                 }
25                 return $instance;
26         }
27
28         /**
29          * Setup a cache pathway with a given back-end storage mechanism.
30          * May be a memcached client or a BagOStuff derivative.
31          *
32          * @param $memCached Object
33          */
34         function __construct( $memCached ) {
35                 if ( !$memCached ) {
36                         throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
37                 }
38                 $this->mMemc = $memCached;
39         }
40
41         protected function getParserOutputKey( $article, $hash ) {
42                 global $wgRequest;
43
44                 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
45                 $pageid = $article->getID();
46                 $renderkey = (int)($wgRequest->getVal('action') == 'render');
47
48                 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
49                 return $key;
50         }
51
52         protected function getOptionsKey( $article ) {
53                 $pageid = $article->getID();
54                 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
55         }
56
57         /**
58          * Provides an E-Tag suitable for the whole page. Note that $article
59          * is just the main wikitext. The E-Tag has to be unique to the whole
60          * page, even if the article itself is the same, so it uses the
61          * complete set of user options. We don't want to use the preference
62          * of a different user on a message just because it wasn't used in
63          * $article. For example give a Chinese interface to a user with
64          * English preferences. That's why we take into account *all* user
65          * options. (r70809 CR)
66          */
67         function getETag( $article, $popts ) {
68                 return 'W/"' . $this->getParserOutputKey( $article,
69                         $popts->optionsHash( ParserOptions::legacyOptions() ) ) .
70                                 "--" . $article->getTouched() . '"';
71         }
72
73         /**
74          * Retrieve the ParserOutput from ParserCache, even if it's outdated.
75          */
76         public function getDirty( $article, $popts ) {
77                 $value = $this->get( $article, $popts, true );
78                 return is_object( $value ) ? $value : false;
79         }
80
81         /**
82          * Used to provide a unique id for the PoolCounter.
83          * It would be preferable to have this code in get()
84          * instead of having Article looking in our internals.
85          */
86         public function getKey( $article, $popts, $useOutdated = true ) {
87                 global $wgCacheEpoch;
88
89                 if( $popts instanceof User ) {
90                         wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
91                         $popts = ParserOptions::newFromUser( $popts );
92                 }
93
94                 // Determine the options which affect this article
95                 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
96                 if ( $optionsKey != false ) {
97                         if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
98                                 wfIncrStats( "pcache_miss_expired" );
99                                 $cacheTime = $optionsKey->getCacheTime();
100                                 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
101                                 return false;
102                         }
103
104                         $usedOptions = $optionsKey->mUsedOptions;
105                         wfDebug( "Parser cache options found.\n" );
106                 } else {
107                         if ( !$useOutdated && !self::try116cache ) {
108                                 return false;
109                         }
110                         $usedOptions = ParserOptions::legacyOptions();
111                 }
112
113                 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
114         }
115
116         /**
117          * Retrieve the ParserOutput from ParserCache.
118          * false if not found or outdated.
119          */
120         public function get( $article, $popts, $useOutdated = false ) {
121                 global $wgCacheEpoch;
122                 wfProfileIn( __METHOD__ );
123
124                 $canCache = $article->checkTouched();
125                 if ( !$canCache ) {
126                         // It's a redirect now
127                         wfProfileOut( __METHOD__ );
128                         return false;
129                 }
130
131                 $touched = $article->getTouched();
132
133                 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
134                 if ( $parserOutputKey === false ) {
135                         wfProfileOut( __METHOD__ );
136                         return false;
137                 }
138
139                 $value = $this->mMemc->get( $parserOutputKey );
140                 if ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
141                         wfDebug( "New format parser cache miss.\n" );
142                         $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( ParserOptions::legacyOptions() ) );
143                         $value = $this->mMemc->get( $parserOutputKey );
144                 }
145                 if ( !$value ) {
146                         wfDebug( "Parser cache miss.\n" );
147                         wfIncrStats( "pcache_miss_absent" );
148                         wfProfileOut( __METHOD__ );
149                         return false;
150                 }
151
152                 wfDebug( "Found.\n" );
153
154                 if ( !$useOutdated && $value->expired( $touched ) ) {
155                         wfIncrStats( "pcache_miss_expired" );
156                         $cacheTime = $value->getCacheTime();
157                         wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
158                         $value = false;
159                 } else {
160                         if ( isset( $value->mTimestamp ) ) {
161                                 $article->mTimestamp = $value->mTimestamp;
162                         }
163                         wfIncrStats( "pcache_hit" );
164                 }
165
166                 wfProfileOut( __METHOD__ );
167                 return $value;
168         }
169
170
171         public function save( $parserOutput, $article, $popts ) {
172                 $expire = $parserOutput->getCacheExpiry();
173
174                 if( $expire > 0 ) {
175                         $now = wfTimestampNow();
176
177                         $optionsKey = new CacheTime;
178                         $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
179                         $optionsKey->updateCacheExpiry( $expire );
180
181                         $optionsKey->setCacheTime( $now );
182                         $parserOutput->setCacheTime( $now );
183
184                         $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
185
186                         $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions ) );
187
188                         // Save the timestamp so that we don't have to load the revision row on view
189                         $parserOutput->mTimestamp = $article->getTimestamp();
190
191                         $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
192                         wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
193
194                         // Save the parser output
195                         $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
196
197                         // ...and its pointer
198                         $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
199                 } else {
200                         wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
201                 }
202         }
203 }