fix subpage linking
[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( $this->hasCached ) ) {
82 if ( !is_null( $cacheExpiry ) ) {
83 $this->cacheExpiry = $cacheExpiry;
84 }
85
86 if ( !is_null( $cacheEnabled ) ) {
87 $this->setCacheEnabled( $cacheEnabled );
88 }
89
90 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
91 $this->hasCached = false;
92 }
93
94 $this->initCaching();
95 }
96 }
97
98 /**
99 * Returns a message that notifies the user he/she is looking at
100 * a cached version of the page, including a refresh link.
101 *
102 * @since 1.20
103 *
104 * @return string
105 */
106 protected function getCachedNotice() {
107 $refreshArgs = $this->getRequest()->getQueryValues();
108 unset( $refreshArgs['title'] );
109 $refreshArgs['action'] = 'purge';
110
111 $subPage = $this->getTitle()->getFullText();
112 $subPage = explode( '/', $subPage, 2 );
113 $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
114
115 $refreshLink = Linker::link(
116 $this->getTitle( $subPage ),
117 $this->msg( 'cachedspecial-refresh-now' )->escaped(),
118 array(),
119 $refreshArgs
120 );
121
122 if ( $this->cacheExpiry < 86400 * 3650 ) {
123 $message = $this->msg(
124 'cachedspecial-viewing-cached-ttl',
125 $this->getLanguage()->formatDuration( $this->cacheExpiry )
126 )->escaped();
127 }
128 else {
129 $message = $this->msg(
130 'cachedspecial-viewing-cached-ts'
131 )->escaped();
132 }
133
134 return $message . ' ' . $refreshLink;
135 }
136
137 /**
138 * Initializes the caching if not already done so.
139 * Should be called before any of the caching functionality is used.
140 *
141 * @since 1.20
142 */
143 protected function initCaching() {
144 if ( $this->cacheEnabled && is_null( $this->hasCached ) ) {
145 $cachedChunks = wfGetCache( CACHE_ANYTHING )->get( $this->getCacheKeyString() );
146
147 $this->hasCached = is_array( $cachedChunks );
148 $this->cachedChunks = $this->hasCached ? $cachedChunks : array();
149
150 $this->onCacheInitialized();
151 }
152 }
153
154 /**
155 * Gets called after the cache got initialized.
156 *
157 * @since 1.20
158 */
159 protected function onCacheInitialized() {
160 if ( $this->hasCached ) {
161 $this->getOutput()->setSubtitle( $this->getCachedNotice() );
162 }
163 }
164
165 /**
166 * Add some HTML to be cached.
167 * This is done by providing a callback function that should
168 * return the HTML to be added. It will only be called if the
169 * item is not in the cache yet or when the cache has been invalidated.
170 *
171 * @since 1.20
172 *
173 * @param {function} $computeFunction
174 * @param array $args
175 * @param string|null $key
176 */
177 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
178 $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
179 }
180
181 /**
182 * Get a cached value if available or compute it if not and then cache it if possible.
183 * The provided $computeFunction is only called when the computation needs to happen
184 * and should return a result value. $args are arguments that will be passed to the
185 * compute function when called.
186 *
187 * @since 1.20
188 *
189 * @param {function} $computeFunction
190 * @param array|mixed $args
191 * @param string|null $key
192 *
193 * @return mixed
194 */
195 protected function getCachedValue( $computeFunction, $args = array(), $key = null ) {
196 $this->initCaching();
197
198 if ( $this->cacheEnabled && $this->hasCached ) {
199 $value = null;
200
201 if ( is_null( $key ) ) {
202 $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) );
203 $itemKey = array_shift( $itemKey );
204
205 if ( !is_integer( $itemKey ) ) {
206 wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ );
207 }
208 elseif ( is_null( $itemKey ) ) {
209 wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ );
210 }
211 else {
212 $value = array_shift( $this->cachedChunks );
213 }
214 }
215 else {
216 if ( array_key_exists( $key, $this->cachedChunks ) ) {
217 $value = $this->cachedChunks[$key];
218 unset( $this->cachedChunks[$key] );
219 }
220 else {
221 wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ );
222 }
223 }
224 }
225 else {
226 if ( !is_array( $args ) ) {
227 $args = array( $args );
228 }
229
230 $value = call_user_func_array( $computeFunction, $args );
231
232 if ( $this->cacheEnabled ) {
233 if ( is_null( $key ) ) {
234 $this->cachedChunks[] = $value;
235 }
236 else {
237 $this->cachedChunks[$key] = $value;
238 }
239 }
240 }
241
242 return $value;
243 }
244
245 /**
246 * Saves the HTML to the cache in case it got recomputed.
247 * Should be called after the last time anything is added via addCachedHTML.
248 *
249 * @since 1.20
250 */
251 public function saveCache() {
252 if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) {
253 wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry );
254 }
255 }
256
257 /**
258 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
259 *
260 * @since 1.20
261 *
262 * @param integer $cacheExpiry
263 */
264 protected function setExpirey( $cacheExpiry ) {
265 $this->cacheExpiry = $cacheExpiry;
266 }
267
268 /**
269 * Returns the cache key to use to cache this page's HTML output.
270 * Is constructed from the special page name and language code.
271 *
272 * @since 1.20
273 *
274 * @return string
275 */
276 protected function getCacheKeyString() {
277 $keyArgs = $this->getCacheKey();
278
279 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
280 unset( $keyArgs['action'] );
281 }
282
283 return call_user_func_array( 'wfMemcKey', $keyArgs );
284 }
285
286 /**
287 * Returns the variables used to constructed the cache key in an array.
288 *
289 * @since 1.20
290 *
291 * @return array
292 */
293 protected function getCacheKey() {
294 return array(
295 $this->mName,
296 $this->getLanguage()->getCode()
297 );
298 }
299
300 }