]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/remove_duplicate_file_urls.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / scripts / remove_duplicate_file_urls.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 = 'y';
24 $longoptions = array('yes');
25
26 $helptext = <<<END_OF_HELP
27 remove_duplicate_file_urls.php [options]
28 Remove duplicate URL entries in the file and file_redirection tables because they for some reason were not unique.
29
30   -y --yes      do not wait for confirmation
31
32 END_OF_HELP;
33
34 require_once INSTALLDIR.'/scripts/commandline.inc';
35
36 if (!have_option('y', 'yes')) {
37     print "About to remove duplicate URL entries in file and file_redirection tables. Are you sure? [y/N] ";
38     $response = fgets(STDIN);
39     if (strtolower(trim($response)) != 'y') {
40         print "Aborting.\n";
41         exit(0);
42     }
43 }
44
45 $file = new File();
46 $file->query('SELECT id, url, COUNT(*) AS c FROM file GROUP BY url HAVING c > 1');
47 print "\nFound {$file->N} URLs with duplicate entries in file table";
48 while ($file->fetch()) {
49     // We've got a URL that is duplicated in the file table
50     $dupfile = new File();
51     $dupfile->url = $file->url;
52     if ($dupfile->find(true)) {
53         print "\nDeleting duplicate entries in file table for URL: {$file->url} [";
54         // Leave one of the URLs in the database by using ->find(true)
55         // and only deleting starting with this fetch.
56         while($dupfile->fetch()) {
57             print ".";
58             $dupfile->delete();
59         }
60         print "]\n";
61     } else {
62         print "\nWarning! URL suddenly disappeared from database: {$file->url}\n";
63     }
64 }
65
66 $file = new File_redirection();
67 $file->query('SELECT file_id, url, COUNT(*) AS c FROM file_redirection GROUP BY url HAVING c > 1');
68 print "\nFound {$file->N} URLs with duplicate entries in file_redirection table";
69 while ($file->fetch()) {
70     // We've got a URL that is duplicated in the file_redirection table
71     $dupfile = new File_redirection();
72     $dupfile->url = $file->url;
73     if ($dupfile->find(true)) {
74         print "\nDeleting duplicate entries in file table for URL: {$file->url} [";
75         // Leave one of the URLs in the database by using ->find(true)
76         // and only deleting starting with this fetch.
77         while($dupfile->fetch()) {
78             print ".";
79             $dupfile->delete();
80         }
81         print "]\n";
82     } else {
83         print "\nWarning! URL suddenly disappeared from database: {$file->url}\n";
84     }
85 }
86 print "\nDONE.\n";