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
{
35 * Open a file and return a subclass instance
37 * @param string $fileName
41 public static function open( $fileName ) {
42 return self
::haveExtension() ?
43 new CdbReaderDBA( $fileName ) :
44 new CdbReaderPHP( $fileName );
48 * Returns true if the native extension is available
52 public static function haveExtension() {
53 if ( !function_exists( 'dba_handlers' ) ) {
56 $handlers = dba_handlers();
57 if ( !in_array( 'cdb', $handlers ) ||
!in_array( 'cdb_make', $handlers ) ) {
65 * Create the object and open the file
67 * @param string $fileName
69 abstract public function __construct( $fileName );
72 * Close the file. Optional, you can just let the variable go out of scope.
74 abstract public function close();
77 * Get a value with a given key. Only string values are supported.
81 abstract public function get( $key );
85 * Write to a CDB file.
86 * Native and pure PHP implementations are provided.
88 abstract class CdbWriter
{
95 * File we'll be writing to when we're done
98 protected $realFileName;
101 * File we write to temporarily until we're done
104 protected $tmpFileName;
107 * Open a writer and return a subclass instance.
108 * The user must have write access to the directory, for temporary file creation.
110 * @param string $fileName
112 * @return CdbWriterDBA|CdbWriterPHP
114 public static function open( $fileName ) {
115 return CdbReader
::haveExtension() ?
116 new CdbWriterDBA( $fileName ) :
117 new CdbWriterPHP( $fileName );
121 * Create the object and open the file
123 * @param string $fileName
125 abstract public function __construct( $fileName );
128 * Set a key to a given value. The value will be converted to string.
130 * @param string $value
132 abstract public function set( $key, $value );
135 * Close the writer object. You should call this function before the object
136 * goes out of scope, to write out the final hashtables.
138 abstract public function close();
141 * If the object goes out of scope, close it for sanity
143 public function __destruct() {
144 if ( isset( $this->handle
) ) {
150 * Are we running on Windows?
153 protected function isWindows() {
154 return substr( php_uname(), 0, 7 ) == 'Windows';
159 * Exception for Cdb errors.
160 * This explicitly doesn't subclass MWException to encourage reuse.
162 class CdbException
extends Exception
{