]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivitySpam/Spam_score.php
Merge ActivitySpam plugin
[quix0rs-gnu-social.git] / plugins / ActivitySpam / 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     /**
57      * Get an instance by key
58      *
59      * @param string $k Key to use to lookup (usually 'notice_id' for this class)
60      * @param mixed  $v Value to lookup
61      *
62      * @return Spam_score object found, or null for no hits
63      *
64      */
65     function staticGet($k, $v=null)
66     {
67         return Managed_DataObject::staticGet('Spam_score', $k, $v);
68     }
69
70     function saveNew($notice, $result) {
71
72         $score = new Spam_score();
73
74         $score->notice_id      = $notice->id;
75         $score->score          = $result->probability;
76         $score->is_spam        = $result->isSpam;
77         $score->scaled         = Spam_score::scale($score->score);
78         $score->created        = common_sql_now();
79         $score->notice_created = $notice->created;
80
81         $score->insert();
82
83         self::blow('spam_score:notice_ids');
84
85         return $score;
86     }
87
88     function save($notice, $result) {
89
90         $orig  = null;
91         $score = Spam_score::staticGet('notice_id', $notice->id);
92
93         if (empty($score)) {
94             $score = new Spam_score();
95         } else {
96             $orig = clone($score);
97         }
98
99         $score->notice_id      = $notice->id;
100         $score->score          = $result->probability;
101         $score->is_spam        = $result->isSpam;
102         $score->scaled         = Spam_score::scale($score->score);
103         $score->created        = common_sql_now();
104         $score->notice_created = $notice->created;
105
106         if (empty($orig)) {
107             $score->insert();
108         } else {
109             $score->update($orig);
110         }
111         
112         self::blow('spam_score:notice_ids');
113
114         return $score;
115     }
116
117     function delete()
118     {
119         self::blow('spam_score:notice_ids');
120         self::blow('spam_score:notice_ids;last');
121         parent::delete();
122     }
123
124     /**
125      * The One True Thingy that must be defined and declared.
126      */
127     public static function schemaDef()
128     {
129         return array(
130             'description' => 'score of the notice per activityspam',
131             'fields' => array(
132                 'notice_id' => array('type' => 'int',
133                                      'not null' => true,
134                                      'description' => 'notice getting scored'),
135                 'score' => array('type' => 'double',
136                                  'not null' => true,
137                                  'description' => 'score for the notice (0.0, 1.0)'),
138                 'scaled' => array('type' => 'int',
139                                   'description' => 'scaled score for the notice (0, 10000)'),
140                 'is_spam' => array('type' => 'tinyint',
141                                    'description' => 'flag for spamosity'),
142                 'created' => array('type' => 'datetime',
143                                    'not null' => true,
144                                    'description' => 'date this record was created'),
145                 'notice_created' => array('type' => 'datetime',
146                                           'description' => 'date the notice was created'),
147             ),
148             'primary key' => array('notice_id'),
149             'foreign keys' => array(
150                 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
151             ),
152             'indexes' => array(
153                 'spam_score_created_idx' => array('created'),
154                 'spam_score_scaled_idx' => array('scaled'),
155             ),
156         );
157     }
158
159     public static function upgrade()
160     {
161         Spam_score::upgradeScaled();
162         Spam_score::upgradeIsSpam();
163         Spam_score::upgradeNoticeCreated();
164     }
165
166     protected static function upgradeScaled()
167     {
168         $score = new Spam_score();
169         $score->whereAdd('scaled IS NULL');
170         
171         if ($score->find()) {
172             while ($score->fetch()) {
173                 $orig = clone($score);
174                 $score->scaled = Spam_score::scale($score->score);
175                 $score->update($orig);
176             }
177         }
178     }
179
180     protected static function upgradeIsSpam()
181     {
182         $score = new Spam_score();
183         $score->whereAdd('is_spam IS NULL');
184         
185         if ($score->find()) {
186             while ($score->fetch()) {
187                 $orig = clone($score);
188                 $score->is_spam = ($score->score >= 0.90) ? 1 : 0;
189                 $score->update($orig);
190             }
191         }
192     }
193
194     protected static function upgradeNoticeCreated()
195     {
196         $score = new Spam_score();
197         $score->whereAdd('notice_created IS NULL');
198         
199         if ($score->find()) {
200             while ($score->fetch()) {
201                 $notice = Notice::staticGet('id', $score->notice_id);
202                 if (!empty($notice)) {
203                     $orig = clone($score);
204                     $score->notice_created = $notice->created;
205                     $score->update($orig);
206                 }
207             }
208         }
209     }
210
211     public static function scale($score)
212     {
213         $raw = round($score * Spam_score::MAX_SCALE);
214         return max(0, min(Spam_score::MAX_SCALE, $raw));
215     }
216 }