nukefile.php 2.4 KB

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