clear_jabber.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. $shortoptions = 'i::n::y';
  23. $longoptions = array('id=', 'nickname=', 'yes', 'all', 'dry-run');
  24. $helptext = <<<END_OF_DELETEUSER_HELP
  25. clear_jabber.php [options]
  26. Deletes a user's confirmed Jabber/XMPP address from the database.
  27. -i --id ID of the user
  28. -n --nickname nickname of the user
  29. --all all users with confirmed Jabber addresses
  30. --dry-run Don't actually delete info.
  31. END_OF_DELETEUSER_HELP;
  32. require_once INSTALLDIR.'/scripts/commandline.inc';
  33. if (have_option('i', 'id')) {
  34. $id = get_option_value('i', 'id');
  35. $user = User::getKV('id', $id);
  36. if (empty($user)) {
  37. print "Can't find user with ID $id\n";
  38. exit(1);
  39. }
  40. } else if (have_option('n', 'nickname')) {
  41. $nickname = get_option_value('n', 'nickname');
  42. $user = User::getKV('nickname', $nickname);
  43. if (empty($user)) {
  44. print "Can't find user with nickname '$nickname'\n";
  45. exit(1);
  46. }
  47. } else if (have_option('all')) {
  48. $user = new User();
  49. $user->whereAdd("jabber != ''");
  50. $user->find(true);
  51. if ($user->N == 0) {
  52. print "No users with registered Jabber addresses in database.\n";
  53. exit(1);
  54. }
  55. } else {
  56. print "You must provide either an ID or a nickname.\n";
  57. print "\n";
  58. print $helptext;
  59. exit(1);
  60. }
  61. function clear_jabber($id)
  62. {
  63. $user = User::getKV('id', $id);
  64. if ($user && $user->jabber) {
  65. echo "clearing user $id's user.jabber, was: $user->jabber";
  66. if (have_option('dry-run')) {
  67. echo " (SKIPPING)";
  68. } else {
  69. $original = clone($user);
  70. $user->jabber = null;
  71. try {
  72. $user->updateWithKeys($original);
  73. } catch (Exception $e) {
  74. echo "WARNING: user update failed (setting jabber to null): ".$e->getMessage()."\n";
  75. }
  76. }
  77. echo "\n";
  78. } else if (!$user) {
  79. echo "Missing user for $id\n";
  80. } else {
  81. echo "Cleared jabber already for $id\n";
  82. }
  83. }
  84. do {
  85. clear_jabber($user->id);
  86. } while ($user->fetch());
  87. print "DONE.\n";