diff --git a/ActivitySpamPlugin.php b/ActivitySpamPlugin.php index 62cfbda22f..63ac94ef5f 100644 --- a/ActivitySpamPlugin.php +++ b/ActivitySpamPlugin.php @@ -81,6 +81,8 @@ class ActivitySpamPlugin extends Plugin $schema = Schema::get(); $schema->ensureTable('spam_score', Spam_score::schemaDef()); + Spam_score::upgrade(); + return true; } @@ -142,7 +144,7 @@ class ActivitySpamPlugin extends Plugin $score->notice_id = $notice->id; $score->score = $result->probability; $score->is_spam = $result->isSpam; - $score->scaled = (int) ($result->probability * Spam_score::MAX_SCALED); + $score->scaled = Spam_score::scale($score->score); $score->created = common_sql_now(); $score->notice_created = $notice->created; diff --git a/Spam_score.php b/Spam_score.php index 440edb6b7f..32eaba803a 100644 --- a/Spam_score.php +++ b/Spam_score.php @@ -46,7 +46,7 @@ if (!defined('STATUSNET')) { class Spam_score extends Managed_DataObject { - const MAX_SCALED = 1000000; + const MAX_SCALED = 10000; public $__table = 'spam_score'; // table name public $notice_id; // int @@ -82,7 +82,7 @@ class Spam_score extends Managed_DataObject 'not null' => true, 'description' => 'score for the notice (0.0, 1.0)'), 'scaled' => array('type' => 'int', - 'description' => 'scaled score for the notice (0, 1000000)'), + 'description' => 'scaled score for the notice (0, 10000)'), 'is_spam' => array('type' => 'tinyint', 'description' => 'flag for spamosity'), 'created' => array('type' => 'datetime', @@ -101,4 +101,62 @@ class Spam_score extends Managed_DataObject ), ); } + + public static function upgrade() + { + Spam_score::upgradeScaled(); + Spam_score::upgradeIsSpam(); + Spam_score::upgradeNoticeCreated(); + } + + protected static function upgradeScaled() + { + $score = new Spam_score(); + $score->whereAdd('scaled IS NULL'); + + if ($score->find()) { + while ($score->fetch()) { + $orig = clone($score); + $score->scaled = Spam_score::scale($score->score); + $score->update($orig); + } + } + } + + protected static function upgradeIsSpam() + { + $score = new Spam_score(); + $score->whereAdd('is_spam IS NULL'); + + if ($score->find()) { + while ($score->fetch()) { + $orig = clone($score); + $score->is_spam = ($score->score >= 0.90) ? 1 : 0; + $score->update($orig); + } + } + } + + protected static function upgradeNoticeCreated() + { + $score = new Spam_score(); + $score->whereAdd('notice_created IS NULL'); + + if ($score->find()) { + while ($score->fetch()) { + $notice = Notice::staticGet('id', $score->notice_id); + if (!empty($notice)) { + $orig = clone($score); + $score->notice_created = $notice->created; + $score->update($orig); + } + } + } + } + + protected static function scale($score) + { + $raw = round($score * Spam_score::MAX_SCALE); + return max(0, min(Spam_score::MAX_SCALE, $raw)); + } }