3 * Native CDB file reader and writer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Read from a CDB file.
25 * Native and pure PHP implementations are provided.
26 * http://cr.yp.to/cdb.html
28 abstract class CdbReader
{
30 * Open a file and return a subclass instance
32 * @param $fileName string
36 public static function open( $fileName ) {
37 if ( self
::haveExtension() ) {
38 return new CdbReaderDBA( $fileName );
40 wfDebug( "Warning: no dba extension found, using emulation.\n" );
42 return new CdbReaderPHP( $fileName );
47 * Returns true if the native extension is available
51 public static function haveExtension() {
52 if ( !function_exists( 'dba_handlers' ) ) {
55 $handlers = dba_handlers();
56 if ( !in_array( 'cdb', $handlers ) ||
!in_array( 'cdb_make', $handlers ) ) {
64 * Construct the object and open the file
66 abstract function __construct( $fileName );
69 * Close the file. Optional, you can just let the variable go out of scope.
71 abstract function close();
74 * Get a value with a given key. Only string values are supported.
78 abstract public function get( $key );
82 * Write to a CDB file.
83 * Native and pure PHP implementations are provided.
85 abstract class CdbWriter
{
87 * Open a writer and return a subclass instance.
88 * The user must have write access to the directory, for temporary file creation.
90 * @param $fileName string
92 * @return CdbWriterDBA|CdbWriterPHP
94 public static function open( $fileName ) {
95 if ( CdbReader
::haveExtension() ) {
96 return new CdbWriterDBA( $fileName );
98 wfDebug( "Warning: no dba extension found, using emulation.\n" );
100 return new CdbWriterPHP( $fileName );
105 * Create the object and open the file
107 * @param $fileName string
109 abstract function __construct( $fileName );
112 * Set a key to a given value. The value will be converted to string.
114 * @param $value string
116 abstract public function set( $key, $value );
119 * Close the writer object. You should call this function before the object
120 * goes out of scope, to write out the final hashtables.
122 abstract public function close();
126 * Reader class which uses the DBA extension
131 function __construct( $fileName ) {
132 $this->handle
= dba_open( $fileName, 'r-', 'cdb' );
133 if ( !$this->handle
) {
134 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
139 if ( isset( $this->handle
) ) {
140 dba_close( $this->handle
);
142 unset( $this->handle
);
145 function get( $key ) {
146 return dba_fetch( $key, $this->handle
);
151 * Writer class which uses the DBA extension
154 var $handle, $realFileName, $tmpFileName;
156 function __construct( $fileName ) {
157 $this->realFileName
= $fileName;
158 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
159 $this->handle
= dba_open( $this->tmpFileName
, 'n', 'cdb_make' );
160 if ( !$this->handle
) {
161 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
165 function set( $key, $value ) {
166 return dba_insert( $key, $value, $this->handle
);
170 if ( isset( $this->handle
) ) {
171 dba_close( $this->handle
);
173 if ( wfIsWindows() ) {
174 unlink( $this->realFileName
);
176 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
177 throw new MWException( 'Unable to move the new CDB file into place.' );
179 unset( $this->handle
);
182 function __destruct() {
183 if ( isset( $this->handle
) ) {