]> git.mxchange.org Git - friendica.git/blob - library/spam/b8/storage/storage_base.php.ORIG
Merge remote branch 'upstream/master'
[friendica.git] / library / spam / b8 / storage / storage_base.php.ORIG
1 <?php
2
3 #   Copyright (C) 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  * Functions used by all storage backends
22  * Copyright (C) 2010 Tobias Leupold <tobias.leupold@web.de>
23  *
24  * @license LGPL
25  * @access public
26  * @package b8
27  * @author Tobias Leupold
28  */
29
30 abstract class b8_storage_base
31 {
32
33         public $connected            = FALSE;
34
35         protected $_degenerator      = NULL;
36
37         const INTERNALS_TEXTS_HAM    = 'bayes*texts.ham';
38         const INTERNALS_TEXTS_SPAM   = 'bayes*texts.spam';
39         const INTERNALS_DBVERSION    = 'bayes*dbversion';
40
41         const BACKEND_NOT_CONNECTED  = 'BACKEND_NOT_CONNECTED';
42         const DATABASE_WRONG_VERSION = 'DATABASE_WRONG_VERSION';
43         const DATABASE_NOT_B8        = 'DATABASE_NOT_B8';
44
45         /**
46          * Validates the class has all it needs to work.
47          *
48          * @access protected
49          * @return mixed Returns TRUE if everything is okay, otherwise an error code.
50          */
51
52         protected function validate()
53         {
54
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();
59
60                 if($this->connected !== TRUE)
61                         return self::BACKEND_NOT_CONNECTED;
62
63                 return TRUE;
64
65         }
66
67         /**
68          * Checks if a b8 database is used and if it's version is okay
69          *
70          * @access protected
71          * @return mixed Returns TRUE if everything is okay, otherwise an error code.
72          */
73
74         protected function check_database()
75         {
76
77                 $internals = $this->get_internals();
78
79                 if(isset($internals['dbversion'])) {
80                         if($internals['dbversion'] == "2") {
81                                 return TRUE;
82                         }
83                         else {
84                                 $this->connected = FALSE;
85                                 return self::DATABASE_WRONG_VERSION;
86                         }
87                 }
88                 else {
89                         $this->connected = FALSE;
90                         return self::DATABASE_NOT_B8;
91                 }
92
93         }
94
95         /**
96          * Parses the "count" data of a token.
97          *
98          * @access private
99          * @param string $data
100          * @return array Returns an array of the parsed data: array(count_ham, count_spam, lastseen).
101          */
102
103         private function _parse_count($data)
104         {
105
106                 list($count_ham, $count_spam, $lastseen) = explode(' ', $data);
107
108                 $count_ham  = (int) $count_ham;
109                 $count_spam = (int) $count_spam;
110
111                 return array(
112                         'count_ham'  => $count_ham,
113                         'count_spam' => $count_spam
114                 );
115
116         }
117
118         /**
119          * Get the database's internal variables.
120          *
121          * @access public
122          * @return array Returns an array of all internals.
123          */
124
125         public function get_internals()
126         {
127
128                 $internals = $this->_get_query(
129                         array(
130                                 self::INTERNALS_TEXTS_HAM,
131                                 self::INTERNALS_TEXTS_SPAM,
132                                 self::INTERNALS_DBVERSION
133                         )
134                 );
135
136                 return array(
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]
140                 );
141
142         }
143
144         /**
145          * Get all data about a list of tags from the database.
146          *
147          * @access public
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))).
150          */
151
152         public function get($tokens)
153         {
154
155                 # Validate the startup
156
157                 $started_up = $this->validate();
158
159                 if($started_up !== TRUE)
160                         return $started_up;
161
162                 # First we see what we have in the database.
163                 $token_data = $this->_get_query($tokens);
164
165                 # Check if we have to degenerate some tokens
166
167                 $missing_tokens = array();
168
169                 foreach($tokens as $token) {
170                         if(!isset($token_data[$token]))
171                                 $missing_tokens[] = $token;
172                 }
173
174                 if(count($missing_tokens) > 0) {
175
176                         # We have to degenerate some tokens
177                         $degenerates_list = array();
178
179                         # Generate a list of degenerated tokens for the missing tokens ...
180                         $degenerates = $this->_degenerator->degenerate($missing_tokens);
181
182                         # ... and look them up
183
184                         foreach($degenerates as $token => $token_degenerates)
185                                 $degenerates_list = array_merge($degenerates_list, $token_degenerates);
186
187                         $token_data = array_merge($token_data, $this->_get_query($degenerates_list));
188
189                 }
190
191                 # Here, we have all availible data in $token_data.
192
193                 $return_data_tokens = array();
194                 $return_data_degenerates = array();
195
196                 foreach($tokens as $token) {
197
198                         if(isset($token_data[$token]) === TRUE) {
199
200                                 # The token was found in the database
201
202                                 # Add the data ...
203                                 $return_data_tokens[$token] = $this->_parse_count($token_data[$token]);
204
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']);
207
208                         }
209
210                         else {
211
212                                 # The token was not found, so we look if we
213                                 # can return data for degenerated tokens
214
215                                 # Check all degenerated forms of the token
216
217                                 foreach($this->_degenerator->degenerates[$token] as $degenerate) {
218
219                                         if(isset($token_data[$degenerate]) === TRUE) {
220
221                                                 # A degeneration of the token way found in the database
222
223                                                 # Add the data ...
224                                                 $return_data_degenerates[$token][$degenerate] = $this->_parse_count($token_data[$degenerate]);
225
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']);
228
229                                         }
230
231                                 }
232
233                         }
234
235                 }
236
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
239
240                 # First, we commit the changes to the lastseen parameters
241                 $this->_commit();
242
243                 # Then, we return what we have
244                 return array(
245                         'tokens'      => $return_data_tokens,
246                         'degenerates' => $return_data_degenerates
247                 );
248
249         }
250
251         /**
252          * Stores or deletes a list of tokens from the given category.
253          *
254          * @access public
255          * @param array $tokens
256          * @param const $category Either b8::HAM or b8::SPAM
257          * @param const $action Either b8::LEARN or b8::UNLEARN
258          * @return void
259          */
260
261         public function process_text($tokens, $category, $action)
262         {
263
264                 # Validate the startup
265
266                 $started_up = $this->validate();
267
268                 if($started_up !== TRUE)
269                         return $started_up;
270
271                 # No matter what we do, we first have to check what data we have.
272
273                 # First get the internals, including the ham texts and spam texts counter
274                 $internals = $this->get_internals();
275
276                 # Then, fetch all data for all tokens we have (and update their lastseen parameters)
277                 $token_data = $this->_get_query(array_keys($tokens));
278
279                 # Process all tokens to learn/unlearn
280
281                 foreach($tokens as $token => $count) {
282
283                         if(isset($token_data[$token])) {
284
285                                 # We already have this token, so update it's data
286
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;
291
292                                 # Increase or decrease the right counter
293
294                                 if($action === b8::LEARN) {
295                                         if($category === b8::HAM)
296                                                 $count_ham += $count;
297                                         elseif($category === b8::SPAM)
298                                                 $count_spam += $count;
299                                 }
300
301                                 elseif($action == b8::UNLEARN) {
302                                         if($category === b8::HAM)
303                                                 $count_ham -= $count;
304                                         elseif($category === b8::SPAM)
305                                                 $count_spam -= $count;
306                                 }
307
308                                 # We don't want to have negative values
309
310                                 if($count_ham < 0)
311                                         $count_ham = 0;
312
313                                 if($count_spam < 0)
314                                         $count_spam = 0;
315
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']);
319                                 else
320                                         $this->_del($token);
321
322                         }
323
324                         else {
325
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
328
329                                 if($action === b8::LEARN) {
330
331                                         if($category === b8::HAM)
332                                                 $data = '1 0 ';
333                                         elseif($category === b8::SPAM)
334                                                 $data = '0 1 ';
335
336                                         $data .= $this->b8_config['today'];
337
338                                         $this->_put($token, $data);
339
340                                 }
341
342                         }
343
344                 }
345
346                 # Now, all token have been processed, so let's update the right text
347
348                 if($action === b8::LEARN) {
349
350                         if($category === b8::HAM) {
351                                 $internals['texts_ham']++;
352                                 $this->_update(self::INTERNALS_TEXTS_HAM, $internals['texts_ham']);
353                         }
354
355                         elseif($category === b8::SPAM) {
356                                 $internals['texts_spam']++;
357                                 $this->_update(self::INTERNALS_TEXTS_SPAM, $internals['texts_spam']);
358                         }
359
360                 }
361
362                 elseif($action == b8::UNLEARN) {
363
364                         if($category === b8::HAM) {
365
366                                 $internals['texts_ham']--;
367
368                                 if($internals['texts_ham'] < 0)
369                                         $internals['texts_ham'] = 0;
370
371                                 $this->_update(self::INTERNALS_TEXTS_HAM, $internals['texts_ham']);
372
373                         }
374
375                         elseif($category === b8::SPAM) {
376
377                                 $internals['texts_spam']--;
378
379                                 if($internals['texts_spam'] < 0)
380                                         $internals['texts_spam'] = 0;
381
382                                 $this->_update(self::INTERNALS_TEXTS_SPAM, $internals['texts_spam']);
383
384                         }
385
386                 }
387
388                 # We're done and can commit all changes to the database now
389                 $this->_commit();
390
391         }
392
393 }
394
395 ?>