MediaWiki REL1_30
CacheDependency.php
Go to the documentation of this file.
1<?php
24
32 private $value;
34 private $deps;
35
42 function __construct( $value = false, $deps = [] ) {
43 $this->value = $value;
44
45 if ( !is_array( $deps ) ) {
46 $deps = [ $deps ];
47 }
48
49 $this->deps = $deps;
50 }
51
57 function isExpired() {
58 foreach ( $this->deps as $dep ) {
59 if ( $dep->isExpired() ) {
60 return true;
61 }
62 }
63
64 return false;
65 }
66
71 function initialiseDeps() {
72 foreach ( $this->deps as $dep ) {
73 $dep->loadDependencyValues();
74 }
75 }
76
81 function getValue() {
82 return $this->value;
83 }
84
92 function storeToCache( $cache, $key, $expiry = 0 ) {
93 $this->initialiseDeps();
94 $cache->set( $key, $this, $expiry );
95 }
96
114 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
115 $callbackParams = [], $deps = []
116 ) {
117 $obj = $cache->get( $key );
118
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;
128 }
129
130 return $value;
131 }
132}
133
137abstract class CacheDependency {
141 abstract function isExpired();
142
147 }
148}
149
154 private $filename;
155 private $timestamp;
156
169 function __construct( $filename, $timestamp = null ) {
170 $this->filename = $filename;
171 $this->timestamp = $timestamp;
172 }
173
177 function __sleep() {
178 $this->loadDependencyValues();
179
180 return [ 'filename', 'timestamp' ];
181 }
182
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();
190 }
191 }
192
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" );
207
208 return true;
209 }
210 } else {
211 if ( $lastmod > $this->timestamp ) {
212 # Modified or created
213 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
214
215 return true;
216 } else {
217 # Not modified
218 return false;
219 }
220 }
221 }
222}
223
228 private $name;
229 private $value;
230
231 function __construct( $name ) {
232 $this->name = $name;
233 $this->value = $GLOBALS[$name];
234 }
235
239 function isExpired() {
240 if ( !isset( $GLOBALS[$this->name] ) ) {
241 return true;
242 }
243
245 }
246}
247
252 private $name;
253 private $value;
254
255 function __construct( $name ) {
256 $this->name = $name;
257 $this->value = $this->getConfig()->get( $this->name );
258 }
259
260 private function getConfig() {
261 return MediaWikiServices::getInstance()->getMainConfig();
262 }
263
267 function isExpired() {
268 if ( !$this->getConfig()->has( $this->name ) ) {
269 return true;
270 }
271
272 return $this->getConfig()->get( $this->name ) != $this->value;
273 }
274}
275
280 private $name;
281 private $value;
282
283 function __construct( $name ) {
284 $this->name = $name;
285 $this->value = constant( $name );
286 }
287
291 function isExpired() {
292 return constant( $this->name ) != $this->value;
293 }
294}
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition Autoload.php:10
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
isExpired()
Returns true if the dependency is expired, false otherwise.
This class stores an arbitrary value along with its dependencies.
__construct( $value=false, $deps=[])
Create an instance.
initialiseDeps()
Initialise dependency values in preparation for storing.
static getValueFromCache( $cache, $key, $expiry=0, $callback=false, $callbackParams=[], $deps=[])
Attempt to get a value from the cache.
CacheDependency[] $deps
storeToCache( $cache, $key, $expiry=0)
Store the wrapper to a cache.
getValue()
Get the user-defined value.
isExpired()
Returns true if any of the dependencies have expired.
__construct( $filename, $timestamp=null)
Create a file dependency.
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
MediaWikiServices is the service locator for the application scope of MediaWiki.
$cache
Definition mcc.php:33