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 * @param $fileName string
91 abstract function __construct( $fileName );
94 * Set a key to a given value. The value will be converted to string.
96 * @param $value string
98 abstract public function set( $key, $value );
101 * Close the writer object. You should call this function before the object
102 * goes out of scope, to write out the final hashtables.
104 abstract public function close();
108 * Reader class which uses the DBA extension
110 class CdbReader_DBA
{
113 function __construct( $fileName ) {
114 $this->handle
= dba_open( $fileName, 'r-', 'cdb' );
115 if ( !$this->handle
) {
116 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
121 if( isset($this->handle
) )
122 dba_close( $this->handle
);
123 unset( $this->handle
);
126 function get( $key ) {
127 return dba_fetch( $key, $this->handle
);
133 * Writer class which uses the DBA extension
135 class CdbWriter_DBA
{
136 var $handle, $realFileName, $tmpFileName;
138 function __construct( $fileName ) {
139 $this->realFileName
= $fileName;
140 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
141 $this->handle
= dba_open( $this->tmpFileName
, 'n', 'cdb_make' );
142 if ( !$this->handle
) {
143 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
147 function set( $key, $value ) {
148 return dba_insert( $key, $value, $this->handle
);
152 if( isset($this->handle
) )
153 dba_close( $this->handle
);
154 if ( wfIsWindows() ) {
155 unlink( $this->realFileName
);
157 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
158 throw new MWException( 'Unable to move the new CDB file into place.' );
160 unset( $this->handle
);
163 function __destruct() {
164 if ( isset( $this->handle
) ) {