Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / Cdb.php
bloba142c7cb2f49b30cb474ceb34c37c4d217ac532b
1 <?php
2 /**
3 * Native CDB file reader and writer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Read from a CDB file.
25 * Native and pure PHP implementations are provided.
26 * http://cr.yp.to/cdb.html
28 abstract class CdbReader {
29 /**
30 * Open a file and return a subclass instance
32 * @param $fileName string
34 * @return CdbReader
36 public static function open( $fileName ) {
37 if ( self::haveExtension() ) {
38 return new CdbReader_DBA( $fileName );
39 } else {
40 wfDebug( "Warning: no dba extension found, using emulation.\n" );
41 return new CdbReader_PHP( $fileName );
45 /**
46 * Returns true if the native extension is available
48 * @return bool
50 public static function haveExtension() {
51 if ( !function_exists( 'dba_handlers' ) ) {
52 return false;
54 $handlers = dba_handlers();
55 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
56 return false;
58 return true;
61 /**
62 * Construct the object and open the file
64 abstract function __construct( $fileName );
66 /**
67 * Close the file. Optional, you can just let the variable go out of scope.
69 abstract function close();
71 /**
72 * Get a value with a given key. Only string values are supported.
74 * @param $key string
76 abstract public function get( $key );
79 /**
80 * Write to a CDB file.
81 * Native and pure PHP implementations are provided.
83 abstract class CdbWriter {
84 /**
85 * Open a writer and return a subclass instance.
86 * The user must have write access to the directory, for temporary file creation.
88 * @param $fileName string
90 * @return CdbWriter_DBA|CdbWriter_PHP
92 public static function open( $fileName ) {
93 if ( CdbReader::haveExtension() ) {
94 return new CdbWriter_DBA( $fileName );
95 } else {
96 wfDebug( "Warning: no dba extension found, using emulation.\n" );
97 return new CdbWriter_PHP( $fileName );
102 * Create the object and open the file
104 * @param $fileName string
106 abstract function __construct( $fileName );
109 * Set a key to a given value. The value will be converted to string.
110 * @param $key string
111 * @param $value string
113 abstract public function set( $key, $value );
116 * Close the writer object. You should call this function before the object
117 * goes out of scope, to write out the final hashtables.
119 abstract public function close();
123 * Reader class which uses the DBA extension
125 class CdbReader_DBA {
126 var $handle;
128 function __construct( $fileName ) {
129 $this->handle = dba_open( $fileName, 'r-', 'cdb' );
130 if ( !$this->handle ) {
131 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
135 function close() {
136 if( isset( $this->handle ) ) {
137 dba_close( $this->handle );
139 unset( $this->handle );
142 function get( $key ) {
143 return dba_fetch( $key, $this->handle );
148 * Writer class which uses the DBA extension
150 class CdbWriter_DBA {
151 var $handle, $realFileName, $tmpFileName;
153 function __construct( $fileName ) {
154 $this->realFileName = $fileName;
155 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
156 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' );
157 if ( !$this->handle ) {
158 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
162 function set( $key, $value ) {
163 return dba_insert( $key, $value, $this->handle );
166 function close() {
167 if( isset( $this->handle ) ) {
168 dba_close( $this->handle );
170 if ( wfIsWindows() ) {
171 unlink( $this->realFileName );
173 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
174 throw new MWException( 'Unable to move the new CDB file into place.' );
176 unset( $this->handle );
179 function __destruct() {
180 if ( isset( $this->handle ) ) {
181 $this->close();