Add a way for packagers to override some installation details
[lhc/web/wiklou.git] / includes / CacheHelper.php
1 <?php
2 /**
3 * Cache of various elements in a single cache entry.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @licence GNU GPL v2 or later
22 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
23 */
24
25 /**
26 * Interface for all classes implementing CacheHelper functionality.
27 *
28 * @since 1.20
29 */
30 interface ICacheHelper {
31
32 /**
33 * Sets if the cache should be enabled or not.
34 *
35 * @since 1.20
36 * @param boolean $cacheEnabled
37 */
38 function setCacheEnabled( $cacheEnabled );
39
40 /**
41 * Initializes the caching.
42 * Should be called before the first time anything is added via addCachedHTML.
43 *
44 * @since 1.20
45 *
46 * @param integer|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
47 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
48 */
49 function startCache( $cacheExpiry = null, $cacheEnabled = null );
50
51 /**
52 * Get a cached value if available or compute it if not and then cache it if possible.
53 * The provided $computeFunction is only called when the computation needs to happen
54 * and should return a result value. $args are arguments that will be passed to the
55 * compute function when called.
56 *
57 * @since 1.20
58 *
59 * @param {function} $computeFunction
60 * @param array|mixed $args
61 * @param string|null $key
62 *
63 * @return mixed
64 */
65 function getCachedValue( $computeFunction, $args = array(), $key = null );
66
67 /**
68 * Saves the HTML to the cache in case it got recomputed.
69 * Should be called after the last time anything is added via addCachedHTML.
70 *
71 * @since 1.20
72 */
73 function saveCache();
74
75 /**
76 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
77 *
78 * @since 1.20
79 *
80 * @param integer $cacheExpiry
81 */
82 function setExpiry( $cacheExpiry );
83
84 }
85
86 /**
87 * Helper class for caching various elements in a single cache entry.
88 *
89 * To get a cached value or compute it, use getCachedValue like this:
90 * $this->getCachedValue( $callback );
91 *
92 * To add HTML that should be cached, use addCachedHTML like this:
93 * $this->addCachedHTML( $callback );
94 *
95 * The callback function is only called when needed, so do all your expensive
96 * computations here. This function should returns the HTML to be cached.
97 * It should not add anything to the PageOutput object!
98 *
99 * Before the first addCachedHTML call, you should call $this->startCache();
100 * After adding the last HTML that should be cached, call $this->saveCache();
101 *
102 * @since 1.20
103 */
104 class CacheHelper implements ICacheHelper {
105
106 /**
107 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
108 *
109 * @since 1.20
110 * @var integer
111 */
112 protected $cacheExpiry = 3600;
113
114 /**
115 * List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
116 * If not cached already, then the newly computed chunks are added here,
117 * if it as cached already, chunks are removed from this list as they are needed.
118 *
119 * @since 1.20
120 * @var array
121 */
122 protected $cachedChunks;
123
124 /**
125 * Indicates if the to be cached content was already cached.
126 * Null if this information is not available yet.
127 *
128 * @since 1.20
129 * @var boolean|null
130 */
131 protected $hasCached = null;
132
133 /**
134 * If the cache is enabled or not.
135 *
136 * @since 1.20
137 * @var boolean
138 */
139 protected $cacheEnabled = true;
140
141 /**
142 * Function that gets called when initialization is done.
143 *
144 * @since 1.20
145 * @var function
146 */
147 protected $onInitHandler = false;
148
149 /**
150 * Sets if the cache should be enabled or not.
151 *
152 * @since 1.20
153 * @param boolean $cacheEnabled
154 */
155 public function setCacheEnabled( $cacheEnabled ) {
156 $this->cacheEnabled = $cacheEnabled;
157 }
158
159 /**
160 * Initializes the caching.
161 * Should be called before the first time anything is added via addCachedHTML.
162 *
163 * @since 1.20
164 *
165 * @param integer|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
166 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
167 */
168 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
169 if ( is_null( $this->hasCached ) ) {
170 if ( !is_null( $cacheExpiry ) ) {
171 $this->cacheExpiry = $cacheExpiry;
172 }
173
174 if ( !is_null( $cacheEnabled ) ) {
175 $this->setCacheEnabled( $cacheEnabled );
176 }
177
178 $this->initCaching();
179 }
180 }
181
182 /**
183 * Returns a message that notifies the user he/she is looking at
184 * a cached version of the page, including a refresh link.
185 *
186 * @since 1.20
187 *
188 * @param IContextSource $context
189 * @param boolean $includePurgeLink
190 *
191 * @return string
192 */
193 public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
194 if ( $this->cacheExpiry < 86400 * 3650 ) {
195 $message = $context->msg(
196 'cachedspecial-viewing-cached-ttl',
197 $context->getLanguage()->formatDuration( $this->cacheExpiry )
198 )->escaped();
199 }
200 else {
201 $message = $context->msg(
202 'cachedspecial-viewing-cached-ts'
203 )->escaped();
204 }
205
206 if ( $includePurgeLink ) {
207 $refreshArgs = $context->getRequest()->getQueryValues();
208 unset( $refreshArgs['title'] );
209 $refreshArgs['action'] = 'purge';
210
211 $subPage = $context->getTitle()->getFullText();
212 $subPage = explode( '/', $subPage, 2 );
213 $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
214
215 $message .= ' ' . Linker::link(
216 $context->getTitle( $subPage ),
217 $context->msg( 'cachedspecial-refresh-now' )->escaped(),
218 array(),
219 $refreshArgs
220 );
221 }
222
223 return $message;
224 }
225
226 /**
227 * Initializes the caching if not already done so.
228 * Should be called before any of the caching functionality is used.
229 *
230 * @since 1.20
231 */
232 protected function initCaching() {
233 if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
234 $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() );
235
236 $this->hasCached = is_array( $cachedChunks );
237 $this->cachedChunks = $this->hasCached ? $cachedChunks : array();
238
239 if ( $this->onInitHandler !== false ) {
240 call_user_func( $this->onInitHandler, $this->hasCached );
241 }
242 }
243 }
244
245 /**
246 * Get a cached value if available or compute it if not and then cache it if possible.
247 * The provided $computeFunction is only called when the computation needs to happen
248 * and should return a result value. $args are arguments that will be passed to the
249 * compute function when called.
250 *
251 * @since 1.20
252 *
253 * @param {function} $computeFunction
254 * @param array|mixed $args
255 * @param string|null $key
256 *
257 * @return mixed
258 */
259 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
260 $this->initCaching();
261
262 if ( $this->cacheEnabled && $this->hasCached ) {
263 $value = null;
264
265 if ( is_null( $key ) ) {
266 $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
267 $itemKey = array_shift( $itemKey );
268
269 if ( !is_integer( $itemKey ) ) {
270 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ );
271 }
272 elseif ( is_null( $itemKey ) ) {
273 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
274 }
275 else {
276 $value = array_shift( $this->cachedChunks );
277 }
278 }
279 else {
280 if ( array_key_exists( $key, $this->cachedChunks ) ) {
281 $value = $this->cachedChunks[$key];
282 unset( $this->cachedChunks[$key] );
283 }
284 else {
285 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
286 }
287 }
288 }
289 else {
290 if ( !is_array( $args ) ) {
291 $args = array( $args );
292 }
293
294 $value = call_user_func_array( $computeFunction, $args );
295
296 if ( $this->cacheEnabled ) {
297 if ( is_null( $key ) ) {
298 $this->cachedChunks[] = $value;
299 }
300 else {
301 $this->cachedChunks[$key] = $value;
302 }
303 }
304 }
305
306 return $value;
307 }
308
309 /**
310 * Saves the HTML to the cache in case it got recomputed.
311 * Should be called after the last time anything is added via addCachedHTML.
312 *
313 * @since 1.20
314 */
315 public function saveCache() {
316 if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
317 wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry );
318 }
319 }
320
321 /**
322 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
323 *
324 * @since 1.20
325 *
326 * @param integer $cacheExpiry
327 */
328 public function setExpiry( $cacheExpiry ) {
329 $this->cacheExpiry = $cacheExpiry;
330 }
331
332 /**
333 * Returns the cache key to use to cache this page's HTML output.
334 * Is constructed from the special page name and language code.
335 *
336 * @since 1.20
337 *
338 * @return string
339 */
340 protected function getCacheKeyString() {
341 return call_user_func_array( 'wfMemcKey', $this->cacheKey );
342 }
343
344 /**
345 * Sets the cache key that should be used.
346 *
347 * @since 1.20
348 *
349 * @param array $cacheKey
350 */
351 public function setCacheKey( array $cacheKey ) {
352 $this->cacheKey = $cacheKey;
353 }
354
355 /**
356 * Rebuild the content, even if it's already cached.
357 * This effectively has the same effect as purging the cache,
358 * since it will be overridden with the new value on the next request.
359 *
360 * @since 1.20
361 */
362 public function rebuildOnDemand() {
363 $this->hasCached = false;
364 }
365
366 /**
367 * Sets a function that gets called when initialization of the cache is done.
368 *
369 * @since 1.20
370 *
371 * @param $handlerFunction
372 */
373 public function setOnInitializedHandler( $handlerFunction ) {
374 $this->onInitHandler = $handlerFunction;
375 }
376
377 }