$schema = Schema::get();
$schema->ensureTable('spam_score', Spam_score::schemaDef());
+ Spam_score::upgrade();
+
return true;
}
$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;
class Spam_score extends Managed_DataObject
{
- const MAX_SCALED = 1000000;
+ const MAX_SCALED = 10000;
public $__table = 'spam_score'; // table name
public $notice_id; // int
'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',
),
);
}
+
+ 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));
+ }
}