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 
136 abstract class CacheDependency {
140  abstract function isExpired();
141 
145  function loadDependencyValues() {
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 
182  function loadDependencyValues() {
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 
243  return $GLOBALS[$this->name] != $this->value;
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 }
FileDependency
Definition: CacheDependency.php:152
DependencyWrapper\getValue
getValue()
Get the user-defined value.
Definition: CacheDependency.php:80
DependencyWrapper\isExpired
isExpired()
Returns true if any of the dependencies have expired.
Definition: CacheDependency.php:56
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
FileDependency\__sleep
__sleep()
Definition: CacheDependency.php:176
MainConfigDependency\$value
$value
Definition: CacheDependency.php:252
GlobalDependency\isExpired
isExpired()
Definition: CacheDependency.php:238
ConstantDependency
Definition: CacheDependency.php:278
MainConfigDependency
Definition: CacheDependency.php:250
CacheDependency\loadDependencyValues
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
Definition: CacheDependency.php:145
DependencyWrapper
This class stores an arbitrary value along with its dependencies.
Definition: CacheDependency.php:31
GlobalDependency
Definition: CacheDependency.php:226
php
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
MainConfigDependency\isExpired
isExpired()
Definition: CacheDependency.php:266
DependencyWrapper\storeToCache
storeToCache( $cache, $key, $expiry=0)
Store the wrapper to a cache.
Definition: CacheDependency.php:91
DependencyWrapper\__construct
__construct( $value=false, $deps=[])
Definition: CacheDependency.php:41
GlobalDependency\$value
$value
Definition: CacheDependency.php:228
FileDependency\isExpired
isExpired()
Definition: CacheDependency.php:195
MainConfigDependency\getConfig
getConfig()
Definition: CacheDependency.php:259
FileDependency\$filename
$filename
Definition: CacheDependency.php:153
ConstantDependency\isExpired
isExpired()
Definition: CacheDependency.php:290
MainConfigDependency\__construct
__construct( $name)
Definition: CacheDependency.php:254
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:994
DependencyWrapper\$value
$value
Definition: CacheDependency.php:32
FileDependency\__construct
__construct( $filename, $timestamp=null)
Create a file dependency.
Definition: CacheDependency.php:168
FileDependency\loadDependencyValues
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
Definition: CacheDependency.php:182
GlobalDependency\__construct
__construct( $name)
Definition: CacheDependency.php:230
ConstantDependency\$value
$value
Definition: CacheDependency.php:280
CacheDependency
Definition: CacheDependency.php:136
ConstantDependency\__construct
__construct( $name)
Definition: CacheDependency.php:282
$cache
$cache
Definition: mcc.php:33
CacheDependency\isExpired
isExpired()
Returns true if the dependency is expired, false otherwise.
as
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
Definition: distributors.txt:22
GlobalDependency\$name
$name
Definition: CacheDependency.php:227
DependencyWrapper\$deps
CacheDependency[] $deps
Definition: CacheDependency.php:34
MainConfigDependency\$name
$name
Definition: CacheDependency.php:251
FileDependency\$timestamp
$timestamp
Definition: CacheDependency.php:154
name
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
DependencyWrapper\getValueFromCache
static getValueFromCache( $cache, $key, $expiry=0, $callback=false, $callbackParams=[], $deps=[])
Attempt to get a value from the cache.
Definition: CacheDependency.php:113
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:25
ConstantDependency\$name
$name
Definition: CacheDependency.php:279
DependencyWrapper\initialiseDeps
initialiseDeps()
Initialise dependency values in preparation for storing.
Definition: CacheDependency.php:70
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6