Fix fixme on r107328, attempting to use $this in a static method
[lhc/web/wiklou.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 * @return ParserCache
20 */
21 public static function singleton() {
22 static $instance;
23 if ( !isset( $instance ) ) {
24 global $parserMemc;
25 $instance = new ParserCache( $parserMemc );
26 }
27 return $instance;
28 }
29
30 /**
31 * Setup a cache pathway with a given back-end storage mechanism.
32 * May be a memcached client or a BagOStuff derivative.
33 *
34 * @param $memCached Object
35 */
36 protected function __construct( $memCached ) {
37 if ( !$memCached ) {
38 throw new MWException( "Tried to create a ParserCache with an invalid memcached" );
39 }
40 $this->mMemc = $memCached;
41 }
42
43 /**
44 * @param $article Article
45 * @param $hash string
46 * @return mixed|string
47 */
48 protected function getParserOutputKey( $article, $hash ) {
49 global $wgRequest;
50
51 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
52 $pageid = $article->getID();
53 $renderkey = (int)($wgRequest->getVal('action') == 'render');
54
55 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
56 return $key;
57 }
58
59 /**
60 * @param $article Article
61 * @return mixed|string
62 */
63 protected function getOptionsKey( $article ) {
64 $pageid = $article->getID();
65 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
66 }
67
68 /**
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)
77 *
78 * @param $article Article
79 * @param $popts ParserOptions
80 */
81 function getETag( $article, $popts ) {
82 return 'W/"' . $this->getParserOutputKey( $article,
83 $popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
84 "--" . $article->getTouched() . '"';
85 }
86
87 /**
88 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
89 * @param $article Article
90 * @param $popts ParserOptions
91 * @return ParserOutput|false
92 */
93 public function getDirty( $article, $popts ) {
94 $value = $this->get( $article, $popts, true );
95 return is_object( $value ) ? $value : false;
96 }
97
98 /**
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.
102 *
103 * @param $article Article
104 * @param $popts ParserOptions
105 */
106 public function getKey( $article, $popts, $useOutdated = true ) {
107 global $wgCacheEpoch;
108
109 if( $popts instanceof User ) {
110 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
111 $popts = ParserOptions::newFromUser( $popts );
112 }
113
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" );
121 return false;
122 }
123
124 $usedOptions = $optionsKey->mUsedOptions;
125 wfDebug( "Parser cache options found.\n" );
126 } else {
127 if ( !$useOutdated && !self::try116cache ) {
128 return false;
129 }
130 $usedOptions = ParserOptions::legacyOptions();
131 }
132
133 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions, $article->getTitle() ) );
134 }
135
136 /**
137 * Retrieve the ParserOutput from ParserCache.
138 * false if not found or outdated.
139 *
140 * @param $article Article
141 * @param $popts ParserOptions
142 * @param $useOutdated
143 *
144 * @return ParserOutput|false
145 */
146 public function get( $article, $popts, $useOutdated = false ) {
147 global $wgCacheEpoch;
148 wfProfileIn( __METHOD__ );
149
150 $canCache = $article->checkTouched();
151 if ( !$canCache ) {
152 // It's a redirect now
153 wfProfileOut( __METHOD__ );
154 return false;
155 }
156
157 $touched = $article->getTouched();
158
159 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
160 if ( $parserOutputKey === false ) {
161 wfProfileOut( __METHOD__ );
162 return false;
163 }
164
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 );
171 }
172 if ( !$value ) {
173 wfDebug( "ParserOutput cache miss.\n" );
174 wfIncrStats( "pcache_miss_absent" );
175 wfProfileOut( __METHOD__ );
176 return false;
177 }
178
179 wfDebug( "ParserOutput cache found.\n" );
180
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() );
185
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" );
190 $value = false;
191 } else {
192 wfIncrStats( "pcache_hit" );
193 }
194
195 wfProfileOut( __METHOD__ );
196 return $value;
197 }
198
199 /**
200 * @param $parserOutput ParserOutput
201 * @param $article Article
202 * @param $popts ParserOptions
203 */
204 public function save( $parserOutput, $article, $popts ) {
205 $expire = $parserOutput->getCacheExpiry();
206
207 if( $expire > 0 ) {
208 $now = wfTimestampNow();
209
210 $optionsKey = new CacheTime;
211 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
212 $optionsKey->updateCacheExpiry( $expire );
213
214 $optionsKey->setCacheTime( $now );
215 $parserOutput->setCacheTime( $now );
216
217 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
218
219 $parserOutputKey = $this->getParserOutputKey( $article,
220 $popts->optionsHash( $optionsKey->mUsedOptions, $article->getTitle() ) );
221
222 // Save the timestamp so that we don't have to load the revision row on view
223 $parserOutput->setTimestamp( $article->getTimestamp() );
224
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" );
227
228 // Save the parser output
229 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
230
231 // ...and its pointer
232 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
233 } else {
234 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
235 }
236 }
237 }