MediaWiki  1.34.0
checkImages.php
Go to the documentation of this file.
1 <?php
23 require_once __DIR__ . '/Maintenance.php';
24 
30 class CheckImages extends Maintenance {
31 
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Check images to see if they exist, are readable, etc' );
35  $this->setBatchSize( 1000 );
36  }
37 
38  public function execute() {
39  $start = '';
40  $dbr = $this->getDB( DB_REPLICA );
41 
42  $numImages = 0;
43  $numGood = 0;
44 
45  $repo = RepoGroup::singleton()->getLocalRepo();
46  $fileQuery = LocalFile::getQueryInfo();
47  do {
48  $res = $dbr->select( $fileQuery['tables'], $fileQuery['fields'],
49  [ 'img_name > ' . $dbr->addQuotes( $start ) ],
50  __METHOD__, [ 'LIMIT' => $this->getBatchSize() ], $fileQuery['joins'] );
51  foreach ( $res as $row ) {
52  $numImages++;
53  $start = $row->img_name;
54  $file = $repo->newFileFromRow( $row );
55  $path = $file->getPath();
56  if ( !$path ) {
57  $this->output( "{$row->img_name}: not locally accessible\n" );
58  continue;
59  }
60  $size = $repo->getFileSize( $file->getPath() );
61  if ( $size === false ) {
62  $this->output( "{$row->img_name}: missing\n" );
63  continue;
64  }
65 
66  if ( $size == 0 && $row->img_size != 0 ) {
67  $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
68  continue;
69  }
70 
71  if ( $size != $row->img_size ) {
72  $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, "
73  . "actual={$size}\n" );
74  continue;
75  }
76 
77  $numGood++;
78  }
79  } while ( $res->numRows() );
80 
81  $this->output( "Good images: $numGood/$numImages\n" );
82  }
83 }
84 
85 $maintClass = CheckImages::class;
86 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
CheckImages\__construct
__construct()
Default constructor.
Definition: checkImages.php:32
$res
$res
Definition: testCompression.php:52
$dbr
$dbr
Definition: testCompression.php:50
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
CheckImages
Maintenance script to check images to see if they exist, are readable, etc.
Definition: checkImages.php:30
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
LocalFile\getQueryInfo
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:216
$path
$path
Definition: NoLocalSettings.php:25
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
CheckImages\execute
execute()
Do the actual work.
Definition: checkImages.php:38
$maintClass
$maintClass
Definition: checkImages.php:85
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394