Change layout of the mustBePosted format to standardise it
[lhc/web/wiklou.git] / includes / CacheDependency.php
1 <?php
2
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;
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 if ( !is_array( $deps ) ) {
22 $deps = array( $deps );
23 }
24 $this->deps = $deps;
25 }
26
27 /**
28 * Returns true if any of the dependencies have expired
29 */
30 function isExpired() {
31 foreach ( $this->deps as $dep ) {
32 if ( $dep->isExpired() ) {
33 return true;
34 }
35 }
36 return false;
37 }
38
39 /**
40 * Initialise dependency values in preparation for storing. This must be
41 * called before serialization.
42 */
43 function initialiseDeps() {
44 foreach ( $this->deps as $dep ) {
45 $dep->loadDependencyValues();
46 }
47 }
48
49 /**
50 * Get the user-defined value
51 */
52 function getValue() {
53 return $this->value;
54 }
55
56 /**
57 * Store the wrapper to a cache
58 */
59 function storeToCache( $cache, $key, $expiry = 0 ) {
60 $this->initialiseDeps();
61 $cache->set( $key, $this, $expiry );
62 }
63
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.
68 *
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.
77 *
78 * @return mixed The value, or null if it was not present in the cache and no
79 * callback was defined.
80 */
81 static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
82 $callbackParams = array(), $deps = array() )
83 {
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;
94 }
95 return $value;
96 }
97 }
98
99 /**
100 * @ingroup Cache
101 */
102 abstract class CacheDependency {
103 /**
104 * Returns true if the dependency is expired, false otherwise
105 */
106 abstract function isExpired();
107
108 /**
109 * Hook to perform any expensive pre-serialize loading of dependency values.
110 */
111 function loadDependencyValues() {}
112 }
113
114 /**
115 * @ingroup Cache
116 */
117 class FileDependency extends CacheDependency {
118 var $filename, $timestamp;
119
120 /**
121 * Create a file dependency
122 *
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.
127 *
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.
131 */
132 function __construct( $filename, $timestamp = null ) {
133 $this->filename = $filename;
134 $this->timestamp = $timestamp;
135 }
136
137 function __sleep() {
138 $this->loadDependencyValues();
139 return array( 'filename', 'timestamp' );
140 }
141
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 );
150 }
151 }
152 }
153
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;
163 }
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;
173 }
174 }
175 }
176 }
177
178 /**
179 * @ingroup Cache
180 */
181 class TitleDependency extends CacheDependency {
182 var $titleObj;
183 var $ns, $dbk;
184 var $touched;
185
186 /**
187 * Construct a title dependency
188 * @param $title Title
189 */
190 function __construct( Title $title ) {
191 $this->titleObj = $title;
192 $this->ns = $title->getNamespace();
193 $this->dbk = $title->getDBkey();
194 }
195
196 function loadDependencyValues() {
197 $this->touched = $this->getTitle()->getTouched();
198 }
199
200 /**
201 * Get rid of bulky Title object for sleep
202 */
203 function __sleep() {
204 return array( 'ns', 'dbk', 'touched' );
205 }
206
207 function getTitle() {
208 if ( !isset( $this->titleObj ) ) {
209 $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
210 }
211 return $this->titleObj;
212 }
213
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;
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 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;
259 }
260 }
261 }
262
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;
272 }
273 }
274 return $timestamps;
275 }
276
277 function loadDependencyValues() {
278 $this->timestamps = $this->calculateTimestamps();
279 }
280
281 function __sleep() {
282 return array( 'timestamps' );
283 }
284
285 function getLinkBatch() {
286 if ( !isset( $this->linkBatch ) ){
287 $this->linkBatch = new LinkBatch;
288 $this->linkBatch->setArray( $this->timestamps );
289 }
290 return $this->linkBatch;
291 }
292
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;
304 }
305 } elseif ( $newTimestamp === false ) {
306 # Deleted
307 return true;
308 } elseif ( $newTimestamp > $oldTimestamp ) {
309 # Updated
310 return true;
311 } else {
312 # Unmodified
313 }
314 }
315 }
316 return false;
317 }
318 }
319
320 /**
321 * @ingroup Cache
322 */
323 class GlobalDependency extends CacheDependency {
324 var $name, $value;
325
326 function __construct( $name ) {
327 $this->name = $name;
328 $this->value = $GLOBALS[$name];
329 }
330
331 function isExpired() {
332 return $GLOBALS[$this->name] != $this->value;
333 }
334 }
335
336 /**
337 * @ingroup Cache
338 */
339 class ConstantDependency extends CacheDependency {
340 var $name, $value;
341
342 function __construct( $name ) {
343 $this->name = $name;
344 $this->value = constant( $name );
345 }
346
347 function isExpired() {
348 return constant( $this->name ) != $this->value;
349 }
350 }