Swap and for &&
[mediawiki.git] / includes / utils / Cdb.php
blob3ceb620f675fdd05258bb5335dc12a919b466c58
1 <?php
2 /**
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
20 * @file
23 /**
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 {
29 /**
30 * The file handle
32 protected $handle;
34 /**
35 * Open a file and return a subclass instance
37 * @param string $fileName
39 * @return CdbReader
41 public static function open( $fileName ) {
42 return self::haveExtension() ?
43 new CdbReaderDBA( $fileName ) :
44 new CdbReaderPHP( $fileName );
47 /**
48 * Returns true if the native extension is available
50 * @return bool
52 public static function haveExtension() {
53 if ( !function_exists( 'dba_handlers' ) ) {
54 return false;
56 $handlers = dba_handlers();
57 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
58 return false;
61 return true;
64 /**
65 * Create the object and open the file
67 * @param string $fileName
69 abstract public function __construct( $fileName );
71 /**
72 * Close the file. Optional, you can just let the variable go out of scope.
74 abstract public function close();
76 /**
77 * Get a value with a given key. Only string values are supported.
79 * @param string $key
81 abstract public function get( $key );
84 /**
85 * Write to a CDB file.
86 * Native and pure PHP implementations are provided.
88 abstract class CdbWriter {
89 /**
90 * The file handle
92 protected $handle;
94 /**
95 * File we'll be writing to when we're done
96 * @var string
98 protected $realFileName;
101 * File we write to temporarily until we're done
102 * @var string
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.
129 * @param string $key
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 ) ) {
145 $this->close();
150 * Are we running on Windows?
151 * @return bool
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 {