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.
59 abstract public function get( $key );
63 * Write to a CDB file.
64 * Native and pure PHP implementations are provided.
66 abstract class CdbWriter
{
68 * Open a writer and return a subclass instance.
69 * The user must have write access to the directory, for temporary file creation.
71 * @param $fileName string
75 public static function open( $fileName ) {
76 if ( CdbReader
::haveExtension() ) {
77 return new CdbWriter_DBA( $fileName );
79 wfDebug( "Warning: no dba extension found, using emulation.\n" );
80 return new CdbWriter_PHP( $fileName );
85 * Create the object and open the file
87 abstract function __construct( $fileName );
90 * Set a key to a given value. The value will be converted to string.
92 abstract public function set( $key, $value );
95 * Close the writer object. You should call this function before the object
96 * goes out of scope, to write out the final hashtables.
98 abstract public function close();
103 * Reader class which uses the DBA extension
105 class CdbReader_DBA
{
108 function __construct( $fileName ) {
109 $this->handle
= dba_open( $fileName, 'r-', 'cdb' );
110 if ( !$this->handle
) {
111 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
116 if( isset($this->handle
) )
117 dba_close( $this->handle
);
118 unset( $this->handle
);
121 function get( $key ) {
122 return dba_fetch( $key, $this->handle
);
128 * Writer class which uses the DBA extension
130 class CdbWriter_DBA
{
131 var $handle, $realFileName, $tmpFileName;
133 function __construct( $fileName ) {
134 $this->realFileName
= $fileName;
135 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
136 $this->handle
= dba_open( $this->tmpFileName
, 'n', 'cdb_make' );
137 if ( !$this->handle
) {
138 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
142 function set( $key, $value ) {
143 return dba_insert( $key, $value, $this->handle
);
147 if( isset($this->handle
) )
148 dba_close( $this->handle
);
149 if ( wfIsWindows() ) {
150 unlink( $this->realFileName
);
152 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
153 throw new MWException( 'Unable to move the new CDB file into place.' );
155 unset( $this->handle
);
158 function __destruct() {
159 if ( isset( $this->handle
) ) {