3 require_once 'lib/ErrorManager.php';
6 define('DBA_DATABASE_DEFAULT_TIMEOUT', 60);
8 define('DBA_DATABASE_DEFAULT_TIMEOUT', 5);
20 public $_dba_open_error;
23 * @param string $filename
25 * @param string $handler
27 function __construct($filename, $mode = false, $handler = 'gdbm')
29 $this->_file = $filename;
30 $this->_handler = $handler;
31 $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
33 if (!in_array($handler, dba_handlers()))
36 _("The DBA handler %s is unsupported!") . "\n" .
37 _("Supported handlers are: %s"),
38 $handler, join(",", dba_handlers())));
39 $this->readonly = false;
44 function set_timeout($timeout)
46 $this->_timeout = $timeout;
49 function open($mode = 'w')
52 return true; // already open.
54 $watchdog = $this->_timeout;
57 $this->_dba_open_error = false;
58 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
60 // oops, you don't have DBA support.
61 if (!function_exists("dba_open")) {
62 echo "You don't seem to have DBA support compiled into PHP.";
69 if ((strlen($mode) == 1)) {
70 // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
72 $mode .= "-"; // suppress locking, or
73 } elseif ($this->_handler != 'gdbm') { // gdbm does it internally
74 $mode .= "d"; // else use internal locking
77 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
80 // "c" failed, try "w" instead.
82 and file_exists($this->_file)
83 and (isWindows() or !is_writable($this->_file))
85 // try to continue with read-only
86 if (!defined("READONLY"))
87 define("READONLY", true);
88 $GLOBALS['request']->_dbi->readonly = true;
89 $this->readonly = true;
92 if (substr($mode, 0, 1) == "c" and file_exists($this->_file) and !READONLY)
94 // conflict: wait some random time to unlock (as with ethernet)
95 $secs = 0.5 + ((double)rand(1, 32767) / 32767);
98 if (strlen($mode) == 2) $mode = substr($mode, 0, -1);
100 $ErrorManager->popErrorHandler();
103 if (($error = $this->_dba_open_error)) {
104 $error->errno = E_USER_ERROR;
105 $error->errstr .= "\nfile: " . $this->_file
107 . "\nhandler: " . $this->_handler;
108 // try to continue with read-only
109 if (!defined("READONLY"))
110 define("READONLY", true);
111 $GLOBALS['request']->_dbi->readonly = true;
112 $this->readonly = true;
113 if (!file_exists($this->_file)) {
114 $ErrorManager->handleError($error);
118 trigger_error("dba_open failed", E_USER_ERROR);
128 dba_close($this->_dbh);
132 function exists($key)
134 return dba_exists($key, $this->_dbh);
139 $val = dba_fetch($key, $this->_dbh);
140 if ($val === false) {
141 $this->_error("fetch($key)");
146 function insert($key, $val)
148 if (!dba_insert($key, $val, $this->_dbh))
149 $this->_error("insert($key)");
152 function replace($key, $val)
154 if (!dba_replace($key, $val, $this->_dbh))
155 $this->_error("replace($key)");
160 return dba_firstkey($this->_dbh);
165 return dba_nextkey($this->_dbh);
168 function delete($key)
172 if (!dba_delete($key, $this->_dbh))
173 $this->_error("delete($key)");
178 return dba_fetch($key, $this->_dbh);
181 function set($key, $val)
186 if (dba_exists($key, $dbh)) {
187 if ($val !== false) {
188 if (!dba_replace($key, $val, $dbh))
189 $this->_error("store[replace]($key)");
191 if (!dba_delete($key, $dbh))
192 $this->_error("store[delete]($key)");
195 if (!dba_insert($key, $val, $dbh))
196 $this->_error("store[insert]($key)");
202 if (!dba_sync($this->_dbh))
203 $this->_error("sync()");
208 if (!dba_optimize($this->_dbh))
209 $this->_error("optimize()");
213 private function _error($mes)
215 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
221 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
222 printf("%10s: %s\n", $key, $this->fetch($key));
225 function _dba_open_error_handler($error)
227 $this->_dba_open_error = $error;
236 // c-hanging-comment-ender-p: nil
237 // indent-tabs-mode: nil