3 # Copyright (C) 2010 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 * Functions used by all storage backends
22 * Copyright (C) 2010 Tobias Leupold <tobias.leupold@web.de>
27 * @author Tobias Leupold
30 abstract class b8_storage_base
33 public $connected = FALSE;
35 protected $_degenerator = NULL;
37 const INTERNALS_TEXTS_HAM = 'bayes*texts.ham';
38 const INTERNALS_TEXTS_SPAM = 'bayes*texts.spam';
39 const INTERNALS_DBVERSION = 'bayes*dbversion';
41 const BACKEND_NOT_CONNECTED = 'BACKEND_NOT_CONNECTED';
42 const DATABASE_WRONG_VERSION = 'DATABASE_WRONG_VERSION';
43 const DATABASE_NOT_B8 = 'DATABASE_NOT_B8';
46 * Validates the class has all it needs to work.
49 * @return mixed Returns TRUE if everything is okay, otherwise an error code.
52 protected function validate()
55 # We set up the degenerator here, as we would have to duplicate code if it
56 # was done in the constructor of the respective storage backend.
57 $class = 'b8_degenerator_' . $this->b8_config['degenerator'];
58 $this->_degenerator = new $class();
60 if($this->connected !== TRUE)
61 return self::BACKEND_NOT_CONNECTED;
68 * Checks if a b8 database is used and if it's version is okay
71 * @return mixed Returns TRUE if everything is okay, otherwise an error code.
74 protected function check_database()
77 $internals = $this->get_internals();
79 if(isset($internals['dbversion'])) {
80 if($internals['dbversion'] == "2") {
84 $this->connected = FALSE;
85 return self::DATABASE_WRONG_VERSION;
89 $this->connected = FALSE;
90 return self::DATABASE_NOT_B8;
96 * Parses the "count" data of a token.
100 * @return array Returns an array of the parsed data: array(count_ham, count_spam, lastseen).
103 private function _parse_count($data)
106 list($count_ham, $count_spam, $lastseen) = explode(' ', $data);
108 $count_ham = (int) $count_ham;
109 $count_spam = (int) $count_spam;
112 'count_ham' => $count_ham,
113 'count_spam' => $count_spam
119 * Get the database's internal variables.
122 * @return array Returns an array of all internals.
125 public function get_internals()
128 $internals = $this->_get_query(
130 self::INTERNALS_TEXTS_HAM,
131 self::INTERNALS_TEXTS_SPAM,
132 self::INTERNALS_DBVERSION
137 'texts_ham' => (int) $internals[self::INTERNALS_TEXTS_HAM],
138 'texts_spam' => (int) $internals[self::INTERNALS_TEXTS_SPAM],
139 'dbversion' => (int) $internals[self::INTERNALS_DBVERSION]
145 * Get all data about a list of tags from the database.
148 * @param array $tokens
149 * @return mixed Returns FALSE on failure, otherwise returns array of returned data in the format array('tokens' => array(token => count), 'degenerates' => array(token => array(degenerate => count))).
152 public function get($tokens)
155 # Validate the startup
157 $started_up = $this->validate();
159 if($started_up !== TRUE)
162 # First we see what we have in the database.
163 $token_data = $this->_get_query($tokens);
165 # Check if we have to degenerate some tokens
167 $missing_tokens = array();
169 foreach($tokens as $token) {
170 if(!isset($token_data[$token]))
171 $missing_tokens[] = $token;
174 if(count($missing_tokens) > 0) {
176 # We have to degenerate some tokens
177 $degenerates_list = array();
179 # Generate a list of degenerated tokens for the missing tokens ...
180 $degenerates = $this->_degenerator->degenerate($missing_tokens);
182 # ... and look them up
184 foreach($degenerates as $token => $token_degenerates)
185 $degenerates_list = array_merge($degenerates_list, $token_degenerates);
187 $token_data = array_merge($token_data, $this->_get_query($degenerates_list));
191 # Here, we have all availible data in $token_data.
193 $return_data_tokens = array();
194 $return_data_degenerates = array();
196 foreach($tokens as $token) {
198 if(isset($token_data[$token]) === TRUE) {
200 # The token was found in the database
203 $return_data_tokens[$token] = $this->_parse_count($token_data[$token]);
205 # ... and update it's lastseen parameter
206 $this->_update($token, "{$return_data_tokens[$token]['count_ham']} {$return_data_tokens[$token]['count_spam']} " . $this->b8_config['today']);
212 # The token was not found, so we look if we
213 # can return data for degenerated tokens
215 # Check all degenerated forms of the token
217 foreach($this->_degenerator->degenerates[$token] as $degenerate) {
219 if(isset($token_data[$degenerate]) === TRUE) {
221 # A degeneration of the token way found in the database
224 $return_data_degenerates[$token][$degenerate] = $this->_parse_count($token_data[$degenerate]);
226 # ... and update it's lastseen parameter
227 $this->_update($degenerate, "{$return_data_degenerates[$token][$degenerate]['count_ham']} {$return_data_degenerates[$token][$degenerate]['count_spam']} " . $this->b8_config['today']);
237 # Now, all token data directly found in the database is in $return_data_tokens
238 # and all data for degenerated versions is in $return_data_degenerates
240 # First, we commit the changes to the lastseen parameters
243 # Then, we return what we have
245 'tokens' => $return_data_tokens,
246 'degenerates' => $return_data_degenerates
252 * Stores or deletes a list of tokens from the given category.
255 * @param array $tokens
256 * @param const $category Either b8::HAM or b8::SPAM
257 * @param const $action Either b8::LEARN or b8::UNLEARN
261 public function process_text($tokens, $category, $action)
264 # Validate the startup
266 $started_up = $this->validate();
268 if($started_up !== TRUE)
271 # No matter what we do, we first have to check what data we have.
273 # First get the internals, including the ham texts and spam texts counter
274 $internals = $this->get_internals();
276 # Then, fetch all data for all tokens we have (and update their lastseen parameters)
277 $token_data = $this->_get_query(array_keys($tokens));
279 # Process all tokens to learn/unlearn
281 foreach($tokens as $token => $count) {
283 if(isset($token_data[$token])) {
285 # We already have this token, so update it's data
287 # Get the existing data
288 list($count_ham, $count_spam, $lastseen) = explode(' ', $token_data[$token]);
289 $count_ham = (int) $count_ham;
290 $count_spam = (int) $count_spam;
292 # Increase or decrease the right counter
294 if($action === b8::LEARN) {
295 if($category === b8::HAM)
296 $count_ham += $count;
297 elseif($category === b8::SPAM)
298 $count_spam += $count;
301 elseif($action == b8::UNLEARN) {
302 if($category === b8::HAM)
303 $count_ham -= $count;
304 elseif($category === b8::SPAM)
305 $count_spam -= $count;
308 # We don't want to have negative values
316 # Now let's see if we have to update or delete the token
317 if($count_ham !== 0 or $count_spam !== 0)
318 $this->_update($token, "$count_ham $count_spam " . $this->b8_config['today']);
326 # We don't have the token. If we unlearn a text, we can't delete it
327 # as we don't have it anyway, so just do something if we learn a text
329 if($action === b8::LEARN) {
331 if($category === b8::HAM)
333 elseif($category === b8::SPAM)
336 $data .= $this->b8_config['today'];
338 $this->_put($token, $data);
346 # Now, all token have been processed, so let's update the right text
348 if($action === b8::LEARN) {
350 if($category === b8::HAM) {
351 $internals['texts_ham']++;
352 $this->_update(self::INTERNALS_TEXTS_HAM, $internals['texts_ham']);
355 elseif($category === b8::SPAM) {
356 $internals['texts_spam']++;
357 $this->_update(self::INTERNALS_TEXTS_SPAM, $internals['texts_spam']);
362 elseif($action == b8::UNLEARN) {
364 if($category === b8::HAM) {
366 $internals['texts_ham']--;
368 if($internals['texts_ham'] < 0)
369 $internals['texts_ham'] = 0;
371 $this->_update(self::INTERNALS_TEXTS_HAM, $internals['texts_ham']);
375 elseif($category === b8::SPAM) {
377 $internals['texts_spam']--;
379 if($internals['texts_spam'] < 0)
380 $internals['texts_spam'] = 0;
382 $this->_update(self::INTERNALS_TEXTS_SPAM, $internals['texts_spam']);
388 # We're done and can commit all changes to the database now