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