testuser.php 3.1 KB

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