Merge "Fix documentation in Linker::formatTemplates."
[lhc/web/wiklou.git] / includes / 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 * Open a file and return a subclass instance
31 *
32 * @param $fileName string
33 *
34 * @return CdbReader
35 */
36 public static function open( $fileName ) {
37 if ( self::haveExtension() ) {
38 return new CdbReader_DBA( $fileName );
39 } else {
40 wfDebug( "Warning: no dba extension found, using emulation.\n" );
41 return new CdbReader_PHP( $fileName );
42 }
43 }
44
45 /**
46 * Returns true if the native extension is available
47 *
48 * @return bool
49 */
50 public static function haveExtension() {
51 if ( !function_exists( 'dba_handlers' ) ) {
52 return false;
53 }
54 $handlers = dba_handlers();
55 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
56 return false;
57 }
58 return true;
59 }
60
61 /**
62 * Construct the object and open the file
63 */
64 abstract function __construct( $fileName );
65
66 /**
67 * Close the file. Optional, you can just let the variable go out of scope.
68 */
69 abstract function close();
70
71 /**
72 * Get a value with a given key. Only string values are supported.
73 *
74 * @param $key string
75 */
76 abstract public function get( $key );
77 }
78
79 /**
80 * Write to a CDB file.
81 * Native and pure PHP implementations are provided.
82 */
83 abstract class CdbWriter {
84 /**
85 * Open a writer and return a subclass instance.
86 * The user must have write access to the directory, for temporary file creation.
87 *
88 * @param $fileName string
89 *
90 * @return CdbWriter_DBA|CdbWriter_PHP
91 */
92 public static function open( $fileName ) {
93 if ( CdbReader::haveExtension() ) {
94 return new CdbWriter_DBA( $fileName );
95 } else {
96 wfDebug( "Warning: no dba extension found, using emulation.\n" );
97 return new CdbWriter_PHP( $fileName );
98 }
99 }
100
101 /**
102 * Create the object and open the file
103 *
104 * @param $fileName string
105 */
106 abstract function __construct( $fileName );
107
108 /**
109 * Set a key to a given value. The value will be converted to string.
110 * @param $key string
111 * @param $value string
112 */
113 abstract public function set( $key, $value );
114
115 /**
116 * Close the writer object. You should call this function before the object
117 * goes out of scope, to write out the final hashtables.
118 */
119 abstract public function close();
120 }
121
122 /**
123 * Reader class which uses the DBA extension
124 */
125 class CdbReader_DBA {
126 var $handle;
127
128 function __construct( $fileName ) {
129 $this->handle = dba_open( $fileName, 'r-', 'cdb' );
130 if ( !$this->handle ) {
131 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
132 }
133 }
134
135 function close() {
136 if( isset( $this->handle ) ) {
137 dba_close( $this->handle );
138 }
139 unset( $this->handle );
140 }
141
142 function get( $key ) {
143 return dba_fetch( $key, $this->handle );
144 }
145 }
146
147
148 /**
149 * Writer class which uses the DBA extension
150 */
151 class CdbWriter_DBA {
152 var $handle, $realFileName, $tmpFileName;
153
154 function __construct( $fileName ) {
155 $this->realFileName = $fileName;
156 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
157 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' );
158 if ( !$this->handle ) {
159 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
160 }
161 }
162
163 function set( $key, $value ) {
164 return dba_insert( $key, $value, $this->handle );
165 }
166
167 function close() {
168 if( isset( $this->handle ) ) {
169 dba_close( $this->handle );
170 }
171 if ( wfIsWindows() ) {
172 unlink( $this->realFileName );
173 }
174 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
175 throw new MWException( 'Unable to move the new CDB file into place.' );
176 }
177 unset( $this->handle );
178 }
179
180 function __destruct() {
181 if ( isset( $this->handle ) ) {
182 $this->close();
183 }
184 }
185 }