Index: trunk/phase3/includes/specials/SpecialCachedPage.php |
— | — | @@ -164,15 +164,33 @@ |
165 | 165 | * |
166 | 166 | * @since 1.20 |
167 | 167 | * |
168 | | - * @param {function} $callback |
| 168 | + * @param {function} $computeFunction |
169 | 169 | * @param array $args |
170 | 170 | * @param string|null $key |
171 | 171 | */ |
172 | | - public function addCachedHTML( $callback, $args = array(), $key = null ) { |
| 172 | + public function addCachedHTML( $computeFunction, $args = array(), $key = null ) { |
| 173 | + $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Get a cached value if available or compute it if not and then cache it if possible. |
| 178 | + * The provided $computeFunction is only called when the computation needs to happen |
| 179 | + * and should return a result value. $args are arguments that will be passed to the |
| 180 | + * compute function when called. |
| 181 | + * |
| 182 | + * @since 1.20 |
| 183 | + * |
| 184 | + * @param {function} $computeFunction |
| 185 | + * @param array|mixed $args |
| 186 | + * @param string|null $key |
| 187 | + * |
| 188 | + * @return mixed |
| 189 | + */ |
| 190 | + protected function getCachedValue( $computeFunction, $args = array(), $key = null ) { |
173 | 191 | $this->initCaching(); |
174 | 192 | |
175 | 193 | if ( $this->cacheEnabled && $this->hasCached ) { |
176 | | - $html = ''; |
| 194 | + $value = null; |
177 | 195 | |
178 | 196 | if ( is_null( $key ) ) { |
179 | 197 | $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) ); |
— | — | @@ -185,12 +203,12 @@ |
186 | 204 | wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ ); |
187 | 205 | } |
188 | 206 | else { |
189 | | - $html = array_shift( $this->cachedChunks ); |
| 207 | + $value = array_shift( $this->cachedChunks ); |
190 | 208 | } |
191 | 209 | } |
192 | 210 | else { |
193 | 211 | if ( array_key_exists( $key, $this->cachedChunks ) ) { |
194 | | - $html = $this->cachedChunks[$key]; |
| 212 | + $value = $this->cachedChunks[$key]; |
195 | 213 | unset( $this->cachedChunks[$key] ); |
196 | 214 | } |
197 | 215 | else { |
— | — | @@ -199,19 +217,23 @@ |
200 | 218 | } |
201 | 219 | } |
202 | 220 | else { |
203 | | - $html = call_user_func_array( $callback, $args ); |
| 221 | + if ( !is_array( $args ) ) { |
| 222 | + $args = array( $args ); |
| 223 | + } |
204 | 224 | |
| 225 | + $value = call_user_func_array( $computeFunction, $args ); |
| 226 | + |
205 | 227 | if ( $this->cacheEnabled ) { |
206 | 228 | if ( is_null( $key ) ) { |
207 | | - $this->cachedChunks[] = $html; |
| 229 | + $this->cachedChunks[] = $value; |
208 | 230 | } |
209 | 231 | else { |
210 | | - $this->cachedChunks[$key] = $html; |
| 232 | + $this->cachedChunks[$key] = $value; |
211 | 233 | } |
212 | 234 | } |
213 | 235 | } |
214 | 236 | |
215 | | - $this->getOutput()->addHTML( $html ); |
| 237 | + return $value; |
216 | 238 | } |
217 | 239 | |
218 | 240 | /** |
— | — | @@ -246,7 +268,13 @@ |
247 | 269 | * @return string |
248 | 270 | */ |
249 | 271 | protected function getCacheKeyString() { |
250 | | - return call_user_func_array( 'wfMemcKey', $this->getCacheKey() ); |
| 272 | + $keyArgs = $this->getCacheKey(); |
| 273 | + |
| 274 | + if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) { |
| 275 | + unset( $keyArgs['action'] ); |
| 276 | + } |
| 277 | + |
| 278 | + return call_user_func_array( 'wfMemcKey', $keyArgs ); |
251 | 279 | } |
252 | 280 | |
253 | 281 | /** |