(bug 22227) Special:Listfiles no longer throws an error on bogus file entries
[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 abstract public function get( $key );
60 }
61
62 /**
63 * Write to a CDB file.
64 * Native and pure PHP implementations are provided.
65 */
66 abstract class CdbWriter {
67 /**
68 * Open a writer and return a subclass instance.
69 * The user must have write access to the directory, for temporary file creation.
70 *
71 * @param $fileName string
72 *
73 * @return bool
74 */
75 public static function open( $fileName ) {
76 if ( CdbReader::haveExtension() ) {
77 return new CdbWriter_DBA( $fileName );
78 } else {
79 wfDebug( "Warning: no dba extension found, using emulation.\n" );
80 return new CdbWriter_PHP( $fileName );
81 }
82 }
83
84 /**
85 * Create the object and open the file
86 */
87 abstract function __construct( $fileName );
88
89 /**
90 * Set a key to a given value. The value will be converted to string.
91 */
92 abstract public function set( $key, $value );
93
94 /**
95 * Close the writer object. You should call this function before the object
96 * goes out of scope, to write out the final hashtables.
97 */
98 abstract public function close();
99 }
100
101
102 /**
103 * Reader class which uses the DBA extension
104 */
105 class CdbReader_DBA {
106 var $handle;
107
108 function __construct( $fileName ) {
109 $this->handle = dba_open( $fileName, 'r-', 'cdb' );
110 if ( !$this->handle ) {
111 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
112 }
113 }
114
115 function close() {
116 if( isset($this->handle) )
117 dba_close( $this->handle );
118 unset( $this->handle );
119 }
120
121 function get( $key ) {
122 return dba_fetch( $key, $this->handle );
123 }
124 }
125
126
127 /**
128 * Writer class which uses the DBA extension
129 */
130 class CdbWriter_DBA {
131 var $handle, $realFileName, $tmpFileName;
132
133 function __construct( $fileName ) {
134 $this->realFileName = $fileName;
135 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
136 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' );
137 if ( !$this->handle ) {
138 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
139 }
140 }
141
142 function set( $key, $value ) {
143 return dba_insert( $key, $value, $this->handle );
144 }
145
146 function close() {
147 if( isset($this->handle) )
148 dba_close( $this->handle );
149 if ( wfIsWindows() ) {
150 unlink( $this->realFileName );
151 }
152 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
153 throw new MWException( 'Unable to move the new CDB file into place.' );
154 }
155 unset( $this->handle );
156 }
157
158 function __destruct() {
159 if ( isset( $this->handle ) ) {
160 $this->close();
161 }
162 }
163 }
164