findOrphanedFiles.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @author Aaron Schulz
  20. */
  21. require_once __DIR__ . '/Maintenance.php';
  22. class FindOrphanedFiles extends Maintenance {
  23. function __construct() {
  24. parent::__construct();
  25. $this->addDescription( "Find unregistered files in the 'public' repo zone." );
  26. $this->addOption( 'subdir',
  27. 'Only scan files in this subdirectory (e.g. "a/a0")', false, true );
  28. $this->addOption( 'verbose', "Mention file paths checked" );
  29. $this->setBatchSize( 500 );
  30. }
  31. function execute() {
  32. $subdir = $this->getOption( 'subdir', '' );
  33. $verbose = $this->hasOption( 'verbose' );
  34. $repo = RepoGroup::singleton()->getLocalRepo();
  35. if ( $repo->hasSha1Storage() ) {
  36. $this->error( "Local repo uses SHA-1 file storage names; aborting.", 1 );
  37. }
  38. $directory = $repo->getZonePath( 'public' );
  39. if ( $subdir != '' ) {
  40. $directory .= "/$subdir/";
  41. }
  42. if ( $verbose ) {
  43. $this->output( "Scanning files under $directory:\n" );
  44. }
  45. $list = $repo->getBackend()->getFileList( [ 'dir' => $directory ] );
  46. if ( $list === null ) {
  47. $this->error( "Could not get file listing.", 1 );
  48. }
  49. $pathBatch = [];
  50. foreach ( $list as $path ) {
  51. if ( preg_match( '#^(thumb|deleted)/#', $path ) ) {
  52. continue; // handle ugly nested containers on stock installs
  53. }
  54. $pathBatch[] = $path;
  55. if ( count( $pathBatch ) >= $this->mBatchSize ) {
  56. $this->checkFiles( $repo, $pathBatch, $verbose );
  57. $pathBatch = [];
  58. }
  59. }
  60. $this->checkFiles( $repo, $pathBatch, $verbose );
  61. }
  62. protected function checkFiles( LocalRepo $repo, array $paths, $verbose ) {
  63. if ( !count( $paths ) ) {
  64. return;
  65. }
  66. $dbr = $repo->getSlaveDB();
  67. $curNames = [];
  68. $oldNames = [];
  69. $imgIN = [];
  70. $oiWheres = [];
  71. foreach ( $paths as $path ) {
  72. $name = basename( $path );
  73. if ( preg_match( '#^archive/#', $path ) ) {
  74. if ( $verbose ) {
  75. $this->output( "Checking old file $name\n" );
  76. }
  77. $oldNames[] = $name;
  78. list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
  79. $oiWheres[] = $dbr->makeList(
  80. [ 'oi_name' => $base, 'oi_archive_name' => $name ],
  81. LIST_AND
  82. );
  83. } else {
  84. if ( $verbose ) {
  85. $this->output( "Checking current file $name\n" );
  86. }
  87. $curNames[] = $name;
  88. $imgIN[] = $name;
  89. }
  90. }
  91. $res = $dbr->query(
  92. $dbr->unionQueries(
  93. [
  94. $dbr->selectSQLText(
  95. 'image',
  96. [ 'name' => 'img_name', 'old' => 0 ],
  97. $imgIN ? [ 'img_name' => $imgIN ] : '1=0'
  98. ),
  99. $dbr->selectSQLText(
  100. 'oldimage',
  101. [ 'name' => 'oi_archive_name', 'old' => 1 ],
  102. $oiWheres ? $dbr->makeList( $oiWheres, LIST_OR ) : '1=0'
  103. )
  104. ],
  105. true // UNION ALL (performance)
  106. ),
  107. __METHOD__
  108. );
  109. $curNamesFound = [];
  110. $oldNamesFound = [];
  111. foreach ( $res as $row ) {
  112. if ( $row->old ) {
  113. $oldNamesFound[] = $row->name;
  114. } else {
  115. $curNamesFound[] = $row->name;
  116. }
  117. }
  118. foreach ( array_diff( $curNames, $curNamesFound ) as $name ) {
  119. $file = $repo->newFile( $name );
  120. // Print name and public URL to ease recovery
  121. if ( $file ) {
  122. $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
  123. } else {
  124. $this->error( "Cannot get URL for bad file title '$name'" );
  125. }
  126. }
  127. foreach ( array_diff( $oldNames, $oldNamesFound ) as $name ) {
  128. list( , $base ) = explode( '!', $name, 2 ); // <TS_MW>!<img_name>
  129. $file = $repo->newFromArchiveName( Title::makeTitle( NS_FILE, $base ), $name );
  130. // Print name and public URL to ease recovery
  131. $this->output( $name . "\n" . $file->getCanonicalUrl() . "\n\n" );
  132. }
  133. }
  134. }
  135. $maintClass = 'FindOrphanedFiles';
  136. require_once RUN_MAINTENANCE_IF_MAIN;