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