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