X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Futils%2FCdb.php;h=f556ee7af74ce74fe990d1e55bf2c2efa7ee0502;hb=b5993f884a3c4b0012fca120d3625452408c159d;hp=996b7af5201f10c7e9da6117099d61726bdb3593;hpb=c62949f6973303eca66033753076f2f9699ff8a9;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/utils/Cdb.php b/includes/utils/Cdb.php index 996b7af520..f556ee7af7 100644 --- a/includes/utils/Cdb.php +++ b/includes/utils/Cdb.php @@ -26,6 +26,11 @@ * http://cr.yp.to/cdb.html */ abstract class CdbReader { + /** + * The file handle + */ + protected $handle; + /** * Open a file and return a subclass instance * @@ -34,13 +39,9 @@ abstract class CdbReader { * @return CdbReader */ public static function open( $fileName ) { - if ( self::haveExtension() ) { - return new CdbReader_DBA( $fileName ); - } else { - wfDebug( "Warning: no dba extension found, using emulation.\n" ); - - return new CdbReader_PHP( $fileName ); - } + return self::haveExtension() ? + new CdbReaderDBA( $fileName ) : + new CdbReaderPHP( $fileName ); } /** @@ -61,14 +62,16 @@ abstract class CdbReader { } /** - * Construct the object and open the file + * Create the object and open the file + * + * @param $fileName string */ - abstract function __construct( $fileName ); + abstract public function __construct( $fileName ); /** * Close the file. Optional, you can just let the variable go out of scope. */ - abstract function close(); + abstract public function close(); /** * Get a value with a given key. Only string values are supported. @@ -83,22 +86,35 @@ abstract class CdbReader { * Native and pure PHP implementations are provided. */ abstract class CdbWriter { + /** + * The file handle + */ + protected $handle; + + /** + * File we'll be writing to when we're done + * @var string + */ + protected $realFileName; + + /** + * File we write to temporarily until we're done + * @var string + */ + protected $tmpFileName; + /** * Open a writer and return a subclass instance. * The user must have write access to the directory, for temporary file creation. * * @param $fileName string * - * @return CdbWriter_DBA|CdbWriter_PHP + * @return CdbWriterDBA|CdbWriterPHP */ public static function open( $fileName ) { - if ( CdbReader::haveExtension() ) { - return new CdbWriter_DBA( $fileName ); - } else { - wfDebug( "Warning: no dba extension found, using emulation.\n" ); - - return new CdbWriter_PHP( $fileName ); - } + return CdbReader::haveExtension() ? + new CdbWriterDBA( $fileName ) : + new CdbWriterPHP( $fileName ); } /** @@ -106,7 +122,7 @@ abstract class CdbWriter { * * @param $fileName string */ - abstract function __construct( $fileName ); + abstract public function __construct( $fileName ); /** * Set a key to a given value. The value will be converted to string. @@ -120,68 +136,26 @@ abstract class CdbWriter { * goes out of scope, to write out the final hashtables. */ abstract public function close(); -} - -/** - * Reader class which uses the DBA extension - */ -class CdbReader_DBA { - var $handle; - function __construct( $fileName ) { - $this->handle = dba_open( $fileName, 'r-', 'cdb' ); - if ( !$this->handle ) { - throw new MWException( 'Unable to open CDB file "' . $fileName . '"' ); - } - } - - function close() { + /** + * If the object goes out of scope, close it for sanity + */ + public function __destruct() { if ( isset( $this->handle ) ) { - dba_close( $this->handle ); + $this->close(); } - unset( $this->handle ); } - function get( $key ) { - return dba_fetch( $key, $this->handle ); + /** + * Are we running on Windows? + */ + protected function isWindows() { + return substr( php_uname(), 0, 7 ) == 'Windows'; } } /** - * Writer class which uses the DBA extension + * Exception for Cdb errors. + * This explicitly doesn't subclass MWException to encourage reuse. */ -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 CDB 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(); - } - } -} +class CdbException extends Exception {}