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
56 function saveNew($notice, $result) {
58 $score = new Spam_score();
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;
69 self::blow('spam_score:notice_ids');
74 function save($notice, $result) {
77 $score = Spam_score::getKV('notice_id', $notice->id);
80 $score = new Spam_score();
82 $orig = clone($score);
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;
95 $score->update($orig);
98 self::blow('spam_score:notice_ids');
105 self::blow('spam_score:notice_ids');
106 self::blow('spam_score:notice_ids;last');
111 * The One True Thingy that must be defined and declared.
113 public static function schemaDef()
116 'description' => 'score of the notice per activityspam',
118 'notice_id' => array('type' => 'int',
120 'description' => 'notice getting scored'),
121 'score' => array('type' => 'double',
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',
130 'description' => 'date this record was created'),
131 'notice_created' => array('type' => 'datetime',
132 'description' => 'date the notice was created'),
134 'primary key' => array('notice_id'),
135 'foreign keys' => array(
136 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
139 'spam_score_created_idx' => array('created'),
140 'spam_score_scaled_idx' => array('scaled'),
145 public static function upgrade()
147 Spam_score::upgradeScaled();
148 Spam_score::upgradeIsSpam();
149 Spam_score::upgradeNoticeCreated();
152 protected static function upgradeScaled()
154 $score = new Spam_score();
155 $score->whereAdd('scaled IS NULL');
157 if ($score->find()) {
158 while ($score->fetch()) {
159 $orig = clone($score);
160 $score->scaled = Spam_score::scale($score->score);
161 $score->update($orig);
166 protected static function upgradeIsSpam()
168 $score = new Spam_score();
169 $score->whereAdd('is_spam IS NULL');
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);
180 protected static function upgradeNoticeCreated()
182 $score = new Spam_score();
183 $score->whereAdd('notice_created IS NULL');
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);
197 public static function scale($score)
199 $raw = round($score * Spam_score::MAX_SCALE);
200 return max(0, min(Spam_score::MAX_SCALE, $raw));