2009-06-20 15:59:56 +00:00
|
|
|
<?php
|
2010-08-14 17:42:40 +00:00
|
|
|
/**
|
2012-05-21 19:56:04 +00:00
|
|
|
* Native CDB file reader and writer.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-14 17:42:40 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read from a CDB file.
|
|
|
|
|
* Native and pure PHP implementations are provided.
|
|
|
|
|
* http://cr.yp.to/cdb.html
|
|
|
|
|
*/
|
|
|
|
|
abstract class CdbReader {
|
2013-11-07 22:03:08 +00:00
|
|
|
/**
|
|
|
|
|
* The file handle
|
|
|
|
|
*/
|
|
|
|
|
protected $handle;
|
|
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Open a file and return a subclass instance
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $fileName
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
|
|
|
|
* @return CdbReader
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
public static function open( $fileName ) {
|
2013-11-07 22:03:08 +00:00
|
|
|
return self::haveExtension() ?
|
|
|
|
|
new CdbReaderDBA( $fileName ) :
|
|
|
|
|
new CdbReaderPHP( $fileName );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the native extension is available
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
public static function haveExtension() {
|
|
|
|
|
if ( !function_exists( 'dba_handlers' ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$handlers = dba_handlers();
|
|
|
|
|
if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* Create the object and open the file
|
|
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $fileName
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
2013-11-07 22:03:08 +00:00
|
|
|
abstract public function __construct( $fileName );
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the file. Optional, you can just let the variable go out of scope.
|
|
|
|
|
*/
|
2013-11-07 22:03:08 +00:00
|
|
|
abstract public function close();
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a value with a given key. Only string values are supported.
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $key
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function get( $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write to a CDB file.
|
|
|
|
|
* Native and pure PHP implementations are provided.
|
|
|
|
|
*/
|
|
|
|
|
abstract class CdbWriter {
|
2013-11-07 22:03:08 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Open a writer and return a subclass instance.
|
|
|
|
|
* The user must have write access to the directory, for temporary file creation.
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $fileName
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
Remove underscore from CdbReader_[DBA|PHP] classes and related file
Also gets rid of some CodeSniffer errors. There are more cases in core
(cache, pool counter, installer, database, load balancer, diff, CSS
Janus, less (argh!), media, parser, revdel, ...), that have class names
with underscores I'd be trying to get rid of later.
Change-Id: I33709c05e597978a5574a445fa43c583cbd7e12b
2013-11-04 10:02:48 +00:00
|
|
|
* @return CdbWriterDBA|CdbWriterPHP
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
public static function open( $fileName ) {
|
2013-11-07 22:03:08 +00:00
|
|
|
return CdbReader::haveExtension() ?
|
|
|
|
|
new CdbWriterDBA( $fileName ) :
|
|
|
|
|
new CdbWriterPHP( $fileName );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create the object and open the file
|
2011-12-09 14:48:34 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $fileName
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
2013-11-07 22:03:08 +00:00
|
|
|
abstract public function __construct( $fileName );
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a key to a given value. The value will be converted to string.
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param string $value
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function set( $key, $value );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the writer object. You should call this function before the object
|
|
|
|
|
* goes out of scope, to write out the final hashtables.
|
|
|
|
|
*/
|
|
|
|
|
abstract public function close();
|
|
|
|
|
|
2013-11-07 22:03:08 +00:00
|
|
|
/**
|
|
|
|
|
* If the object goes out of scope, close it for sanity
|
|
|
|
|
*/
|
|
|
|
|
public function __destruct() {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( isset( $this->handle ) ) {
|
2013-11-07 22:03:08 +00:00
|
|
|
$this->close();
|
2012-09-27 19:46:22 +00:00
|
|
|
}
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
2013-11-07 22:26:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Are we running on Windows?
|
2014-08-23 20:34:25 +00:00
|
|
|
* @return bool
|
2013-11-07 22:26:54 +00:00
|
|
|
*/
|
|
|
|
|
protected function isWindows() {
|
|
|
|
|
return substr( php_uname(), 0, 7 ) == 'Windows';
|
|
|
|
|
}
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-21 23:42:05 +00:00
|
|
|
* Exception for Cdb errors.
|
|
|
|
|
* This explicitly doesn't subclass MWException to encourage reuse.
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
2014-05-08 19:15:09 +00:00
|
|
|
class CdbException extends Exception {
|
|
|
|
|
}
|