cdb: One class per file
[lhc/web/wiklou.git] / includes / libs / cdb / CdbWriter.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 * Write to a CDB file.
25 * Native and pure PHP implementations are provided.
26 * http://cr.yp.to/cdb.html
27 */
28 abstract class CdbWriter {
29 /**
30 * The file handle
31 */
32 protected $handle;
33
34 /**
35 * File we'll be writing to when we're done
36 * @var string
37 */
38 protected $realFileName;
39
40 /**
41 * File we write to temporarily until we're done
42 * @var string
43 */
44 protected $tmpFileName;
45
46 /**
47 * Open a writer and return a subclass instance.
48 * The user must have write access to the directory, for temporary file creation.
49 *
50 * @param string $fileName
51 *
52 * @return CdbWriterDBA|CdbWriterPHP
53 */
54 public static function open( $fileName ) {
55 return CdbReader::haveExtension() ?
56 new CdbWriterDBA( $fileName ) :
57 new CdbWriterPHP( $fileName );
58 }
59
60 /**
61 * Create the object and open the file
62 *
63 * @param string $fileName
64 */
65 abstract public function __construct( $fileName );
66
67 /**
68 * Set a key to a given value. The value will be converted to string.
69 * @param string $key
70 * @param string $value
71 */
72 abstract public function set( $key, $value );
73
74 /**
75 * Close the writer object. You should call this function before the object
76 * goes out of scope, to write out the final hashtables.
77 */
78 abstract public function close();
79
80 /**
81 * If the object goes out of scope, close it for sanity
82 */
83 public function __destruct() {
84 if ( isset( $this->handle ) ) {
85 $this->close();
86 }
87 }
88
89 /**
90 * Are we running on Windows?
91 * @return bool
92 */
93 protected function isWindows() {
94 return substr( php_uname(), 0, 7 ) == 'Windows';
95 }
96 }