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