3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Score of a notice by activity spam service
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
36 * Score of a notice per the activity spam service
40 * @author Evan Prodromou <evan@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
42 * @link http://status.net/
47 class Spam_score extends Managed_DataObject
49 const MAX_SCALE = 10000;
50 public $__table = 'spam_score'; // table name
52 public $notice_id; // int
53 public $score; // float
54 public $created; // datetime
57 * Get an instance by key
59 * @param string $k Key to use to lookup (usually 'notice_id' for this class)
60 * @param mixed $v Value to lookup
62 * @return Spam_score object found, or null for no hits
65 function staticGet($k, $v=null)
67 return Managed_DataObject::staticGet('Spam_score', $k, $v);
70 function saveNew($notice, $result) {
72 $score = new Spam_score();
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;
83 self::blow('spam_score:notice_ids');
88 function save($notice, $result) {
91 $score = Spam_score::staticGet('notice_id', $notice->id);
94 $score = new Spam_score();
96 $orig = clone($score);
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;
109 $score->update($orig);
112 self::blow('spam_score:notice_ids');
119 self::blow('spam_score:notice_ids');
120 self::blow('spam_score:notice_ids;last');
125 * The One True Thingy that must be defined and declared.
127 public static function schemaDef()
130 'description' => 'score of the notice per activityspam',
132 'notice_id' => array('type' => 'int',
134 'description' => 'notice getting scored'),
135 'score' => array('type' => 'double',
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',
144 'description' => 'date this record was created'),
145 'notice_created' => array('type' => 'datetime',
146 'description' => 'date the notice was created'),
148 'primary key' => array('notice_id'),
149 'foreign keys' => array(
150 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
153 'spam_score_created_idx' => array('created'),
154 'spam_score_scaled_idx' => array('scaled'),
159 public static function upgrade()
161 Spam_score::upgradeScaled();
162 Spam_score::upgradeIsSpam();
163 Spam_score::upgradeNoticeCreated();
166 protected static function upgradeScaled()
168 $score = new Spam_score();
169 $score->whereAdd('scaled IS NULL');
171 if ($score->find()) {
172 while ($score->fetch()) {
173 $orig = clone($score);
174 $score->scaled = Spam_score::scale($score->score);
175 $score->update($orig);
180 protected static function upgradeIsSpam()
182 $score = new Spam_score();
183 $score->whereAdd('is_spam IS NULL');
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);
194 protected static function upgradeNoticeCreated()
196 $score = new Spam_score();
197 $score->whereAdd('notice_created IS NULL');
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);
211 public static function scale($score)
213 $raw = round($score * Spam_score::MAX_SCALE);
214 return max(0, min(Spam_score::MAX_SCALE, $raw));