remove_duplicate_file_urls.php 3.1 KB

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