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