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