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