clean_profiles.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. define('INSTALLDIR', dirname(__DIR__));
  18. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  19. $shortoptions = 'y';
  20. $longoptions = ['yes'];
  21. $helptext = <<<END_OF_HELP
  22. clean_profiles.php [options]
  23. Deletes all profile table entries where the profile does not occur in the
  24. notice table, is not a group and is not a local user.
  25. WARNING: This has not been tested thoroughly. Maybe we've missed a table to compare somewhere.
  26. -y --yes do not wait for confirmation
  27. END_OF_HELP;
  28. require_once INSTALLDIR . '/scripts/commandline.inc';
  29. if (!have_option('y', 'yes')) {
  30. print "About to delete profiles that we think are useless to save. Are you sure? [y/N] ";
  31. $response = fgets(STDIN);
  32. if (strtolower(trim($response)) != 'y') {
  33. print "Aborting.\n";
  34. exit(0);
  35. }
  36. }
  37. echo 'Deleting';
  38. $user_table = common_database_tablename('user');
  39. $profile = new Profile();
  40. $profile->query(
  41. <<<END
  42. SELECT profile.*
  43. FROM profile
  44. LEFT JOIN (
  45. SELECT profile_id AS id FROM notice
  46. UNION ALL
  47. SELECT id FROM {$user_table}
  48. UNION ALL
  49. SELECT profile_id AS id FROM user_group
  50. UNION ALL
  51. SELECT subscriber FROM subscription
  52. UNION ALL
  53. SELECT subscribed FROM subscription
  54. ) AS t1 USING (id)
  55. WHERE t1.id IS NULL
  56. END
  57. );
  58. while ($profile->fetch()) {
  59. echo ' '.$profile->getID().':'.$profile->getNickname();
  60. $profile->delete();
  61. }
  62. print "\nDONE.\n";