1<?php
2/**
3 * Abstract action class with scaffolding for caching HTML and other values
4 * in a single blob.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Actions
23 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
24 * @since 1.20
25 */
26
27/**
28 * Abstract action class with scaffolding for caching HTML and other values
29 * in a single blob.
30 *
31 * Before using any of the caching functionality, call startCache.
32 * After the last call to either getCachedValue or addCachedHTML, call saveCache.
33 *
34 * To get a cached value or compute it, use getCachedValue like this:
35 * $this->getCachedValue( $callback );
36 *
37 * To add HTML that should be cached, use addCachedHTML like this:
38 * $this->addCachedHTML( $callback );
39 *
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!
43 *
44 * @ingroup Actions
45 */
46abstract class CachedAction extends FormlessAction implements ICacheHelper {
47
48	/**
49	 * CacheHelper object to which we forward the non-SpecialPage specific caching work.
50	 * Initialized in startCache.
51	 *
52	 * @since 1.20
53	 * @var CacheHelper
54	 */
55	protected $cacheHelper;
56
57	/**
58	 * If the cache is enabled or not.
59	 *
60	 * @since 1.20
61	 * @var bool
62	 */
63	protected $cacheEnabled = true;
64
65	public function __construct(
66		Page $page,
67		IContextSource $context = null
68	) {
69		parent::__construct( $page, $context );
70		wfDeprecated( __CLASS__, '1.36' );
71	}
72
73	/**
74	 * Sets if the cache should be enabled or not.
75	 *
76	 * @since 1.20
77	 * @param bool $cacheEnabled
78	 */
79	public function setCacheEnabled( $cacheEnabled ) {
80		$this->cacheHelper->setCacheEnabled( $cacheEnabled );
81	}
82
83	/**
84	 * Initializes the caching.
85	 * Should be called before the first time anything is added via addCachedHTML.
86	 *
87	 * @since 1.20
88	 *
89	 * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
90	 * @param bool|null $cacheEnabled Sets if the cache should be enabled or not.
91	 */
92	public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
93		$this->cacheHelper = new CacheHelper();
94
95		$this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
96		$this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] );
97
98		$keyArgs = $this->getCacheKey();
99
100		if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
101			unset( $keyArgs['action'] );
102		}
103
104		$this->cacheHelper->setCacheKey( $keyArgs );
105
106		if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
107			$this->cacheHelper->rebuildOnDemand();
108		}
109
110		$this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
111	}
112
113	/**
114	 * Get a cached value if available or compute it if not and then cache it if possible.
115	 * The provided $computeFunction is only called when the computation needs to happen
116	 * and should return a result value. $args are arguments that will be passed to the
117	 * compute function when called.
118	 *
119	 * @since 1.20
120	 *
121	 * @param callable $computeFunction
122	 * @param array|mixed $args
123	 * @param string|null $key
124	 *
125	 * @return mixed
126	 */
127	public function getCachedValue( $computeFunction, $args = [], $key = null ) {
128		return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
129	}
130
131	/**
132	 * Add some HTML to be cached.
133	 * This is done by providing a callback function that should
134	 * return the HTML to be added. It will only be called if the
135	 * item is not in the cache yet or when the cache has been invalidated.
136	 *
137	 * @since 1.20
138	 *
139	 * @param callable $computeFunction
140	 * @param array $args
141	 * @param string|null $key
142	 */
143	public function addCachedHTML( $computeFunction, $args = [], $key = null ) {
144		$html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
145		$this->getOutput()->addHTML( $html );
146	}
147
148	/**
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.
151	 *
152	 * @since 1.20
153	 */
154	public function saveCache() {
155		$this->cacheHelper->saveCache();
156	}
157
158	/**
159	 * Sets the time to live for the cache, in seconds or a unix timestamp
160	 * indicating the point of expiry.
161	 *
162	 * @since 1.20
163	 *
164	 * @param int $cacheExpiry
165	 */
166	public function setExpiry( $cacheExpiry ) {
167		$this->cacheHelper->setExpiry( $cacheExpiry );
168	}
169
170	/**
171	 * Returns the variables used to constructed the cache key in an array.
172	 *
173	 * @since 1.20
174	 *
175	 * @return array
176	 */
177	protected function getCacheKey() {
178		return [
179			get_class( $this->getArticle() ),
180			$this->getName(),
181			$this->getLanguage()->getCode()
182		];
183	}
184
185	/**
186	 * Gets called after the cache got initialized.
187	 *
188	 * @since 1.20
189	 *
190	 * @param bool $hasCached
191	 */
192	public function onCacheInitialized( $hasCached ) {
193		if ( $hasCached ) {
194			$this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
195		}
196	}
197}
198