nukefile.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, 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::yv';
  23. $longoptions = array('id=', 'yes', 'verbose');
  24. $helptext = <<<END_OF_HELP
  25. nukefile.php [options]
  26. deletes a file and related notices from the database
  27. -i --id ID of the file
  28. -v --verbose Be verbose (print the contents of the notices deleted).
  29. END_OF_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. if (have_option('i', 'id')) {
  32. $id = get_option_value('i', 'id');
  33. $file = File::getKV('id', $id);
  34. if (!$file instanceof File) {
  35. print "Can't find file with ID $id\n";
  36. exit(1);
  37. }
  38. } else {
  39. print "You must provide a file ID.\n";
  40. exit(1);
  41. }
  42. $verbose = have_option('v', 'verbose');
  43. if (!have_option('y', 'yes')) {
  44. try {
  45. $filename = $file->getFilename();
  46. } catch (Exception $e) {
  47. $filename = '(remote file or no filename)';
  48. }
  49. print "About to PERMANENTLY delete file ($filename) with id=={$file->id}) AND its related notices. Are you sure? [y/N] ";
  50. $response = fgets(STDIN);
  51. if (strtolower(trim($response)) != 'y') {
  52. print "Aborting.\n";
  53. exit(0);
  54. }
  55. }
  56. print "Finding notices...\n";
  57. try {
  58. $ids = File_to_post::getNoticeIDsByFile($file);
  59. $notice = Notice::multiGet('id', $ids);
  60. while ($notice->fetch()) {
  61. print "Deleting notice {$notice->id}".($verbose ? ": $notice->content\n" : "\n");
  62. $notice->delete();
  63. }
  64. } catch (NoResultException $e) {
  65. print "No notices found with this File attached.\n";
  66. }
  67. print "Deleting File object together with possibly locally stored copy.\n";
  68. $file->delete();
  69. print "DONE.\n";