wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / actions / CachedAction.php
blobbc4df349b268f616f87fd49a609cdcb4d3f3b56f
1 <?php
2 /**
3 * Abstract action class with scaffolding for caching HTML and other values
4 * in a single blob.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Actions
23 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
24 * @since 1.20
27 /**
28 * Abstract action class with scaffolding for caching HTML and other values
29 * in a single blob.
31 * Before using any of the caching functionality, call startCache.
32 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
34 * To get a cached value or compute it, use getCachedValue like this:
35 * $this->getCachedValue( $callback );
37 * To add HTML that should be cached, use addCachedHTML like this:
38 * $this->addCachedHTML( $callback );
40 * The callback function is only called when needed, so do all your expensive
41 * computations here. This function should returns the HTML to be cached.
42 * It should not add anything to the PageOutput object!
44 * @ingroup Actions
46 abstract class CachedAction extends FormlessAction implements ICacheHelper {
48 /**
49 * CacheHelper object to which we forward the non-SpecialPage specific caching work.
50 * Initialized in startCache.
52 * @since 1.20
53 * @var CacheHelper
55 protected $cacheHelper;
57 /**
58 * If the cache is enabled or not.
60 * @since 1.20
61 * @var bool
63 protected $cacheEnabled = true;
65 /**
66 * Sets if the cache should be enabled or not.
68 * @since 1.20
69 * @param bool $cacheEnabled
71 public function setCacheEnabled( $cacheEnabled ) {
72 $this->cacheHelper->setCacheEnabled( $cacheEnabled );
75 /**
76 * Initializes the caching.
77 * Should be called before the first time anything is added via addCachedHTML.
79 * @since 1.20
81 * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
82 * @param bool|null $cacheEnabled Sets if the cache should be enabled or not.
84 public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
85 $this->cacheHelper = new CacheHelper();
87 $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
88 $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
90 $keyArgs = $this->getCacheKey();
92 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
93 unset( $keyArgs['action'] );
96 $this->cacheHelper->setCacheKey( $keyArgs );
98 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
99 $this->cacheHelper->rebuildOnDemand();
102 $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
106 * Get a cached value if available or compute it if not and then cache it if possible.
107 * The provided $computeFunction is only called when the computation needs to happen
108 * and should return a result value. $args are arguments that will be passed to the
109 * compute function when called.
111 * @since 1.20
113 * @param callable $computeFunction
114 * @param array|mixed $args
115 * @param string|null $key
117 * @return mixed
119 public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
120 return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
124 * Add some HTML to be cached.
125 * This is done by providing a callback function that should
126 * return the HTML to be added. It will only be called if the
127 * item is not in the cache yet or when the cache has been invalidated.
129 * @since 1.20
131 * @param callable $computeFunction
132 * @param array $args
133 * @param string|null $key
135 public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
136 $html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
137 $this->getOutput()->addHTML( $html );
141 * Saves the HTML to the cache in case it got recomputed.
142 * Should be called after the last time anything is added via addCachedHTML.
144 * @since 1.20
146 public function saveCache() {
147 $this->cacheHelper->saveCache();
151 * Sets the time to live for the cache, in seconds or a unix timestamp
152 * indicating the point of expiry.
154 * @since 1.20
156 * @param int $cacheExpiry
158 public function setExpiry( $cacheExpiry ) {
159 $this->cacheHelper->setExpiry( $cacheExpiry );
163 * Returns the variables used to constructed the cache key in an array.
165 * @since 1.20
167 * @return array
169 protected function getCacheKey() {
170 return array(
171 get_class( $this->page ),
172 $this->getName(),
173 $this->getLanguage()->getCode()
178 * Gets called after the cache got initialized.
180 * @since 1.20
182 * @param bool $hasCached
184 public function onCacheInitialized( $hasCached ) {
185 if ( $hasCached ) {
186 $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );