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