b721c26ff6c4abe07ffea07f846820e5a243e640
[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 * Before using any of the cahing functionality, call startCache.
7 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
8 *
9 * To get a cached value or compute it, use getCachedValue like this:
10 * $this->getCachedValue( $callback );
11 *
12 * To add HTML that should be cached, use addCachedHTML like this:
13 * $this->addCachedHTML( $callback );
14 *
15 * The callback function is only called when needed, so do all your expensive
16 * computations here. This function should returns the HTML to be cached.
17 * It should not add anything to the PageOutput object!
18 *
19 * @since 1.20
20 *
21 * @file SpecialCachedPage.php
22 * @ingroup SpecialPage
23 *
24 * @licence GNU GPL v2 or later
25 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
26 */
27 abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
28
29 /**
30 * CacheHelper object to which we foreward the non-SpecialPage specific caching work.
31 * Initialized in startCache.
32 *
33 * @since 1.20
34 * @var CacheHelper
35 */
36 protected $cacheHelper;
37
38 /**
39 * Sets if the cache should be enabled or not.
40 *
41 * @since 1.20
42 * @param boolean $cacheEnabled
43 */
44 public function setCacheEnabled( $cacheEnabled ) {
45 $this->cacheHelper->setCacheEnabled( $cacheEnabled );
46 }
47
48 /**
49 * Initializes the caching.
50 * Should be called before the first time anything is added via addCachedHTML.
51 *
52 * @since 1.20
53 *
54 * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
55 * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
56 */
57 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
58 $this->cacheHelper = new CacheHelper( $this->get );
59
60 $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
61
62 $keyArgs = $this->getCacheKey();
63
64 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
65 unset( $keyArgs['action'] );
66 }
67
68 $this->cacheHelper->setCacheKey( $keyArgs );
69
70 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
71 $this->cacheHelper->purge();
72 }
73
74 $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
75 }
76
77 /**
78 * Add some HTML to be cached.
79 * This is done by providing a callback function that should
80 * return the HTML to be added. It will only be called if the
81 * item is not in the cache yet or when the cache has been invalidated.
82 *
83 * @since 1.20
84 *
85 * @param {function} $computeFunction
86 * @param array $args
87 * @param string|null $key
88 */
89 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
90 $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
91 }
92
93 /**
94 * Saves the HTML to the cache in case it got recomputed.
95 * Should be called after the last time anything is added via addCachedHTML.
96 *
97 * @since 1.20
98 */
99 public function saveCache() {
100 $this->cacheHelper->saveCache();
101 }
102
103 /**
104 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry..
105 *
106 * @since 1.20
107 *
108 * @param integer $cacheExpiry
109 */
110 public function setExpirey( $cacheExpiry ) {
111 $this->cacheHelper->setExpirey( $cacheExpiry );
112 }
113
114 /**
115 * Returns the variables used to constructed the cache key in an array.
116 *
117 * @since 1.20
118 *
119 * @return array
120 */
121 protected function getCacheKey() {
122 return array(
123 $this->mName,
124 $this->getLanguage()->getCode()
125 );
126 }
127
128 /**
129 * Gets called after the cache got initialized.
130 *
131 * @since 1.20
132 *
133 * @param boolean $hasCached
134 */
135 public function onCacheInitialized( $hasCached ) {
136 if ( $hasCached ) {
137 $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
138 }
139 }
140
141 }