]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivitySpam/classes/spam_score.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / ActivitySpam / classes / spam_score.php
1 <?php
2   /**
3    * StatusNet - the distributed open-source microblogging tool
4    * Copyright (C) 2011, StatusNet, Inc.
5    *
6    * Score of a notice by activity spam service
7    * 
8    * PHP version 5
9    *
10    * This program is free software: you can redistribute it and/or modify
11    * it under the terms of the GNU Affero General Public License as published by
12    * the Free Software Foundation, either version 3 of the License, or
13    * (at your option) any later version.
14    *
15    * This program is distributed in the hope that it will be useful,
16    * but WITHOUT ANY WARRANTY; without even the implied warranty of
17    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    * GNU Affero General Public License for more details.
19    *
20    * You should have received a copy of the GNU Affero General Public License
21    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22    *
23    * @category  Spam
24    * @package   StatusNet
25    * @author    Evan Prodromou <evan@status.net>
26    * @copyright 2011 StatusNet, Inc.
27    * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28    * @link      http://status.net/
29    */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Score of a notice per the activity spam service
37  *
38  * @category Spam
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
42  * @link     http://status.net/
43  *
44  * @see      DB_DataObject
45  */
46
47 class Spam_score extends Managed_DataObject
48 {
49     const MAX_SCALE = 10000;
50     public $__table = 'spam_score'; // table name
51
52     public $notice_id;   // int
53     public $score;       // float
54     public $created;     // datetime
55
56     function saveNew(Notice $notice, $result) {
57
58         $score = new Spam_score();
59
60         $score->notice_id      = $notice->id;
61         $score->score          = $result->probability;
62         $score->is_spam        = $result->isSpam;
63         $score->scaled         = Spam_score::scale($score->score);
64         $score->created        = common_sql_now();
65         $score->notice_created = $notice->created;
66
67         $score->insert();
68
69         self::blow('spam_score:notice_ids');
70
71         return $score;
72     }
73
74     function save($notice, $result) {
75
76         $orig  = null;
77         $score = Spam_score::getKV('notice_id', $notice->id);
78
79         if (empty($score)) {
80             $score = new Spam_score();
81         } else {
82             $orig = clone($score);
83         }
84
85         $score->notice_id      = $notice->id;
86         $score->score          = $result->probability;
87         $score->is_spam        = $result->isSpam;
88         $score->scaled         = Spam_score::scale($score->score);
89         $score->created        = common_sql_now();
90         $score->notice_created = $notice->created;
91
92         if (empty($orig)) {
93             $score->insert();
94         } else {
95             $score->update($orig);
96         }
97         
98         self::blow('spam_score:notice_ids');
99
100         return $score;
101     }
102
103     function delete($useWhere=false)
104     {
105         self::blow('spam_score:notice_ids');
106         self::blow('spam_score:notice_ids;last');
107         return parent::delete($useWhere);
108     }
109
110     /**
111      * The One True Thingy that must be defined and declared.
112      */
113     public static function schemaDef()
114     {
115         return array(
116             'description' => 'score of the notice per activityspam',
117             'fields' => array(
118                 'notice_id' => array('type' => 'int',
119                                      'not null' => true,
120                                      'description' => 'notice getting scored'),
121                 'score' => array('type' => 'double',
122                                  'not null' => true,
123                                  'description' => 'score for the notice (0.0, 1.0)'),
124                 'scaled' => array('type' => 'int',
125                                   'description' => 'scaled score for the notice (0, 10000)'),
126                 'is_spam' => array('type' => 'tinyint',
127                                    'description' => 'flag for spamosity'),
128                 'created' => array('type' => 'datetime',
129                                    'not null' => true,
130                                    'description' => 'date this record was created'),
131                 'notice_created' => array('type' => 'datetime',
132                                           'description' => 'date the notice was created'),
133             ),
134             'primary key' => array('notice_id'),
135             'foreign keys' => array(
136                 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
137             ),
138             'indexes' => array(
139                 'spam_score_created_idx' => array('created'),
140                 'spam_score_scaled_idx' => array('scaled'),
141             ),
142         );
143     }
144
145     public static function upgrade()
146     {
147         Spam_score::upgradeScaled();
148         Spam_score::upgradeIsSpam();
149         Spam_score::upgradeNoticeCreated();
150     }
151
152     protected static function upgradeScaled()
153     {
154         $score = new Spam_score();
155         $score->whereAdd('scaled IS NULL');
156         
157         if ($score->find()) {
158             while ($score->fetch()) {
159                 $orig = clone($score);
160                 $score->scaled = Spam_score::scale($score->score);
161                 $score->update($orig);
162             }
163         }
164     }
165
166     protected static function upgradeIsSpam()
167     {
168         $score = new Spam_score();
169         $score->whereAdd('is_spam IS NULL');
170         
171         if ($score->find()) {
172             while ($score->fetch()) {
173                 $orig = clone($score);
174                 $score->is_spam = ($score->score >= 0.90) ? 1 : 0;
175                 $score->update($orig);
176             }
177         }
178     }
179
180     protected static function upgradeNoticeCreated()
181     {
182         $score = new Spam_score();
183         $score->whereAdd('notice_created IS NULL');
184         
185         if ($score->find()) {
186             while ($score->fetch()) {
187                 $notice = Notice::getKV('id', $score->notice_id);
188                 if (!empty($notice)) {
189                     $orig = clone($score);
190                     $score->notice_created = $notice->created;
191                     $score->update($orig);
192                 }
193             }
194         }
195     }
196
197     public static function scale($score)
198     {
199         $raw = round($score * Spam_score::MAX_SCALE);
200         return max(0, min(Spam_score::MAX_SCALE, $raw));
201     }
202 }