Apply $wgMaxArticleSize more exactly
[mediawiki.git] / includes / cache / CacheDependency.php
bloba59ba97d0a007311e1ba5872265e6ba3f422fb2d
1 <?php
2 /**
3 * Data caching with dependencies.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
23 use MediaWiki\MediaWikiServices;
25 /**
26 * This class stores an arbitrary value along with its dependencies.
27 * Users should typically only use DependencyWrapper::getValueFromCache(),
28 * rather than instantiating one of these objects directly.
29 * @ingroup Cache
31 class DependencyWrapper {
32 private $value;
33 /** @var CacheDependency[] */
34 private $deps;
36 /**
37 * Create an instance.
38 * @param mixed $value The user-supplied value
39 * @param CacheDependency|CacheDependency[] $deps A dependency or dependency
40 * array. All dependencies must be objects implementing CacheDependency.
42 function __construct( $value = false, $deps = [] ) {
43 $this->value = $value;
45 if ( !is_array( $deps ) ) {
46 $deps = [ $deps ];
49 $this->deps = $deps;
52 /**
53 * Returns true if any of the dependencies have expired
55 * @return bool
57 function isExpired() {
58 foreach ( $this->deps as $dep ) {
59 if ( $dep->isExpired() ) {
60 return true;
64 return false;
67 /**
68 * Initialise dependency values in preparation for storing. This must be
69 * called before serialization.
71 function initialiseDeps() {
72 foreach ( $this->deps as $dep ) {
73 $dep->loadDependencyValues();
77 /**
78 * Get the user-defined value
79 * @return bool|mixed
81 function getValue() {
82 return $this->value;
85 /**
86 * Store the wrapper to a cache
88 * @param BagOStuff $cache
89 * @param string $key
90 * @param int $expiry
92 function storeToCache( $cache, $key, $expiry = 0 ) {
93 $this->initialiseDeps();
94 $cache->set( $key, $this, $expiry );
97 /**
98 * Attempt to get a value from the cache. If the value is expired or missing,
99 * it will be generated with the callback function (if present), and the newly
100 * calculated value will be stored to the cache in a wrapper.
102 * @param BagOStuff $cache A cache object
103 * @param string $key The cache key
104 * @param int $expiry The expiry timestamp or interval in seconds
105 * @param bool|callable $callback The callback for generating the value, or false
106 * @param array $callbackParams The function parameters for the callback
107 * @param array $deps The dependencies to store on a cache miss. Note: these
108 * are not the dependencies used on a cache hit! Cache hits use the stored
109 * dependency array.
111 * @return mixed The value, or null if it was not present in the cache and no
112 * callback was defined.
114 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
115 $callbackParams = [], $deps = []
117 $obj = $cache->get( $key );
119 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
120 $value = $obj->value;
121 } elseif ( $callback ) {
122 $value = call_user_func_array( $callback, $callbackParams );
123 # Cache the newly-generated value
124 $wrapper = new DependencyWrapper( $value, $deps );
125 $wrapper->storeToCache( $cache, $key, $expiry );
126 } else {
127 $value = null;
130 return $value;
135 * @ingroup Cache
137 abstract class CacheDependency {
139 * Returns true if the dependency is expired, false otherwise
141 abstract function isExpired();
144 * Hook to perform any expensive pre-serialize loading of dependency values.
146 function loadDependencyValues() {
151 * @ingroup Cache
153 class FileDependency extends CacheDependency {
154 private $filename;
155 private $timestamp;
158 * Create a file dependency
160 * @param string $filename The name of the file, preferably fully qualified
161 * @param null|bool|int $timestamp The unix last modified timestamp, or false if the
162 * file does not exist. If omitted, the timestamp will be loaded from
163 * the file.
165 * A dependency on a nonexistent file will be triggered when the file is
166 * created. A dependency on an existing file will be triggered when the
167 * file is changed.
169 function __construct( $filename, $timestamp = null ) {
170 $this->filename = $filename;
171 $this->timestamp = $timestamp;
175 * @return array
177 function __sleep() {
178 $this->loadDependencyValues();
180 return [ 'filename', 'timestamp' ];
183 function loadDependencyValues() {
184 if ( is_null( $this->timestamp ) ) {
185 MediaWiki\suppressWarnings();
186 # Dependency on a non-existent file stores "false"
187 # This is a valid concept!
188 $this->timestamp = filemtime( $this->filename );
189 MediaWiki\restoreWarnings();
194 * @return bool
196 function isExpired() {
197 MediaWiki\suppressWarnings();
198 $lastmod = filemtime( $this->filename );
199 MediaWiki\restoreWarnings();
200 if ( $lastmod === false ) {
201 if ( $this->timestamp === false ) {
202 # Still nonexistent
203 return false;
204 } else {
205 # Deleted
206 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
208 return true;
210 } else {
211 if ( $lastmod > $this->timestamp ) {
212 # Modified or created
213 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
215 return true;
216 } else {
217 # Not modified
218 return false;
225 * @ingroup Cache
227 class GlobalDependency extends CacheDependency {
228 private $name;
229 private $value;
231 function __construct( $name ) {
232 $this->name = $name;
233 $this->value = $GLOBALS[$name];
237 * @return bool
239 function isExpired() {
240 if ( !isset( $GLOBALS[$this->name] ) ) {
241 return true;
244 return $GLOBALS[$this->name] != $this->value;
249 * @ingroup Cache
251 class MainConfigDependency extends CacheDependency {
252 private $name;
253 private $value;
255 function __construct( $name ) {
256 $this->name = $name;
257 $this->value = $this->getConfig()->get( $this->name );
260 private function getConfig() {
261 return MediaWikiServices::getInstance()->getMainConfig();
265 * @return bool
267 function isExpired() {
268 if ( !$this->getConfig()->has( $this->name ) ) {
269 return true;
272 return $this->getConfig()->get( $this->name ) != $this->value;
277 * @ingroup Cache
279 class ConstantDependency extends CacheDependency {
280 private $name;
281 private $value;
283 function __construct( $name ) {
284 $this->name = $name;
285 $this->value = constant( $name );
289 * @return bool
291 function isExpired() {
292 return constant( $this->name ) != $this->value;