4 * Read from a CDB file.
5 * Native and pure PHP implementations are provided.
6 * http://cr.yp.to/cdb.html
8 abstract class CdbReader
{
10 * Open a file and return a subclass instance
12 public static function open( $fileName ) {
13 if ( self
::haveExtension() ) {
14 return new CdbReader_DBA( $fileName );
16 wfDebug( "Warning: no dba extension found, using emulation.\n" );
17 return new CdbReader_PHP( $fileName );
22 * Returns true if the native extension is available
24 public static function haveExtension() {
25 if ( !function_exists( 'dba_handlers' ) ) {
28 $handlers = dba_handlers();
29 if ( !in_array( 'cdb', $handlers ) ||
!in_array( 'cdb_make', $handlers ) ) {
36 * Construct the object and open the file
38 abstract function __construct( $fileName );
41 * Close the file. Optional, you can just let the variable go out of scope.
43 abstract function close();
46 * Get a value with a given key. Only string values are supported.
48 abstract public function get( $key );
52 * Write to a CDB file.
53 * Native and pure PHP implementations are provided.
55 abstract class CdbWriter
{
57 * Open a writer and return a subclass instance.
58 * The user must have write access to the directory, for temporary file creation.
60 public static function open( $fileName ) {
61 if ( CdbReader
::haveExtension() ) {
62 return new CdbWriter_DBA( $fileName );
64 wfDebug( "Warning: no dba extension found, using emulation.\n" );
65 return new CdbWriter_PHP( $fileName );
70 * Create the object and open the file
72 abstract function __construct( $fileName );
75 * Set a key to a given value. The value will be converted to string.
77 abstract public function set( $key, $value );
80 * Close the writer object. You should call this function before the object
81 * goes out of scope, to write out the final hashtables.
83 abstract public function close();
88 * Reader class which uses the DBA extension
93 function __construct( $fileName ) {
94 $this->handle
= dba_open( $fileName, 'r-', 'cdb' );
95 if ( !$this->handle
) {
96 throw new MWException( 'Unable to open DB file "' . $fileName . '"' );
101 if( isset($this->handle
) )
102 dba_close( $this->handle
);
103 unset( $this->handle
);
106 function get( $key ) {
107 return dba_fetch( $key, $this->handle
);
113 * Writer class which uses the DBA extension
115 class CdbWriter_DBA
{
116 var $handle, $realFileName, $tmpFileName;
118 function __construct( $fileName ) {
119 $this->realFileName
= $fileName;
120 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
121 $this->handle
= dba_open( $this->tmpFileName
, 'n', 'cdb_make' );
122 if ( !$this->handle
) {
123 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' );
127 function set( $key, $value ) {
128 return dba_insert( $key, $value, $this->handle
);
132 if( isset($this->handle
) )
133 dba_close( $this->handle
);
134 if ( wfIsWindows() ) {
135 unlink( $this->realFileName
);
137 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
138 throw new MWException( 'Unable to move the new CDB file into place.' );
140 unset( $this->handle
);
143 function __destruct() {
144 if ( isset( $this->handle
) ) {