findMissingFiles.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 FindMissingFiles extends Maintenance {
  23. function __construct() {
  24. parent::__construct();
  25. $this->addDescription( 'Find registered files with no corresponding file.' );
  26. $this->addOption( 'start', 'Start after this file name', false, true );
  27. $this->addOption( 'mtimeafter', 'Only include files changed since this time', false, true );
  28. $this->addOption( 'mtimebefore', 'Only includes files changed before this time', false, true );
  29. $this->setBatchSize( 300 );
  30. }
  31. function execute() {
  32. $lastName = $this->getOption( 'start', '' );
  33. $repo = RepoGroup::singleton()->getLocalRepo();
  34. $dbr = $repo->getSlaveDB();
  35. $be = $repo->getBackend();
  36. $mtime1 = $dbr->timestampOrNull( $this->getOption( 'mtimeafter', null ) );
  37. $mtime2 = $dbr->timestampOrNull( $this->getOption( 'mtimebefore', null ) );
  38. $joinTables = [];
  39. $joinConds = [];
  40. if ( $mtime1 || $mtime2 ) {
  41. $joinTables[] = 'page';
  42. $joinConds['page'] = [ 'INNER JOIN',
  43. [ 'page_title = img_name', 'page_namespace' => NS_FILE ] ];
  44. $joinTables[] = 'logging';
  45. $on = [ 'log_page = page_id', 'log_type' => [ 'upload', 'move', 'delete' ] ];
  46. if ( $mtime1 ) {
  47. $on[] = "log_timestamp > {$dbr->addQuotes($mtime1)}";
  48. }
  49. if ( $mtime2 ) {
  50. $on[] = "log_timestamp < {$dbr->addQuotes($mtime2)}";
  51. }
  52. $joinConds['logging'] = [ 'INNER JOIN', $on ];
  53. }
  54. do {
  55. $res = $dbr->select(
  56. array_merge( [ 'image' ], $joinTables ),
  57. [ 'name' => 'img_name' ],
  58. [ "img_name > " . $dbr->addQuotes( $lastName ) ],
  59. __METHOD__,
  60. // DISTINCT causes a pointless filesort
  61. [ 'ORDER BY' => 'name', 'GROUP BY' => 'name',
  62. 'LIMIT' => $this->mBatchSize ],
  63. $joinConds
  64. );
  65. // Check if any of these files are missing...
  66. $pathsByName = [];
  67. foreach ( $res as $row ) {
  68. $file = $repo->newFile( $row->name );
  69. $pathsByName[$row->name] = $file->getPath();
  70. $lastName = $row->name;
  71. }
  72. $be->preloadFileStat( [ 'srcs' => $pathsByName ] );
  73. foreach ( $pathsByName as $path ) {
  74. if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
  75. $this->output( "$path\n" );
  76. }
  77. }
  78. // Find all missing old versions of any of the files in this batch...
  79. if ( count( $pathsByName ) ) {
  80. $ores = $dbr->select( 'oldimage',
  81. [ 'oi_name', 'oi_archive_name' ],
  82. [ 'oi_name' => array_keys( $pathsByName ) ],
  83. __METHOD__
  84. );
  85. $checkPaths = [];
  86. foreach ( $ores as $row ) {
  87. if ( !strlen( $row->oi_archive_name ) ) {
  88. continue; // broken row
  89. }
  90. $file = $repo->newFromArchiveName( $row->oi_name, $row->oi_archive_name );
  91. $checkPaths[] = $file->getPath();
  92. }
  93. foreach ( array_chunk( $checkPaths, $this->mBatchSize ) as $paths ) {
  94. $be->preloadFileStat( [ 'srcs' => $paths ] );
  95. foreach ( $paths as $path ) {
  96. if ( $be->fileExists( [ 'src' => $path ] ) === false ) {
  97. $this->output( "$path\n" );
  98. }
  99. }
  100. }
  101. }
  102. } while ( $res->numRows() >= $this->mBatchSize );
  103. }
  104. }
  105. $maintClass = 'FindMissingFiles';
  106. require_once RUN_MAINTENANCE_IF_MAIN;