ILoadBalancer: Rename runMasterTransactionListenerCallbacks to runPrimaryTransactionL...
[mediawiki.git] / maintenance / checkImages.php
blobd9cb659e417df79f7bea19975bc1d694f1b2a110
1 <?php
2 /**
3 * Check images to see if they exist, are readable, etc.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices;
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script to check images to see if they exist, are readable, etc.
31 * @ingroup Maintenance
33 class CheckImages extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Check images to see if they exist, are readable, etc' );
38 $this->setBatchSize( 1000 );
41 public function execute() {
42 $start = '';
43 $dbr = $this->getDB( DB_REPLICA );
45 $numImages = 0;
46 $numGood = 0;
48 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
49 $fileQuery = LocalFile::getQueryInfo();
50 do {
51 $res = $dbr->select( $fileQuery['tables'], $fileQuery['fields'],
52 [ 'img_name > ' . $dbr->addQuotes( $start ) ],
53 __METHOD__, [ 'LIMIT' => $this->getBatchSize() ], $fileQuery['joins'] );
54 foreach ( $res as $row ) {
55 $numImages++;
56 $start = $row->img_name;
57 $file = $repo->newFileFromRow( $row );
58 $path = $file->getPath();
59 if ( !$path ) {
60 $this->output( "{$row->img_name}: not locally accessible\n" );
61 continue;
63 $size = $repo->getFileSize( $file->getPath() );
64 if ( $size === false ) {
65 $this->output( "{$row->img_name}: missing\n" );
66 continue;
69 if ( $size == 0 && $row->img_size != 0 ) {
70 $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
71 continue;
74 if ( $size != $row->img_size ) {
75 $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, "
76 . "actual={$size}\n" );
77 continue;
80 $numGood++;
82 } while ( $res->numRows() );
84 $this->output( "Good images: $numGood/$numImages\n" );
88 $maintClass = CheckImages::class;
89 require_once RUN_MAINTENANCE_IF_MAIN;