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