More reversion of r77297, 1 of 2 commits to keep it readable in CR (hopefully)
[mediawiki.git] / includes / Cdb.php
blob604774853cf22385fa9a9dd859c0bb172c2e81a5
1 <?php
2 /**
3 * Native CDB file reader and writer
5 * @file
6 */
8 /**
9 * Read from a CDB file.
10 * Native and pure PHP implementations are provided.
11 * http://cr.yp.to/cdb.html
13 abstract class CdbReader {
14 /**
15 * Open a file and return a subclass instance
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 );
26 /**
27 * Returns true if the native extension is available
29 public static function haveExtension() {
30 if ( !function_exists( 'dba_handlers' ) ) {
31 return false;
33 $handlers = dba_handlers();
34 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
35 return false;
37 return true;
40 /**
41 * Construct the object and open the file
43 abstract function __construct( $fileName );
45 /**
46 * Close the file. Optional, you can just let the variable go out of scope.
48 abstract function close();
50 /**
51 * Get a value with a given key. Only string values are supported.
53 abstract public function get( $key );
56 /**
57 * Write to a CDB file.
58 * Native and pure PHP implementations are provided.
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.
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 );
74 /**
75 * Create the object and open the file
77 abstract function __construct( $fileName );
79 /**
80 * Set a key to a given value. The value will be converted to string.
82 abstract public function set( $key, $value );
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.
88 abstract public function close();
92 /**
93 * Reader class which uses the DBA extension
95 class CdbReader_DBA {
96 var $handle;
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 . '"' );
105 function close() {
106 if( isset($this->handle) )
107 dba_close( $this->handle );
108 unset( $this->handle );
111 function get( $key ) {
112 return dba_fetch( $key, $this->handle );
118 * Writer class which uses the DBA extension
120 class CdbWriter_DBA {
121 var $handle, $realFileName, $tmpFileName;
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 . '"' );
132 function set( $key, $value ) {
133 return dba_insert( $key, $value, $this->handle );
136 function close() {
137 if( isset($this->handle) )
138 dba_close( $this->handle );
139 if ( wfIsWindows() ) {
140 unlink( $this->realFileName );
142 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
143 throw new MWException( 'Unable to move the new CDB file into place.' );
145 unset( $this->handle );
148 function __destruct() {
149 if ( isset( $this->handle ) ) {
150 $this->close();