Completed
Branch master (54277f)
by
unknown
24:54
created

RefreshImageMetadata::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 49
rs 9.2258
c 1
b 0
f 0
cc 1
eloc 39
nc 1
nop 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 37 and the first side effect is on line 30.

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
 * Refresh image metadata fields. See also rebuildImages.php
4
 *
5
 * Usage: php refreshImageMetadata.php
6
 *
7
 * Copyright © 2011 Brian Wolff
8
 * https://www.mediawiki.org/
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License along
21
 * with this program; if not, write to the Free Software Foundation, Inc.,
22
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
 * http://www.gnu.org/copyleft/gpl.html
24
 *
25
 * @file
26
 * @author Brian Wolff
27
 * @ingroup Maintenance
28
 */
29
30
require_once __DIR__ . '/Maintenance.php';
31
32
/**
33
 * Maintenance script to refresh image metadata fields.
34
 *
35
 * @ingroup Maintenance
36
 */
37
class RefreshImageMetadata extends Maintenance {
38
39
	/**
40
	 * @var DatabaseBase
41
	 */
42
	protected $dbw;
43
44
	function __construct() {
45
		parent::__construct();
46
47
		$this->addDescription( 'Script to update image metadata records' );
48
		$this->setBatchSize( 200 );
49
50
		$this->addOption(
51
			'force',
52
			'Reload metadata from file even if the metadata looks ok',
53
			false,
54
			false,
55
			'f'
56
		);
57
		$this->addOption(
58
			'broken-only',
59
			'Only fix really broken records, leave old but still compatible records alone.'
60
		);
61
		$this->addOption(
62
			'verbose',
63
			'Output extra information about each upgraded/non-upgraded file.',
64
			false,
65
			false,
66
			'v'
67
		);
68
		$this->addOption( 'start', 'Name of file to start with', false, true );
69
		$this->addOption( 'end', 'Name of file to end with', false, true );
70
71
		$this->addOption(
72
			'mediatype',
73
			'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
74
			false,
75
			true
76
		);
77
		$this->addOption(
78
			'mime',
79
			"Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
80
				. "Potentially inefficient unless 'mediatype' is also specified",
81
			false,
82
			true
83
		);
84
		$this->addOption(
85
			'metadata-contains',
86
			'(Inefficient!) Only refresh files where the img_metadata field '
87
				. 'contains this string. Can be used if its known a specific '
88
				. 'property was being extracted incorrectly.',
89
			false,
90
			true
91
		);
92
	}
93
94
	public function execute() {
95
		$force = $this->hasOption( 'force' );
96
		$brokenOnly = $this->hasOption( 'broken-only' );
97
		$verbose = $this->hasOption( 'verbose' );
98
		$start = $this->getOption( 'start', false );
99
		$this->setupParameters( $force, $brokenOnly );
100
101
		$upgraded = 0;
102
		$leftAlone = 0;
103
		$error = 0;
104
105
		$dbw = $this->getDB( DB_MASTER );
106
		if ( $this->mBatchSize <= 0 ) {
107
			$this->error( "Batch size is too low...", 12 );
108
		}
109
110
		$repo = RepoGroup::singleton()->getLocalRepo();
111
		$conds = $this->getConditions( $dbw );
0 ignored issues
show
Compatibility introduced by
$dbw of type object<IDatabase> is not a sub-type of object<DatabaseBase>. It seems like you assume a concrete implementation of the interface IDatabase to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
112
113
		// For the WHERE img_name > 'foo' condition that comes after doing a batch
114
		$conds2 = [];
115
		if ( $start !== false ) {
116
			$conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
117
		}
118
119
		$options = [
120
			'LIMIT' => $this->mBatchSize,
121
			'ORDER BY' => 'img_name ASC',
122
		];
123
124
		do {
125
			$res = $dbw->select(
126
				'image',
127
				'*',
128
				array_merge( $conds, $conds2 ),
129
				__METHOD__,
130
				$options
131
			);
132
133
			if ( $res->numRows() > 0 ) {
134
				$row1 = $res->current();
135
				$this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
136
				$res->rewind();
137
			} else {
138
				$this->error( "No images to process.", 4 );
139
			}
140
141
			foreach ( $res as $row ) {
0 ignored issues
show
Bug introduced by
The expression $res of type object<ResultWrapper>|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
142
				$file = $repo->newFileFromRow( $row );
143
				if ( $file->getUpgraded() ) {
144
					// File was upgraded.
145
					$upgraded++;
146
					$newLength = strlen( $file->getMetadata() );
147
					$oldLength = strlen( $row->img_metadata );
148 View Code Duplication
					if ( $newLength < $oldLength - 5 ) {
149
						// If after updating, the metadata is smaller then
150
						// what it was before, that's probably not a good thing
151
						// because we extract more data with time, not less.
152
						// Thus this probably indicates an error of some sort,
153
						// or at the very least is suspicious. Have the - 5 just
154
						// to weed out any inconsequential changes.
155
						$error++;
156
						$this->output( "Warning: File:{$row->img_name} used to have " .
157
							"$oldLength bytes of metadata but now has $newLength bytes.\n" );
158
					} elseif ( $verbose ) {
159
						$this->output( "Refreshed File:{$row->img_name}.\n" );
160
					}
161
				} else {
162
					$leftAlone++;
163
					if ( $force ) {
164
						$file->upgradeRow();
165
						$newLength = strlen( $file->getMetadata() );
166
						$oldLength = strlen( $row->img_metadata );
167 View Code Duplication
						if ( $newLength < $oldLength - 5 ) {
168
							$error++;
169
							$this->output( "Warning: File:{$row->img_name} used to have " .
170
								"$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
171
						}
172
						if ( $verbose ) {
173
							$this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
174
						}
175
					} else {
176
						if ( $verbose ) {
177
							$this->output( "Skipping File:{$row->img_name}.\n" );
178
						}
179
					}
180
				}
181
			}
182
			$conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name ) ];
0 ignored issues
show
Bug introduced by
The variable $row seems to be defined by a foreach iteration on line 141. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
183
			wfWaitForSlaves();
0 ignored issues
show
Deprecated Code introduced by
The function wfWaitForSlaves() has been deprecated with message: since 1.27 Use LBFactory::waitForReplication

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
184
		} while ( $res->numRows() === $this->mBatchSize );
185
186
		$total = $upgraded + $leftAlone;
187
		if ( $force ) {
188
			$this->output( "\nFinished refreshing file metadata for $total files. "
189
				. "$upgraded needed to be refreshed, $leftAlone did not need to "
190
				. "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
191
		} else {
192
			$this->output( "\nFinished refreshing file metadata for $total files. "
193
				. "$upgraded were refreshed, $leftAlone were already up to date, "
194
				. "and $error refreshes were suspicious.\n" );
195
		}
196
	}
197
198
	/**
199
	 * @param DatabaseBase $dbw
200
	 * @return array
201
	 */
202
	function getConditions( $dbw ) {
203
		$conds = [];
204
205
		$end = $this->getOption( 'end', false );
206
		$mime = $this->getOption( 'mime', false );
207
		$mediatype = $this->getOption( 'mediatype', false );
208
		$like = $this->getOption( 'metadata-contains', false );
209
210
		if ( $end !== false ) {
211
			$conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
212
		}
213
		if ( $mime !== false ) {
214
			list( $major, $minor ) = File::splitMime( $mime );
215
			$conds['img_major_mime'] = $major;
216
			if ( $minor !== '*' ) {
217
				$conds['img_minor_mime'] = $minor;
218
			}
219
		}
220
		if ( $mediatype !== false ) {
221
			$conds['img_media_type'] = $mediatype;
222
		}
223
		if ( $like ) {
224
			$conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
225
		}
226
227
		return $conds;
228
	}
229
230
	/**
231
	 * @param bool $force
232
	 * @param bool $brokenOnly
233
	 */
234
	function setupParameters( $force, $brokenOnly ) {
235
		global $wgUpdateCompatibleMetadata;
236
237
		if ( $brokenOnly ) {
238
			$wgUpdateCompatibleMetadata = false;
239
		} else {
240
			$wgUpdateCompatibleMetadata = true;
241
		}
242
243
		if ( $brokenOnly && $force ) {
244
			$this->error( 'Cannot use --broken-only and --force together. ', 2 );
245
		}
246
	}
247
}
248
249
$maintClass = 'RefreshImageMetadata';
250
require_once RUN_MAINTENANCE_IF_MAIN;
251