3 require_once 'lib/ErrorManager.php';
6 define('DBA_DATABASE_DEFAULT_TIMEOUT', 60);
8 define('DBA_DATABASE_DEFAULT_TIMEOUT', 5);
12 function DbaDatabase($filename, $mode = false, $handler = 'gdbm')
14 $this->_file = $filename;
15 $this->_handler = $handler;
16 $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
18 if (!in_array($handler, dba_handlers()))
21 _("The DBA handler %s is unsupported!") . "\n" .
22 _("Supported handlers are: %s"),
23 $handler, join(",", dba_handlers())));
24 $this->readonly = false;
29 function set_timeout($timeout)
31 $this->_timeout = $timeout;
34 function open($mode = 'w')
37 return; // already open.
39 $watchdog = $this->_timeout;
42 $this->_dba_open_error = false;
43 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
45 // oops, you don't have DBA support.
46 if (!function_exists("dba_open")) {
47 echo "You don't seem to have DBA support compiled into PHP.";
54 if ((strlen($mode) == 1)) {
55 // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
57 $mode .= "-"; // suppress locking, or
58 } elseif ($this->_handler != 'gdbm') { // gdbm does it internally
59 $mode .= "d"; // else use internal locking
62 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
65 // "c" failed, try "w" instead.
67 and file_exists($this->_file)
68 and (isWindows() or !is_writable($this->_file))
70 // try to continue with read-only
71 if (!defined("READONLY"))
72 define("READONLY", true);
73 $GLOBALS['request']->_dbi->readonly = true;
74 $this->readonly = true;
77 if (substr($mode, 0, 1) == "c" and file_exists($this->_file) and !READONLY)
79 // conflict: wait some random time to unlock (as with ethernet)
80 $secs = 0.5 + ((double)rand(1, 32767) / 32767);
83 if (strlen($mode) == 2) $mode = substr($mode, 0, -1);
85 $ErrorManager->popErrorHandler();
88 if (($error = $this->_dba_open_error)) {
89 $error->errno = E_USER_ERROR;
90 $error->errstr .= "\nfile: " . $this->_file
92 . "\nhandler: " . $this->_handler;
93 // try to continue with read-only
94 if (!defined("READONLY"))
95 define("READONLY", true);
96 $GLOBALS['request']->_dbi->readonly = true;
97 $this->readonly = true;
98 if (!file_exists($this->_file)) {
99 $ErrorManager->handleError($error);
103 trigger_error("dba_open failed", E_USER_ERROR);
113 dba_close($this->_dbh);
117 function exists($key)
119 return dba_exists($key, $this->_dbh);
124 $val = dba_fetch($key, $this->_dbh);
126 return $this->_error("fetch($key)");
130 function insert($key, $val)
132 if (!dba_insert($key, $val, $this->_dbh))
133 return $this->_error("insert($key)");
136 function replace($key, $val)
138 if (!dba_replace($key, $val, $this->_dbh))
139 return $this->_error("replace($key)");
144 return dba_firstkey($this->_dbh);
149 return dba_nextkey($this->_dbh);
152 function delete($key)
154 if ($this->readonly) return;
155 if (!dba_delete($key, $this->_dbh))
156 return $this->_error("delete($key)");
161 return dba_fetch($key, $this->_dbh);
164 function set($key, $val)
167 if ($this->readonly) return;
168 if (dba_exists($key, $dbh)) {
169 if ($val !== false) {
170 if (!dba_replace($key, $val, $dbh))
171 return $this->_error("store[replace]($key)");
173 if (!dba_delete($key, $dbh))
174 return $this->_error("store[delete]($key)");
177 if (!dba_insert($key, $val, $dbh))
178 return $this->_error("store[insert]($key)");
184 if (!dba_sync($this->_dbh))
185 return $this->_error("sync()");
190 if (!dba_optimize($this->_dbh))
191 return $this->_error("optimize()");
195 function _error($mes)
197 //trigger_error("DbaDatabase: $mes", E_USER_WARNING);
199 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
205 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
206 printf("%10s: %s\n", $key, $this->fetch($key));
209 function _dba_open_error_handler($error)
211 $this->_dba_open_error = $error;
220 // c-hanging-comment-ender-p: nil
221 // indent-tabs-mode: nil