MediaWiki  master
checkImages.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class CheckImages extends Maintenance {
34 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Check images to see if they exist, are readable, etc' );
38  $this->setBatchSize( 1000 );
39  }
40 
41  public function execute() {
42  $start = '';
43  $dbr = $this->getDB( DB_REPLICA );
44 
45  $numImages = 0;
46  $numGood = 0;
47 
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;
62  }
63  $size = $repo->getFileSize( $file->getPath() );
64  if ( $size === false ) {
65  $this->output( "{$row->img_name}: missing\n" );
66  continue;
67  }
68 
69  if ( $size == 0 && $row->img_size != 0 ) {
70  $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
71  continue;
72  }
73 
74  if ( $size != $row->img_size ) {
75  $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, "
76  . "actual={$size}\n" );
77  continue;
78  }
79 
80  $numGood++;
81  }
82  } while ( $res->numRows() );
83 
84  $this->output( "Good images: $numGood/$numImages\n" );
85  }
86 }
87 
88 $maintClass = CheckImages::class;
89 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$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:55
CheckImages\__construct
__construct()
Default constructor.
Definition: checkImages.php:35
$res
$res
Definition: testCompression.php:57
$dbr
$dbr
Definition: testCompression.php:54
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:33
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
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:227
$path
$path
Definition: NoLocalSettings.php:25
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
CheckImages\execute
execute()
Do the actual work.
Definition: checkImages.php:41
$maintClass
$maintClass
Definition: checkImages.php:88
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:374