MediaWiki REL1_31
CacheDependency.php
Go to the documentation of this file.
1<?php
24
32 private $value;
34 private $deps;
35
41 function __construct( $value = false, $deps = [] ) {
42 $this->value = $value;
43
44 if ( !is_array( $deps ) ) {
45 $deps = [ $deps ];
46 }
47
48 $this->deps = $deps;
49 }
50
56 function isExpired() {
57 foreach ( $this->deps as $dep ) {
58 if ( $dep->isExpired() ) {
59 return true;
60 }
61 }
62
63 return false;
64 }
65
70 function initialiseDeps() {
71 foreach ( $this->deps as $dep ) {
72 $dep->loadDependencyValues();
73 }
74 }
75
80 function getValue() {
81 return $this->value;
82 }
83
91 function storeToCache( $cache, $key, $expiry = 0 ) {
92 $this->initialiseDeps();
93 $cache->set( $key, $this, $expiry );
94 }
95
113 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
114 $callbackParams = [], $deps = []
115 ) {
116 $obj = $cache->get( $key );
117
118 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
119 $value = $obj->value;
120 } elseif ( $callback ) {
121 $value = call_user_func_array( $callback, $callbackParams );
122 # Cache the newly-generated value
123 $wrapper = new DependencyWrapper( $value, $deps );
124 $wrapper->storeToCache( $cache, $key, $expiry );
125 } else {
126 $value = null;
127 }
128
129 return $value;
130 }
131}
132
136abstract class CacheDependency {
140 abstract function isExpired();
141
146 }
147}
148
153 private $filename;
154 private $timestamp;
155
168 function __construct( $filename, $timestamp = null ) {
169 $this->filename = $filename;
170 $this->timestamp = $timestamp;
171 }
172
176 function __sleep() {
177 $this->loadDependencyValues();
178
179 return [ 'filename', 'timestamp' ];
180 }
181
183 if ( is_null( $this->timestamp ) ) {
184 Wikimedia\suppressWarnings();
185 # Dependency on a non-existent file stores "false"
186 # This is a valid concept!
187 $this->timestamp = filemtime( $this->filename );
188 Wikimedia\restoreWarnings();
189 }
190 }
191
195 function isExpired() {
196 Wikimedia\suppressWarnings();
197 $lastmod = filemtime( $this->filename );
198 Wikimedia\restoreWarnings();
199 if ( $lastmod === false ) {
200 if ( $this->timestamp === false ) {
201 # Still nonexistent
202 return false;
203 } else {
204 # Deleted
205 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
206
207 return true;
208 }
209 } else {
210 if ( $lastmod > $this->timestamp ) {
211 # Modified or created
212 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
213
214 return true;
215 } else {
216 # Not modified
217 return false;
218 }
219 }
220 }
221}
222
227 private $name;
228 private $value;
229
230 function __construct( $name ) {
231 $this->name = $name;
232 $this->value = $GLOBALS[$name];
233 }
234
238 function isExpired() {
239 if ( !isset( $GLOBALS[$this->name] ) ) {
240 return true;
241 }
242
244 }
245}
246
251 private $name;
252 private $value;
253
254 function __construct( $name ) {
255 $this->name = $name;
256 $this->value = $this->getConfig()->get( $this->name );
257 }
258
259 private function getConfig() {
260 return MediaWikiServices::getInstance()->getMainConfig();
261 }
262
266 function isExpired() {
267 if ( !$this->getConfig()->has( $this->name ) ) {
268 return true;
269 }
270
271 return $this->getConfig()->get( $this->name ) != $this->value;
272 }
273}
274
279 private $name;
280 private $value;
281
282 function __construct( $name ) {
283 $this->name = $name;
284 $this->value = constant( $name );
285 }
286
290 function isExpired() {
291 return constant( $this->name ) != $this->value;
292 }
293}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$GLOBALS['IP']
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=[])
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.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition design.txt:12
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$cache
Definition mcc.php:33