Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckImages
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
56
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
24use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
25use MediaWiki\Maintenance\Maintenance;
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
31/**
32 * Maintenance script to check images to see if they exist, are readable, etc.
33 *
34 * @ingroup Maintenance
35 */
36class CheckImages extends Maintenance {
37
38    public function __construct() {
39        parent::__construct();
40        $this->addDescription( 'Check images to see if they exist, are readable, etc' );
41        $this->setBatchSize( 1000 );
42    }
43
44    public function execute() {
45        $start = '';
46        $dbr = $this->getReplicaDB();
47
48        $numImages = 0;
49        $numGood = 0;
50
51        $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
52        do {
53            $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
54
55            $res = $queryBuilder->where( $dbr->expr( 'img_name', '>', $start ) )
56                ->limit( $this->getBatchSize() )
57                ->caller( __METHOD__ )->fetchResultSet();
58            foreach ( $res as $row ) {
59                $numImages++;
60                $start = $row->img_name;
61                $file = $repo->newFileFromRow( $row );
62                $path = $file->getPath();
63                if ( !$path ) {
64                    $this->output( "{$row->img_name}: not locally accessible\n" );
65                    continue;
66                }
67                $size = $repo->getFileSize( $file->getPath() );
68                if ( $size === false ) {
69                    $this->output( "{$row->img_name}: missing\n" );
70                    continue;
71                }
72
73                if ( $size == 0 && $row->img_size != 0 ) {
74                    $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
75                    continue;
76                }
77
78                if ( $size != $row->img_size ) {
79                    $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}"
80                        . "actual={$size}\n" );
81                    continue;
82                }
83
84                $numGood++;
85            }
86        } while ( $res->numRows() );
87
88        $this->output( "Good images: $numGood/$numImages\n" );
89    }
90}
91
92// @codeCoverageIgnoreStart
93$maintClass = CheckImages::class;
94require_once RUN_MAINTENANCE_IF_MAIN;
95// @codeCoverageIgnoreEnd