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