MediaWiki  1.23.12
CacheDependency.php
Go to the documentation of this file.
1 <?php
31  private $value;
33  private $deps;
34 
41  function __construct( $value = false, $deps = array() ) {
42  $this->value = $value;
43 
44  if ( !is_array( $deps ) ) {
45  $deps = array( $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 = array(), $deps = array()
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 array( 'filename', 'timestamp' );
180  }
181 
182  function loadDependencyValues() {
183  if ( is_null( $this->timestamp ) ) {
184  if ( !file_exists( $this->filename ) ) {
185  # Dependency on a non-existent file
186  # This is a valid concept!
187  $this->timestamp = false;
188  } else {
189  $this->timestamp = filemtime( $this->filename );
190  }
191  }
192  }
193 
197  function isExpired() {
198  if ( !file_exists( $this->filename ) ) {
199  if ( $this->timestamp === false ) {
200  # Still nonexistent
201  return false;
202  } else {
203  # Deleted
204  wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
205 
206  return true;
207  }
208  } else {
209  $lastmod = filemtime( $this->filename );
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 = constant( $name );
257  }
258 
262  function isExpired() {
263  return constant( $this->name ) != $this->value;
264  }
265 }
FileDependency
Definition: CacheDependency.php:151
DependencyWrapper\getValue
getValue()
Get the user-defined value.
Definition: CacheDependency.php:79
DependencyWrapper\isExpired
isExpired()
Returns true if any of the dependencies have expired.
Definition: CacheDependency.php:55
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
FileDependency\__sleep
__sleep()
Definition: CacheDependency.php:175
GlobalDependency\isExpired
isExpired()
Definition: CacheDependency.php:237
ConstantDependency
Definition: CacheDependency.php:249
CacheDependency\loadDependencyValues
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
Definition: CacheDependency.php:144
DependencyWrapper
This class stores an arbitrary value along with its dependencies.
Definition: CacheDependency.php:30
GlobalDependency
Definition: CacheDependency.php:225
DependencyWrapper\__construct
__construct( $value=false, $deps=array())
Create an instance.
Definition: CacheDependency.php:40
DependencyWrapper\storeToCache
storeToCache( $cache, $key, $expiry=0)
Store the wrapper to a cache.
Definition: CacheDependency.php:90
GlobalDependency\$value
$value
Definition: CacheDependency.php:227
FileDependency\isExpired
isExpired()
Definition: CacheDependency.php:196
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FileDependency\$filename
$filename
Definition: CacheDependency.php:152
ConstantDependency\isExpired
isExpired()
Definition: CacheDependency.php:261
DependencyWrapper\$value
$value
Definition: CacheDependency.php:31
FileDependency\__construct
__construct( $filename, $timestamp=null)
Create a file dependency.
Definition: CacheDependency.php:167
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:980
FileDependency\loadDependencyValues
loadDependencyValues()
Hook to perform any expensive pre-serialize loading of dependency values.
Definition: CacheDependency.php:181
GlobalDependency\__construct
__construct( $name)
Definition: CacheDependency.php:229
ConstantDependency\$value
$value
Definition: CacheDependency.php:251
CacheDependency
Definition: CacheDependency.php:135
ConstantDependency\__construct
__construct( $name)
Definition: CacheDependency.php:253
$cache
$cache
Definition: mcc.php:32
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:226
DependencyWrapper\$deps
CacheDependency[] $deps
Definition: CacheDependency.php:32
DependencyWrapper\getValueFromCache
static getValueFromCache( $cache, $key, $expiry=0, $callback=false, $callbackParams=array(), $deps=array())
Attempt to get a value from the cache.
Definition: CacheDependency.php:112
FileDependency\$timestamp
$timestamp
Definition: CacheDependency.php:153
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
ConstantDependency\$name
$name
Definition: CacheDependency.php:250
DependencyWrapper\initialiseDeps
initialiseDeps()
Initialise dependency values in preparation for storing.
Definition: CacheDependency.php:69
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6