Remove useless comments from search output
[mediawiki.git] / includes / specials / SpecialCachedPage.php
blob7763166ac61835f373918b44a5d18639594ca78c
1 <?php
3 /**
4 * Abstract special page class with scaffolding for caching HTML and other values
5 * in a single blob.
7 * Before using any of the caching functionality, call startCache.
8 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
10 * To get a cached value or compute it, use getCachedValue like this:
11 * $this->getCachedValue( $callback );
13 * To add HTML that should be cached, use addCachedHTML like this:
14 * $this->addCachedHTML( $callback );
16 * The callback function is only called when needed, so do all your expensive
17 * computations here. This function should returns the HTML to be cached.
18 * It should not add anything to the PageOutput object!
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 * http://www.gnu.org/copyleft/gpl.html
35 * @file
36 * @ingroup SpecialPage
37 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
38 * @since 1.20
40 abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
42 /**
43 * CacheHelper object to which we forward the non-SpecialPage specific caching work.
44 * Initialized in startCache.
46 * @since 1.20
47 * @var CacheHelper
49 protected $cacheHelper;
51 /**
52 * If the cache is enabled or not.
54 * @since 1.20
55 * @var bool
57 protected $cacheEnabled = true;
59 /**
60 * Gets called after @see SpecialPage::execute.
62 * @since 1.20
64 * @param string|null $subPage
66 protected function afterExecute( $subPage ) {
67 $this->saveCache();
69 parent::afterExecute( $subPage );
72 /**
73 * Sets if the cache should be enabled or not.
75 * @since 1.20
76 * @param bool $cacheEnabled
78 public function setCacheEnabled( $cacheEnabled ) {
79 $this->cacheHelper->setCacheEnabled( $cacheEnabled );
82 /**
83 * Initializes the caching.
84 * Should be called before the first time anything is added via addCachedHTML.
86 * @since 1.20
88 * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
89 * @param bool|null $cacheEnabled Sets if the cache should be enabled or not.
91 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
92 if ( !isset( $this->cacheHelper ) ) {
93 $this->cacheHelper = new CacheHelper();
95 $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
96 $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
98 $keyArgs = $this->getCacheKey();
100 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
101 unset( $keyArgs['action'] );
104 $this->cacheHelper->setCacheKey( $keyArgs );
106 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
107 $this->cacheHelper->rebuildOnDemand();
111 $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
115 * Get a cached value if available or compute it if not and then cache it if possible.
116 * The provided $computeFunction is only called when the computation needs to happen
117 * and should return a result value. $args are arguments that will be passed to the
118 * compute function when called.
120 * @since 1.20
122 * @param callable $computeFunction
123 * @param array|mixed $args
124 * @param string|null $key
126 * @return mixed
128 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
129 return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
133 * Add some HTML to be cached.
134 * This is done by providing a callback function that should
135 * return the HTML to be added. It will only be called if the
136 * item is not in the cache yet or when the cache has been invalidated.
138 * @since 1.20
140 * @param callable $computeFunction
141 * @param array $args
142 * @param string|null $key
144 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
145 $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
149 * Saves the HTML to the cache in case it got recomputed.
150 * Should be called after the last time anything is added via addCachedHTML.
152 * @since 1.20
154 public function saveCache() {
155 if ( isset( $this->cacheHelper ) ) {
156 $this->cacheHelper->saveCache();
161 * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.
163 * @since 1.20
165 * @param int $cacheExpiry
167 public function setExpiry( $cacheExpiry ) {
168 $this->cacheHelper->setExpiry( $cacheExpiry );
172 * Returns the variables used to constructed the cache key in an array.
174 * @since 1.20
176 * @return array
178 protected function getCacheKey() {
179 return array(
180 $this->mName,
181 $this->getLanguage()->getCode()
186 * Gets called after the cache got initialized.
188 * @since 1.20
190 * @param bool $hasCached
192 public function onCacheInitialized( $hasCached ) {
193 if ( $hasCached ) {
194 $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );