* Made __construct() protected
[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 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 protected 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 /**
42 * @param $article Article
43 * @param $hash string
44 * @return mixed|string
45 */
46 protected function getParserOutputKey( $article, $hash ) {
47 global $wgRequest;
48
49 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
50 $pageid = $article->getID();
51 $renderkey = (int)($wgRequest->getVal('action') == 'render');
52
53 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
54 return $key;
55 }
56
57 /**
58 * @param $article Article
59 * @return mixed|string
60 */
61 protected function getOptionsKey( $article ) {
62 $pageid = $article->getID();
63 return wfMemcKey( 'pcache', 'idoptions', "{$pageid}" );
64 }
65
66 /**
67 * Provides an E-Tag suitable for the whole page. Note that $article
68 * is just the main wikitext. The E-Tag has to be unique to the whole
69 * page, even if the article itself is the same, so it uses the
70 * complete set of user options. We don't want to use the preference
71 * of a different user on a message just because it wasn't used in
72 * $article. For example give a Chinese interface to a user with
73 * English preferences. That's why we take into account *all* user
74 * options. (r70809 CR)
75 *
76 * @param $article Article
77 * @param $popts ParserOptions
78 */
79 function getETag( $article, $popts ) {
80 return 'W/"' . $this->getParserOutputKey( $article,
81 $popts->optionsHash( ParserOptions::legacyOptions() ) ) .
82 "--" . $article->getTouched() . '"';
83 }
84
85 /**
86 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
87 * @param $article Article
88 * @param $popts ParserOptions
89 * @return ParserOutput|false
90 */
91 public function getDirty( $article, $popts ) {
92 $value = $this->get( $article, $popts, true );
93 return is_object( $value ) ? $value : false;
94 }
95
96 /**
97 * Used to provide a unique id for the PoolCounter.
98 * It would be preferable to have this code in get()
99 * instead of having Article looking in our internals.
100 *
101 * @param $article Article
102 * @param $popts ParserOptions
103 */
104 public function getKey( $article, $popts, $useOutdated = true ) {
105 global $wgCacheEpoch;
106
107 if( $popts instanceof User ) {
108 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
109 $popts = ParserOptions::newFromUser( $popts );
110 }
111
112 // Determine the options which affect this article
113 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
114 if ( $optionsKey != false ) {
115 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
116 wfIncrStats( "pcache_miss_expired" );
117 $cacheTime = $optionsKey->getCacheTime();
118 wfDebug( "Parser options key expired, touched " . $article->getTouched() . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
119 return false;
120 }
121
122 $usedOptions = $optionsKey->mUsedOptions;
123 wfDebug( "Parser cache options found.\n" );
124 } else {
125 if ( !$useOutdated && !self::try116cache ) {
126 return false;
127 }
128 $usedOptions = ParserOptions::legacyOptions();
129 }
130
131 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
132 }
133
134 /**
135 * Retrieve the ParserOutput from ParserCache.
136 * false if not found or outdated.
137 * @return ParserOutput|false
138 */
139 public function get( $article, $popts, $useOutdated = false ) {
140 global $wgCacheEpoch;
141 wfProfileIn( __METHOD__ );
142
143 $canCache = $article->checkTouched();
144 if ( !$canCache ) {
145 // It's a redirect now
146 wfProfileOut( __METHOD__ );
147 return false;
148 }
149
150 $touched = $article->getTouched();
151
152 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
153 if ( $parserOutputKey === false ) {
154 wfProfileOut( __METHOD__ );
155 return false;
156 }
157
158 $value = $this->mMemc->get( $parserOutputKey );
159 if ( self::try116cache && !$value && strpos( $value, '*' ) !== -1 ) {
160 wfDebug( "New format parser cache miss.\n" );
161 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( ParserOptions::legacyOptions() ) );
162 $value = $this->mMemc->get( $parserOutputKey );
163 }
164 if ( !$value ) {
165 wfDebug( "Parser cache miss.\n" );
166 wfIncrStats( "pcache_miss_absent" );
167 wfProfileOut( __METHOD__ );
168 return false;
169 }
170
171 wfDebug( "Found.\n" );
172
173 if ( !$useOutdated && $value->expired( $touched ) ) {
174 wfIncrStats( "pcache_miss_expired" );
175 $cacheTime = $value->getCacheTime();
176 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
177 $value = false;
178 } else {
179 if ( isset( $value->mTimestamp ) ) {
180 $article->mTimestamp = $value->mTimestamp;
181 }
182 wfIncrStats( "pcache_hit" );
183 }
184
185 wfProfileOut( __METHOD__ );
186 return $value;
187 }
188
189 /**
190 * @param $parserOutput ParserOutput
191 * @param $article Article
192 * @param $popts ParserOptions
193 * @return void
194 */
195 public function save( $parserOutput, $article, $popts ) {
196 $expire = $parserOutput->getCacheExpiry();
197
198 if( $expire > 0 ) {
199 $now = wfTimestampNow();
200
201 $optionsKey = new CacheTime;
202 $optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
203 $optionsKey->updateCacheExpiry( $expire );
204
205 $optionsKey->setCacheTime( $now );
206 $parserOutput->setCacheTime( $now );
207
208 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
209
210 $parserOutputKey = $this->getParserOutputKey( $article,
211 $popts->optionsHash( $optionsKey->mUsedOptions ) );
212
213 // Save the timestamp so that we don't have to load the revision row on view
214 $parserOutput->mTimestamp = $article->getTimestamp();
215
216 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
217 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
218
219 // Save the parser output
220 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
221
222 // ...and its pointer
223 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
224 } else {
225 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
226 }
227 }
228 }