Spam_score.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Score of a notice by activity spam service
  18. *
  19. * @category Spam
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Score of a notice per the activity spam service
  28. *
  29. * @copyright 2011 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. *
  32. * @see DB_DataObject
  33. */
  34. class Spam_score extends Managed_DataObject
  35. {
  36. const MAX_SCALE = 10000;
  37. public $__table = 'spam_score'; // table name
  38. public $notice_id; // int
  39. public $score; // float
  40. public $created; // datetime
  41. public static function save($notice, $result)
  42. {
  43. $orig = null;
  44. $score = Spam_score::getKV('notice_id', $notice->id);
  45. if (empty($score)) {
  46. $score = new Spam_score();
  47. } else {
  48. $orig = clone($score);
  49. }
  50. $score->notice_id = $notice->id;
  51. $score->score = $result->probability;
  52. $score->is_spam = $result->isSpam;
  53. $score->scaled = Spam_score::scale($score->score);
  54. $score->created = common_sql_now();
  55. $score->notice_created = $notice->created;
  56. if (empty($orig)) {
  57. $score->insert();
  58. } else {
  59. $score->update($orig);
  60. }
  61. self::blow('spam_score:notice_ids');
  62. return $score;
  63. }
  64. /**
  65. * The One True Thingy that must be defined and declared.
  66. */
  67. public static function schemaDef()
  68. {
  69. return array(
  70. 'description' => 'score of the notice per activityspam',
  71. 'fields' => array(
  72. 'notice_id' => array('type' => 'int',
  73. 'not null' => true,
  74. 'description' => 'notice getting scored'),
  75. 'score' => array('type' => 'float',
  76. 'size' => 'big',
  77. 'not null' => true,
  78. 'description' => 'score for the notice (0.0, 1.0)'),
  79. 'scaled' => array('type' => 'int',
  80. 'description' => 'scaled score for the notice (0, 10000)'),
  81. 'is_spam' => array('type' => 'bool',
  82. 'description' => 'flag for spamosity'),
  83. 'created' => array('type' => 'datetime',
  84. 'not null' => true,
  85. 'description' => 'date this record was created'),
  86. 'notice_created' => array('type' => 'datetime',
  87. 'description' => 'date the notice was created'),
  88. ),
  89. 'primary key' => array('notice_id'),
  90. 'foreign keys' => array(
  91. 'spam_score_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  92. ),
  93. 'indexes' => array(
  94. 'spam_score_created_idx' => array('created'),
  95. 'spam_score_scaled_idx' => array('scaled'),
  96. ),
  97. );
  98. }
  99. public static function upgrade()
  100. {
  101. Spam_score::upgradeScaled();
  102. Spam_score::upgradeIsSpam();
  103. Spam_score::upgradeNoticeCreated();
  104. }
  105. protected static function upgradeScaled()
  106. {
  107. $score = new Spam_score();
  108. $score->whereAdd('scaled IS NULL');
  109. if ($score->find()) {
  110. while ($score->fetch()) {
  111. $orig = clone($score);
  112. $score->scaled = Spam_score::scale($score->score);
  113. $score->update($orig);
  114. }
  115. }
  116. }
  117. protected static function upgradeIsSpam()
  118. {
  119. $score = new Spam_score();
  120. $score->whereAdd('is_spam IS NULL');
  121. if ($score->find()) {
  122. while ($score->fetch()) {
  123. $orig = clone($score);
  124. $score->is_spam = ($score->score >= 0.90) ? true : false;
  125. $score->update($orig);
  126. }
  127. }
  128. }
  129. protected static function upgradeNoticeCreated()
  130. {
  131. $score = new Spam_score();
  132. $score->whereAdd('notice_created IS NULL');
  133. if ($score->find()) {
  134. while ($score->fetch()) {
  135. $notice = Notice::getKV('id', $score->notice_id);
  136. if (!empty($notice)) {
  137. $orig = clone($score);
  138. $score->notice_created = $notice->created;
  139. $score->update($orig);
  140. }
  141. }
  142. }
  143. }
  144. public function saveNew($notice, $result)
  145. {
  146. $score = new Spam_score();
  147. $score->notice_id = $notice->id;
  148. $score->score = $result->probability;
  149. $score->is_spam = $result->isSpam;
  150. $score->scaled = Spam_score::scale($score->score);
  151. $score->created = common_sql_now();
  152. $score->notice_created = $notice->created;
  153. $score->insert();
  154. self::blow('spam_score:notice_ids');
  155. return $score;
  156. }
  157. public static function scale($score)
  158. {
  159. $raw = round($score * Spam_score::MAX_SCALE);
  160. return max(0, min(Spam_score::MAX_SCALE, $raw));
  161. }
  162. public function delete($useWhere = false)
  163. {
  164. self::blow('spam_score:notice_ids');
  165. self::blow('spam_score:notice_ids;last');
  166. return parent::delete($useWhere);
  167. }
  168. }