3ceb620f675fdd05258bb5335dc12a919b466c58
[lhc/web/wiklou.git] / includes / libs / cdb / Cdb.php
1 <?php
2 /**
3 * Native CDB file reader and writer.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * Read from a CDB file.
25 * Native and pure PHP implementations are provided.
26 * http://cr.yp.to/cdb.html
27 */
28 abstract class CdbReader {
29 /**
30 * The file handle
31 */
32 protected $handle;
33
34 /**
35 * Open a file and return a subclass instance
36 *
37 * @param string $fileName
38 *
39 * @return CdbReader
40 */
41 public static function open( $fileName ) {
42 return self::haveExtension() ?
43 new CdbReaderDBA( $fileName ) :
44 new CdbReaderPHP( $fileName );
45 }
46
47 /**
48 * Returns true if the native extension is available
49 *
50 * @return bool
51 */
52 public static function haveExtension() {
53 if ( !function_exists( 'dba_handlers' ) ) {
54 return false;
55 }
56 $handlers = dba_handlers();
57 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
58 return false;
59 }
60
61 return true;
62 }
63
64 /**
65 * Create the object and open the file
66 *
67 * @param string $fileName
68 */
69 abstract public function __construct( $fileName );
70
71 /**
72 * Close the file. Optional, you can just let the variable go out of scope.
73 */
74 abstract public function close();
75
76 /**
77 * Get a value with a given key. Only string values are supported.
78 *
79 * @param string $key
80 */
81 abstract public function get( $key );
82 }
83
84 /**
85 * Write to a CDB file.
86 * Native and pure PHP implementations are provided.
87 */
88 abstract class CdbWriter {
89 /**
90 * The file handle
91 */
92 protected $handle;
93
94 /**
95 * File we'll be writing to when we're done
96 * @var string
97 */
98 protected $realFileName;
99
100 /**
101 * File we write to temporarily until we're done
102 * @var string
103 */
104 protected $tmpFileName;
105
106 /**
107 * Open a writer and return a subclass instance.
108 * The user must have write access to the directory, for temporary file creation.
109 *
110 * @param string $fileName
111 *
112 * @return CdbWriterDBA|CdbWriterPHP
113 */
114 public static function open( $fileName ) {
115 return CdbReader::haveExtension() ?
116 new CdbWriterDBA( $fileName ) :
117 new CdbWriterPHP( $fileName );
118 }
119
120 /**
121 * Create the object and open the file
122 *
123 * @param string $fileName
124 */
125 abstract public function __construct( $fileName );
126
127 /**
128 * Set a key to a given value. The value will be converted to string.
129 * @param string $key
130 * @param string $value
131 */
132 abstract public function set( $key, $value );
133
134 /**
135 * Close the writer object. You should call this function before the object
136 * goes out of scope, to write out the final hashtables.
137 */
138 abstract public function close();
139
140 /**
141 * If the object goes out of scope, close it for sanity
142 */
143 public function __destruct() {
144 if ( isset( $this->handle ) ) {
145 $this->close();
146 }
147 }
148
149 /**
150 * Are we running on Windows?
151 * @return bool
152 */
153 protected function isWindows() {
154 return substr( php_uname(), 0, 7 ) == 'Windows';
155 }
156 }
157
158 /**
159 * Exception for Cdb errors.
160 * This explicitly doesn't subclass MWException to encourage reuse.
161 */
162 class CdbException extends Exception {
163 }