remove_duplicate_file_urls.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @copyright 2008, 2009 StatusNet, Inc.
  19. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  20. */
  21. define('INSTALLDIR', dirname(__DIR__));
  22. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  23. $shortoptions = 'y';
  24. $longoptions = array('yes');
  25. $helptext = <<<END_OF_HELP
  26. remove_duplicate_file_urls.php [options]
  27. Remove duplicate URL entries in the file and file_redirection tables because they for some reason were not unique.
  28. -y --yes do not wait for confirmation
  29. END_OF_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. if (!have_option('y', 'yes')) {
  32. print "About to remove duplicate URL entries in file and file_redirection tables. Are you sure? [y/N] ";
  33. $response = fgets(STDIN);
  34. if (strtolower(trim($response)) != 'y') {
  35. print "Aborting.\n";
  36. exit(0);
  37. }
  38. }
  39. $file = new File();
  40. $file->query('SELECT url FROM file GROUP BY url HAVING COUNT(*) > 1');
  41. print "\nFound {$file->N} URLs with duplicate entries in file table";
  42. while ($file->fetch()) {
  43. // We've got a URL that is duplicated in the file table
  44. $dupfile = new File();
  45. $dupfile->url = $file->url;
  46. if ($dupfile->find(true)) {
  47. print "\nDeleting duplicate entries in file table for URL: {$file->url} [";
  48. // Leave one of the URLs in the database by using ->find(true)
  49. // and only deleting starting with this fetch.
  50. while ($dupfile->fetch()) {
  51. print ".";
  52. $dupfile->delete();
  53. }
  54. print "]\n";
  55. } else {
  56. print "\nWarning! URL suddenly disappeared from database: {$file->url}\n";
  57. }
  58. }
  59. $file = new File_redirection();
  60. $file->query('SELECT url FROM file_redirection GROUP BY url HAVING COUNT(*) > 1');
  61. print "\nFound {$file->N} URLs with duplicate entries in file_redirection table";
  62. while ($file->fetch()) {
  63. // We've got a URL that is duplicated in the file_redirection table
  64. $dupfile = new File_redirection();
  65. $dupfile->url = $file->url;
  66. if ($dupfile->find(true)) {
  67. print "\nDeleting duplicate entries in file table for URL: {$file->url} [";
  68. // Leave one of the URLs in the database by using ->find(true)
  69. // and only deleting starting with this fetch.
  70. while ($dupfile->fetch()) {
  71. print ".";
  72. $dupfile->delete();
  73. }
  74. print "]\n";
  75. } else {
  76. print "\nWarning! URL suddenly disappeared from database: {$file->url}\n";
  77. }
  78. }
  79. print "\nDONE.\n";