trainuser.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @package GNUsocial
  19. * @copyright 2012 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. define('INSTALLDIR', dirname(__DIR__, 3));
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. $shortoptions = 'i:n:t:';
  25. $longoptions = array('id=', 'nickname=', 'category=');
  26. $helptext = <<<END_OF_TRAINUSER_HELP
  27. trainuser.php [options]
  28. Train user activities against the spam filter
  29. -i --id ID of user to export
  30. -n --nickname nickname of the user to export
  31. -t --category Category; one of "spam" or "ham"
  32. END_OF_TRAINUSER_HELP;
  33. require_once INSTALLDIR.'/scripts/commandline.inc';
  34. function trainUser($filter, $user, $category)
  35. {
  36. printfnq("Training user %s\n", $user->nickname);
  37. $profile = Profile::getKV('id', $user->id);
  38. $str = new ProfileNoticeStream($profile, $profile);
  39. $offset = 0;
  40. $limit = 100;
  41. do {
  42. $notice = $str->getNotices($offset, $limit);
  43. while ($notice->fetch()) {
  44. try {
  45. printfv("Training notice %d...", $notice->id);
  46. $filter->trainOnError($notice, $category);
  47. $result = $filter->test($notice);
  48. $score = Spam_score::save($notice, $result);
  49. printfv("%s\n", ($result->isSpam) ? "SPAM" : "HAM");
  50. } catch (Exception $e) {
  51. printfnq("ERROR training notice %d\n: %s", $notice->id, $e->getMessage());
  52. }
  53. }
  54. $offset += $notice->N;
  55. } while ($notice->N > 0);
  56. }
  57. try {
  58. $filter = null;
  59. Event::handle('GetSpamFilter', array(&$filter));
  60. if (empty($filter)) {
  61. throw new Exception(_("No spam filter."));
  62. }
  63. $user = getUser();
  64. $category = get_option_value('t', 'category');
  65. if ($category !== SpamFilter::HAM &&
  66. $category !== SpamFilter::SPAM) {
  67. throw new Exception(_("No such category."));
  68. }
  69. trainUser($filter, $user, $category);
  70. } catch (Exception $e) {
  71. print $e->getMessage()."\n";
  72. exit(1);
  73. }