00001 <?php 00002 00008 abstract class CdbReader { 00012 public static function open( $fileName ) { 00013 if ( self::haveExtension() ) { 00014 return new CdbReader_DBA( $fileName ); 00015 } else { 00016 wfDebug( "Warning: no dba extension found, using emulation.\n" ); 00017 return new CdbReader_PHP( $fileName ); 00018 } 00019 } 00020 00024 public static function haveExtension() { 00025 if ( !function_exists( 'dba_handlers' ) ) { 00026 return false; 00027 } 00028 $handlers = dba_handlers(); 00029 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) { 00030 return false; 00031 } 00032 return true; 00033 } 00034 00038 abstract function __construct( $fileName ); 00039 00043 abstract function close(); 00044 00048 abstract public function get( $key ); 00049 } 00050 00055 abstract class CdbWriter { 00060 public static function open( $fileName ) { 00061 if ( CdbReader::haveExtension() ) { 00062 return new CdbWriter_DBA( $fileName ); 00063 } else { 00064 wfDebug( "Warning: no dba extension found, using emulation.\n" ); 00065 return new CdbWriter_PHP( $fileName ); 00066 } 00067 } 00068 00072 abstract function __construct( $fileName ); 00073 00077 abstract public function set( $key, $value ); 00078 00083 abstract public function close(); 00084 } 00085 00086 00090 class CdbReader_DBA { 00091 var $handle; 00092 00093 function __construct( $fileName ) { 00094 $this->handle = dba_open( $fileName, 'r-', 'cdb' ); 00095 if ( !$this->handle ) { 00096 throw new MWException( 'Unable to open DB file "' . $fileName . '"' ); 00097 } 00098 } 00099 00100 function close() { 00101 if( isset($this->handle) ) 00102 dba_close( $this->handle ); 00103 unset( $this->handle ); 00104 } 00105 00106 function get( $key ) { 00107 return dba_fetch( $key, $this->handle ); 00108 } 00109 } 00110 00111 00115 class CdbWriter_DBA { 00116 var $handle, $realFileName, $tmpFileName; 00117 00118 function __construct( $fileName ) { 00119 $this->realFileName = $fileName; 00120 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff ); 00121 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' ); 00122 if ( !$this->handle ) { 00123 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' ); 00124 } 00125 } 00126 00127 function set( $key, $value ) { 00128 return dba_insert( $key, $value, $this->handle ); 00129 } 00130 00131 function close() { 00132 if( isset($this->handle) ) 00133 dba_close( $this->handle ); 00134 if ( wfIsWindows() ) { 00135 unlink( $this->realFileName ); 00136 } 00137 if ( !rename( $this->tmpFileName, $this->realFileName ) ) { 00138 throw new MWException( 'Unable to move the new CDB file into place.' ); 00139 } 00140 unset( $this->handle ); 00141 } 00142 00143 function __destruct() { 00144 if ( isset( $this->handle ) ) { 00145 $this->close(); 00146 } 00147 } 00148 } 00149