make more generic: do not assume we want to add html to the output. Also get rid...
[lhc/web/wiklou.git] / includes / specials / SpecialCachedPage.php
1 <?php
2
3 /**
4 * Abstract special page class with scaffolding for caching the HTML output.
5 *
6 * To add HTML that should be cached, use addCachedHTML like this:
7 * $this->addCachedHTML( $callback );
8 *
9 * The callback function is only called when needed, so do all your expensive
10 * computations here. This function should returns the HTML to be cached.
11 * It should not add anything to the PageOutput object!
12 *
13 * Before the first addCachedHTML call, you should call $this->startCache();
14 * After adding the last HTML that should be cached, call $this->saveCache();
15 *
16 * @since 1.20
17 *
18 * @file SpecialCachedPage.php
19 * @ingroup SpecialPage
20 *
21 * @licence GNU GPL v2 or later
22 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
23 */
24 abstract class SpecialCachedPage extends SpecialPage {
25
26 /**
27 * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
28 *
29 * @since 1.20
30 * @var integer
31 */
32 protected $cacheExpiry = 3600;
33
34 /**
35 * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached).
36 * If no cached already, then the newly computed chunks are added here,
37 * if it as cached already, chunks are removed from this list as they are needed.
38 *
39 * @since 1.20
40 * @var array
41 */
42 protected $cachedChunks;
43
44 /**
45 * Indicates if the to be cached content was already cached.
46 * Null if this information is not available yet.
47 *
48 * @since 1.20
49 * @var boolean|null
50 */
51 protected $hasCached = null;
52
53 /**
54 * If the cache is enabled or not.
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.
63 *
64 * @since 1.20
65 * @param boolean $cacheEnabled
66 */
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
89 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
90 $this->hasCached = false;
91 }
92
93 $this->initCaching();
94 }
95
96 /**
97 * Returns a message that notifies the user he/she is looking at
98 * a cached version of the page, including a refresh link.
99 *
100 * @since 1.20
101 *
102 * @return string
103 */
104 protected function getCachedNotice() {
105 $refreshArgs = $this->getRequest()->getQueryValues();
106 unset( $refreshArgs['title'] );
107 $refreshArgs['action'] = 'purge';
108
109 $refreshLink = Linker::link(
110 $this->getTitle( $this->getTitle()->getSubpageText() ),
111 $this->msg( 'cachedspecial-refresh-now' )->escaped(),
112 array(),
113 $refreshArgs
114 );
115
116 if ( $this->cacheExpiry < 86400 * 3650 ) {
117 $message = $this->msg(
118 'cachedspecial-viewing-cached-ttl',
119 $this->getLanguage()->formatDuration( $this->cacheExpiry )
120 )->escaped();
121 }
122 else {
123 $message = $this->msg(
124 'cachedspecial-viewing-cached-ts'
125 )->escaped();
126 }
127
128 return $message . ' ' . $refreshLink;
129 }
130
131 /**
132 * Initializes the caching if not already done so.
133 * Should be called before any of the caching functionality is used.
134 *
135 * @since 1.20
136 */
137 protected function initCaching() {
138 if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
139 $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() );
140
141 $this->hasCached = is_array( $cachedChunks );
142 $this->cachedChunks = $this->hasCached ? $cachedChunks : array();
143
144 $this->onCacheInitialized();
145 }
146 }
147
148 /**
149 * Gets called after the cache got initialized.
150 *
151 * @since 1.20
152 */
153 protected function onCacheInitialized() {
154 if ( $this->hasCached ) {
155 $this->getOutput()->setSubtitle( $this->getCachedNotice() );
156 }
157 }
158
159 /**
160 * Add some HTML to be cached.
161 * This is done by providing a callback function that should
162 * return the HTML to be added. It will only be called if the
163 * item is not in the cache yet or when the cache has been invalidated.
164 *
165 * @since 1.20
166 *
167 * @param {function} $computeFunction
168 * @param array $args
169 * @param string|null $key
170 */
171 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
172 $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
173 }
174
175 /**
176 * Get a cached value if available or compute it if not and then cache it if possible.
177 * The provided $computeFunction is only called when the computation needs to happen
178 * and should return a result value. $args are arguments that will be passed to the
179 * compute function when called.
180 *
181 * @since 1.20
182 *
183 * @param {function} $computeFunction
184 * @param array|mixed $args
185 * @param string|null $key
186 *
187 * @return mixed
188 */
189 protected function getCachedValue( $computeFunction, $args = array(), $key = null ) {
190 $this->initCaching();
191
192 if ( $this->cacheEnabled && $this->hasCached ) {
193 $value = null;
194
195 if ( is_null( $key ) ) {
196 $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
197 $itemKey = array_shift( $itemKey );
198
199 if ( !is_integer( $itemKey ) ) {
200 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ );
201 }
202 elseif ( is_null( $itemKey ) ) {
203 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
204 }
205 else {
206 $value = array_shift( $this->cachedChunks );
207 }
208 }
209 else {
210 if ( array_key_exists( $key, $this->cachedChunks ) ) {
211 $value = $this->cachedChunks[$key];
212 unset( $this->cachedChunks[$key] );
213 }
214 else {
215 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
216 }
217 }
218 }
219 else {
220 if ( !is_array( $args ) ) {
221 $args = array( $args );
222 }
223
224 $value = call_user_func_array( $computeFunction, $args );
225
226 if ( $this->cacheEnabled ) {
227 if ( is_null( $key ) ) {
228 $this->cachedChunks[] = $value;
229 }
230 else {
231 $this->cachedChunks[$key] = $value;
232 }
233 }
234 }
235
236 return $value;
237 }
238
239 /**
240 * Saves the HTML to the cache in case it got recomputed.
241 * Should be called after the last time anything is added via addCachedHTML.
242 *
243 * @since 1.20
244 */
245 public function saveCache() {
246 if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
247 wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry );
248 }
249 }
250
251 /**
252 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
253 *
254 * @since 1.20
255 *
256 * @param integer $cacheExpiry
257 */
258 protected function setExpirey( $cacheExpiry ) {
259 $this->cacheExpiry = $cacheExpiry;
260 }
261
262 /**
263 * Returns the cache key to use to cache this page's HTML output.
264 * Is constructed from the special page name and language code.
265 *
266 * @since 1.20
267 *
268 * @return string
269 */
270 protected function getCacheKeyString() {
271 $keyArgs = $this->getCacheKey();
272
273 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
274 unset( $keyArgs['action'] );
275 }
276
277 return call_user_func_array( 'wfMemcKey', $keyArgs );
278 }
279
280 /**
281 * Returns the variables used to constructed the cache key in an array.
282 *
283 * @since 1.20
284 *
285 * @return array
286 */
287 protected function getCacheKey() {
288 return array(
289 $this->mName,
290 $this->getLanguage()->getCode()
291 );
292 }
293
294 }