3 * Native CDB file reader and writer
9 * Read from a CDB file.
10 * Native and pure PHP implementations are provided.
11 * http://cr.yp.to/cdb.html
13 abstract class CdbReader
{
15 * Open a file and return a subclass instance
17 * @param $fileName string
21 public static function open( $fileName ) {
22 if ( self
::haveExtension() ) {
23 return new CdbReader_DBA( $fileName );
25 wfDebug( "Warning: no dba extension found, using emulation.\n" );
26 return new CdbReader_PHP( $fileName );
31 * Returns true if the native extension is available
35 public static function haveExtension() {
36 if ( !function_exists( 'dba_handlers' ) ) {
39 $handlers = dba_handlers();
40 if ( !in_array( 'cdb', $handlers ) ||
!in_array( 'cdb_make', $handlers ) ) {
47 * Construct the object and open the file
49 abstract function __construct( $fileName );
52 * Close the file. Optional, you can just let the variable go out of scope.
54 abstract function close();
57 * Get a value with a given key. Only string values are supported.
61 abstract public function get( $key );
65 * Write to a CDB file.
66 * Native and pure PHP implementations are provided.
68 abstract class CdbWriter
{
70 * Open a writer and return a subclass instance.
71 * The user must have write access to the directory, for temporary file creation.
73 * @param $fileName string
75 * @return CdbWriter_DBA|CdbWriter_PHP
77 public static function open( $fileName ) {
78 if ( CdbReader
::haveExtension() ) {
79 return new CdbWriter_DBA( $fileName );
81 wfDebug( "Warning: no dba extension found, using emulation.\n" );
82 return new CdbWriter_PHP( $fileName );
87 * Create the object and open the file
89 abstract function __construct( $fileName );
92 * Set a key to a given value. The value will be converted to string.
94 abstract public function set( $key, $value );
97 * Close the writer object. You should call this function before the object
98 * goes out of scope, to write out the final hashtables.
100 abstract public function close();
105 * Reader class which uses the DBA extension
107 class CdbReader_DBA
{
110 function __construct( $fileName ) {
111 $this->handle
= dba_open( $fileName, 'r-', 'cdb' );
112 if ( !$this->handle
) {
113 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
118 if( isset($this->handle
) )
119 dba_close( $this->handle
);
120 unset( $this->handle
);
123 function get( $key ) {
124 return dba_fetch( $key, $this->handle
);
130 * Writer class which uses the DBA extension
132 class CdbWriter_DBA
{
133 var $handle, $realFileName, $tmpFileName;
135 function __construct( $fileName ) {
136 $this->realFileName
= $fileName;
137 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
138 $this->handle
= dba_open( $this->tmpFileName
, 'n', 'cdb_make' );
139 if ( !$this->handle
) {
140 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
144 function set( $key, $value ) {
145 return dba_insert( $key, $value, $this->handle
);
149 if( isset($this->handle
) )
150 dba_close( $this->handle
);
151 if ( wfIsWindows() ) {
152 unlink( $this->realFileName
);
154 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
155 throw new MWException( 'Unable to move the new CDB file into place.' );
157 unset( $this->handle
);
160 function __destruct() {
161 if ( isset( $this->handle
) ) {