testuser.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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:a';
  21. $longoptions = array('id=', 'nickname=', 'all');
  22. $helptext = <<<END_OF_TESTUSER_HELP
  23. testuser.php [options]
  24. Test user activities against the spam filter
  25. -i --id ID of user to export
  26. -n --nickname nickname of the user to export
  27. -a --all All users
  28. END_OF_TESTUSER_HELP;
  29. require_once INSTALLDIR.'/scripts/commandline.inc';
  30. function testAllUsers($filter) {
  31. $found = false;
  32. $offset = 0;
  33. $limit = 1000;
  34. do {
  35. $user = new User();
  36. $user->orderBy('created');
  37. $user->limit($offset, $limit);
  38. $found = $user->find();
  39. if ($found) {
  40. while ($user->fetch()) {
  41. try {
  42. testUser($filter, $user);
  43. } catch (Exception $e) {
  44. printfnq("ERROR testing user %s\n: %s", $user->nickname, $e->getMessage());
  45. }
  46. }
  47. $offset += $found;
  48. }
  49. } while ($found > 0);
  50. }
  51. function testUser($filter, $user) {
  52. printfnq("Testing user %s\n", $user->nickname);
  53. $profile = Profile::getKV('id', $user->id);
  54. $str = new ProfileNoticeStream($profile, $profile);
  55. $offset = 0;
  56. $limit = 100;
  57. do {
  58. $notice = $str->getNotices($offset, $limit);
  59. while ($notice->fetch()) {
  60. try {
  61. printfv("Testing notice %d...", $notice->id);
  62. $result = $filter->test($notice);
  63. Spam_score::save($notice, $result);
  64. printfv("%s\n", ($result->isSpam) ? "SPAM" : "HAM");
  65. } catch (Exception $e) {
  66. printfnq("ERROR testing notice %d: %s\n", $notice->id, $e->getMessage());
  67. }
  68. }
  69. $offset += $notice->N;
  70. } while ($notice->N > 0);
  71. }
  72. try {
  73. $filter = null;
  74. Event::handle('GetSpamFilter', array(&$filter));
  75. if (empty($filter)) {
  76. throw new Exception(_("No spam filter."));
  77. }
  78. if (have_option('a', 'all')) {
  79. testAllUsers($filter);
  80. } else {
  81. $user = getUser();
  82. testUser($filter, $user);
  83. }
  84. } catch (Exception $e) {
  85. print $e->getMessage()."\n";
  86. exit(1);
  87. }