Index: trunk/phase3/includes/specials/SpecialCachedPage.php |
— | — | @@ -4,11 +4,12 @@ |
5 | 5 | * Abstract special page class with scaffolding for caching the HTML output. |
6 | 6 | * |
7 | 7 | * To enable the caching functionality, the cacheExpiry field should be set |
8 | | - * in the constructor. |
| 8 | + * before parent::execute is called (and it should be called from execute). |
9 | 9 | * |
10 | 10 | * To add HTML that should be cached, use addCachedHTML like this: |
11 | 11 | * $this->addCachedHTML( array( $this, 'displayCachedContent' ) ); |
12 | 12 | * |
| 13 | + * Before the first addCachedHTML call, you should call $this->startCache(); |
13 | 14 | * After adding the last HTML that should be cached, call $this->saveCache(); |
14 | 15 | * |
15 | 16 | * @since 1.20 |
— | — | @@ -25,9 +26,9 @@ |
26 | 27 | * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. |
27 | 28 | * |
28 | 29 | * @since 1.20 |
29 | | - * @var integer|null |
| 30 | + * @var integer |
30 | 31 | */ |
31 | | - protected $cacheExpiry = null; |
| 32 | + protected $cacheExpiry = 3600; |
32 | 33 | |
33 | 34 | /** |
34 | 35 | * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached). |
— | — | @@ -49,24 +50,46 @@ |
50 | 51 | protected $hasCached = null; |
51 | 52 | |
52 | 53 | /** |
53 | | - * Main method. |
| 54 | + * If the cache is enabled or not. |
54 | 55 | * |
55 | 56 | * @since 1.20 |
| 57 | + * @var boolean |
| 58 | + */ |
| 59 | + protected $cacheEnabled = true; |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets if the cache should be enabled or not. |
56 | 63 | * |
57 | | - * @param string|null $subPage |
| 64 | + * @since 1.20 |
| 65 | + * @param boolean $cacheEnabled |
58 | 66 | */ |
59 | | - public function execute( $subPage ) { |
| 67 | + public function setCacheEnabled( $cacheEnabled ) { |
| 68 | + $this->cacheEnabled = $cacheEnabled; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Initializes the caching. |
| 73 | + * Should be called before the first time anything is added via addCachedHTML. |
| 74 | + * |
| 75 | + * @since 1.20 |
| 76 | + * |
| 77 | + * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp. |
| 78 | + * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not. |
| 79 | + */ |
| 80 | + public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { |
| 81 | + if ( !is_null( $cacheExpiry ) ) { |
| 82 | + $this->cacheExpiry = $cacheExpiry; |
| 83 | + } |
| 84 | + |
| 85 | + if ( !is_null( $cacheEnabled ) ) { |
| 86 | + $this->setCacheEnabled( $cacheEnabled ); |
| 87 | + } |
| 88 | + |
60 | 89 | if ( $this->getRequest()->getText( 'action' ) === 'purge' ) { |
61 | 90 | $this->hasCached = false; |
62 | 91 | } |
63 | 92 | |
64 | | - if ( !is_null( $this->cacheExpiry ) ) { |
65 | | - $this->initCaching(); |
66 | | - |
67 | | - if ( $this->hasCached === true ) { |
68 | | - $this->getOutput()->setSubtitle( $this->getCachedNotice( $subPage ) ); |
69 | | - } |
70 | | - } |
| 93 | + $this->initCaching(); |
71 | 94 | } |
72 | 95 | |
73 | 96 | /** |
— | — | @@ -75,17 +98,15 @@ |
76 | 99 | * |
77 | 100 | * @since 1.20 |
78 | 101 | * |
79 | | - * @param string|null $subPage |
80 | | - * |
81 | 102 | * @return string |
82 | 103 | */ |
83 | | - protected function getCachedNotice( $subPage ) { |
| 104 | + protected function getCachedNotice() { |
84 | 105 | $refreshArgs = $this->getRequest()->getQueryValues(); |
85 | 106 | unset( $refreshArgs['title'] ); |
86 | 107 | $refreshArgs['action'] = 'purge'; |
87 | 108 | |
88 | 109 | $refreshLink = Linker::link( |
89 | | - $this->getTitle( $subPage ), |
| 110 | + $this->getTitle( $this->getTitle()->getSubpageText() ), |
90 | 111 | $this->msg( 'cachedspecial-refresh-now' )->escaped(), |
91 | 112 | array(), |
92 | 113 | $refreshArgs |
— | — | @@ -114,13 +135,21 @@ |
115 | 136 | */ |
116 | 137 | protected function initCaching() { |
117 | 138 | if ( is_null( $this->hasCached ) ) { |
118 | | - $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKey() ); |
| 139 | + $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() ); |
119 | 140 | |
120 | 141 | $this->hasCached = is_array( $cachedChunks ); |
121 | 142 | $this->cachedChunks = $this->hasCached ? $cachedChunks : array(); |
| 143 | + |
| 144 | + $this->onCacheInitialized(); |
122 | 145 | } |
123 | 146 | } |
124 | 147 | |
| 148 | + protected function onCacheInitialized() { |
| 149 | + if ( $this->hasCached ) { |
| 150 | + $this->getOutput()->setSubtitle( $this->getCachedNotice() ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
125 | 154 | /** |
126 | 155 | * Add some HTML to be cached. |
127 | 156 | * This is done by providing a callback function that should |
— | — | @@ -136,7 +165,7 @@ |
137 | 166 | public function addCachedHTML( $callback, $args = array(), $key = null ) { |
138 | 167 | $this->initCaching(); |
139 | 168 | |
140 | | - if ( $this->hasCached ) { |
| 169 | + if ( $this->cacheEnabled && $this->hasCached ) { |
141 | 170 | $html = ''; |
142 | 171 | |
143 | 172 | if ( is_null( $key ) ) { |
— | — | @@ -166,12 +195,14 @@ |
167 | 196 | else { |
168 | 197 | $html = call_user_func_array( $callback, $args ); |
169 | 198 | |
170 | | - if ( is_null( $key ) ) { |
171 | | - $this->cachedChunks[] = $html; |
| 199 | + if ( $this->cacheEnabled ) { |
| 200 | + if ( is_null( $key ) ) { |
| 201 | + $this->cachedChunks[] = $html; |
| 202 | + } |
| 203 | + else { |
| 204 | + $this->cachedChunks[$key] = $html; |
| 205 | + } |
172 | 206 | } |
173 | | - else { |
174 | | - $this->cachedChunks[$key] = $html; |
175 | | - } |
176 | 207 | } |
177 | 208 | |
178 | 209 | $this->getOutput()->addHTML( $html ); |
— | — | @@ -184,8 +215,8 @@ |
185 | 216 | * @since 1.20 |
186 | 217 | */ |
187 | 218 | public function saveCache() { |
188 | | - if ( $this->hasCached === false && !empty( $this->cachedChunks ) ) { |
189 | | - wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKey(), $this->cachedChunks, $this->cacheExpiry ); |
| 219 | + if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) { |
| 220 | + wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry ); |
190 | 221 | } |
191 | 222 | } |
192 | 223 | |
— | — | @@ -208,8 +239,22 @@ |
209 | 240 | * |
210 | 241 | * @return string |
211 | 242 | */ |
| 243 | + protected function getCacheKeyString() { |
| 244 | + return call_user_func_array( 'wfMemcKey', $this->getCacheKey() ); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Returns the variables used to constructed the cache key in an array. |
| 249 | + * |
| 250 | + * @since 1.20 |
| 251 | + * |
| 252 | + * @return array |
| 253 | + */ |
212 | 254 | protected function getCacheKey() { |
213 | | - return wfMemcKey( $this->mName, $this->getLanguage()->getCode() ); |
| 255 | + return array( |
| 256 | + $this->mName, |
| 257 | + $this->getLanguage()->getCode() |
| 258 | + ); |
214 | 259 | } |
215 | 260 | |
216 | 261 | } |