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