Followup r63914, function must match parent
[mediawiki.git] / includes / Cdb.php
blobab429872d1041f408d2b7909b2bc6a5207131a58
1 <?php
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
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 );
21 /**
22 * Returns true if the native extension is available
24 public static function haveExtension() {
25 if ( !function_exists( 'dba_handlers' ) ) {
26 return false;
28 $handlers = dba_handlers();
29 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
30 return false;
32 return true;
35 /**
36 * Construct the object and open the file
38 abstract function __construct( $fileName );
40 /**
41 * Close the file. Optional, you can just let the variable go out of scope.
43 abstract function close();
45 /**
46 * Get a value with a given key. Only string values are supported.
48 abstract public function get( $key );
51 /**
52 * Write to a CDB file.
53 * Native and pure PHP implementations are provided.
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.
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 );
69 /**
70 * Create the object and open the file
72 abstract function __construct( $fileName );
74 /**
75 * Set a key to a given value. The value will be converted to string.
77 abstract public function set( $key, $value );
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.
83 abstract public function close();
87 /**
88 * Reader class which uses the DBA extension
90 class CdbReader_DBA {
91 var $handle;
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 . '"' );
100 function close() {
101 if( isset($this->handle) )
102 dba_close( $this->handle );
103 unset( $this->handle );
106 function get( $key ) {
107 return dba_fetch( $key, $this->handle );
113 * Writer class which uses the DBA extension
115 class CdbWriter_DBA {
116 var $handle, $realFileName, $tmpFileName;
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 . '"' );
127 function set( $key, $value ) {
128 return dba_insert( $key, $value, $this->handle );
131 function close() {
132 if( isset($this->handle) )
133 dba_close( $this->handle );
134 if ( wfIsWindows() ) {
135 unlink( $this->realFileName );
137 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
138 throw new MWException( 'Unable to move the new CDB file into place.' );
140 unset( $this->handle );
143 function __destruct() {
144 if ( isset( $this->handle ) ) {
145 $this->close();