* Changed OutputPage's handling of subtitles to use an array and implode it with...
[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 abstract function __construct( $fileName );
90
91 /**
92 * Set a key to a given value. The value will be converted to string.
93 */
94 abstract public function set( $key, $value );
95
96 /**
97 * Close the writer object. You should call this function before the object
98 * goes out of scope, to write out the final hashtables.
99 */
100 abstract public function close();
101 }
102
103
104 /**
105 * Reader class which uses the DBA extension
106 */
107 class CdbReader_DBA {
108 var $handle;
109
110 function __construct( $fileName ) {
111 $this->handle = dba_open( $fileName, 'r-', 'cdb' );
112 if ( !$this->handle ) {
113 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
114 }
115 }
116
117 function close() {
118 if( isset($this->handle) )
119 dba_close( $this->handle );
120 unset( $this->handle );
121 }
122
123 function get( $key ) {
124 return dba_fetch( $key, $this->handle );
125 }
126 }
127
128
129 /**
130 * Writer class which uses the DBA extension
131 */
132 class CdbWriter_DBA {
133 var $handle, $realFileName, $tmpFileName;
134
135 function __construct( $fileName ) {
136 $this->realFileName = $fileName;
137 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
138 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' );
139 if ( !$this->handle ) {
140 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
141 }
142 }
143
144 function set( $key, $value ) {
145 return dba_insert( $key, $value, $this->handle );
146 }
147
148 function close() {
149 if( isset($this->handle) )
150 dba_close( $this->handle );
151 if ( wfIsWindows() ) {
152 unlink( $this->realFileName );
153 }
154 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
155 throw new MWException( 'Unable to move the new CDB file into place.' );
156 }
157 unset( $this->handle );
158 }
159
160 function __destruct() {
161 if ( isset( $this->handle ) ) {
162 $this->close();
163 }
164 }
165 }
166