Added fname parameter to the query() call
[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 global $parserMemc;
36 $parserMemc = $memCached = wfGetParserCacheStorage();
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->mTouched . '"';
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 * Precondition: $article->checkTouched() has been called.
87 */
88 public function getKey( $article, $popts, $useOutdated = true ) {
89 global $wgCacheEpoch;
90
91 if( $popts instanceof User ) {
92 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
93 $popts = ParserOptions::newFromUser( $popts );
94 }
95
96 // Determine the options which affect this article
97 $optionsKey = $this->mMemc->get( $this->getOptionsKey( $article ) );
98 if ( $optionsKey != false ) {
99 if ( !$useOutdated && $optionsKey->expired( $article->mTouched ) ) {
100 wfIncrStats( "pcache_miss_expired" );
101 $cacheTime = $optionsKey->getCacheTime();
102 wfDebug( "Parser options key expired, touched {$article->mTouched}, epoch $wgCacheEpoch, cached $cacheTime\n" );
103 return false;
104 }
105
106 $usedOptions = $optionsKey->mUsedOptions;
107 wfDebug( "Parser cache options found.\n" );
108 } else {
109 # TODO: Fail here $wgParserCacheExpireTime after deployment unless $useOutdated
110
111 $usedOptions = ParserOptions::legacyOptions();
112 }
113
114 return $this->getParserOutputKey( $article, $popts->optionsHash( $usedOptions ) );
115 }
116
117 /**
118 * Retrieve the ParserOutput from ParserCache.
119 * false if not found or outdated.
120 */
121 public function get( $article, $popts, $useOutdated = false ) {
122 global $wgCacheEpoch;
123 wfProfileIn( __METHOD__ );
124
125 $canCache = $article->checkTouched();
126 if ( !$canCache ) {
127 // It's a redirect now
128 wfProfileOut( __METHOD__ );
129 return false;
130 }
131
132 // Having called checkTouched() ensures this will be loaded
133 $touched = $article->mTouched;
134
135 $parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
136 if ( $parserOutputKey === false ) {
137 wfProfileOut( __METHOD__ );
138 return false;
139 }
140
141 $value = $this->mMemc->get( $parserOutputKey );
142 if ( !$value ) {
143 wfDebug( "Parser cache miss.\n" );
144 wfIncrStats( "pcache_miss_absent" );
145 wfProfileOut( __METHOD__ );
146 return false;
147 }
148
149 wfDebug( "Found.\n" );
150
151 if ( !$useOutdated && $value->expired( $touched ) ) {
152 wfIncrStats( "pcache_miss_expired" );
153 $cacheTime = $value->getCacheTime();
154 wfDebug( "ParserOutput key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
155 $value = false;
156 } else {
157 if ( isset( $value->mTimestamp ) ) {
158 $article->mTimestamp = $value->mTimestamp;
159 }
160 wfIncrStats( "pcache_hit" );
161 }
162
163 wfProfileOut( __METHOD__ );
164 return $value;
165 }
166
167
168 public function save( $parserOutput, $article, $popts ) {
169 $expire = $parserOutput->getCacheExpiry();
170
171 if( $expire > 0 ) {
172 $now = wfTimestampNow();
173
174 $optionsKey = new CacheTime;
175 $optionsKey->mUsedOptions = $popts->usedOptions();
176 $optionsKey->updateCacheExpiry( $expire );
177
178 $optionsKey->setCacheTime( $now );
179 $parserOutput->setCacheTime( $now );
180
181 $optionsKey->setContainsOldMagic( $parserOutput->containsOldMagic() );
182
183 $parserOutputKey = $this->getParserOutputKey( $article, $popts->optionsHash( $optionsKey->mUsedOptions ) );
184
185 // Save the timestamp so that we don't have to load the revision row on view
186 $parserOutput->mTimestamp = $article->getTimestamp();
187
188 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $parserOutputKey and timestamp $now -->\n";
189 wfDebug( "Saved in parser cache with key $parserOutputKey and timestamp $now\n" );
190
191 // Save the parser output
192 $this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
193
194 // ...and its pointer
195 $this->mMemc->set( $this->getOptionsKey( $article ), $optionsKey, $expire );
196 } else {
197 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
198 }
199 }
200 }