MediaWiki master
checkImages.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
37class CheckImages extends Maintenance {
38
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Check images to see if they exist, are readable, etc' );
42 $this->setBatchSize( 1000 );
43 }
44
45 public function execute() {
46 $start = '';
47 $dbr = $this->getReplicaDB();
48
49 $numImages = 0;
50 $numGood = 0;
51
52 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
53 do {
54 $res = FileSelectQueryBuilder::newForFile( $dbr )
55 ->where( $dbr->expr( 'img_name', '>', $start ) )
56 ->limit( $this->getBatchSize() )
57 ->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC )
58 ->caller( __METHOD__ )
59 ->fetchResultSet();
60 foreach ( $res as $row ) {
61 $numImages++;
62 $start = $row->img_name;
63 $file = $repo->newFileFromRow( $row );
64 $path = $file->getPath();
65 if ( !$path ) {
66 $this->output( "{$row->img_name}: not locally accessible\n" );
67 continue;
68 }
69 $size = $repo->getFileSize( $file->getPath() );
70 if ( $size === false ) {
71 $this->output( "{$row->img_name}: missing\n" );
72 continue;
73 }
74
75 if ( $size == 0 && $row->img_size != 0 ) {
76 $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
77 continue;
78 }
79
80 if ( $size != $row->img_size ) {
81 $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, "
82 . "actual={$size}\n" );
83 continue;
84 }
85
86 $numGood++;
87 }
88 } while ( $res->numRows() );
89
90 $this->output( "Good images: $numGood/$numImages\n" );
91 }
92}
93
94// @codeCoverageIgnoreStart
95$maintClass = CheckImages::class;
96require_once RUN_MAINTENANCE_IF_MAIN;
97// @codeCoverageIgnoreEnd
$maintClass
Maintenance script to check images to see if they exist, are readable, etc.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Build SELECT queries with a fluent interface.