Index: trunk/phase3/includes/CacheHelper.php |
— | — | @@ -0,0 +1,348 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +interface ICacheHelper { |
| 5 | + |
| 6 | + /** |
| 7 | + * Sets if the cache should be enabled or not. |
| 8 | + * |
| 9 | + * @since 1.20 |
| 10 | + * @param boolean $cacheEnabled |
| 11 | + */ |
| 12 | + function setCacheEnabled( $cacheEnabled ); |
| 13 | + |
| 14 | + /** |
| 15 | + * Initializes the caching. |
| 16 | + * Should be called before the first time anything is added via addCachedHTML. |
| 17 | + * |
| 18 | + * @since 1.20 |
| 19 | + * |
| 20 | + * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp. |
| 21 | + * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not. |
| 22 | + */ |
| 23 | + function startCache( $cacheExpiry = null, $cacheEnabled = null ); |
| 24 | + |
| 25 | + /** |
| 26 | + * Add some HTML to be cached. |
| 27 | + * This is done by providing a callback function that should |
| 28 | + * return the HTML to be added. It will only be called if the |
| 29 | + * item is not in the cache yet or when the cache has been invalidated. |
| 30 | + * |
| 31 | + * @since 1.20 |
| 32 | + * |
| 33 | + * @param {function} $computeFunction |
| 34 | + * @param array $args |
| 35 | + * @param string|null $key |
| 36 | + */ |
| 37 | + function addCachedHTML( $computeFunction, $args = array(), $key = null ); |
| 38 | + |
| 39 | + /** |
| 40 | + * Saves the HTML to the cache in case it got recomputed. |
| 41 | + * Should be called after the last time anything is added via addCachedHTML. |
| 42 | + * |
| 43 | + * @since 1.20 |
| 44 | + */ |
| 45 | + function saveCache(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.. |
| 49 | + * |
| 50 | + * @since 1.20 |
| 51 | + * |
| 52 | + * @param integer $cacheExpiry |
| 53 | + */ |
| 54 | + function setExpirey( $cacheExpiry ); |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Helper class for caching various elements in a single cache entry. |
| 60 | + * |
| 61 | + * To get a cached value or compute it, use getCachedValue like this: |
| 62 | + * $this->getCachedValue( $callback ); |
| 63 | + * |
| 64 | + * To add HTML that should be cached, use addCachedHTML like this: |
| 65 | + * $this->addCachedHTML( $callback ); |
| 66 | + * |
| 67 | + * The callback function is only called when needed, so do all your expensive |
| 68 | + * computations here. This function should returns the HTML to be cached. |
| 69 | + * It should not add anything to the PageOutput object! |
| 70 | + * |
| 71 | + * Before the first addCachedHTML call, you should call $this->startCache(); |
| 72 | + * After adding the last HTML that should be cached, call $this->saveCache(); |
| 73 | + * |
| 74 | + * @since 1.20 |
| 75 | + * |
| 76 | + * @file CacheHelper.php |
| 77 | + * @ingroup SpecialPage |
| 78 | + * |
| 79 | + * @licence GNU GPL v2 or later |
| 80 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 81 | + */ |
| 82 | +class CacheHelper implements ICacheHelper { |
| 83 | + |
| 84 | + /** |
| 85 | + * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. |
| 86 | + * |
| 87 | + * @since 1.20 |
| 88 | + * @var integer |
| 89 | + */ |
| 90 | + protected $cacheExpiry = 3600; |
| 91 | + |
| 92 | + /** |
| 93 | + * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached). |
| 94 | + * If no cached already, then the newly computed chunks are added here, |
| 95 | + * if it as cached already, chunks are removed from this list as they are needed. |
| 96 | + * |
| 97 | + * @since 1.20 |
| 98 | + * @var array |
| 99 | + */ |
| 100 | + protected $cachedChunks; |
| 101 | + |
| 102 | + /** |
| 103 | + * Indicates if the to be cached content was already cached. |
| 104 | + * Null if this information is not available yet. |
| 105 | + * |
| 106 | + * @since 1.20 |
| 107 | + * @var boolean|null |
| 108 | + */ |
| 109 | + protected $hasCached = null; |
| 110 | + |
| 111 | + /** |
| 112 | + * If the cache is enabled or not. |
| 113 | + * |
| 114 | + * @since 1.20 |
| 115 | + * @var boolean |
| 116 | + */ |
| 117 | + protected $cacheEnabled = true; |
| 118 | + |
| 119 | + /** |
| 120 | + * Function that gets called when initialization is done. |
| 121 | + * |
| 122 | + * @since 1.20 |
| 123 | + * @var function |
| 124 | + */ |
| 125 | + protected $onInitHandler = false; |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets if the cache should be enabled or not. |
| 129 | + * |
| 130 | + * @since 1.20 |
| 131 | + * @param boolean $cacheEnabled |
| 132 | + */ |
| 133 | + public function setCacheEnabled( $cacheEnabled ) { |
| 134 | + $this->cacheEnabled = $cacheEnabled; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Initializes the caching. |
| 139 | + * Should be called before the first time anything is added via addCachedHTML. |
| 140 | + * |
| 141 | + * @since 1.20 |
| 142 | + * |
| 143 | + * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp. |
| 144 | + * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not. |
| 145 | + */ |
| 146 | + public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { |
| 147 | + if ( is_null( $this->hasCached ) ) { |
| 148 | + if ( !is_null( $cacheExpiry ) ) { |
| 149 | + $this->cacheExpiry = $cacheExpiry; |
| 150 | + } |
| 151 | + |
| 152 | + if ( !is_null( $cacheEnabled ) ) { |
| 153 | + $this->setCacheEnabled( $cacheEnabled ); |
| 154 | + } |
| 155 | + |
| 156 | + $this->initCaching(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns a message that notifies the user he/she is looking at |
| 162 | + * a cached version of the page, including a refresh link. |
| 163 | + * |
| 164 | + * @since 1.20 |
| 165 | + * |
| 166 | + * @param IContextSource $context |
| 167 | + * |
| 168 | + * @return string |
| 169 | + */ |
| 170 | + public function getCachedNotice( IContextSource $context ) { |
| 171 | + $refreshArgs = $context->getRequest()->getQueryValues(); |
| 172 | + unset( $refreshArgs['title'] ); |
| 173 | + $refreshArgs['action'] = 'purge'; |
| 174 | + |
| 175 | + $subPage = $context->getTitle()->getFullText(); |
| 176 | + $subPage = explode( '/', $subPage, 2 ); |
| 177 | + $subPage = count( $subPage ) > 1 ? $subPage[1] : false; |
| 178 | + |
| 179 | + $refreshLink = Linker::link( |
| 180 | + $context->getTitle( $subPage ), |
| 181 | + $context->msg( 'cachedspecial-refresh-now' )->escaped(), |
| 182 | + array(), |
| 183 | + $refreshArgs |
| 184 | + ); |
| 185 | + |
| 186 | + if ( $this->cacheExpiry < 86400 * 3650 ) { |
| 187 | + $message = $context->msg( |
| 188 | + 'cachedspecial-viewing-cached-ttl', |
| 189 | + $context->getLanguage()->formatDuration( $this->cacheExpiry ) |
| 190 | + )->escaped(); |
| 191 | + } |
| 192 | + else { |
| 193 | + $message = $context->msg( |
| 194 | + 'cachedspecial-viewing-cached-ts' |
| 195 | + )->escaped(); |
| 196 | + } |
| 197 | + |
| 198 | + return $message . ' ' . $refreshLink; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Initializes the caching if not already done so. |
| 203 | + * Should be called before any of the caching functionality is used. |
| 204 | + * |
| 205 | + * @since 1.20 |
| 206 | + */ |
| 207 | + protected function initCaching() { |
| 208 | + if ( $this->cacheEnabled && is_null( $this->hasCached ) ) { |
| 209 | + $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() ); |
| 210 | + |
| 211 | + $this->hasCached = is_array( $cachedChunks ); |
| 212 | + $this->cachedChunks = $this->hasCached ? $cachedChunks : array(); |
| 213 | + |
| 214 | + if ( $this->onInitHandler !== false ) { |
| 215 | + call_user_func( $this->onInitHandler, $this->hasCached ); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + /** |
| 223 | + * Add some HTML to be cached. |
| 224 | + * This is done by providing a callback function that should |
| 225 | + * return the HTML to be added. It will only be called if the |
| 226 | + * item is not in the cache yet or when the cache has been invalidated. |
| 227 | + * |
| 228 | + * @since 1.20 |
| 229 | + * |
| 230 | + * @param {function} $computeFunction |
| 231 | + * @param array $args |
| 232 | + * @param string|null $key |
| 233 | + */ |
| 234 | + public function addCachedHTML( $computeFunction, $args = array(), $key = null ) { |
| 235 | + $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) ); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Get a cached value if available or compute it if not and then cache it if possible. |
| 240 | + * The provided $computeFunction is only called when the computation needs to happen |
| 241 | + * and should return a result value. $args are arguments that will be passed to the |
| 242 | + * compute function when called. |
| 243 | + * |
| 244 | + * @since 1.20 |
| 245 | + * |
| 246 | + * @param {function} $computeFunction |
| 247 | + * @param array|mixed $args |
| 248 | + * @param string|null $key |
| 249 | + * |
| 250 | + * @return mixed |
| 251 | + */ |
| 252 | + public function getCachedValue( $computeFunction, $args = array(), $key = null ) { |
| 253 | + $this->initCaching(); |
| 254 | + |
| 255 | + if ( $this->cacheEnabled && $this->hasCached ) { |
| 256 | + $value = null; |
| 257 | + |
| 258 | + if ( is_null( $key ) ) { |
| 259 | + $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) ); |
| 260 | + $itemKey = array_shift( $itemKey ); |
| 261 | + |
| 262 | + if ( !is_integer( $itemKey ) ) { |
| 263 | + wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ ); |
| 264 | + } |
| 265 | + elseif ( is_null( $itemKey ) ) { |
| 266 | + wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ ); |
| 267 | + } |
| 268 | + else { |
| 269 | + $value = array_shift( $this->cachedChunks ); |
| 270 | + } |
| 271 | + } |
| 272 | + else { |
| 273 | + if ( array_key_exists( $key, $this->cachedChunks ) ) { |
| 274 | + $value = $this->cachedChunks[$key]; |
| 275 | + unset( $this->cachedChunks[$key] ); |
| 276 | + } |
| 277 | + else { |
| 278 | + wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ ); |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + else { |
| 283 | + if ( !is_array( $args ) ) { |
| 284 | + $args = array( $args ); |
| 285 | + } |
| 286 | + |
| 287 | + $value = call_user_func_array( $computeFunction, $args ); |
| 288 | + |
| 289 | + if ( $this->cacheEnabled ) { |
| 290 | + if ( is_null( $key ) ) { |
| 291 | + $this->cachedChunks[] = $value; |
| 292 | + } |
| 293 | + else { |
| 294 | + $this->cachedChunks[$key] = $value; |
| 295 | + } |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + return $value; |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Saves the HTML to the cache in case it got recomputed. |
| 304 | + * Should be called after the last time anything is added via addCachedHTML. |
| 305 | + * |
| 306 | + * @since 1.20 |
| 307 | + */ |
| 308 | + public function saveCache() { |
| 309 | + if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) { |
| 310 | + wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry ); |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.. |
| 316 | + * |
| 317 | + * @since 1.20 |
| 318 | + * |
| 319 | + * @param integer $cacheExpiry |
| 320 | + */ |
| 321 | + public function setExpirey( $cacheExpiry ) { |
| 322 | + $this->cacheExpiry = $cacheExpiry; |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Returns the cache key to use to cache this page's HTML output. |
| 327 | + * Is constructed from the special page name and language code. |
| 328 | + * |
| 329 | + * @since 1.20 |
| 330 | + * |
| 331 | + * @return string |
| 332 | + */ |
| 333 | + protected function getCacheKeyString() { |
| 334 | + return call_user_func_array( 'wfMemcKey', $this->cacheKey ); |
| 335 | + } |
| 336 | + |
| 337 | + public function setCacheKey( array $cacheKey ) { |
| 338 | + $this->cacheKey = $cacheKey; |
| 339 | + } |
| 340 | + |
| 341 | + public function purge() { |
| 342 | + $this->hasCached = false; |
| 343 | + } |
| 344 | + |
| 345 | + public function setOnInitializedHandler( $handlerFunction ) { |
| 346 | + $this->onInitHandler = $handlerFunction; |
| 347 | + } |
| 348 | + |
| 349 | +} |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -27,6 +27,7 @@ |
28 | 28 | 'BadTitleError' => 'includes/Exception.php', |
29 | 29 | 'BaseTemplate' => 'includes/SkinTemplate.php', |
30 | 30 | 'Block' => 'includes/Block.php', |
| 31 | + 'CacheHelper' => 'includes/CacheHelper.php', |
31 | 32 | 'Category' => 'includes/Category.php', |
32 | 33 | 'Categoryfinder' => 'includes/Categoryfinder.php', |
33 | 34 | 'CategoryPage' => 'includes/CategoryPage.php', |
— | — | @@ -119,6 +120,7 @@ |
120 | 121 | 'Http' => 'includes/HttpFunctions.php', |
121 | 122 | 'HttpError' => 'includes/Exception.php', |
122 | 123 | 'HttpRequest' => 'includes/HttpFunctions.old.php', |
| 124 | + 'ICacheHelper' => 'includes/CacheHelper.php', |
123 | 125 | 'IcuCollation' => 'includes/Collation.php', |
124 | 126 | 'IdentityCollation' => 'includes/Collation.php', |
125 | 127 | 'ImageGallery' => 'includes/ImageGallery.php', |
Index: trunk/phase3/includes/specials/SpecialCachedPage.php |
— | — | @@ -3,6 +3,12 @@ |
4 | 4 | /** |
5 | 5 | * Abstract special page class with scaffolding for caching the HTML output. |
6 | 6 | * |
| 7 | + * Before using any of the cahing functionality, call startCache. |
| 8 | + * After the last call to either getCachedValue or addCachedHTML, call saveCache. |
| 9 | + * |
| 10 | + * To get a cached value or compute it, use getCachedValue like this: |
| 11 | + * $this->getCachedValue( $callback ); |
| 12 | + * |
7 | 13 | * To add HTML that should be cached, use addCachedHTML like this: |
8 | 14 | * $this->addCachedHTML( $callback ); |
9 | 15 | * |
— | — | @@ -10,9 +16,6 @@ |
11 | 17 | * computations here. This function should returns the HTML to be cached. |
12 | 18 | * It should not add anything to the PageOutput object! |
13 | 19 | * |
14 | | - * Before the first addCachedHTML call, you should call $this->startCache(); |
15 | | - * After adding the last HTML that should be cached, call $this->saveCache(); |
16 | | - * |
17 | 20 | * @since 1.20 |
18 | 21 | * |
19 | 22 | * @file SpecialCachedPage.php |
— | — | @@ -21,51 +24,25 @@ |
22 | 25 | * @licence GNU GPL v2 or later |
23 | 26 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
24 | 27 | */ |
25 | | -abstract class SpecialCachedPage extends SpecialPage { |
| 28 | +abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper { |
26 | 29 | |
27 | 30 | /** |
28 | | - * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. |
| 31 | + * CacheHelper object to which we foreward the non-SpecialPage specific caching work. |
| 32 | + * Initialized in startCache. |
29 | 33 | * |
30 | 34 | * @since 1.20 |
31 | | - * @var integer |
| 35 | + * @var CacheHelper |
32 | 36 | */ |
33 | | - protected $cacheExpiry = 3600; |
| 37 | + protected $cacheHelper; |
34 | 38 | |
35 | 39 | /** |
36 | | - * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached). |
37 | | - * If no cached already, then the newly computed chunks are added here, |
38 | | - * if it as cached already, chunks are removed from this list as they are needed. |
39 | | - * |
40 | | - * @since 1.20 |
41 | | - * @var array |
42 | | - */ |
43 | | - protected $cachedChunks; |
44 | | - |
45 | | - /** |
46 | | - * Indicates if the to be cached content was already cached. |
47 | | - * Null if this information is not available yet. |
48 | | - * |
49 | | - * @since 1.20 |
50 | | - * @var boolean|null |
51 | | - */ |
52 | | - protected $hasCached = null; |
53 | | - |
54 | | - /** |
55 | | - * If the cache is enabled or not. |
56 | | - * |
57 | | - * @since 1.20 |
58 | | - * @var boolean |
59 | | - */ |
60 | | - protected $cacheEnabled = true; |
61 | | - |
62 | | - /** |
63 | 40 | * Sets if the cache should be enabled or not. |
64 | 41 | * |
65 | 42 | * @since 1.20 |
66 | 43 | * @param boolean $cacheEnabled |
67 | 44 | */ |
68 | 45 | public function setCacheEnabled( $cacheEnabled ) { |
69 | | - $this->cacheEnabled = $cacheEnabled; |
| 46 | + $this->cacheHelper->setCacheEnabled( $cacheEnabled ); |
70 | 47 | } |
71 | 48 | |
72 | 49 | /** |
— | — | @@ -78,91 +55,26 @@ |
79 | 56 | * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not. |
80 | 57 | */ |
81 | 58 | public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { |
82 | | - if ( is_null( $this->hasCached ) ) { |
83 | | - if ( !is_null( $cacheExpiry ) ) { |
84 | | - $this->cacheExpiry = $cacheExpiry; |
85 | | - } |
| 59 | + $this->cacheHelper = new CacheHelper( $this->get ); |
86 | 60 | |
87 | | - if ( !is_null( $cacheEnabled ) ) { |
88 | | - $this->setCacheEnabled( $cacheEnabled ); |
89 | | - } |
| 61 | + $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) ); |
90 | 62 | |
91 | | - if ( $this->getRequest()->getText( 'action' ) === 'purge' ) { |
92 | | - $this->hasCached = false; |
93 | | - } |
| 63 | + $keyArgs = $this->getCacheKey(); |
94 | 64 | |
95 | | - $this->initCaching(); |
| 65 | + if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) { |
| 66 | + unset( $keyArgs['action'] ); |
96 | 67 | } |
97 | | - } |
98 | 68 | |
99 | | - /** |
100 | | - * Returns a message that notifies the user he/she is looking at |
101 | | - * a cached version of the page, including a refresh link. |
102 | | - * |
103 | | - * @since 1.20 |
104 | | - * |
105 | | - * @return string |
106 | | - */ |
107 | | - protected function getCachedNotice() { |
108 | | - $refreshArgs = $this->getRequest()->getQueryValues(); |
109 | | - unset( $refreshArgs['title'] ); |
110 | | - $refreshArgs['action'] = 'purge'; |
| 69 | + $this->cacheHelper->setCacheKey( $keyArgs ); |
111 | 70 | |
112 | | - $subPage = $this->getTitle()->getFullText(); |
113 | | - $subPage = explode( '/', $subPage, 2 ); |
114 | | - $subPage = count( $subPage ) > 1 ? $subPage[1] : false; |
115 | | - |
116 | | - $refreshLink = Linker::link( |
117 | | - $this->getTitle( $subPage ), |
118 | | - $this->msg( 'cachedspecial-refresh-now' )->escaped(), |
119 | | - array(), |
120 | | - $refreshArgs |
121 | | - ); |
122 | | - |
123 | | - if ( $this->cacheExpiry < 86400 * 3650 ) { |
124 | | - $message = $this->msg( |
125 | | - 'cachedspecial-viewing-cached-ttl', |
126 | | - $this->getLanguage()->formatDuration( $this->cacheExpiry ) |
127 | | - )->escaped(); |
| 71 | + if ( $this->getRequest()->getText( 'action' ) === 'purge' ) { |
| 72 | + $this->cacheHelper->purge(); |
128 | 73 | } |
129 | | - else { |
130 | | - $message = $this->msg( |
131 | | - 'cachedspecial-viewing-cached-ts' |
132 | | - )->escaped(); |
133 | | - } |
134 | 74 | |
135 | | - return $message . ' ' . $refreshLink; |
| 75 | + $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled ); |
136 | 76 | } |
137 | 77 | |
138 | 78 | /** |
139 | | - * Initializes the caching if not already done so. |
140 | | - * Should be called before any of the caching functionality is used. |
141 | | - * |
142 | | - * @since 1.20 |
143 | | - */ |
144 | | - protected function initCaching() { |
145 | | - if ( $this->cacheEnabled && is_null( $this->hasCached ) ) { |
146 | | - $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() ); |
147 | | - |
148 | | - $this->hasCached = is_array( $cachedChunks ); |
149 | | - $this->cachedChunks = $this->hasCached ? $cachedChunks : array(); |
150 | | - |
151 | | - $this->onCacheInitialized(); |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - /** |
156 | | - * Gets called after the cache got initialized. |
157 | | - * |
158 | | - * @since 1.20 |
159 | | - */ |
160 | | - protected function onCacheInitialized() { |
161 | | - if ( $this->hasCached ) { |
162 | | - $this->getOutput()->setSubtitle( $this->getCachedNotice() ); |
163 | | - } |
164 | | - } |
165 | | - |
166 | | - /** |
167 | 79 | * Add some HTML to be cached. |
168 | 80 | * This is done by providing a callback function that should |
169 | 81 | * return the HTML to be added. It will only be called if the |
— | — | @@ -175,83 +87,17 @@ |
176 | 88 | * @param string|null $key |
177 | 89 | */ |
178 | 90 | public function addCachedHTML( $computeFunction, $args = array(), $key = null ) { |
179 | | - $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) ); |
| 91 | + $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) ); |
180 | 92 | } |
181 | 93 | |
182 | 94 | /** |
183 | | - * Get a cached value if available or compute it if not and then cache it if possible. |
184 | | - * The provided $computeFunction is only called when the computation needs to happen |
185 | | - * and should return a result value. $args are arguments that will be passed to the |
186 | | - * compute function when called. |
187 | | - * |
188 | | - * @since 1.20 |
189 | | - * |
190 | | - * @param {function} $computeFunction |
191 | | - * @param array|mixed $args |
192 | | - * @param string|null $key |
193 | | - * |
194 | | - * @return mixed |
195 | | - */ |
196 | | - protected function getCachedValue( $computeFunction, $args = array(), $key = null ) { |
197 | | - $this->initCaching(); |
198 | | - |
199 | | - if ( $this->cacheEnabled && $this->hasCached ) { |
200 | | - $value = null; |
201 | | - |
202 | | - if ( is_null( $key ) ) { |
203 | | - $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) ); |
204 | | - $itemKey = array_shift( $itemKey ); |
205 | | - |
206 | | - if ( !is_integer( $itemKey ) ) { |
207 | | - wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ ); |
208 | | - } |
209 | | - elseif ( is_null( $itemKey ) ) { |
210 | | - wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ ); |
211 | | - } |
212 | | - else { |
213 | | - $value = array_shift( $this->cachedChunks ); |
214 | | - } |
215 | | - } |
216 | | - else { |
217 | | - if ( array_key_exists( $key, $this->cachedChunks ) ) { |
218 | | - $value = $this->cachedChunks[$key]; |
219 | | - unset( $this->cachedChunks[$key] ); |
220 | | - } |
221 | | - else { |
222 | | - wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ ); |
223 | | - } |
224 | | - } |
225 | | - } |
226 | | - else { |
227 | | - if ( !is_array( $args ) ) { |
228 | | - $args = array( $args ); |
229 | | - } |
230 | | - |
231 | | - $value = call_user_func_array( $computeFunction, $args ); |
232 | | - |
233 | | - if ( $this->cacheEnabled ) { |
234 | | - if ( is_null( $key ) ) { |
235 | | - $this->cachedChunks[] = $value; |
236 | | - } |
237 | | - else { |
238 | | - $this->cachedChunks[$key] = $value; |
239 | | - } |
240 | | - } |
241 | | - } |
242 | | - |
243 | | - return $value; |
244 | | - } |
245 | | - |
246 | | - /** |
247 | 95 | * Saves the HTML to the cache in case it got recomputed. |
248 | 96 | * Should be called after the last time anything is added via addCachedHTML. |
249 | 97 | * |
250 | 98 | * @since 1.20 |
251 | 99 | */ |
252 | 100 | public function saveCache() { |
253 | | - if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) { |
254 | | - wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry ); |
255 | | - } |
| 101 | + $this->cacheHelper->saveCache(); |
256 | 102 | } |
257 | 103 | |
258 | 104 | /** |
— | — | @@ -261,29 +107,11 @@ |
262 | 108 | * |
263 | 109 | * @param integer $cacheExpiry |
264 | 110 | */ |
265 | | - protected function setExpirey( $cacheExpiry ) { |
266 | | - $this->cacheExpiry = $cacheExpiry; |
| 111 | + public function setExpirey( $cacheExpiry ) { |
| 112 | + $this->cacheHelper->setExpirey( $cacheExpiry ); |
267 | 113 | } |
268 | 114 | |
269 | 115 | /** |
270 | | - * Returns the cache key to use to cache this page's HTML output. |
271 | | - * Is constructed from the special page name and language code. |
272 | | - * |
273 | | - * @since 1.20 |
274 | | - * |
275 | | - * @return string |
276 | | - */ |
277 | | - protected function getCacheKeyString() { |
278 | | - $keyArgs = $this->getCacheKey(); |
279 | | - |
280 | | - if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) { |
281 | | - unset( $keyArgs['action'] ); |
282 | | - } |
283 | | - |
284 | | - return call_user_func_array( 'wfMemcKey', $keyArgs ); |
285 | | - } |
286 | | - |
287 | | - /** |
288 | 116 | * Returns the variables used to constructed the cache key in an array. |
289 | 117 | * |
290 | 118 | * @since 1.20 |
— | — | @@ -297,4 +125,17 @@ |
298 | 126 | ); |
299 | 127 | } |
300 | 128 | |
| 129 | + /** |
| 130 | + * Gets called after the cache got initialized. |
| 131 | + * |
| 132 | + * @since 1.20 |
| 133 | + * |
| 134 | + * @param boolean $hasCached |
| 135 | + */ |
| 136 | + public function onCacheInitialized( $hasCached ) { |
| 137 | + if ( $hasCached ) { |
| 138 | + $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
301 | 142 | } |