trainuser.php 2.6 KB

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