3 require_once('lib/ErrorManager.php');
5 define('DBA_DATABASE_DEFAULT_TIMEOUT', 5);
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 php-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())));
22 $this->readonly = false;
27 function set_timeout($timeout) {
28 $this->_timeout = $timeout;
31 function open($mode = 'w') {
33 return; // already open.
35 $watchdog = $this->_timeout;
38 $this->_dba_open_error = false;
39 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
41 // oops, you don't have DBA support.
42 if (!function_exists("dba_open")) {
43 echo "You don't seem to have DBA support compiled into PHP.";
50 // lock supported since 4.3.0:
51 if (check_php_version(4,3,0) and (strlen($mode) == 1)) {
52 // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
54 $mode .= "-"; // suppress locking, or
55 } elseif ($this->_handler != 'gdbm') { // gdbm does it internally
56 $mode .= "d"; // else use internal locking
59 while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
62 // "c" failed, try "w" instead.
64 and file_exists($this->_file)
65 and (isWindows() or !is_writable($this->_file)))
67 // try to continue with read-only
68 if (!defined("READONLY"))
69 define("READONLY", true);
70 $GLOBALS['request']->_dbi->readonly = true;
71 $this->readonly = true;
74 if (substr($mode,0,1) == "c" and file_exists($this->_file) and !READONLY)
76 // conflict: wait some random time to unlock (as with ethernet)
77 $secs = 0.5 + ((double)rand(1,32767)/32767);
80 if (strlen($mode) == 2) $mode = substr($mode,0,-1);
82 $ErrorManager->popErrorHandler();
85 if ( ($error = $this->_dba_open_error) ) {
86 $error->errno = E_USER_ERROR;
87 $error->errstr .= "\nfile: " . $this->_file
89 . "\nhandler: " . $this->_handler;
90 // try to continue with read-only
91 if (!defined("READONLY"))
92 define("READONLY", true);
93 $GLOBALS['request']->_dbi->readonly = true;
94 $this->readonly = true;
95 if (!file_exist($this->_file)) {
96 $ErrorManager->handleError($error);
101 trigger_error("dba_open failed", E_USER_ERROR);
110 dba_close($this->_dbh);
114 function exists($key) {
115 return dba_exists($key, $this->_dbh);
118 function fetch($key) {
119 $val = dba_fetch($key, $this->_dbh);
121 return $this->_error("fetch($key)");
125 function insert($key, $val) {
126 if (!dba_insert($key, $val, $this->_dbh))
127 return $this->_error("insert($key)");
130 function replace($key, $val) {
131 if (!dba_replace($key, $val, $this->_dbh))
132 return $this->_error("replace($key)");
136 function firstkey() {
137 return dba_firstkey($this->_dbh);
141 return dba_nextkey($this->_dbh);
144 function delete($key) {
145 if ($this->readonly) return;
146 if (!dba_delete($key, $this->_dbh))
147 return $this->_error("delete($key)");
151 return dba_fetch($key, $this->_dbh);
154 function set($key, $val) {
156 if ($this->readonly) return;
157 if (dba_exists($key, $dbh)) {
158 if ($val !== false) {
159 if (!dba_replace($key, $val, $dbh))
160 return $this->_error("store[replace]($key)");
163 if (!dba_delete($key, $dbh))
164 return $this->_error("store[delete]($key)");
168 if (!dba_insert($key, $val, $dbh))
169 return $this->_error("store[insert]($key)");
174 if (!dba_sync($this->_dbh))
175 return $this->_error("sync()");
178 function optimize() {
179 if (!dba_optimize($this->_dbh))
180 return $this->_error("optimize()");
184 function _error($mes) {
185 //trigger_error("DbaDatabase: $mes", E_USER_WARNING);
187 trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
192 for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
193 printf("%10s: %s\n", $key, $this->fetch($key));
196 function _dba_open_error_handler($error) {
197 $this->_dba_open_error = $error;
202 // (c-file-style: "gnu")
207 // c-hanging-comment-ender-p: nil
208 // indent-tabs-mode: nil