Revert r55842 "Can now pass in element attributes other than just id to buildTable...
[lhc/web/wiklou.git] / includes / BagOStuff.php
1 <?php
2 #
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20
21 /**
22 * @defgroup Cache Cache
23 *
24 * @file
25 * @ingroup Cache
26 */
27
28 /**
29 * interface is intended to be more or less compatible with
30 * the PHP memcached client.
31 *
32 * backends for local hash array and SQL table included:
33 * <code>
34 * $bag = new HashBagOStuff();
35 * $bag = new MediaWikiBagOStuff($tablename); # connect to db first
36 * </code>
37 *
38 * @ingroup Cache
39 */
40 abstract class BagOStuff {
41 var $debugMode = false;
42
43 public function set_debug( $bool ) {
44 $this->debugMode = $bool;
45 }
46
47 /* *** THE GUTS OF THE OPERATION *** */
48 /* Override these with functional things in subclasses */
49
50 /**
51 * Get an item with the given key. Returns false if it does not exist.
52 * @param $key string
53 */
54 abstract public function get( $key );
55
56 /**
57 * Set an item.
58 * @param $key string
59 * @param $value mixed
60 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
61 */
62 abstract public function set( $key, $value, $exptime = 0 );
63
64 /*
65 * Delete an item.
66 * @param $key string
67 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
68 */
69 abstract public function delete( $key, $time = 0 );
70
71 public function lock( $key, $timeout = 0 ) {
72 /* stub */
73 return true;
74 }
75
76 public function unlock( $key ) {
77 /* stub */
78 return true;
79 }
80
81 public function keys() {
82 /* stub */
83 return array();
84 }
85
86 /* *** Emulated functions *** */
87 /* Better performance can likely be got with custom written versions */
88 public function get_multi( $keys ) {
89 $out = array();
90 foreach ( $keys as $key ) {
91 $out[$key] = $this->get( $key );
92 }
93 return $out;
94 }
95
96 public function set_multi( $hash, $exptime = 0 ) {
97 foreach ( $hash as $key => $value ) {
98 $this->set( $key, $value, $exptime );
99 }
100 }
101
102 public function add( $key, $value, $exptime = 0 ) {
103 if ( $this->get( $key ) == false ) {
104 $this->set( $key, $value, $exptime );
105 return true;
106 }
107 }
108
109 public function add_multi( $hash, $exptime = 0 ) {
110 foreach ( $hash as $key => $value ) {
111 $this->add( $key, $value, $exptime );
112 }
113 }
114
115 public function delete_multi( $keys, $time = 0 ) {
116 foreach ( $keys as $key ) {
117 $this->delete( $key, $time );
118 }
119 }
120
121 public function replace( $key, $value, $exptime = 0 ) {
122 if ( $this->get( $key ) !== false ) {
123 $this->set( $key, $value, $exptime );
124 }
125 }
126
127 public function incr( $key, $value = 1 ) {
128 if ( !$this->lock( $key ) ) {
129 return false;
130 }
131 $value = intval( $value );
132
133 $n = false;
134 if ( ( $n = $this->get( $key ) ) !== false ) {
135 $n += $value;
136 $this->set( $key, $n ); // exptime?
137 }
138 $this->unlock( $key );
139 return $n;
140 }
141
142 public function decr( $key, $value = 1 ) {
143 return $this->incr( $key, -$value );
144 }
145
146 public function debug( $text ) {
147 if ( $this->debugMode )
148 wfDebug( "BagOStuff debug: $text\n" );
149 }
150
151 /**
152 * Convert an optionally relative time to an absolute time
153 */
154 protected function convertExpiry( $exptime ) {
155 if ( ( $exptime != 0 ) && ( $exptime < 3600 * 24 * 30 ) ) {
156 return time() + $exptime;
157 } else {
158 return $exptime;
159 }
160 }
161 }
162
163
164 /**
165 * Functional versions!
166 * This is a test of the interface, mainly. It stores things in an associative
167 * array, which is not going to persist between program runs.
168 *
169 * @ingroup Cache
170 */
171 class HashBagOStuff extends BagOStuff {
172 var $bag;
173
174 function __construct() {
175 $this->bag = array();
176 }
177
178 protected function expire( $key ) {
179 $et = $this->bag[$key][1];
180 if ( ( $et == 0 ) || ( $et > time() ) ) {
181 return false;
182 }
183 $this->delete( $key );
184 return true;
185 }
186
187 function get( $key ) {
188 if ( !isset( $this->bag[$key] ) ) {
189 return false;
190 }
191 if ( $this->expire( $key ) ) {
192 return false;
193 }
194 return $this->bag[$key][0];
195 }
196
197 function set( $key, $value, $exptime = 0 ) {
198 $this->bag[$key] = array( $value, $this->convertExpiry( $exptime ) );
199 }
200
201 function delete( $key, $time = 0 ) {
202 if ( !isset( $this->bag[$key] ) ) {
203 return false;
204 }
205 unset( $this->bag[$key] );
206 return true;
207 }
208
209 function keys() {
210 return array_keys( $this->bag );
211 }
212 }
213
214 /**
215 * Class to store objects in the database
216 *
217 * @ingroup Cache
218 */
219 class SqlBagOStuff extends BagOStuff {
220 var $lb, $db;
221 var $lastExpireAll = 0;
222
223 protected function getDB() {
224 if ( !isset( $this->lb ) ) {
225 $this->lb = wfGetLBFactory()->newMainLB();
226 $this->db = $this->lb->getConnection( DB_MASTER );
227 $this->db->clearFlag( DBO_TRX );
228 }
229 return $this->db;
230 }
231
232 public function get( $key ) {
233 # expire old entries if any
234 $this->garbageCollect();
235 $db = $this->getDB();
236 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
237 array( 'keyname' => $key ), __METHOD__ );
238 if ( !$row ) {
239 $this->debug( 'get: no matching rows' );
240 return false;
241 }
242
243 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
244 if ( $this->isExpired( $row->exptime ) ) {
245 $this->debug( "get: key has expired, deleting" );
246 try {
247 $db->begin();
248 # Put the expiry time in the WHERE condition to avoid deleting a
249 # newly-inserted value
250 $db->delete( 'objectcache',
251 array(
252 'keyname' => $key,
253 'exptime' => $row->exptime
254 ), __METHOD__ );
255 $db->commit();
256 } catch ( DBQueryError $e ) {
257 $this->handleWriteError( $e );
258 }
259 return false;
260 }
261 return $this->unserialize( $db->decodeBlob( $row->value ) );
262 }
263
264 public function set( $key, $value, $exptime = 0 ) {
265 $db = $this->getDB();
266 $exptime = intval( $exptime );
267 if ( $exptime < 0 ) $exptime = 0;
268 if ( $exptime == 0 ) {
269 $encExpiry = $this->getMaxDateTime();
270 } else {
271 if ( $exptime < 3.16e8 ) # ~10 years
272 $exptime += time();
273 $encExpiry = $db->timestamp( $exptime );
274 }
275 try {
276 $db->begin();
277 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
278 $db->insert( 'objectcache',
279 array(
280 'keyname' => $key,
281 'value' => $db->encodeBlob( $this->serialize( $value ) ),
282 'exptime' => $encExpiry
283 ), __METHOD__ );
284 $db->commit();
285 } catch ( DBQueryError $e ) {
286 $this->handleWriteError( $e );
287 return false;
288 }
289 return true;
290 }
291
292 public function delete( $key, $time = 0 ) {
293 $db = $this->getDB();
294 try {
295 $db->begin();
296 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
297 $db->commit();
298 } catch ( DBQueryError $e ) {
299 $this->handleWriteError( $e );
300 return false;
301 }
302 return true;
303 }
304
305 public function incr( $key, $step = 1 ) {
306 $db = $this->getDB();
307 $step = intval( $step );
308
309 try {
310 $db->begin();
311 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
312 array( 'keyname' => $key ), __METHOD__, array( 'FOR UPDATE' ) );
313 if ( $row === false ) {
314 // Missing
315 $db->commit();
316 return false;
317 }
318 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
319 if ( $this->isExpired( $row->exptime ) ) {
320 // Expired, do not reinsert
321 $db->commit();
322 return false;
323 }
324
325 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
326 $newValue = $oldValue + $step;
327 $db->insert( 'objectcache',
328 array(
329 'keyname' => $key,
330 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
331 'exptime' => $row->exptime
332 ), __METHOD__ );
333 $db->commit();
334 } catch ( DBQueryError $e ) {
335 $this->handleWriteError( $e );
336 return false;
337 }
338 return $newValue;
339 }
340
341 public function keys() {
342 $db = $this->getDB();
343 $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
344 $result = array();
345 foreach ( $res as $row ) {
346 $result[] = $row->keyname;
347 }
348 return $result;
349 }
350
351 protected function isExpired( $exptime ) {
352 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX, $exptime ) < time();
353 }
354
355 protected function getMaxDateTime() {
356 if ( time() > 0x7fffffff ) {
357 return $this->getDB()->timestamp( 1 << 62 );
358 } else {
359 return $this->getDB()->timestamp( 0x7fffffff );
360 }
361 }
362
363 protected function garbageCollect() {
364 /* Ignore 99% of requests */
365 if ( !mt_rand( 0, 100 ) ) {
366 $now = time();
367 /* Avoid repeating the delete within a few seconds */
368 if ( $now > ( $this->lastExpireAll + 1 ) ) {
369 $this->lastExpireAll = $now;
370 $this->expireAll();
371 }
372 }
373 }
374
375 public function expireAll() {
376 $db = $this->getDB();
377 $now = $db->timestamp();
378 try {
379 $db->begin();
380 $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
381 $db->commit();
382 } catch ( DBQueryError $e ) {
383 $this->handleWriteError( $e );
384 }
385 }
386
387 public function deleteAll() {
388 $db = $this->getDB();
389 try {
390 $db->begin();
391 $db->delete( 'objectcache', '*', __METHOD__ );
392 $db->commit();
393 } catch ( DBQueryError $e ) {
394 $this->handleWriteError( $e );
395 }
396 }
397
398 /**
399 * Serialize an object and, if possible, compress the representation.
400 * On typical message and page data, this can provide a 3X decrease
401 * in storage requirements.
402 *
403 * @param $data mixed
404 * @return string
405 */
406 protected function serialize( &$data ) {
407 $serial = serialize( $data );
408 if ( function_exists( 'gzdeflate' ) ) {
409 return gzdeflate( $serial );
410 } else {
411 return $serial;
412 }
413 }
414
415 /**
416 * Unserialize and, if necessary, decompress an object.
417 * @param $serial string
418 * @return mixed
419 */
420 protected function unserialize( $serial ) {
421 if ( function_exists( 'gzinflate' ) ) {
422 $decomp = @gzinflate( $serial );
423 if ( false !== $decomp ) {
424 $serial = $decomp;
425 }
426 }
427 $ret = unserialize( $serial );
428 return $ret;
429 }
430
431 /**
432 * Handle a DBQueryError which occurred during a write operation.
433 * Ignore errors which are due to a read-only database, rethrow others.
434 */
435 protected function handleWriteError( $exception ) {
436 $db = $this->getDB();
437 if ( !$db->wasReadOnlyError() ) {
438 throw $exception;
439 }
440 try {
441 $db->rollback();
442 } catch ( DBQueryError $e ) {
443 }
444 wfDebug( __METHOD__ . ": ignoring query error\n" );
445 $db->ignoreErrors( false );
446 }
447 }
448
449 /**
450 * Backwards compatibility alias
451 */
452 class MediaWikiBagOStuff extends SqlBagOStuff {}
453
454 /**
455 * This is a wrapper for Turck MMCache's shared memory functions.
456 *
457 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
458 * to use a weird custom serializer that randomly segfaults. So we wrap calls
459 * with serialize()/unserialize().
460 *
461 * The thing I noticed about the Turck serialized data was that unlike ordinary
462 * serialize(), it contained the names of methods, and judging by the amount of
463 * binary data, perhaps even the bytecode of the methods themselves. It may be
464 * that Turck's serializer is faster, so a possible future extension would be
465 * to use it for arrays but not for objects.
466 *
467 * @ingroup Cache
468 */
469 class TurckBagOStuff extends BagOStuff {
470 public function get( $key ) {
471 $val = mmcache_get( $key );
472 if ( is_string( $val ) ) {
473 $val = unserialize( $val );
474 }
475 return $val;
476 }
477
478 public function set( $key, $value, $exptime = 0 ) {
479 mmcache_put( $key, serialize( $value ), $exptime );
480 return true;
481 }
482
483 public function delete( $key, $time = 0 ) {
484 mmcache_rm( $key );
485 return true;
486 }
487
488 public function lock( $key, $waitTimeout = 0 ) {
489 mmcache_lock( $key );
490 return true;
491 }
492
493 public function unlock( $key ) {
494 mmcache_unlock( $key );
495 return true;
496 }
497 }
498
499 /**
500 * This is a wrapper for APC's shared memory functions
501 *
502 * @ingroup Cache
503 */
504 class APCBagOStuff extends BagOStuff {
505 public function get( $key ) {
506 $val = apc_fetch( $key );
507 if ( is_string( $val ) ) {
508 $val = unserialize( $val );
509 }
510 return $val;
511 }
512
513 public function set( $key, $value, $exptime = 0 ) {
514 apc_store( $key, serialize( $value ), $exptime );
515 return true;
516 }
517
518 public function delete( $key, $time = 0 ) {
519 apc_delete( $key );
520 return true;
521 }
522 }
523
524
525 /**
526 * This is a wrapper for eAccelerator's shared memory functions.
527 *
528 * This is basically identical to the Turck MMCache version,
529 * mostly because eAccelerator is based on Turck MMCache.
530 *
531 * @ingroup Cache
532 */
533 class eAccelBagOStuff extends BagOStuff {
534 public function get( $key ) {
535 $val = eaccelerator_get( $key );
536 if ( is_string( $val ) ) {
537 $val = unserialize( $val );
538 }
539 return $val;
540 }
541
542 public function set( $key, $value, $exptime = 0 ) {
543 eaccelerator_put( $key, serialize( $value ), $exptime );
544 return true;
545 }
546
547 public function delete( $key, $time = 0 ) {
548 eaccelerator_rm( $key );
549 return true;
550 }
551
552 public function lock( $key, $waitTimeout = 0 ) {
553 eaccelerator_lock( $key );
554 return true;
555 }
556
557 public function unlock( $key ) {
558 eaccelerator_unlock( $key );
559 return true;
560 }
561 }
562
563 /**
564 * Wrapper for XCache object caching functions; identical interface
565 * to the APC wrapper
566 *
567 * @ingroup Cache
568 */
569 class XCacheBagOStuff extends BagOStuff {
570
571 /**
572 * Get a value from the XCache object cache
573 *
574 * @param $key String: cache key
575 * @return mixed
576 */
577 public function get( $key ) {
578 $val = xcache_get( $key );
579 if ( is_string( $val ) )
580 $val = unserialize( $val );
581 return $val;
582 }
583
584 /**
585 * Store a value in the XCache object cache
586 *
587 * @param $key String: cache key
588 * @param $value Mixed: object to store
589 * @param $expire Int: expiration time
590 * @return bool
591 */
592 public function set( $key, $value, $expire = 0 ) {
593 xcache_set( $key, serialize( $value ), $expire );
594 return true;
595 }
596
597 /**
598 * Remove a value from the XCache object cache
599 *
600 * @param $key String: cache key
601 * @param $time Int: not used in this implementation
602 * @return bool
603 */
604 public function delete( $key, $time = 0 ) {
605 xcache_unset( $key );
606 return true;
607 }
608
609 }
610
611 /**
612 * Cache that uses DBA as a backend.
613 * Slow due to the need to constantly open and close the file to avoid holding
614 * writer locks. Intended for development use only, as a memcached workalike
615 * for systems that don't have it.
616 *
617 * @ingroup Cache
618 */
619 class DBABagOStuff extends BagOStuff {
620 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
621
622 public function __construct( $handler = 'db3', $dir = false ) {
623 if ( $dir === false ) {
624 global $wgTmpDirectory;
625 $dir = $wgTmpDirectory;
626 }
627 $this->mFile = "$dir/mw-cache-" . wfWikiID();
628 $this->mFile .= '.db';
629 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
630 $this->mHandler = $handler;
631 }
632
633 /**
634 * Encode value and expiry for storage
635 */
636 function encode( $value, $expiry ) {
637 # Convert to absolute time
638 $expiry = $this->convertExpiry( $expiry );
639 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
640 }
641
642 /**
643 * @return list containing value first and expiry second
644 */
645 function decode( $blob ) {
646 if ( !is_string( $blob ) ) {
647 return array( null, 0 );
648 } else {
649 return array(
650 unserialize( substr( $blob, 11 ) ),
651 intval( substr( $blob, 0, 10 ) )
652 );
653 }
654 }
655
656 function getReader() {
657 if ( file_exists( $this->mFile ) ) {
658 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
659 } else {
660 $handle = $this->getWriter();
661 }
662 if ( !$handle ) {
663 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
664 }
665 return $handle;
666 }
667
668 function getWriter() {
669 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
670 if ( !$handle ) {
671 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
672 }
673 return $handle;
674 }
675
676 function get( $key ) {
677 wfProfileIn( __METHOD__ );
678 wfDebug( __METHOD__ . "($key)\n" );
679 $handle = $this->getReader();
680 if ( !$handle ) {
681 return null;
682 }
683 $val = dba_fetch( $key, $handle );
684 list( $val, $expiry ) = $this->decode( $val );
685 # Must close ASAP because locks are held
686 dba_close( $handle );
687
688 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
689 # Key is expired, delete it
690 $handle = $this->getWriter();
691 dba_delete( $key, $handle );
692 dba_close( $handle );
693 wfDebug( __METHOD__ . ": $key expired\n" );
694 $val = null;
695 }
696 wfProfileOut( __METHOD__ );
697 return $val;
698 }
699
700 function set( $key, $value, $exptime = 0 ) {
701 wfProfileIn( __METHOD__ );
702 wfDebug( __METHOD__ . "($key)\n" );
703 $blob = $this->encode( $value, $exptime );
704 $handle = $this->getWriter();
705 if ( !$handle ) {
706 return false;
707 }
708 $ret = dba_replace( $key, $blob, $handle );
709 dba_close( $handle );
710 wfProfileOut( __METHOD__ );
711 return $ret;
712 }
713
714 function delete( $key, $time = 0 ) {
715 wfProfileIn( __METHOD__ );
716 wfDebug( __METHOD__ . "($key)\n" );
717 $handle = $this->getWriter();
718 if ( !$handle ) {
719 return false;
720 }
721 $ret = dba_delete( $key, $handle );
722 dba_close( $handle );
723 wfProfileOut( __METHOD__ );
724 return $ret;
725 }
726
727 function add( $key, $value, $exptime = 0 ) {
728 wfProfileIn( __METHOD__ );
729 $blob = $this->encode( $value, $exptime );
730 $handle = $this->getWriter();
731 if ( !$handle ) {
732 return false;
733 }
734 $ret = dba_insert( $key, $blob, $handle );
735 # Insert failed, check to see if it failed due to an expired key
736 if ( !$ret ) {
737 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
738 if ( $expiry < time() ) {
739 # Yes expired, delete and try again
740 dba_delete( $key, $handle );
741 $ret = dba_insert( $key, $blob, $handle );
742 # This time if it failed then it will be handled by the caller like any other race
743 }
744 }
745
746 dba_close( $handle );
747 wfProfileOut( __METHOD__ );
748 return $ret;
749 }
750
751 function keys() {
752 $reader = $this->getReader();
753 $k1 = dba_firstkey( $reader );
754 if ( !$k1 ) {
755 return array();
756 }
757 $result[] = $k1;
758 while ( $key = dba_nextkey( $reader ) ) {
759 $result[] = $key;
760 }
761 return $result;
762 }
763 }