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') {
13 $this->_file = $filename;
14 $this->_handler = $handler;
15 $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
17 if (function_exists("dba_handlers")) { // since php-4.3.0
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())));
25 $this->readonly = false;
30 function set_timeout($timeout) {
31 $this->_timeout = $timeout;
34 function open($mode = 'w') {
36 return; // already open.
38 $watchdog = $this->_timeout;
41 $this->_dba_open_error = false;
42 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
44 // oops, you don't have DBA support.
45 if (!function_exists("dba_open")) {
46 echo "You don't seem to have DBA support compiled into PHP.";
53 if ((strlen($mode) == 1)) {
54 // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
56 $mode .= "-"; // suppress locking, or
57 } elseif ($this->_handler != 'gdbm') { // gdbm does it internally
58 $mode .= "d"; // else use internal locking
61 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
64 // "c" failed, try "w" instead.
66 and file_exists($this->_file)
67 and (isWindows() or !is_writable($this->_file)))
69 // try to continue with read-only
70 if (!defined("READONLY"))
71 define("READONLY", true);
72 $GLOBALS['request']->_dbi->readonly = true;
73 $this->readonly = true;
76 if (substr($mode,0,1) == "c" and file_exists($this->_file) and !READONLY)
78 // conflict: wait some random time to unlock (as with ethernet)
79 $secs = 0.5 + ((double)rand(1,32767)/32767);
82 if (strlen($mode) == 2) $mode = substr($mode,0,-1);
84 $ErrorManager->popErrorHandler();
87 if ( ($error = $this->_dba_open_error) ) {
88 $error->errno = E_USER_ERROR;
89 $error->errstr .= "\nfile: " . $this->_file
91 . "\nhandler: " . $this->_handler;
92 // try to continue with read-only
93 if (!defined("READONLY"))
94 define("READONLY", true);
95 $GLOBALS['request']->_dbi->readonly = true;
96 $this->readonly = true;
97 if (!file_exists($this->_file)) {
98 $ErrorManager->handleError($error);
103 trigger_error("dba_open failed", E_USER_ERROR);
112 dba_close($this->_dbh);
116 function exists($key) {
117 return dba_exists($key, $this->_dbh);
120 function fetch($key) {
121 $val = dba_fetch($key, $this->_dbh);
123 return $this->_error("fetch($key)");
127 function insert($key, $val) {
128 if (!dba_insert($key, $val, $this->_dbh))
129 return $this->_error("insert($key)");
132 function replace($key, $val) {
133 if (!dba_replace($key, $val, $this->_dbh))
134 return $this->_error("replace($key)");
138 function firstkey() {
139 return dba_firstkey($this->_dbh);
143 return dba_nextkey($this->_dbh);
146 function delete($key) {
147 if ($this->readonly) return;
148 if (!dba_delete($key, $this->_dbh))
149 return $this->_error("delete($key)");
153 return dba_fetch($key, $this->_dbh);
156 function set($key, $val) {
158 if ($this->readonly) return;
159 if (dba_exists($key, $dbh)) {
160 if ($val !== false) {
161 if (!dba_replace($key, $val, $dbh))
162 return $this->_error("store[replace]($key)");
165 if (!dba_delete($key, $dbh))
166 return $this->_error("store[delete]($key)");
170 if (!dba_insert($key, $val, $dbh))
171 return $this->_error("store[insert]($key)");
176 if (!dba_sync($this->_dbh))
177 return $this->_error("sync()");
180 function optimize() {
181 if (!dba_optimize($this->_dbh))
182 return $this->_error("optimize()");
186 function _error($mes) {
187 //trigger_error("DbaDatabase: $mes", E_USER_WARNING);
189 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
194 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
195 printf("%10s: %s\n", $key, $this->fetch($key));
198 function _dba_open_error_handler($error) {
199 $this->_dba_open_error = $error;
208 // c-hanging-comment-ender-p: nil
209 // indent-tabs-mode: nil