MediaWiki
REL1_22
|
00001 <?php 00028 abstract class CdbReader { 00036 public static function open( $fileName ) { 00037 if ( self::haveExtension() ) { 00038 return new CdbReader_DBA( $fileName ); 00039 } else { 00040 wfDebug( "Warning: no dba extension found, using emulation.\n" ); 00041 return new CdbReader_PHP( $fileName ); 00042 } 00043 } 00044 00050 public static function haveExtension() { 00051 if ( !function_exists( 'dba_handlers' ) ) { 00052 return false; 00053 } 00054 $handlers = dba_handlers(); 00055 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) { 00056 return false; 00057 } 00058 return true; 00059 } 00060 00064 abstract function __construct( $fileName ); 00065 00069 abstract function close(); 00070 00076 abstract public function get( $key ); 00077 } 00078 00083 abstract class CdbWriter { 00092 public static function open( $fileName ) { 00093 if ( CdbReader::haveExtension() ) { 00094 return new CdbWriter_DBA( $fileName ); 00095 } else { 00096 wfDebug( "Warning: no dba extension found, using emulation.\n" ); 00097 return new CdbWriter_PHP( $fileName ); 00098 } 00099 } 00100 00106 abstract function __construct( $fileName ); 00107 00113 abstract public function set( $key, $value ); 00114 00119 abstract public function close(); 00120 } 00121 00125 class CdbReader_DBA { 00126 var $handle; 00127 00128 function __construct( $fileName ) { 00129 $this->handle = dba_open( $fileName, 'r-', 'cdb' ); 00130 if ( !$this->handle ) { 00131 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' ); 00132 } 00133 } 00134 00135 function close() { 00136 if ( isset( $this->handle ) ) { 00137 dba_close( $this->handle ); 00138 } 00139 unset( $this->handle ); 00140 } 00141 00142 function get( $key ) { 00143 return dba_fetch( $key, $this->handle ); 00144 } 00145 } 00146 00150 class CdbWriter_DBA { 00151 var $handle, $realFileName, $tmpFileName; 00152 00153 function __construct( $fileName ) { 00154 $this->realFileName = $fileName; 00155 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff ); 00156 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' ); 00157 if ( !$this->handle ) { 00158 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' ); 00159 } 00160 } 00161 00162 function set( $key, $value ) { 00163 return dba_insert( $key, $value, $this->handle ); 00164 } 00165 00166 function close() { 00167 if ( isset( $this->handle ) ) { 00168 dba_close( $this->handle ); 00169 } 00170 if ( wfIsWindows() ) { 00171 unlink( $this->realFileName ); 00172 } 00173 if ( !rename( $this->tmpFileName, $this->realFileName ) ) { 00174 throw new MWException( 'Unable to move the new CDB file into place.' ); 00175 } 00176 unset( $this->handle ); 00177 } 00178 00179 function __destruct() { 00180 if ( isset( $this->handle ) ) { 00181 $this->close(); 00182 } 00183 } 00184 }