more specific error message, using WikiError, if user trys to create account with...
[mediawiki.git] / includes / BagOStuff.php
blob2ec615369aedaf5b0f21eed264d1d77c5d3dcebf
1 <?php
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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.
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.
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
21 /**
22 * @defgroup Cache Cache
24 * @file
25 * @ingroup Cache
28 /**
29 * interface is intended to be more or less compatible with
30 * the PHP memcached client.
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>
38 * @ingroup Cache
40 abstract class BagOStuff {
41 var $debugMode = false;
43 public function set_debug( $bool ) {
44 $this->debugMode = $bool;
47 /* *** THE GUTS OF THE OPERATION *** */
48 /* Override these with functional things in subclasses */
50 /**
51 * Get an item with the given key. Returns false if it does not exist.
52 * @param $key string
54 abstract public function get( $key );
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
62 abstract public function set( $key, $value, $exptime = 0 );
65 * Delete an item.
66 * @param $key string
67 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
69 abstract public function delete( $key, $time = 0 );
71 public function lock( $key, $timeout = 0 ) {
72 /* stub */
73 return true;
76 public function unlock( $key ) {
77 /* stub */
78 return true;
81 public function keys() {
82 /* stub */
83 return array();
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 );
93 return $out;
96 public function set_multi( $hash, $exptime = 0 ) {
97 foreach ( $hash as $key => $value ) {
98 $this->set( $key, $value, $exptime );
102 public function add( $key, $value, $exptime = 0 ) {
103 if ( $this->get( $key ) == false ) {
104 $this->set( $key, $value, $exptime );
105 return true;
109 public function add_multi( $hash, $exptime = 0 ) {
110 foreach ( $hash as $key => $value ) {
111 $this->add( $key, $value, $exptime );
115 public function delete_multi( $keys, $time = 0 ) {
116 foreach ( $keys as $key ) {
117 $this->delete( $key, $time );
121 public function replace( $key, $value, $exptime = 0 ) {
122 if ( $this->get( $key ) !== false ) {
123 $this->set( $key, $value, $exptime );
127 public function incr( $key, $value = 1 ) {
128 if ( !$this->lock( $key ) ) {
129 return false;
131 $value = intval( $value );
133 $n = false;
134 if ( ( $n = $this->get( $key ) ) !== false ) {
135 $n += $value;
136 $this->set( $key, $n ); // exptime?
138 $this->unlock( $key );
139 return $n;
142 public function decr( $key, $value = 1 ) {
143 return $this->incr( $key, -$value );
146 public function debug( $text ) {
147 if ( $this->debugMode )
148 wfDebug( "BagOStuff debug: $text\n" );
152 * Convert an optionally relative time to an absolute time
154 protected function convertExpiry( $exptime ) {
155 if ( ( $exptime != 0 ) && ( $exptime < 3600 * 24 * 30 ) ) {
156 return time() + $exptime;
157 } else {
158 return $exptime;
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.
169 * @ingroup Cache
171 class HashBagOStuff extends BagOStuff {
172 var $bag;
174 function __construct() {
175 $this->bag = array();
178 protected function expire( $key ) {
179 $et = $this->bag[$key][1];
180 if ( ( $et == 0 ) || ( $et > time() ) ) {
181 return false;
183 $this->delete( $key );
184 return true;
187 function get( $key ) {
188 if ( !isset( $this->bag[$key] ) ) {
189 return false;
191 if ( $this->expire( $key ) ) {
192 return false;
194 return $this->bag[$key][0];
197 function set( $key, $value, $exptime = 0 ) {
198 $this->bag[$key] = array( $value, $this->convertExpiry( $exptime ) );
201 function delete( $key, $time = 0 ) {
202 if ( !isset( $this->bag[$key] ) ) {
203 return false;
205 unset( $this->bag[$key] );
206 return true;
209 function keys() {
210 return array_keys( $this->bag );
215 * Class to store objects in the database
217 * @ingroup Cache
219 class SqlBagOStuff extends BagOStuff {
220 var $lb, $db;
221 var $lastExpireAll = 0;
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 );
229 return $this->db;
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;
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 );
259 return false;
261 return $this->unserialize( $db->decodeBlob( $row->value ) );
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 );
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;
289 return true;
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;
302 return true;
305 public function incr( $key, $step = 1 ) {
306 $db = $this->getDB();
307 $step = intval( $step );
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;
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;
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;
338 return $newValue;
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;
348 return $result;
351 protected function isExpired( $exptime ) {
352 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX, $exptime ) < time();
355 protected function getMaxDateTime() {
356 if ( time() > 0x7fffffff ) {
357 return $this->getDB()->timestamp( 1 << 62 );
358 } else {
359 return $this->getDB()->timestamp( 0x7fffffff );
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();
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 );
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 );
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.
403 * @param $data mixed
404 * @return string
406 protected function serialize( &$data ) {
407 $serial = serialize( $data );
408 if ( function_exists( 'gzdeflate' ) ) {
409 return gzdeflate( $serial );
410 } else {
411 return $serial;
416 * Unserialize and, if necessary, decompress an object.
417 * @param $serial string
418 * @return mixed
420 protected function unserialize( $serial ) {
421 if ( function_exists( 'gzinflate' ) ) {
422 $decomp = @gzinflate( $serial );
423 if ( false !== $decomp ) {
424 $serial = $decomp;
427 $ret = unserialize( $serial );
428 return $ret;
432 * Handle a DBQueryError which occurred during a write operation.
433 * Ignore errors which are due to a read-only database, rethrow others.
435 protected function handleWriteError( $exception ) {
436 $db = $this->getDB();
437 if ( !$db->wasReadOnlyError() ) {
438 throw $exception;
440 try {
441 $db->rollback();
442 } catch ( DBQueryError $e ) {
444 wfDebug( __METHOD__ . ": ignoring query error\n" );
445 $db->ignoreErrors( false );
450 * Backwards compatibility alias
452 class MediaWikiBagOStuff extends SqlBagOStuff {}
455 * This is a wrapper for Turck MMCache's shared memory functions.
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().
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.
467 * @ingroup Cache
469 class TurckBagOStuff extends BagOStuff {
470 public function get( $key ) {
471 $val = mmcache_get( $key );
472 if ( is_string( $val ) ) {
473 $val = unserialize( $val );
475 return $val;
478 public function set( $key, $value, $exptime = 0 ) {
479 mmcache_put( $key, serialize( $value ), $exptime );
480 return true;
483 public function delete( $key, $time = 0 ) {
484 mmcache_rm( $key );
485 return true;
488 public function lock( $key, $waitTimeout = 0 ) {
489 mmcache_lock( $key );
490 return true;
493 public function unlock( $key ) {
494 mmcache_unlock( $key );
495 return true;
500 * This is a wrapper for APC's shared memory functions
502 * @ingroup Cache
504 class APCBagOStuff extends BagOStuff {
505 public function get( $key ) {
506 $val = apc_fetch( $key );
507 if ( is_string( $val ) ) {
508 $val = unserialize( $val );
510 return $val;
513 public function set( $key, $value, $exptime = 0 ) {
514 apc_store( $key, serialize( $value ), $exptime );
515 return true;
518 public function delete( $key, $time = 0 ) {
519 apc_delete( $key );
520 return true;
526 * This is a wrapper for eAccelerator's shared memory functions.
528 * This is basically identical to the Turck MMCache version,
529 * mostly because eAccelerator is based on Turck MMCache.
531 * @ingroup Cache
533 class eAccelBagOStuff extends BagOStuff {
534 public function get( $key ) {
535 $val = eaccelerator_get( $key );
536 if ( is_string( $val ) ) {
537 $val = unserialize( $val );
539 return $val;
542 public function set( $key, $value, $exptime = 0 ) {
543 eaccelerator_put( $key, serialize( $value ), $exptime );
544 return true;
547 public function delete( $key, $time = 0 ) {
548 eaccelerator_rm( $key );
549 return true;
552 public function lock( $key, $waitTimeout = 0 ) {
553 eaccelerator_lock( $key );
554 return true;
557 public function unlock( $key ) {
558 eaccelerator_unlock( $key );
559 return true;
564 * Wrapper for XCache object caching functions; identical interface
565 * to the APC wrapper
567 * @ingroup Cache
569 class XCacheBagOStuff extends BagOStuff {
572 * Get a value from the XCache object cache
574 * @param $key String: cache key
575 * @return mixed
577 public function get( $key ) {
578 $val = xcache_get( $key );
579 if ( is_string( $val ) )
580 $val = unserialize( $val );
581 return $val;
585 * Store a value in the XCache object cache
587 * @param $key String: cache key
588 * @param $value Mixed: object to store
589 * @param $expire Int: expiration time
590 * @return bool
592 public function set( $key, $value, $expire = 0 ) {
593 xcache_set( $key, serialize( $value ), $expire );
594 return true;
598 * Remove a value from the XCache object cache
600 * @param $key String: cache key
601 * @param $time Int: not used in this implementation
602 * @return bool
604 public function delete( $key, $time = 0 ) {
605 xcache_unset( $key );
606 return true;
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.
617 * @ingroup Cache
619 class DBABagOStuff extends BagOStuff {
620 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
622 public function __construct( $handler = 'db3', $dir = false ) {
623 if ( $dir === false ) {
624 global $wgTmpDirectory;
625 $dir = $wgTmpDirectory;
627 $this->mFile = "$dir/mw-cache-" . wfWikiID();
628 $this->mFile .= '.db';
629 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
630 $this->mHandler = $handler;
634 * Encode value and expiry for storage
636 function encode( $value, $expiry ) {
637 # Convert to absolute time
638 $expiry = $this->convertExpiry( $expiry );
639 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
643 * @return list containing value first and expiry second
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 ) )
656 function getReader() {
657 if ( file_exists( $this->mFile ) ) {
658 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
659 } else {
660 $handle = $this->getWriter();
662 if ( !$handle ) {
663 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
665 return $handle;
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" );
673 return $handle;
676 function get( $key ) {
677 wfProfileIn( __METHOD__ );
678 wfDebug( __METHOD__ . "($key)\n" );
679 $handle = $this->getReader();
680 if ( !$handle ) {
681 return null;
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 );
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;
696 wfProfileOut( __METHOD__ );
697 return $val;
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;
708 $ret = dba_replace( $key, $blob, $handle );
709 dba_close( $handle );
710 wfProfileOut( __METHOD__ );
711 return $ret;
714 function delete( $key, $time = 0 ) {
715 wfProfileIn( __METHOD__ );
716 wfDebug( __METHOD__ . "($key)\n" );
717 $handle = $this->getWriter();
718 if ( !$handle ) {
719 return false;
721 $ret = dba_delete( $key, $handle );
722 dba_close( $handle );
723 wfProfileOut( __METHOD__ );
724 return $ret;
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;
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
746 dba_close( $handle );
747 wfProfileOut( __METHOD__ );
748 return $ret;
751 function keys() {
752 $reader = $this->getReader();
753 $k1 = dba_firstkey( $reader );
754 if ( !$k1 ) {
755 return array();
757 $result[] = $k1;
758 while ( $key = dba_nextkey( $reader ) ) {
759 $result[] = $key;
761 return $result;