The war on redundant ampersand usage!
[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 */
8 class DependencyWrapper {
9 var $value;
10 var $deps;
11
12 /**
13 * Create an instance.
14 * @param mixed $value The user-supplied value
15 * @param mixed $deps 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 object $cache A cache object such as $wgMemc
69 * @param string $key The cache key
70 * @param integer $expiry The expiry timestamp or interval in seconds
71 * @param mixed $callback The callback for generating the value, or false
72 * @param array $callbackParams The function parameters for the callback
73 * @param array $deps 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 abstract class CacheDependency {
99 /**
100 * Returns true if the dependency is expired, false otherwise
101 */
102 abstract function isExpired();
103
104 /**
105 * Hook to perform any expensive pre-serialize loading of dependency values.
106 */
107 function loadDependencyValues() {}
108 }
109
110 class FileDependency extends CacheDependency {
111 var $filename, $timestamp;
112
113 /**
114 * Create a file dependency
115 *
116 * @param string $filename The name of the file, preferably fully qualified
117 * @param mixed $timestamp The unix last modified timestamp, or false if the
118 * file does not exist. If omitted, the timestamp will be loaded from
119 * the file.
120 *
121 * A dependency on a nonexistent file will be triggered when the file is
122 * created. A dependency on an existing file will be triggered when the
123 * file is changed.
124 */
125 function __construct( $filename, $timestamp = null ) {
126 $this->filename = $filename;
127 $this->timestamp = $timestamp;
128 }
129
130 function loadDependencyValues() {
131 if ( is_null( $this->timestamp ) ) {
132 if ( !file_exists( $this->filename ) ) {
133 # Dependency on a non-existent file
134 # This is a valid concept!
135 $this->timestamp = false;
136 } else {
137 $this->timestamp = filemtime( $this->filename );
138 }
139 }
140 }
141
142 function isExpired() {
143 if ( !file_exists( $this->filename ) ) {
144 if ( $this->timestamp === false ) {
145 # Still nonexistent
146 return false;
147 } else {
148 # Deleted
149 wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
150 return true;
151 }
152 } else {
153 $lastmod = filemtime( $this->filename );
154 if ( $lastmod > $this->timestamp ) {
155 # Modified or created
156 wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
157 return true;
158 } else {
159 # Not modified
160 return false;
161 }
162 }
163 }
164 }
165
166 class TitleDependency extends CacheDependency {
167 var $titleObj;
168 var $ns, $dbk;
169 var $touched;
170
171 /**
172 * Construct a title dependency
173 * @param Title $title
174 */
175 function __construct( Title $title ) {
176 $this->titleObj = $title;
177 $this->ns = $title->getNamespace();
178 $this->dbk = $title->getDBkey();
179 }
180
181 function loadDependencyValues() {
182 $this->touched = $this->getTitle()->getTouched();
183 }
184
185 /**
186 * Get rid of bulky Title object for sleep
187 */
188 function __sleep() {
189 return array( 'ns', 'dbk', 'touched' );
190 }
191
192 function getTitle() {
193 if ( !isset( $this->titleObj ) ) {
194 $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
195 }
196 return $this->titleObj;
197 }
198
199 function isExpired() {
200 $touched = $this->getTitle()->getTouched();
201 if ( $this->touched === false ) {
202 if ( $touched === false ) {
203 # Still missing
204 return false;
205 } else {
206 # Created
207 return true;
208 }
209 } elseif ( $touched === false ) {
210 # Deleted
211 return true;
212 } elseif ( $touched > $this->touched ) {
213 # Updated
214 return true;
215 } else {
216 # Unmodified
217 return false;
218 }
219 }
220 }
221
222 class TitleListDependency extends CacheDependency {
223 var $linkBatch;
224 var $timestamps;
225
226 /**
227 * Construct a dependency on a list of titles
228 */
229 function __construct( LinkBatch $linkBatch ) {
230 $this->linkBatch = $linkBatch;
231 }
232
233 function calculateTimestamps() {
234 # Initialise values to false
235 $timestamps = array();
236 foreach ( $this->getLinkBatch()->data as $ns => $dbks ) {
237 if ( count( $dbks ) > 0 ) {
238 $timestamps[$ns] = array();
239 foreach ( $dbks as $dbk => $value ) {
240 $timestamps[$ns][$dbk] = false;
241 }
242 }
243 }
244
245 # Do the query
246 if ( count( $timestamps ) ) {
247 $dbr = wfGetDB( DB_SLAVE );
248 $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
249 $res = $dbr->select( 'page',
250 array( 'page_namespace', 'page_title', 'page_touched' ),
251 $where, __METHOD__ );
252 while ( $row = $dbr->fetchObject( $res ) ) {
253 $timestamps[$row->page_namespace][$row->page_title] = $row->page_touched;
254 }
255 }
256 return $timestamps;
257 }
258
259 function loadDependencyValues() {
260 $this->timestamps = $this->calculateTimestamps();
261 }
262
263 function __sleep() {
264 return array( 'timestamps' );
265 }
266
267 function getLinkBatch() {
268 if ( !isset( $this->linkBatch ) ){
269 $this->linkBatch = new LinkBatch;
270 $this->linkBatch->setArray( $this->timestamps );
271 }
272 return $this->linkBatch;
273 }
274
275 function isExpired() {
276 $newTimestamps = $this->calculateTimestamps();
277 foreach ( $this->timestamps as $ns => $dbks ) {
278 foreach ( $dbks as $dbk => $oldTimestamp ) {
279 $newTimestamp = $newTimestamps[$ns][$dbk];
280 if ( $oldTimestamp === false ) {
281 if ( $newTimestamp === false ) {
282 # Still missing
283 } else {
284 # Created
285 return true;
286 }
287 } elseif ( $newTimestamp === false ) {
288 # Deleted
289 return true;
290 } elseif ( $newTimestamp > $oldTimestamp ) {
291 # Updated
292 return true;
293 } else {
294 # Unmodified
295 }
296 }
297 }
298 return false;
299 }
300 }
301
302 class GlobalDependency extends CacheDependency {
303 var $name, $value;
304
305 function __construct( $name ) {
306 $this->name = $name;
307 $this->value = $GLOBALS[$name];
308 }
309
310 function isExpired() {
311 return $GLOBALS[$this->name] != $this->value;
312 }
313 }
314
315 class ConstantDependency extends CacheDependency {
316 var $name, $value;
317
318 function __construct( $name ) {
319 $this->name = $name;
320 $this->value = constant( $name );
321 }
322
323 function isExpired() {
324 return constant( $this->name ) != $this->value;
325 }
326 }
327
328 ?>