Merge "prop=duplicatefiles does not show duplicates under same name"
[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
77 * indicating the point of expiry...
78 *
79 * @since 1.20
80 *
81 * @param integer $cacheExpiry
82 */
83 function setExpiry( $cacheExpiry );
84
85 }
86
87 /**
88 * Helper class for caching various elements in a single cache entry.
89 *
90 * To get a cached value or compute it, use getCachedValue like this:
91 * $this->getCachedValue( $callback );
92 *
93 * To add HTML that should be cached, use addCachedHTML like this:
94 * $this->addCachedHTML( $callback );
95 *
96 * The callback function is only called when needed, so do all your expensive
97 * computations here. This function should returns the HTML to be cached.
98 * It should not add anything to the PageOutput object!
99 *
100 * Before the first addCachedHTML call, you should call $this->startCache();
101 * After adding the last HTML that should be cached, call $this->saveCache();
102 *
103 * @since 1.20
104 */
105 class CacheHelper implements ICacheHelper {
106
107 /**
108 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
109 *
110 * @since 1.20
111 * @var integer
112 */
113 protected $cacheExpiry = 3600;
114
115 /**
116 * List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached).
117 * If not cached already, then the newly computed chunks are added here,
118 * if it as cached already, chunks are removed from this list as they are needed.
119 *
120 * @since 1.20
121 * @var array
122 */
123 protected $cachedChunks;
124
125 /**
126 * Indicates if the to be cached content was already cached.
127 * Null if this information is not available yet.
128 *
129 * @since 1.20
130 * @var boolean|null
131 */
132 protected $hasCached = null;
133
134 /**
135 * If the cache is enabled or not.
136 *
137 * @since 1.20
138 * @var boolean
139 */
140 protected $cacheEnabled = true;
141
142 /**
143 * Function that gets called when initialization is done.
144 *
145 * @since 1.20
146 * @var function
147 */
148 protected $onInitHandler = false;
149
150 /**
151 * Sets if the cache should be enabled or not.
152 *
153 * @since 1.20
154 * @param boolean $cacheEnabled
155 */
156 public function setCacheEnabled( $cacheEnabled ) {
157 $this->cacheEnabled = $cacheEnabled;
158 }
159
160 /**
161 * Initializes the caching.
162 * Should be called before the first time anything is added via addCachedHTML.
163 *
164 * @since 1.20
165 *
166 * @param integer|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
167 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
168 */
169 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
170 if ( is_null( $this->hasCached ) ) {
171 if ( !is_null( $cacheExpiry ) ) {
172 $this->cacheExpiry = $cacheExpiry;
173 }
174
175 if ( !is_null( $cacheEnabled ) ) {
176 $this->setCacheEnabled( $cacheEnabled );
177 }
178
179 $this->initCaching();
180 }
181 }
182
183 /**
184 * Returns a message that notifies the user he/she is looking at
185 * a cached version of the page, including a refresh link.
186 *
187 * @since 1.20
188 *
189 * @param IContextSource $context
190 * @param boolean $includePurgeLink
191 *
192 * @return string
193 */
194 public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
195 if ( $this->cacheExpiry < 86400 * 3650 ) {
196 $message = $context->msg(
197 'cachedspecial-viewing-cached-ttl',
198 $context->getLanguage()->formatDuration( $this->cacheExpiry )
199 )->escaped();
200 }
201 else {
202 $message = $context->msg(
203 'cachedspecial-viewing-cached-ts'
204 )->escaped();
205 }
206
207 if ( $includePurgeLink ) {
208 $refreshArgs = $context->getRequest()->getQueryValues();
209 unset( $refreshArgs['title'] );
210 $refreshArgs['action'] = 'purge';
211
212 $subPage = $context->getTitle()->getFullText();
213 $subPage = explode( '/', $subPage, 2 );
214 $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
215
216 $message .= ' ' . Linker::link(
217 $context->getTitle( $subPage ),
218 $context->msg( 'cachedspecial-refresh-now' )->escaped(),
219 array(),
220 $refreshArgs
221 );
222 }
223
224 return $message;
225 }
226
227 /**
228 * Initializes the caching if not already done so.
229 * Should be called before any of the caching functionality is used.
230 *
231 * @since 1.20
232 */
233 protected function initCaching() {
234 if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
235 $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() );
236
237 $this->hasCached = is_array( $cachedChunks );
238 $this->cachedChunks = $this->hasCached ? $cachedChunks : array();
239
240 if ( $this->onInitHandler !== false ) {
241 call_user_func( $this->onInitHandler, $this->hasCached );
242 }
243 }
244 }
245
246 /**
247 * Get a cached value if available or compute it if not and then cache it if possible.
248 * The provided $computeFunction is only called when the computation needs to happen
249 * and should return a result value. $args are arguments that will be passed to the
250 * compute function when called.
251 *
252 * @since 1.20
253 *
254 * @param {function} $computeFunction
255 * @param array|mixed $args
256 * @param string|null $key
257 *
258 * @return mixed
259 */
260 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
261 $this->initCaching();
262
263 if ( $this->cacheEnabled && $this->hasCached ) {
264 $value = null;
265
266 if ( is_null( $key ) ) {
267 $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
268 $itemKey = array_shift( $itemKey );
269
270 if ( !is_integer( $itemKey ) ) {
271 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ );
272 }
273 elseif ( is_null( $itemKey ) ) {
274 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
275 }
276 else {
277 $value = array_shift( $this->cachedChunks );
278 }
279 }
280 else {
281 if ( array_key_exists( $key, $this->cachedChunks ) ) {
282 $value = $this->cachedChunks[$key];
283 unset( $this->cachedChunks[$key] );
284 }
285 else {
286 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
287 }
288 }
289 }
290 else {
291 if ( !is_array( $args ) ) {
292 $args = array( $args );
293 }
294
295 $value = call_user_func_array( $computeFunction, $args );
296
297 if ( $this->cacheEnabled ) {
298 if ( is_null( $key ) ) {
299 $this->cachedChunks[] = $value;
300 }
301 else {
302 $this->cachedChunks[$key] = $value;
303 }
304 }
305 }
306
307 return $value;
308 }
309
310 /**
311 * Saves the HTML to the cache in case it got recomputed.
312 * Should be called after the last time anything is added via addCachedHTML.
313 *
314 * @since 1.20
315 */
316 public function saveCache() {
317 if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
318 wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry );
319 }
320 }
321
322 /**
323 * Sets the time to live for the cache, in seconds or a unix timestamp
324 * indicating the point of expiry...
325 *
326 * @since 1.20
327 *
328 * @param integer $cacheExpiry
329 */
330 public function setExpiry( $cacheExpiry ) {
331 $this->cacheExpiry = $cacheExpiry;
332 }
333
334 /**
335 * Returns the cache key to use to cache this page's HTML output.
336 * Is constructed from the special page name and language code.
337 *
338 * @since 1.20
339 *
340 * @return string
341 */
342 protected function getCacheKeyString() {
343 return call_user_func_array( 'wfMemcKey', $this->cacheKey );
344 }
345
346 /**
347 * Sets the cache key that should be used.
348 *
349 * @since 1.20
350 *
351 * @param array $cacheKey
352 */
353 public function setCacheKey( array $cacheKey ) {
354 $this->cacheKey = $cacheKey;
355 }
356
357 /**
358 * Rebuild the content, even if it's already cached.
359 * This effectively has the same effect as purging the cache,
360 * since it will be overridden with the new value on the next request.
361 *
362 * @since 1.20
363 */
364 public function rebuildOnDemand() {
365 $this->hasCached = false;
366 }
367
368 /**
369 * Sets a function that gets called when initialization of the cache is done.
370 *
371 * @since 1.20
372 *
373 * @param $handlerFunction
374 */
375 public function setOnInitializedHandler( $handlerFunction ) {
376 $this->onInitHandler = $handlerFunction;
377 }
378
379 }