CheckImages::execute()   C
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 43
Code Lines 31

Duplication

Lines 9
Ratio 20.93 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 6
nop 0
dl 9
loc 43
rs 5.3846
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 30 and the first side effect is on line 23.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
require_once __DIR__ . '/Maintenance.php';
24
25
/**
26
 * Maintenance script to check images to see if they exist, are readable, etc.
27
 *
28
 * @ingroup Maintenance
29
 */
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
		do {
47
			$res = $dbr->select( 'image', '*', [ 'img_name > ' . $dbr->addQuotes( $start ) ],
48
				__METHOD__, [ 'LIMIT' => $this->mBatchSize ] );
49
			foreach ( $res as $row ) {
50
				$numImages++;
51
				$start = $row->img_name;
52
				$file = $repo->newFileFromRow( $row );
53
				$path = $file->getPath();
54
				if ( !$path ) {
55
					$this->output( "{$row->img_name}: not locally accessible\n" );
56
					continue;
57
				}
58
				$size = $repo->getFileSize( $file->getPath() );
0 ignored issues
show
Bug introduced by
It seems like $file->getPath() targeting File::getPath() can also be of type boolean; however, FileRepo::getFileSize() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
				if ( $size === false ) {
60
					$this->output( "{$row->img_name}: missing\n" );
61
					continue;
62
				}
63
64 View Code Duplication
				if ( $size == 0 && $row->img_size != 0 ) {
65
					$this->output( "{$row->img_name}: truncated, was {$row->img_size}\n" );
66
					continue;
67
				}
68
69 View Code Duplication
				if ( $size != $row->img_size ) {
70
					$this->output( "{$row->img_name}: size mismatch DB={$row->img_size}, "
71
						. "actual={$size}\n" );
72
					continue;
73
				}
74
75
				$numGood++;
76
			}
77
		} while ( $res->numRows() );
78
79
		$this->output( "Good images: $numGood/$numImages\n" );
80
	}
81
}
82
83
$maintClass = "CheckImages";
84
require_once RUN_MAINTENANCE_IF_MAIN;
85