X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Futils%2FCdb.php;h=f556ee7af74ce74fe990d1e55bf2c2efa7ee0502;hb=6640bdf0d8be3d7b9a9be473f855c9c99ff061aa;hp=81c0afe171f06d892fd2a34c6beb0501345b12c5;hpb=853c6852ecb28f1a4bbcfa9b7f14a4759050b05b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/utils/Cdb.php b/includes/utils/Cdb.php index 81c0afe171..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,12 +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 ); } /** @@ -55,18 +57,21 @@ abstract class CdbReader { if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) { return false; } + return true; } /** - * 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. @@ -81,21 +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 ); } /** @@ -103,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. @@ -117,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 {}