MediaWiki  1.30.2
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 
137 abstract class CacheDependency {
141  abstract function isExpired();
142 
146  function loadDependencyValues() {
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 
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();
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 
244  return $GLOBALS[$this->name] != $this->value;
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 }
FileDependency
Definition: CacheDependency.php:153
DependencyWrapper\getValue
getValue()
Get the user-defined value.
Definition: CacheDependency.php:81
DependencyWrapper\isExpired
isExpired()
Returns true if any of the dependencies have expired.
Definition: CacheDependency.php:57
FileDependency\__sleep
__sleep()
Definition: CacheDependency.php:177
MainConfigDependency\$value
$value
Definition: CacheDependency.php:253
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
GlobalDependency\isExpired
isExpired()
Definition: CacheDependency.php:239
ConstantDependency
Definition: CacheDependency.php:279
MainConfigDependency
Definition: CacheDependency.php:251
CacheDependency\loadDependencyValues
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
Definition: CacheDependency.php:146
DependencyWrapper
This class stores an arbitrary value along with its dependencies.
Definition: CacheDependency.php:31
GlobalDependency
Definition: CacheDependency.php:227
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:35
MainConfigDependency\isExpired
isExpired()
Definition: CacheDependency.php:267
DependencyWrapper\storeToCache
storeToCache( $cache, $key, $expiry=0)
Store the wrapper to a cache.
Definition: CacheDependency.php:92
DependencyWrapper\__construct
__construct( $value=false, $deps=[])
Create an instance.
Definition: CacheDependency.php:42
GlobalDependency\$value
$value
Definition: CacheDependency.php:229
value
$status value
Definition: SyntaxHighlight.class.php:315
FileDependency\isExpired
isExpired()
Definition: CacheDependency.php:196
MainConfigDependency\getConfig
getConfig()
Definition: CacheDependency.php:260
FileDependency\$filename
$filename
Definition: CacheDependency.php:154
$GLOBALS
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition: Autoload.php:10
ConstantDependency\isExpired
isExpired()
Definition: CacheDependency.php:291
MainConfigDependency\__construct
__construct( $name)
Definition: CacheDependency.php:255
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:1047
DependencyWrapper\$value
$value
Definition: CacheDependency.php:32
FileDependency\__construct
__construct( $filename, $timestamp=null)
Create a file dependency.
Definition: CacheDependency.php:169
FileDependency\loadDependencyValues
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
Definition: CacheDependency.php:183
GlobalDependency\__construct
__construct( $name)
Definition: CacheDependency.php:231
ConstantDependency\$value
$value
Definition: CacheDependency.php:281
CacheDependency
Definition: CacheDependency.php:137
ConstantDependency\__construct
__construct( $name)
Definition: CacheDependency.php:283
$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:9
GlobalDependency\$name
$name
Definition: CacheDependency.php:228
DependencyWrapper\$deps
CacheDependency[] $deps
Definition: CacheDependency.php:34
MainConfigDependency\$name
$name
Definition: CacheDependency.php:252
FileDependency\$timestamp
$timestamp
Definition: CacheDependency.php:155
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:114
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:23
ConstantDependency\$name
$name
Definition: CacheDependency.php:280
DependencyWrapper\initialiseDeps
initialiseDeps()
Initialise dependency values in preparation for storing.
Definition: CacheDependency.php:71