spam_score.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. function saveNew($notice, $result) {
  52. $score = new Spam_score();
  53. $score->notice_id = $notice->id;
  54. $score->score = $result->probability;
  55. $score->is_spam = $result->isSpam;
  56. $score->scaled = Spam_score::scale($score->score);
  57. $score->created = common_sql_now();
  58. $score->notice_created = $notice->created;
  59. $score->insert();
  60. self::blow('spam_score:notice_ids');
  61. return $score;
  62. }
  63. function save($notice, $result) {
  64. $orig = null;
  65. $score = Spam_score::getKV('notice_id', $notice->id);
  66. if (empty($score)) {
  67. $score = new Spam_score();
  68. } else {
  69. $orig = clone($score);
  70. }
  71. $score->notice_id = $notice->id;
  72. $score->score = $result->probability;
  73. $score->is_spam = $result->isSpam;
  74. $score->scaled = Spam_score::scale($score->score);
  75. $score->created = common_sql_now();
  76. $score->notice_created = $notice->created;
  77. if (empty($orig)) {
  78. $score->insert();
  79. } else {
  80. $score->update($orig);
  81. }
  82. self::blow('spam_score:notice_ids');
  83. return $score;
  84. }
  85. function delete($useWhere=false)
  86. {
  87. self::blow('spam_score:notice_ids');
  88. self::blow('spam_score:notice_ids;last');
  89. return parent::delete($useWhere);
  90. }
  91. /**
  92. * The One True Thingy that must be defined and declared.
  93. */
  94. public static function schemaDef()
  95. {
  96. return array(
  97. 'description' => 'score of the notice per activityspam',
  98. 'fields' => array(
  99. 'notice_id' => array('type' => 'int',
  100. 'not null' => true,
  101. 'description' => 'notice getting scored'),
  102. 'score' => array('type' => 'double',
  103. 'not null' => true,
  104. 'description' => 'score for the notice (0.0, 1.0)'),
  105. 'scaled' => array('type' => 'int',
  106. 'description' => 'scaled score for the notice (0, 10000)'),
  107. 'is_spam' => array('type' => 'tinyint',
  108. 'description' => 'flag for spamosity'),
  109. 'created' => array('type' => 'datetime',
  110. 'not null' => true,
  111. 'description' => 'date this record was created'),
  112. 'notice_created' => array('type' => 'datetime',
  113. 'description' => 'date the notice was created'),
  114. ),
  115. 'primary key' => array('notice_id'),
  116. 'foreign keys' => array(
  117. 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  118. ),
  119. 'indexes' => array(
  120. 'spam_score_created_idx' => array('created'),
  121. 'spam_score_scaled_idx' => array('scaled'),
  122. ),
  123. );
  124. }
  125. public static function upgrade()
  126. {
  127. Spam_score::upgradeScaled();
  128. Spam_score::upgradeIsSpam();
  129. Spam_score::upgradeNoticeCreated();
  130. }
  131. protected static function upgradeScaled()
  132. {
  133. $score = new Spam_score();
  134. $score->whereAdd('scaled IS NULL');
  135. if ($score->find()) {
  136. while ($score->fetch()) {
  137. $orig = clone($score);
  138. $score->scaled = Spam_score::scale($score->score);
  139. $score->update($orig);
  140. }
  141. }
  142. }
  143. protected static function upgradeIsSpam()
  144. {
  145. $score = new Spam_score();
  146. $score->whereAdd('is_spam IS NULL');
  147. if ($score->find()) {
  148. while ($score->fetch()) {
  149. $orig = clone($score);
  150. $score->is_spam = ($score->score >= 0.90) ? 1 : 0;
  151. $score->update($orig);
  152. }
  153. }
  154. }
  155. protected static function upgradeNoticeCreated()
  156. {
  157. $score = new Spam_score();
  158. $score->whereAdd('notice_created IS NULL');
  159. if ($score->find()) {
  160. while ($score->fetch()) {
  161. $notice = Notice::getKV('id', $score->notice_id);
  162. if (!empty($notice)) {
  163. $orig = clone($score);
  164. $score->notice_created = $notice->created;
  165. $score->update($orig);
  166. }
  167. }
  168. }
  169. }
  170. public static function scale($score)
  171. {
  172. $raw = round($score * Spam_score::MAX_SCALE);
  173. return max(0, min(Spam_score::MAX_SCALE, $raw));
  174. }
  175. }