MediaWiki
REL1_19
|
00001 <?php 00013 abstract class CdbReader { 00021 public static function open( $fileName ) { 00022 if ( self::haveExtension() ) { 00023 return new CdbReader_DBA( $fileName ); 00024 } else { 00025 wfDebug( "Warning: no dba extension found, using emulation.\n" ); 00026 return new CdbReader_PHP( $fileName ); 00027 } 00028 } 00029 00035 public static function haveExtension() { 00036 if ( !function_exists( 'dba_handlers' ) ) { 00037 return false; 00038 } 00039 $handlers = dba_handlers(); 00040 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) { 00041 return false; 00042 } 00043 return true; 00044 } 00045 00049 abstract function __construct( $fileName ); 00050 00054 abstract function close(); 00055 00061 abstract public function get( $key ); 00062 } 00063 00068 abstract class CdbWriter { 00077 public static function open( $fileName ) { 00078 if ( CdbReader::haveExtension() ) { 00079 return new CdbWriter_DBA( $fileName ); 00080 } else { 00081 wfDebug( "Warning: no dba extension found, using emulation.\n" ); 00082 return new CdbWriter_PHP( $fileName ); 00083 } 00084 } 00085 00091 abstract function __construct( $fileName ); 00092 00098 abstract public function set( $key, $value ); 00099 00104 abstract public function close(); 00105 } 00106 00110 class CdbReader_DBA { 00111 var $handle; 00112 00113 function __construct( $fileName ) { 00114 $this->handle = dba_open( $fileName, 'r-', 'cdb' ); 00115 if ( !$this->handle ) { 00116 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' ); 00117 } 00118 } 00119 00120 function close() { 00121 if( isset($this->handle) ) 00122 dba_close( $this->handle ); 00123 unset( $this->handle ); 00124 } 00125 00126 function get( $key ) { 00127 return dba_fetch( $key, $this->handle ); 00128 } 00129 } 00130 00131 00135 class CdbWriter_DBA { 00136 var $handle, $realFileName, $tmpFileName; 00137 00138 function __construct( $fileName ) { 00139 $this->realFileName = $fileName; 00140 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff ); 00141 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' ); 00142 if ( !$this->handle ) { 00143 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' ); 00144 } 00145 } 00146 00147 function set( $key, $value ) { 00148 return dba_insert( $key, $value, $this->handle ); 00149 } 00150 00151 function close() { 00152 if( isset($this->handle) ) 00153 dba_close( $this->handle ); 00154 if ( wfIsWindows() ) { 00155 unlink( $this->realFileName ); 00156 } 00157 if ( !rename( $this->tmpFileName, $this->realFileName ) ) { 00158 throw new MWException( 'Unable to move the new CDB file into place.' ); 00159 } 00160 unset( $this->handle ); 00161 } 00162 00163 function __destruct() { 00164 if ( isset( $this->handle ) ) { 00165 $this->close(); 00166 } 00167 } 00168 } 00169