]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
upgrade script for spam score
authorEvan Prodromou <evan@status.net>
Fri, 2 Mar 2012 16:37:20 +0000 (10:37 -0600)
committerEvan Prodromou <evan@status.net>
Fri, 2 Mar 2012 16:37:20 +0000 (10:37 -0600)
ActivitySpamPlugin.php
Spam_score.php

index 62cfbda22f23d2886f47d14ae951b1209f97f978..63ac94ef5f17dd71dea7bb402003554527ad3f94 100644 (file)
@@ -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;
 
index 440edb6b7fcfee80106344385165d8fca2dbcb67..32eaba803a7c24a54db00ece4fb44bedc934dbb9 100644 (file)
@@ -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));
+    }
 }