From fb64cb63b6ba54966cc2784c5e5bba0e7483e28e Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 2 Mar 2012 10:37:20 -0600 Subject: [PATCH] upgrade script for spam score --- ActivitySpamPlugin.php | 4 ++- Spam_score.php | 62 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 3 deletions(-) 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)); + } }