Merge maintenance-work branch:
[lhc/web/wiklou.git] / maintenance / checkImages.php
1 <?php
2 /**
3 * Check images to see if they exist, are readable, etc etc
4 */
5 require_once( "Maintenance.php" );
6
7 class CheckImages extends Maintenance {
8
9 public function __construct() {
10 parent::__construct();
11 $this->mDescription = "Check images to see if they exist, are readable, etc";
12 }
13
14 public function execute() {
15 $batchSize = 1000;
16 $start = '';
17 $dbr = wfGetDB( DB_SLAVE );
18
19 $numImages = 0;
20 $numGood = 0;
21
22 do {
23 $res = $dbr->select( 'image', '*', array( 'img_name > ' . $dbr->addQuotes( $start ) ),
24 __METHOD__, array( 'LIMIT' => $batchSize ) );
25 foreach ( $res as $row ) {
26 $numImages++;
27 $start = $row->img_name;
28 $file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
29 $path = $file->getPath();
30 if ( !$path ) {
31 $this->output( "{$row->img_name}: not locally accessible\n";
32 continue;
33 }
34 $stat = @stat( $file->getPath() );
35 if ( !$stat ) {
36 $this->output( "{$row->img_name}: missing\n" );
37 continue;
38 }
39
40 if ( $stat['mode'] & 040000 ) {
41 $this->output( "{$row->img_name}: is a directory\n" );
42 continue;
43 }
44
45 if ( $stat['size'] == 0 && $row->img_size != 0 ) {
46 $this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
47 continue;
48 }
49
50 if ( $stat['size'] != $row->img_size ) {
51 $this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, actual={$stat['size']}\n" );
52 continue;
53 }
54
55 $numGood++;
56 }
57
58 } while ( $res->numRows() );
59
60 $this->output( "Good images: $numGood/$numImages\n" );
61 }
62 }
63