X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/mediawiki.git/blobdiff_plain/f75295fdeb4bb0dc64fff481a386e0f48081a492..005c63129390e462618d69b42e7fd90eb81f8b9e:/includes/Cdb.php diff --git a/includes/Cdb.php b/includes/Cdb.php new file mode 100644 index 00000000..ab429872 --- /dev/null +++ b/includes/Cdb.php @@ -0,0 +1,149 @@ +handle = dba_open( $fileName, 'r-', 'cdb' ); + if ( !$this->handle ) { + throw new MWException( 'Unable to open DB file "' . $fileName . '"' ); + } + } + + function close() { + if( isset($this->handle) ) + dba_close( $this->handle ); + unset( $this->handle ); + } + + function get( $key ) { + return dba_fetch( $key, $this->handle ); + } +} + + +/** + * Writer class which uses the DBA extension + */ +class CdbWriter_DBA { + var $handle, $realFileName, $tmpFileName; + + function __construct( $fileName ) { + $this->realFileName = $fileName; + $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff ); + $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' ); + if ( !$this->handle ) { + throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' ); + } + } + + function set( $key, $value ) { + return dba_insert( $key, $value, $this->handle ); + } + + function close() { + if( isset($this->handle) ) + dba_close( $this->handle ); + if ( wfIsWindows() ) { + unlink( $this->realFileName ); + } + if ( !rename( $this->tmpFileName, $this->realFileName ) ) { + throw new MWException( 'Unable to move the new CDB file into place.' ); + } + unset( $this->handle ); + } + + function __destruct() { + if ( isset( $this->handle ) ) { + $this->close(); + } + } +} +