4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
24 $longoptions = array('yes');
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.
30 -y --yes do not wait for confirmation
34 require_once INSTALLDIR.'/scripts/commandline.inc';
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') {
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()) {
62 print "\nWarning! URL suddenly disappeared from database: {$file->url}\n";
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()) {
83 print "\nWarning! URL suddenly disappeared from database: {$file->url}\n";