]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbaDatabase.php
Reformat code
[SourceForge/phpwiki.git] / lib / DbaDatabase.php
1 <?php
2
3 require_once 'lib/ErrorManager.php';
4
5 if (isWindows())
6     define('DBA_DATABASE_DEFAULT_TIMEOUT', 60);
7 else
8     define('DBA_DATABASE_DEFAULT_TIMEOUT', 5);
9
10 class DbaDatabase
11 {
12     function DbaDatabase($filename, $mode = false, $handler = 'gdbm')
13     {
14         $this->_file = $filename;
15         $this->_handler = $handler;
16         $this->_timeout = DBA_DATABASE_DEFAULT_TIMEOUT;
17         $this->_dbh = false;
18         if (!in_array($handler, dba_handlers()))
19             $this->_error(
20                 sprintf(
21                     _("The DBA handler %s is unsupported!") . "\n" .
22                         _("Supported handlers are: %s"),
23                     $handler, join(",", dba_handlers())));
24         $this->readonly = false;
25         if ($mode)
26             $this->open($mode);
27     }
28
29     function set_timeout($timeout)
30     {
31         $this->_timeout = $timeout;
32     }
33
34     function open($mode = 'w')
35     {
36         if ($this->_dbh)
37             return; // already open.
38
39         $watchdog = $this->_timeout;
40
41         global $ErrorManager;
42         $this->_dba_open_error = false;
43         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler'));
44
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.";
48         }
49
50         if (READONLY) {
51             $mode = 'r';
52         }
53
54         if ((strlen($mode) == 1)) {
55             // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975
56             if (isWindows()) {
57                 $mode .= "-"; // suppress locking, or
58             } elseif ($this->_handler != 'gdbm') { // gdbm does it internally
59                 $mode .= "d"; // else use internal locking
60             }
61         }
62         while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) {
63             if ($watchdog <= 0)
64                 break;
65             // "c" failed, try "w" instead.
66             if ($mode == "w"
67                 and file_exists($this->_file)
68                     and (isWindows() or !is_writable($this->_file))
69             ) {
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;
75                 $mode = "r";
76             }
77             if (substr($mode, 0, 1) == "c" and file_exists($this->_file) and !READONLY)
78                 $mode = "w";
79             // conflict: wait some random time to unlock (as with ethernet)
80             $secs = 0.5 + ((double)rand(1, 32767) / 32767);
81             sleep($secs);
82             $watchdog -= $secs;
83             if (strlen($mode) == 2) $mode = substr($mode, 0, -1);
84         }
85         $ErrorManager->popErrorHandler();
86
87         if (!$dbh) {
88             if (($error = $this->_dba_open_error)) {
89                 $error->errno = E_USER_ERROR;
90                 $error->errstr .= "\nfile: " . $this->_file
91                     . "\nmode: " . $mode
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);
100                     flush();
101                 }
102             } else {
103                 trigger_error("dba_open failed", E_USER_ERROR);
104             }
105         }
106         $this->_dbh = $dbh;
107         return !empty($dbh);
108     }
109
110     function close()
111     {
112         if ($this->_dbh)
113             dba_close($this->_dbh);
114         $this->_dbh = false;
115     }
116
117     function exists($key)
118     {
119         return dba_exists($key, $this->_dbh);
120     }
121
122     function fetch($key)
123     {
124         $val = dba_fetch($key, $this->_dbh);
125         if ($val === false)
126             return $this->_error("fetch($key)");
127         return $val;
128     }
129
130     function insert($key, $val)
131     {
132         if (!dba_insert($key, $val, $this->_dbh))
133             return $this->_error("insert($key)");
134     }
135
136     function replace($key, $val)
137     {
138         if (!dba_replace($key, $val, $this->_dbh))
139             return $this->_error("replace($key)");
140     }
141
142
143     function firstkey()
144     {
145         return dba_firstkey($this->_dbh);
146     }
147
148     function nextkey()
149     {
150         return dba_nextkey($this->_dbh);
151     }
152
153     function delete($key)
154     {
155         if ($this->readonly) return;
156         if (!dba_delete($key, $this->_dbh))
157             return $this->_error("delete($key)");
158     }
159
160     function get($key)
161     {
162         return dba_fetch($key, $this->_dbh);
163     }
164
165     function set($key, $val)
166     {
167         $dbh = &$this->_dbh;
168         if ($this->readonly) return;
169         if (dba_exists($key, $dbh)) {
170             if ($val !== false) {
171                 if (!dba_replace($key, $val, $dbh))
172                     return $this->_error("store[replace]($key)");
173             } else {
174                 if (!dba_delete($key, $dbh))
175                     return $this->_error("store[delete]($key)");
176             }
177         } else {
178             if (!dba_insert($key, $val, $dbh))
179                 return $this->_error("store[insert]($key)");
180         }
181     }
182
183     function sync()
184     {
185         if (!dba_sync($this->_dbh))
186             return $this->_error("sync()");
187     }
188
189     function optimize()
190     {
191         if (!dba_optimize($this->_dbh))
192             return $this->_error("optimize()");
193         return 1;
194     }
195
196     function _error($mes)
197     {
198         //trigger_error("DbaDatabase: $mes", E_USER_WARNING);
199         //return false;
200         trigger_error("$this->_file: dba error: $mes", E_USER_ERROR);
201     }
202
203     function _dump()
204     {
205         $dbh = &$this->_dbh;
206         for ($key = $this->firstkey($dbh); $key; $key = $this->nextkey($dbh))
207             printf("%10s: %s\n", $key, $this->fetch($key));
208     }
209
210     function _dba_open_error_handler($error)
211     {
212         $this->_dba_open_error = $error;
213         return true;
214     }
215 }
216
217 // Local Variables:
218 // mode: php
219 // tab-width: 8
220 // c-basic-offset: 4
221 // c-hanging-comment-ender-p: nil
222 // indent-tabs-mode: nil
223 // End: