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