MediaWiki  REL1_20
BagOStuff.php
Go to the documentation of this file.
00001 <?php
00043 abstract class BagOStuff {
00044         private $debugMode = false;
00045 
00049         public function setDebug( $bool ) {
00050                 $this->debugMode = $bool;
00051         }
00052 
00053         /* *** THE GUTS OF THE OPERATION *** */
00054         /* Override these with functional things in subclasses */
00055 
00061         abstract public function get( $key );
00062 
00070         abstract public function set( $key, $value, $exptime = 0 );
00071 
00078         abstract public function delete( $key, $time = 0 );
00079 
00085         public function lock( $key, $timeout = 0 ) {
00086                 /* stub */
00087                 return true;
00088         }
00089 
00094         public function unlock( $key ) {
00095                 /* stub */
00096                 return true;
00097         }
00098 
00103         public function keys() {
00104                 /* stub */
00105                 return array();
00106         }
00107 
00117         public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
00118                 // stub
00119                 return false;
00120         }
00121 
00122         /* *** Emulated functions *** */
00123 
00129         public function getMulti( array $keys ) {
00130                 $res = array();
00131                 foreach ( $keys as $key ) {
00132                         $val = $this->get( $key );
00133                         if ( $val !== false ) {
00134                                 $res[$key] = $val;
00135                         }
00136                 }
00137                 return $res;
00138         }
00139 
00146         public function add( $key, $value, $exptime = 0 ) {
00147                 if ( $this->get( $key ) === false ) {
00148                         return $this->set( $key, $value, $exptime );
00149                 }
00150                 return false; // key already set
00151         }
00152 
00159         public function replace( $key, $value, $exptime = 0 ) {
00160                 if ( $this->get( $key ) !== false ) {
00161                         return $this->set( $key, $value, $exptime );
00162                 }
00163                 return false; // key not already set
00164         }
00165 
00172         public function incr( $key, $value = 1 ) {
00173                 if ( !$this->lock( $key ) ) {
00174                         return false;
00175                 }
00176                 $n = $this->get( $key );
00177                 if ( $this->isInteger( $n ) ) { // key exists?
00178                         $n += intval( $value );
00179                         $this->set( $key, max( 0, $n ) ); // exptime?
00180                 } else {
00181                         $n = false;
00182                 }
00183                 $this->unlock( $key );
00184 
00185                 return $n;
00186         }
00187 
00194         public function decr( $key, $value = 1 ) {
00195                 return $this->incr( $key, - $value );
00196         }
00197 
00201         public function debug( $text ) {
00202                 if ( $this->debugMode ) {
00203                         $class = get_class( $this );
00204                         wfDebug( "$class debug: $text\n" );
00205                 }
00206         }
00207 
00213         protected function convertExpiry( $exptime ) {
00214                 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
00215                         return time() + $exptime;
00216                 } else {
00217                         return $exptime;
00218                 }
00219         }
00220 
00228         protected function convertToRelative( $exptime ) {
00229                 if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
00230                         $exptime -= time();
00231                         if ( $exptime <= 0 ) {
00232                                 $exptime = 1;
00233                         }
00234                         return $exptime;
00235                 } else {
00236                         return $exptime;
00237                 }
00238         }
00239 
00246         protected function isInteger( $value ) {
00247                 return ( is_int( $value ) || ctype_digit( $value ) );
00248         }
00249 }