3 # Copyright (C) 2006-2011 Tobias Leupold <tobias.leupold@web.de>
5 # This file is part of the b8 package
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.
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.
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.
21 * The MySQL abstraction layer for communicating with the database.
22 * Copyright (C) 2009 Oliver Lillie (aka buggedcom)
23 * Copyright (C) 2010-2011 Tobias Leupold <tobias.leupold@web.de>
28 * @author Oliver Lillie (aka buggedcom) (original PHP 5 port and optimizations)
29 * @author Tobias Leupold
32 class b8_storage_frndc extends b8_storage_base
35 public $config = array(
36 'database' => 'b8_wordlist',
37 'table_name' => 'b8_wordlist',
38 'host' => 'localhost',
44 public $b8_config = array(
45 'degenerator' => NULL,
49 private $_connection = NULL;
50 private $_deletes = array();
51 private $_puts = array();
52 private $_updates = array();
55 const DATABASE_CONNECTION_FAIL = 'DATABASE_CONNECTION_FAIL';
56 const DATABASE_CONNECTION_ERROR = 'DATABASE_CONNECTION_ERROR';
57 const DATABASE_CONNECTION_BAD_RESOURCE = 'DATABASE_CONNECTION_BAD_RESOURCE';
58 const DATABASE_SELECT_ERROR = 'DATABASE_SELECT_ERROR';
59 const DATABASE_TABLE_ACCESS_FAIL = 'DATABASE_TABLE_ACCESS_FAIL';
60 const DATABASE_WRONG_VERSION = 'DATABASE_WRONG_VERSION';
63 * Constructs the database layer.
66 * @param string $config
69 function __construct($config, $degenerator, $today)
72 # Pass some variables of the main b8 config to this class
73 $this->b8_config['degenerator'] = $degenerator;
74 $this->b8_config['today'] = $today;
76 # Validate the config items
78 if(count($config) > 0) {
80 foreach ($config as $name => $value) {
89 $this->config[$name] = (string) $value;
96 if(is_resource($value) === TRUE) {
97 $resource_type = get_resource_type($value);
98 $this->config['connection'] = $resource_type !== 'mysql link' && $resource_type !== 'mysql link persistent' ? FALSE : $value;
102 $this->config['connection'] = FALSE;
117 * Closes the database connection.
123 function __destruct()
126 if($this->_connection === NULL)
129 # Commit any changes before closing
132 # Just close the connection if no link-resource was passed and b8 created it's own connection
133 if($this->config['connection'] === NULL)
134 mysql_close($this->_connection);
136 $this->connected = FALSE;
141 * Connect to the database and do some checks.
144 * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8.
147 public function connect()
150 $this->connected = TRUE;
156 * Does the actual interaction with the database when fetching data.
159 * @param array $tokens
160 * @return mixed Returns an array of the returned data in the format array(token => data) or an empty array if there was no data.
163 protected function _get_query($tokens, $uid)
166 # Construct the query ...
168 if(count($tokens) > 0) {
172 foreach ($tokens as $token) {
173 $token = dbesc($token);
174 array_push($where, $token);
177 $where = 'term IN ("' . implode('", "', $where) . '")';
181 $token = dbesc($token);
182 $where = 'term = "' . $token . '"';
185 # ... and fetch the data
188 SELECT * FROM spam WHERE ' . $where . ' AND uid = ' . $uid );
191 $returned_tokens = array();
193 foreach($result as $rr)
194 $returned_tokens[] = $rr['term'];
196 $to_create = array();
198 if(count($tokens) > 0) {
199 foreach($tokens as $token)
200 if(! in_array($token,$returned_tokens))
201 $to_create[] = str_tolower($token);
203 if(count($to_create)) {
205 foreach($to_create as $term) {
208 $sql .= sprintf("(term,datetime,uid) values('%s','%s',%d)",
209 dbesc(str_tolower($term))
210 dbesc(datetime_convert()),
213 q("insert into spam " . $sql);
221 * Store a token to the database.
224 * @param string $token
225 * @param string $count
229 protected function _put($token, $count, $uid) {
230 $token = dbesc($token);
231 $count = dbesc($count);
233 array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
237 * Update an existing token.
240 * @param string $token
241 * @param string $count
245 protected function _update($token, $count, $uid)
247 $token = dbesc($token);
248 $count = dbesc($count);
250 array_push($this->_puts, '("' . $token . '", "' . $count . '", "' . $uid .'")');
254 * Remove a token from the database.
257 * @param string $token
261 protected function _del($token, $uid)
263 $token = dbesc($token);
266 array_push($this->_deletes, $token);
270 * Commits any modification queries.
276 protected function _commit($uid)
279 if(count($this->_deletes) > 0) {
282 DELETE FROM ' . $this->config['table_name'] . '
283 WHERE token IN ("' . implode('", "', $this->_deletes) . '") AND uid = ' . $this->uid);
285 $this->_deletes = array();
289 if(count($this->_puts) > 0) {
292 INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
293 VALUES ' . implode(', ', $this->_puts));
295 $this->_puts = array();
299 if(count($this->_updates) > 0) {
301 // this still needs work
302 $result = q("select * from " . $this->config['table_name'] . ' where token = ');
306 INSERT INTO ' . $this->config['table_name'] . '(token, count, uid)
307 VALUES ' . implode(', ', $this->_updates) . ', ' . $uid . '
308 ON DUPLICATE KEY UPDATE ' . $this->config['table_name'] . '.count = VALUES(count);', $this->_connection);
310 $this->_updates = array();