checkImages.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Check images to see if they exist, are readable, etc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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 General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to check images to see if they exist, are readable, etc.
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class CheckImages extends Maintenance {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Check images to see if they exist, are readable, etc' );
  33. $this->setBatchSize( 1000 );
  34. }
  35. public function execute() {
  36. $start = '';
  37. $dbr = $this->getDB( DB_REPLICA );
  38. $numImages = 0;
  39. $numGood = 0;
  40. $repo = RepoGroup::singleton()->getLocalRepo();
  41. $fileQuery = LocalFile::getQueryInfo();
  42. do {
  43. $res = $dbr->select( $fileQuery['tables'], $fileQuery['fields'],
  44. [ 'img_name > ' . $dbr->addQuotes( $start ) ],
  45. __METHOD__, [ 'LIMIT' => $this->getBatchSize() ], $fileQuery['joins'] );
  46. foreach ( $res as $row ) {
  47. $numImages++;
  48. $start = $row->img_name;
  49. $file = $repo->newFileFromRow( $row );
  50. $path = $file->getPath();
  51. if ( !$path ) {
  52. $this->output( "{$row->img_name}: not locally accessible\n" );
  53. continue;
  54. }
  55. $size = $repo->getFileSize( $file->getPath() );
  56. if ( $size === false ) {
  57. $this->output( "{$row->img_name}: missing\n" );
  58. continue;
  59. }
  60. if ( $size == 0 && $row->img_size != 0 ) {
  61. $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
  62. continue;
  63. }
  64. if ( $size != $row->img_size ) {
  65. $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, "
  66. . "actual={$size}\n" );
  67. continue;
  68. }
  69. $numGood++;
  70. }
  71. } while ( $res->numRows() );
  72. $this->output( "Good images: $numGood/$numImages\n" );
  73. }
  74. }
  75. $maintClass = CheckImages::class;
  76. require_once RUN_MAINTENANCE_IF_MAIN;