testuser.php 3.1 KB

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