1 <?php rcs_id('$Id: DbaDatabase.php,v 1.13 2004-05-04 14:24:07 rurban Exp $');
3 require_once('lib/ErrorManager.php');
5 define('DBA_DATABASE_DEFAULT_TIMEOUT', 20);
9 function DbaDatabase($filename, $mode = false, $handler = 'gdbm') {
10 $this->_file = $filename;
11 $this->_handler = $handler;
12 $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
14 if (function_exists("dba_handlers")) { // since 4.3.0
15 if (!in_array($handler,dba_handlers()))
18 _("The DBA handler %s is unsupported!")."\n".
19 _("Supported handlers are: %s"),
20 $handler, join(",",dba_handlers())));
26 function set_timeout($timeout) {
27 $this->_timeout = $timeout;
30 function open($mode = 'w') {
32 return; // already open.
34 $watchdog = $this->_timeout;
37 $this->_dba_open_error = false;
38 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
40 // oops, you don't have DBA support.
41 if (!function_exists("dba_open")) {
42 echo "You don't seem to have DBA support compiled into PHP.";
45 // lock supported since 4.3.0:
46 if (check_php_version(4,3,0) and (strlen($mode) == 1)) {
47 // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
49 $mode .= "-"; // suppress locking, or
50 } elseif ($handler != 'gdbm') { // gdbm does it internally
51 $mode .= "d"; // else use internal locking
54 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
58 // "c" failed, try "w" instead.
59 if (substr($mode,0,1) == "c" and file_exists($this->_file))
61 // conflict: wait some random time to unlock (see ethernet)
62 $secs = 0.5 + ((double)rand(1,32767)/32767);
65 if (strlen($mode) == 2) $mode = substr($mode,0,-1);
67 $ErrorManager->popErrorHandler();
70 if ( ($error = $this->_dba_open_error) ) {
71 $error->errno = E_USER_ERROR;
72 $error->errstr .= "\nfile: " . $this->_file
74 . "\nhandler: " . $this->_handler;
75 $ErrorManager->handleError($error);
78 trigger_error("dba_open failed", E_USER_ERROR);
87 dba_close($this->_dbh);
91 function exists($key) {
92 return dba_exists($key, $this->_dbh);
95 function fetch($key) {
96 $val = dba_fetch($key, $this->_dbh);
98 return $this->_error("fetch($key)");
102 function insert($key, $val) {
103 if (!dba_insert($key, $val, $this->_dbh))
104 return $this->_error("insert($key)");
107 function replace($key, $val) {
108 if (!dba_replace($key, $val, $this->_dbh))
109 return $this->_error("replace($key)");
113 function firstkey() {
114 return dba_firstkey($this->_dbh);
118 return dba_nextkey($this->_dbh);
121 function delete($key) {
122 if (!dba_delete($key, $this->_dbh))
123 return $this->_error("delete($key)");
127 return dba_fetch($key, $this->_dbh);
130 function set($key, $val) {
132 if (dba_exists($key, $dbh)) {
133 if ($val !== false) {
134 if (!dba_replace($key, $val, $dbh))
135 return $this->_error("store[replace]($key)");
138 if (!dba_delete($key, $dbh))
139 return $this->_error("store[delete]($key)");
143 if (!dba_insert($key, $val, $this->_dbh))
144 return $this->_error("store[insert]($key)");
149 if (!dba_sync($this->_dbh))
150 return $this->_error("sync()");
153 function optimize() {
154 if (!dba_optimize($this->_dbh))
155 return $this->_error("optimize()");
159 function _error($mes) {
160 trigger_error("DbaDatabase: $mes", E_USER_WARNING);
163 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
168 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
169 printf("%10s: %s\n", $key, $this->fetch($key));
172 function _dba_open_error_handler ($error) {
173 $this->_dba_open_error = $error;
179 // (c-file-style: "gnu")
184 // c-hanging-comment-ender-p: nil
185 // indent-tabs-mode: nil