clear_jabber.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @copyright 2008-2010 StatusNet, Inc.
  19. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  20. */
  21. define('INSTALLDIR', dirname(__DIR__));
  22. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  23. $shortoptions = 'i::n::y';
  24. $longoptions = array('id=', 'nickname=', 'yes', 'all', 'dry-run');
  25. $helptext = <<<END_OF_DELETEUSER_HELP
  26. clear_jabber.php [options]
  27. Deletes a user's confirmed Jabber/XMPP address from the database.
  28. -i --id ID of the user
  29. -n --nickname nickname of the user
  30. --all all users with confirmed Jabber addresses
  31. --dry-run Don't actually delete info.
  32. END_OF_DELETEUSER_HELP;
  33. require_once INSTALLDIR.'/scripts/commandline.inc';
  34. if (have_option('i', 'id')) {
  35. $id = get_option_value('i', 'id');
  36. $user = User::getKV('id', $id);
  37. if (empty($user)) {
  38. print "Can't find user with ID $id\n";
  39. exit(1);
  40. }
  41. } elseif (have_option('n', 'nickname')) {
  42. $nickname = get_option_value('n', 'nickname');
  43. $user = User::getKV('nickname', $nickname);
  44. if (empty($user)) {
  45. print "Can't find user with nickname '$nickname'\n";
  46. exit(1);
  47. }
  48. } elseif (have_option('all')) {
  49. $user = new User();
  50. $user->whereAdd("jabber <> ''");
  51. $user->find(true);
  52. if ($user->N == 0) {
  53. print "No users with registered Jabber addresses in database.\n";
  54. exit(1);
  55. }
  56. } else {
  57. print "You must provide either an ID or a nickname.\n";
  58. print "\n";
  59. print $helptext;
  60. exit(1);
  61. }
  62. function clear_jabber($id)
  63. {
  64. $user = User::getKV('id', $id);
  65. if ($user && $user->jabber) {
  66. echo "clearing user $id's user.jabber, was: $user->jabber";
  67. if (have_option('dry-run')) {
  68. echo " (SKIPPING)";
  69. } else {
  70. $original = clone($user);
  71. $user->jabber = null;
  72. try {
  73. $user->updateWithKeys($original);
  74. } catch (Exception $e) {
  75. echo "WARNING: user update failed (setting jabber to null): ".$e->getMessage()."\n";
  76. }
  77. }
  78. echo "\n";
  79. } elseif (!$user) {
  80. echo "Missing user for $id\n";
  81. } else {
  82. echo "Cleared jabber already for $id\n";
  83. }
  84. }
  85. do {
  86. clear_jabber($user->id);
  87. } while ($user->fetch());
  88. print "DONE.\n";