]> git.mxchange.org Git - friendica.git/blob - library/spam/b8/storage/storage_dba.php
Merge remote branch 'upstream/master'
[friendica.git] / library / spam / b8 / storage / storage_dba.php
1 <?php
2
3 #   Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
4 #
5 #   This file is part of the b8 package
6 #
7 #   This program is free software; you can redistribute it and/or modify it
8 #   under the terms of the GNU Lesser General Public License as published by
9 #   the Free Software Foundation in version 2.1 of the License.
10 #
11 #   This program is distributed in the hope that it will be useful, but
12 #   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 #   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 #   License for more details.
15 #
16 #   You should have received a copy of the GNU Lesser General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19
20 /**
21  * The DBA (Berkeley DB) abstraction layer for communicating with the database.
22  * Copyright (C) 2006-2010 Tobias Leupold <tobias.leupold@web.de>
23  *
24  * @license LGPL
25  * @access public
26  * @package b8
27  * @author Tobias Leupold
28  */
29
30 class b8_storage_dba extends b8_storage_base
31 {
32
33         public $config = array(
34                 'database'    => 'wordlist.db',
35                 'handler'     => 'db4',
36         );
37
38         public $b8_config = array(
39                 'degenerator' => NULL,
40                 'today'       => NULL
41         );
42
43         private $_db          = NULL;
44
45         const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL';
46
47         /**
48          * Constructs the database layer.
49          *
50          * @access public
51          * @param string $config
52          */
53
54         function __construct($config, $degenerator, $today)
55         {
56
57                 # Pass some variables of the main b8 config to this class
58                 $this->b8_config['degenerator'] = $degenerator;
59                 $this->b8_config['today']       = $today;
60
61                 # Validate the config items
62                 if(count($config) > 0) {
63                         foreach ($config as $name => $value) {
64                                 $this->config[$name] = (string) $value;
65                         }
66                 }
67
68         }
69
70         /**
71          * Closes the database connection.
72          *
73          * @access public
74          * @return void
75          */
76
77         function __destruct()
78         {
79                 if($this->_db !== NULL) {
80                         dba_close($this->_db);
81                         $this->connected = FALSE;
82                 }
83         }
84
85         /**
86          * Connect to the database and do some checks.
87          *
88          * @access public
89          * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
90          */
91
92         public function connect()
93         {
94
95                 # Have we already connected?
96                 if($this->_db !== NULL)
97                         return TRUE;
98
99                 # Open the database connection
100                 $this->_db = dba_open(dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . $this->config['database'], "w", $this->config['handler']);
101
102                 if($this->_db === FALSE) {
103                         $this->connected = FALSE;
104                         $this->_db = NULL;
105                         return self::DATABASE_CONNECTION_FAIL;
106                 }
107
108                 # Everything is okay and connected
109
110                 $this->connected = TRUE;
111
112                 # Let's see if this is a b8 database and the version is okay
113                 return $this->check_database();
114
115         }
116
117         /**
118          * Does the actual interaction with the database when fetching data.
119          *
120          * @access protected
121          * @param array $tokens
122          * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
123          */
124
125         protected function _get_query($tokens)
126         {
127
128                 $data = array();
129
130                 foreach ($tokens as $token) {
131
132                         $count = dba_fetch($token, $this->_db);
133
134                         if($count !== FALSE)
135                                 $data[$token] = $count;
136
137                 }
138
139                 return $data;
140
141         }
142
143         /**
144          * Store a token to the database.
145          *
146          * @access protected
147          * @param string $token
148          * @param string $count
149          * @return bool TRUE on success or FALSE on failure
150          */
151
152         protected function _put($token, $count) {
153                 return dba_insert($token, $count, $this->_db);
154         }
155
156         /**
157          * Update an existing token.
158          *
159          * @access protected
160          * @param string $token
161          * @param string $count
162          * @return bool TRUE on success or FALSE on failure
163          */
164
165         protected function _update($token, $count)
166         {
167                 return dba_replace($token, $count, $this->_db);
168         }
169
170         /**
171          * Remove a token from the database.
172          *
173          * @access protected
174          * @param string $token
175          * @return bool TRUE on success or FALSE on failure
176          */
177
178         protected function _del($token)
179         {
180                 return dba_delete($token, $this->_db);
181         }
182
183         /**
184          * Does nothing :-D
185          *
186          * @access protected
187          * @return void
188          */
189
190         protected function _commit()
191         {
192                 # We just need this function because the (My)SQL backend(s) need it.
193                 return;
194         }
195
196 }
197
198 ?>