]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/cdb/src/Writer.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / cdb / src / Writer.php
1 <?php
2
3 namespace Cdb;
4
5 /**
6  * Native CDB file reader and writer.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @file
24  */
25
26 /**
27  * Write to a CDB file.
28  * Native and pure PHP implementations are provided.
29  * http://cr.yp.to/cdb.html
30  */
31 abstract class Writer {
32         /**
33          * The file handle
34          */
35         protected $handle;
36
37         /**
38          * File we'll be writing to when we're done
39          * @var string
40          */
41         protected $realFileName;
42
43         /**
44          * File we write to temporarily until we're done
45          * @var string
46          */
47         protected $tmpFileName;
48
49         /**
50          * Open a writer and return a subclass instance.
51          * The user must have write access to the directory, for temporary file creation.
52          *
53          * @param string $fileName
54          *
55          * @return Writer
56          */
57         public static function open( $fileName ) {
58                 return Reader::haveExtension() ?
59                         new Writer\DBA( $fileName ) :
60                         new Writer\PHP( $fileName );
61         }
62
63         /**
64          * Create the object and open the file
65          *
66          * @param string $fileName
67          */
68         abstract public function __construct( $fileName );
69
70         /**
71          * Set a key to a given value. The value will be converted to string.
72          * @param string $key
73          * @param string $value
74          */
75         abstract public function set( $key, $value );
76
77         /**
78          * Close the writer object. You should call this function before the object
79          * goes out of scope, to write out the final hashtables.
80          */
81         abstract public function close();
82
83         /**
84          * If the object goes out of scope, close it for sanity
85          */
86         public function __destruct() {
87                 if ( isset( $this->handle ) ) {
88                         $this->close();
89                 }
90         }
91
92         /**
93          * Are we running on Windows?
94          * @return bool
95          */
96         protected function isWindows() {
97                 return strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN';
98         }
99 }