Use exact counts in updateCollation.php
[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.\n" );
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.\n" );
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 if( isset($this->handle) )
102 dba_close( $this->handle );
103 unset( $this->handle );
104 }
105
106 function get( $key ) {
107 return dba_fetch( $key, $this->handle );
108 }
109 }
110
111
112 /**
113 * Writer class which uses the DBA extension
114 */
115 class CdbWriter_DBA {
116 var $handle, $realFileName, $tmpFileName;
117
118 function __construct( $fileName ) {
119 $this->realFileName = $fileName;
120 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
121 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' );
122 if ( !$this->handle ) {
123 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' );
124 }
125 }
126
127 function set( $key, $value ) {
128 return dba_insert( $key, $value, $this->handle );
129 }
130
131 function close() {
132 if( isset($this->handle) )
133 dba_close( $this->handle );
134 if ( wfIsWindows() ) {
135 unlink( $this->realFileName );
136 }
137 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
138 throw new MWException( 'Unable to move the new CDB file into place.' );
139 }
140 unset( $this->handle );
141 }
142
143 function __destruct() {
144 if ( isset( $this->handle ) ) {
145 $this->close();
146 }
147 }
148 }
149