* Added Id to "deleted only" check to make label work
[mediawiki.git] / includes / CacheDependency.php
blob9b5ab452ed6a5075cb88ed091d1ed3c1712e87e6
1 <?php
3 /**
4 * This class stores an arbitrary value along with its dependencies.
5 * Users should typically only use DependencyWrapper::getFromCache(), rather
6 * than instantiating one of these objects directly.
7 * @ingroup Cache
8 */
9 class DependencyWrapper {
10 var $value;
11 var $deps;
13 /**
14 * Create an instance.
15 * @param $value Mixed: the user-supplied value
16 * @param $deps Mixed: a dependency or dependency array. All dependencies
17 * must be objects implementing CacheDependency.
19 function __construct( $value = false, $deps = array() ) {
20 $this->value = $value;
21 if ( !is_array( $deps ) ) {
22 $deps = array( $deps );
24 $this->deps = $deps;
27 /**
28 * Returns true if any of the dependencies have expired
30 function isExpired() {
31 foreach ( $this->deps as $dep ) {
32 if ( $dep->isExpired() ) {
33 return true;
36 return false;
39 /**
40 * Initialise dependency values in preparation for storing. This must be
41 * called before serialization.
43 function initialiseDeps() {
44 foreach ( $this->deps as $dep ) {
45 $dep->loadDependencyValues();
49 /**
50 * Get the user-defined value
52 function getValue() {
53 return $this->value;
56 /**
57 * Store the wrapper to a cache
59 function storeToCache( $cache, $key, $expiry = 0 ) {
60 $this->initialiseDeps();
61 $cache->set( $key, $this, $expiry );
64 /**
65 * Attempt to get a value from the cache. If the value is expired or missing,
66 * it will be generated with the callback function (if present), and the newly
67 * calculated value will be stored to the cache in a wrapper.
69 * @param $cache Object: a cache object such as $wgMemc
70 * @param $key String: the cache key
71 * @param $expiry Integer: the expiry timestamp or interval in seconds
72 * @param $callback Mixed: the callback for generating the value, or false
73 * @param $callbackParams Array: the function parameters for the callback
74 * @param $deps Array: the dependencies to store on a cache miss. Note: these
75 * are not the dependencies used on a cache hit! Cache hits use the stored
76 * dependency array.
78 * @return mixed The value, or null if it was not present in the cache and no
79 * callback was defined.
81 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
82 $callbackParams = array(), $deps = array() )
84 $obj = $cache->get( $key );
85 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
86 $value = $obj->value;
87 } elseif ( $callback ) {
88 $value = call_user_func_array( $callback, $callbackParams );
89 # Cache the newly-generated value
90 $wrapper = new DependencyWrapper( $value, $deps );
91 $wrapper->storeToCache( $cache, $key, $expiry );
92 } else {
93 $value = null;
95 return $value;
99 /**
100 * @ingroup Cache
102 abstract class CacheDependency {
104 * Returns true if the dependency is expired, false otherwise
106 abstract function isExpired();
109 * Hook to perform any expensive pre-serialize loading of dependency values.
111 function loadDependencyValues() {}
115 * @ingroup Cache
117 class FileDependency extends CacheDependency {
118 var $filename, $timestamp;
121 * Create a file dependency
123 * @param $filename String: the name of the file, preferably fully qualified
124 * @param $timestamp Mixed: the unix last modified timestamp, or false if the
125 * file does not exist. If omitted, the timestamp will be loaded from
126 * the file.
128 * A dependency on a nonexistent file will be triggered when the file is
129 * created. A dependency on an existing file will be triggered when the
130 * file is changed.
132 function __construct( $filename, $timestamp = null ) {
133 $this->filename = $filename;
134 $this->timestamp = $timestamp;
137 function __sleep() {
138 $this->loadDependencyValues();
139 return array( 'filename', 'timestamp' );
142 function loadDependencyValues() {
143 if ( is_null( $this->timestamp ) ) {
144 if ( !file_exists( $this->filename ) ) {
145 # Dependency on a non-existent file
146 # This is a valid concept!
147 $this->timestamp = false;
148 } else {
149 $this->timestamp = filemtime( $this->filename );
154 function isExpired() {
155 if ( !file_exists( $this->filename ) ) {
156 if ( $this->timestamp === false ) {
157 # Still nonexistent
158 return false;
159 } else {
160 # Deleted
161 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
162 return true;
164 } else {
165 $lastmod = filemtime( $this->filename );
166 if ( $lastmod > $this->timestamp ) {
167 # Modified or created
168 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
169 return true;
170 } else {
171 # Not modified
172 return false;
179 * @ingroup Cache
181 class TitleDependency extends CacheDependency {
182 var $titleObj;
183 var $ns, $dbk;
184 var $touched;
187 * Construct a title dependency
188 * @param $title Title
190 function __construct( Title $title ) {
191 $this->titleObj = $title;
192 $this->ns = $title->getNamespace();
193 $this->dbk = $title->getDBkey();
196 function loadDependencyValues() {
197 $this->touched = $this->getTitle()->getTouched();
201 * Get rid of bulky Title object for sleep
203 function __sleep() {
204 return array( 'ns', 'dbk', 'touched' );
207 function getTitle() {
208 if ( !isset( $this->titleObj ) ) {
209 $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
211 return $this->titleObj;
214 function isExpired() {
215 $touched = $this->getTitle()->getTouched();
216 if ( $this->touched === false ) {
217 if ( $touched === false ) {
218 # Still missing
219 return false;
220 } else {
221 # Created
222 return true;
224 } elseif ( $touched === false ) {
225 # Deleted
226 return true;
227 } elseif ( $touched > $this->touched ) {
228 # Updated
229 return true;
230 } else {
231 # Unmodified
232 return false;
238 * @ingroup Cache
240 class TitleListDependency extends CacheDependency {
241 var $linkBatch;
242 var $timestamps;
245 * Construct a dependency on a list of titles
247 function __construct( LinkBatch $linkBatch ) {
248 $this->linkBatch = $linkBatch;
251 function calculateTimestamps() {
252 # Initialise values to false
253 $timestamps = array();
254 foreach ( $this->getLinkBatch()->data as $ns => $dbks ) {
255 if ( count( $dbks ) > 0 ) {
256 $timestamps[$ns] = array();
257 foreach ( $dbks as $dbk => $value ) {
258 $timestamps[$ns][$dbk] = false;
263 # Do the query
264 if ( count( $timestamps ) ) {
265 $dbr = wfGetDB( DB_SLAVE );
266 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
267 $res = $dbr->select( 'page',
268 array( 'page_namespace', 'page_title', 'page_touched' ),
269 $where, __METHOD__ );
270 while ( $row = $dbr->fetchObject( $res ) ) {
271 $timestamps[$row->page_namespace][$row->page_title] = $row->page_touched;
274 return $timestamps;
277 function loadDependencyValues() {
278 $this->timestamps = $this->calculateTimestamps();
281 function __sleep() {
282 return array( 'timestamps' );
285 function getLinkBatch() {
286 if ( !isset( $this->linkBatch ) ){
287 $this->linkBatch = new LinkBatch;
288 $this->linkBatch->setArray( $this->timestamps );
290 return $this->linkBatch;
293 function isExpired() {
294 $newTimestamps = $this->calculateTimestamps();
295 foreach ( $this->timestamps as $ns => $dbks ) {
296 foreach ( $dbks as $dbk => $oldTimestamp ) {
297 $newTimestamp = $newTimestamps[$ns][$dbk];
298 if ( $oldTimestamp === false ) {
299 if ( $newTimestamp === false ) {
300 # Still missing
301 } else {
302 # Created
303 return true;
305 } elseif ( $newTimestamp === false ) {
306 # Deleted
307 return true;
308 } elseif ( $newTimestamp > $oldTimestamp ) {
309 # Updated
310 return true;
311 } else {
312 # Unmodified
316 return false;
321 * @ingroup Cache
323 class GlobalDependency extends CacheDependency {
324 var $name, $value;
326 function __construct( $name ) {
327 $this->name = $name;
328 $this->value = $GLOBALS[$name];
331 function isExpired() {
332 return $GLOBALS[$this->name] != $this->value;
337 * @ingroup Cache
339 class ConstantDependency extends CacheDependency {
340 var $name, $value;
342 function __construct( $name ) {
343 $this->name = $name;
344 $this->value = constant( $name );
347 function isExpired() {
348 return constant( $this->name ) != $this->value;