spam_score.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Score of a notice by activity spam service
  7. *
  8. * PHP version 5
  9. *
  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.
  14. *
  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.
  19. *
  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/>.
  22. *
  23. * @category Spam
  24. * @package StatusNet
  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/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Score of a notice per the activity spam service
  35. *
  36. * @category Spam
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  40. * @link http://status.net/
  41. *
  42. * @see DB_DataObject
  43. */
  44. class Spam_score extends Managed_DataObject
  45. {
  46. const MAX_SCALE = 10000;
  47. public $__table = 'spam_score'; // table name
  48. public $notice_id; // int
  49. public $score; // float
  50. public $created; // datetime
  51. public static function save($notice, $result)
  52. {
  53. $orig = null;
  54. $score = Spam_score::getKV('notice_id', $notice->id);
  55. if (empty($score)) {
  56. $score = new Spam_score();
  57. } else {
  58. $orig = clone($score);
  59. }
  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;
  66. if (empty($orig)) {
  67. $score->insert();
  68. } else {
  69. $score->update($orig);
  70. }
  71. self::blow('spam_score:notice_ids');
  72. return $score;
  73. }
  74. /**
  75. * The One True Thingy that must be defined and declared.
  76. */
  77. public static function schemaDef()
  78. {
  79. return array(
  80. 'description' => 'score of the notice per activityspam',
  81. 'fields' => array(
  82. 'notice_id' => array('type' => 'int',
  83. 'not null' => true,
  84. 'description' => 'notice getting scored'),
  85. 'score' => array('type' => 'double',
  86. 'not null' => true,
  87. 'description' => 'score for the notice (0.0, 1.0)'),
  88. 'scaled' => array('type' => 'int',
  89. 'description' => 'scaled score for the notice (0, 10000)'),
  90. 'is_spam' => array('type' => 'tinyint',
  91. 'description' => 'flag for spamosity'),
  92. 'created' => array('type' => 'datetime',
  93. 'not null' => true,
  94. 'description' => 'date this record was created'),
  95. 'notice_created' => array('type' => 'datetime',
  96. 'description' => 'date the notice was created'),
  97. ),
  98. 'primary key' => array('notice_id'),
  99. 'foreign keys' => array(
  100. 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  101. ),
  102. 'indexes' => array(
  103. 'spam_score_created_idx' => array('created'),
  104. 'spam_score_scaled_idx' => array('scaled'),
  105. ),
  106. );
  107. }
  108. public static function upgrade()
  109. {
  110. Spam_score::upgradeScaled();
  111. Spam_score::upgradeIsSpam();
  112. Spam_score::upgradeNoticeCreated();
  113. }
  114. protected static function upgradeScaled()
  115. {
  116. $score = new Spam_score();
  117. $score->whereAdd('scaled IS NULL');
  118. if ($score->find()) {
  119. while ($score->fetch()) {
  120. $orig = clone($score);
  121. $score->scaled = Spam_score::scale($score->score);
  122. $score->update($orig);
  123. }
  124. }
  125. }
  126. protected static function upgradeIsSpam()
  127. {
  128. $score = new Spam_score();
  129. $score->whereAdd('is_spam IS NULL');
  130. if ($score->find()) {
  131. while ($score->fetch()) {
  132. $orig = clone($score);
  133. $score->is_spam = ($score->score >= 0.90) ? 1 : 0;
  134. $score->update($orig);
  135. }
  136. }
  137. }
  138. protected static function upgradeNoticeCreated()
  139. {
  140. $score = new Spam_score();
  141. $score->whereAdd('notice_created IS NULL');
  142. if ($score->find()) {
  143. while ($score->fetch()) {
  144. $notice = Notice::getKV('id', $score->notice_id);
  145. if (!empty($notice)) {
  146. $orig = clone($score);
  147. $score->notice_created = $notice->created;
  148. $score->update($orig);
  149. }
  150. }
  151. }
  152. }
  153. function saveNew($notice, $result)
  154. {
  155. $score = new Spam_score();
  156. $score->notice_id = $notice->id;
  157. $score->score = $result->probability;
  158. $score->is_spam = $result->isSpam;
  159. $score->scaled = Spam_score::scale($score->score);
  160. $score->created = common_sql_now();
  161. $score->notice_created = $notice->created;
  162. $score->insert();
  163. self::blow('spam_score:notice_ids');
  164. return $score;
  165. }
  166. public static function scale($score)
  167. {
  168. $raw = round($score * Spam_score::MAX_SCALE);
  169. return max(0, min(Spam_score::MAX_SCALE, $raw));
  170. }
  171. public function delete($useWhere = false)
  172. {
  173. self::blow('spam_score:notice_ids');
  174. self::blow('spam_score:notice_ids;last');
  175. return parent::delete($useWhere);
  176. }
  177. }