More parameter documentation!!
[lhc/web/wiklou.git] / includes / cache / CacheDependency.php
1 <?php
2 /**
3 * This class stores an arbitrary value along with its dependencies.
4 * Users should typically only use DependencyWrapper::getValueFromCache(),
5 * rather than instantiating one of these objects directly.
6 * @ingroup Cache
7 */
8
9 class DependencyWrapper {
10 var $value;
11 var $deps;
12
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.
18 */
19 function __construct( $value = false, $deps = array() ) {
20 $this->value = $value;
21
22 if ( !is_array( $deps ) ) {
23 $deps = array( $deps );
24 }
25
26 $this->deps = $deps;
27 }
28
29 /**
30 * Returns true if any of the dependencies have expired
31 */
32 function isExpired() {
33 foreach ( $this->deps as $dep ) {
34 if ( $dep->isExpired() ) {
35 return true;
36 }
37 }
38
39 return false;
40 }
41
42 /**
43 * Initialise dependency values in preparation for storing. This must be
44 * called before serialization.
45 */
46 function initialiseDeps() {
47 foreach ( $this->deps as $dep ) {
48 $dep->loadDependencyValues();
49 }
50 }
51
52 /**
53 * Get the user-defined value
54 */
55 function getValue() {
56 return $this->value;
57 }
58
59 /**
60 * Store the wrapper to a cache
61 *
62 * @param $cache BagOStuff
63 * @param $key
64 * @param $expiry
65 */
66 function storeToCache( $cache, $key, $expiry = 0 ) {
67 $this->initialiseDeps();
68 $cache->set( $key, $this, $expiry );
69 }
70
71 /**
72 * Attempt to get a value from the cache. If the value is expired or missing,
73 * it will be generated with the callback function (if present), and the newly
74 * calculated value will be stored to the cache in a wrapper.
75 *
76 * @param $cache BagOStuff a cache object such as $wgMemc
77 * @param $key String: the cache key
78 * @param $expiry Integer: the expiry timestamp or interval in seconds
79 * @param $callback Mixed: the callback for generating the value, or false
80 * @param $callbackParams Array: the function parameters for the callback
81 * @param $deps Array: the dependencies to store on a cache miss. Note: these
82 * are not the dependencies used on a cache hit! Cache hits use the stored
83 * dependency array.
84 *
85 * @return mixed The value, or null if it was not present in the cache and no
86 * callback was defined.
87 */
88 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
89 $callbackParams = array(), $deps = array() )
90 {
91 $obj = $cache->get( $key );
92
93 if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
94 $value = $obj->value;
95 } elseif ( $callback ) {
96 $value = call_user_func_array( $callback, $callbackParams );
97 # Cache the newly-generated value
98 $wrapper = new DependencyWrapper( $value, $deps );
99 $wrapper->storeToCache( $cache, $key, $expiry );
100 } else {
101 $value = null;
102 }
103
104 return $value;
105 }
106 }
107
108 /**
109 * @ingroup Cache
110 */
111 abstract class CacheDependency {
112 /**
113 * Returns true if the dependency is expired, false otherwise
114 */
115 abstract function isExpired();
116
117 /**
118 * Hook to perform any expensive pre-serialize loading of dependency values.
119 */
120 function loadDependencyValues() { }
121 }
122
123 /**
124 * @ingroup Cache
125 */
126 class FileDependency extends CacheDependency {
127 var $filename, $timestamp;
128
129 /**
130 * Create a file dependency
131 *
132 * @param $filename String: the name of the file, preferably fully qualified
133 * @param $timestamp Mixed: the unix last modified timestamp, or false if the
134 * file does not exist. If omitted, the timestamp will be loaded from
135 * the file.
136 *
137 * A dependency on a nonexistent file will be triggered when the file is
138 * created. A dependency on an existing file will be triggered when the
139 * file is changed.
140 */
141 function __construct( $filename, $timestamp = null ) {
142 $this->filename = $filename;
143 $this->timestamp = $timestamp;
144 }
145
146 function __sleep() {
147 $this->loadDependencyValues();
148 return array( 'filename', 'timestamp' );
149 }
150
151 function loadDependencyValues() {
152 if ( is_null( $this->timestamp ) ) {
153 if ( !file_exists( $this->filename ) ) {
154 # Dependency on a non-existent file
155 # This is a valid concept!
156 $this->timestamp = false;
157 } else {
158 $this->timestamp = filemtime( $this->filename );
159 }
160 }
161 }
162
163 /**
164 * @return bool
165 */
166 function isExpired() {
167 if ( !file_exists( $this->filename ) ) {
168 if ( $this->timestamp === false ) {
169 # Still nonexistent
170 return false;
171 } else {
172 # Deleted
173 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
174 return true;
175 }
176 } else {
177 $lastmod = filemtime( $this->filename );
178 if ( $lastmod > $this->timestamp ) {
179 # Modified or created
180 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
181 return true;
182 } else {
183 # Not modified
184 return false;
185 }
186 }
187 }
188 }
189
190 /**
191 * @ingroup Cache
192 */
193 class TitleDependency extends CacheDependency {
194 var $titleObj;
195 var $ns, $dbk;
196 var $touched;
197
198 /**
199 * Construct a title dependency
200 * @param $title Title
201 */
202 function __construct( Title $title ) {
203 $this->titleObj = $title;
204 $this->ns = $title->getNamespace();
205 $this->dbk = $title->getDBkey();
206 }
207
208 function loadDependencyValues() {
209 $this->touched = $this->getTitle()->getTouched();
210 }
211
212 /**
213 * Get rid of bulky Title object for sleep
214 *
215 * @return array
216 */
217 function __sleep() {
218 return array( 'ns', 'dbk', 'touched' );
219 }
220
221 /**
222 * @return Title
223 */
224 function getTitle() {
225 if ( !isset( $this->titleObj ) ) {
226 $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
227 }
228
229 return $this->titleObj;
230 }
231
232 /**
233 * @return bool
234 */
235 function isExpired() {
236 $touched = $this->getTitle()->getTouched();
237
238 if ( $this->touched === false ) {
239 if ( $touched === false ) {
240 # Still missing
241 return false;
242 } else {
243 # Created
244 return true;
245 }
246 } elseif ( $touched === false ) {
247 # Deleted
248 return true;
249 } elseif ( $touched > $this->touched ) {
250 # Updated
251 return true;
252 } else {
253 # Unmodified
254 return false;
255 }
256 }
257 }
258
259 /**
260 * @ingroup Cache
261 */
262 class TitleListDependency extends CacheDependency {
263 var $linkBatch;
264 var $timestamps;
265
266 /**
267 * Construct a dependency on a list of titles
268 */
269 function __construct( LinkBatch $linkBatch ) {
270 $this->linkBatch = $linkBatch;
271 }
272
273 function calculateTimestamps() {
274 # Initialise values to false
275 $timestamps = array();
276
277 foreach ( $this->getLinkBatch()->data as $ns => $dbks ) {
278 if ( count( $dbks ) > 0 ) {
279 $timestamps[$ns] = array();
280
281 foreach ( $dbks as $dbk => $value ) {
282 $timestamps[$ns][$dbk] = false;
283 }
284 }
285 }
286
287 # Do the query
288 if ( count( $timestamps ) ) {
289 $dbr = wfGetDB( DB_SLAVE );
290 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
291 $res = $dbr->select(
292 'page',
293 array( 'page_namespace', 'page_title', 'page_touched' ),
294 $where,
295 __METHOD__
296 );
297
298 foreach ( $res as $row ) {
299 $timestamps[$row->page_namespace][$row->page_title] = $row->page_touched;
300 }
301 }
302
303 return $timestamps;
304 }
305
306 function loadDependencyValues() {
307 $this->timestamps = $this->calculateTimestamps();
308 }
309
310 /**
311 * @return array
312 */
313 function __sleep() {
314 return array( 'timestamps' );
315 }
316
317 function getLinkBatch() {
318 if ( !isset( $this->linkBatch ) ) {
319 $this->linkBatch = new LinkBatch;
320 $this->linkBatch->setArray( $this->timestamps );
321 }
322 return $this->linkBatch;
323 }
324
325 /**
326 * @return bool
327 */
328 function isExpired() {
329 $newTimestamps = $this->calculateTimestamps();
330
331 foreach ( $this->timestamps as $ns => $dbks ) {
332 foreach ( $dbks as $dbk => $oldTimestamp ) {
333 $newTimestamp = $newTimestamps[$ns][$dbk];
334
335 if ( $oldTimestamp === false ) {
336 if ( $newTimestamp === false ) {
337 # Still missing
338 } else {
339 # Created
340 return true;
341 }
342 } elseif ( $newTimestamp === false ) {
343 # Deleted
344 return true;
345 } elseif ( $newTimestamp > $oldTimestamp ) {
346 # Updated
347 return true;
348 } else {
349 # Unmodified
350 }
351 }
352 }
353
354 return false;
355 }
356 }
357
358 /**
359 * @ingroup Cache
360 */
361 class GlobalDependency extends CacheDependency {
362 var $name, $value;
363
364 function __construct( $name ) {
365 $this->name = $name;
366 $this->value = $GLOBALS[$name];
367 }
368
369 /**
370 * @return bool
371 */
372 function isExpired() {
373 return $GLOBALS[$this->name] != $this->value;
374 }
375 }
376
377 /**
378 * @ingroup Cache
379 */
380 class ConstantDependency extends CacheDependency {
381 var $name, $value;
382
383 function __construct( $name ) {
384 $this->name = $name;
385 $this->value = constant( $name );
386 }
387
388 /**
389 * @return bool
390 */
391 function isExpired() {
392 return constant( $this->name ) != $this->value;
393 }
394 }