Completed
Branch master (e2eefa)
by
unknown
25:58
created

BackupDumper::processOptions()   F

Complexity

Conditions 19
Paths 513

Size

Total Lines 92
Code Lines 61

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 19
eloc 61
nc 513
nop 0
dl 0
loc 92
rs 2.9376

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Base classes for database dumpers
4
 *
5
 * Copyright © 2005 Brion Vibber <[email protected]>
6
 * https://www.mediawiki.org/
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program; if not, write to the Free Software Foundation, Inc.,
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 * http://www.gnu.org/copyleft/gpl.html
22
 *
23
 * @file
24
 * @ingroup Dump Maintenance
25
 */
26
27
require_once __DIR__ . '/Maintenance.php';
28
require_once __DIR__ . '/../includes/export/DumpFilter.php';
29
30
/**
31
 * @ingroup Dump Maintenance
32
 */
33
class BackupDumper extends Maintenance {
34
	public $reporting = true;
35
	public $pages = null; // all pages
36
	public $skipHeader = false; // don't output <mediawiki> and <siteinfo>
37
	public $skipFooter = false; // don't output </mediawiki>
38
	public $startId = 0;
39
	public $endId = 0;
40
	public $revStartId = 0;
41
	public $revEndId = 0;
42
	public $dumpUploads = false;
43
	public $dumpUploadFileContents = false;
44
45
	protected $reportingInterval = 100;
46
	protected $pageCount = 0;
47
	protected $revCount = 0;
48
	protected $server = null; // use default
49
	protected $sink = null; // Output filters
50
	protected $lastTime = 0;
51
	protected $pageCountLast = 0;
52
	protected $revCountLast = 0;
53
54
	protected $outputTypes = [];
55
	protected $filterTypes = [];
56
57
	protected $ID = 0;
58
59
	/**
60
	 * The dependency-injected database to use.
61
	 *
62
	 * @var DatabaseBase|null
63
	 *
64
	 * @see self::setDB
65
	 */
66
	protected $forcedDb = null;
67
68
	/** @var LoadBalancer */
69
	protected $lb;
70
71
	// @todo Unused?
72
	private $stubText = false; // include rev_text_id instead of text; for 2-pass dump
73
74
	/**
75
	 * @param array $args For backward compatibility
76
	 */
77
	function __construct( $args = null ) {
78
		parent::__construct();
79
		$this->stderr = fopen( "php://stderr", "wt" );
0 ignored issues
show
Bug introduced by
The property stderr does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
81
		// Built-in output and filter plugins
82
		$this->registerOutput( 'file', 'DumpFileOutput' );
83
		$this->registerOutput( 'gzip', 'DumpGZipOutput' );
84
		$this->registerOutput( 'bzip2', 'DumpBZip2Output' );
85
		$this->registerOutput( 'dbzip2', 'DumpDBZip2Output' );
86
		$this->registerOutput( '7zip', 'Dump7ZipOutput' );
87
88
		$this->registerFilter( 'latest', 'DumpLatestFilter' );
89
		$this->registerFilter( 'notalk', 'DumpNotalkFilter' );
90
		$this->registerFilter( 'namespace', 'DumpNamespaceFilter' );
91
92
		// These three can be specified multiple times
93
		$this->addOption( 'plugin', 'Load a dump plugin class. Specify as <class>[:<file>].',
94
			false, true, false, true );
95
		$this->addOption( 'output', 'Begin a filtered output stream; Specify as <type>:<file>. ' .
96
			'<type>s: file, gzip, bzip2, 7zip, dbzip2', false, true, false, true );
97
		$this->addOption( 'filter', 'Add a filter on an output branch. Specify as ' .
98
			'<type>[:<options>]. <types>s: latest, notalk, namespace', false, true, false, true );
99
		$this->addOption( 'report', 'Report position and speed after every n pages processed. ' .
100
			'Default: 100.', false, true );
101
		$this->addOption( 'server', 'Force reading from MySQL server', false, true );
102
		$this->addOption( '7ziplevel', '7zip compression level for all 7zip outputs. Used for ' .
103
			'-mx option to 7za command.', false, true );
104
105
		if ( $args ) {
106
			// Args should be loaded and processed so that dump() can be called directly
107
			// instead of execute()
108
			$this->loadWithArgv( $args );
109
			$this->processOptions();
110
		}
111
	}
112
113
	/**
114
	 * @param string $name
115
	 * @param string $class Name of output filter plugin class
116
	 */
117
	function registerOutput( $name, $class ) {
118
		$this->outputTypes[$name] = $class;
119
	}
120
121
	/**
122
	 * @param string $name
123
	 * @param string $class Name of filter plugin class
124
	 */
125
	function registerFilter( $name, $class ) {
126
		$this->filterTypes[$name] = $class;
127
	}
128
129
	/**
130
	 * Load a plugin and register it
131
	 *
132
	 * @param string $class Name of plugin class; must have a static 'register'
133
	 *   method that takes a BackupDumper as a parameter.
134
	 * @param string $file Full or relative path to the PHP file to load, or empty
135
	 */
136
	function loadPlugin( $class, $file ) {
137
		if ( $file != '' ) {
138
			require_once $file;
139
		}
140
		$register = [ $class, 'register' ];
141
		call_user_func_array( $register, [ $this ] );
142
	}
143
144
	function execute() {
145
		throw new MWException( 'execute() must be overridden in subclasses' );
146
	}
147
148
	/**
149
	 * Processes arguments and sets $this->$sink accordingly
150
	 */
151
	function processOptions() {
152
		$sink = null;
153
		$sinks = [];
154
155
		$options = $this->orderedOptions;
156
		foreach ( $options as $arg ) {
157
			$opt = $arg[0];
158
			$param = $arg[1];
159
160
			switch ( $opt ) {
161
				case 'plugin':
162
					$val = explode( ':', $param );
163
164
					if ( count( $val ) === 1 ) {
165
						$this->loadPlugin( $val[0] );
0 ignored issues
show
Bug introduced by
The call to loadPlugin() misses a required argument $file.

This check looks for function calls that miss required arguments.

Loading history...
166
					} elseif ( count( $val ) === 2 ) {
167
						$this->loadPlugin( $val[0], $val[1] );
168
					} else {
169
						$this->fatalError( 'Invalid plugin parameter' );
170
						return;
171
					}
172
173
					break;
174
				case 'output':
175
					$split = explode( ':', $param, 2 );
176
					if ( count( $split ) !== 2 ) {
177
						$this->fatalError( 'Invalid output parameter' );
178
					}
179
					list( $type, $file ) = $split;
180
					if ( !is_null( $sink ) ) {
181
						$sinks[] = $sink;
182
					}
183
					if ( !isset( $this->outputTypes[$type] ) ) {
184
						$this->fatalError( "Unrecognized output sink type '$type'" );
185
					}
186
					$class = $this->outputTypes[$type];
187
					if ( $type === "7zip" ) {
188
						$sink = new $class( $file, intval( $this->getOption( '7ziplevel' ) ) );
189
					} else {
190
						$sink = new $class( $file );
191
					}
192
193
					break;
194
				case 'filter':
195
					if ( is_null( $sink ) ) {
196
						$sink = new DumpOutput();
197
					}
198
199
					$split = explode( ':', $param );
200
					$key = $split[0];
201
202
					if ( !isset( $this->filterTypes[$key] ) ) {
203
						$this->fatalError( "Unrecognized filter type '$key'" );
204
					}
205
206
					$type = $this->filterTypes[$key];
207
208
					if ( count( $split ) === 1 ) {
209
						$filter = new $type( $sink );
210
					} elseif ( count( $split ) === 2 ) {
211
						$filter = new $type( $sink, $split[1] );
212
					} else {
213
						$this->fatalError( 'Invalid filter parameter' );
214
					}
215
216
					// references are lame in php...
217
					unset( $sink );
218
					$sink = $filter;
0 ignored issues
show
Bug introduced by
The variable $filter does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
219
220
					break;
221
			}
222
		}
223
224
		if ( $this->hasOption( 'report' ) ) {
225
			$this->reportingInterval = intval( $this->getOption( 'report' ) );
226
		}
227
228
		if ( $this->hasOption( 'server' ) ) {
229
			$this->server = $this->getOption( 'server' );
230
		}
231
232
		if ( is_null( $sink ) ) {
233
			$sink = new DumpOutput();
234
		}
235
		$sinks[] = $sink;
236
237
		if ( count( $sinks ) > 1 ) {
238
			$this->sink = new DumpMultiWriter( $sinks );
239
		} else {
240
			$this->sink = $sink;
241
		}
242
	}
243
244
	function dump( $history, $text = WikiExporter::TEXT ) {
245
		# Notice messages will foul up your XML output even if they're
246
		# relatively harmless.
247
		if ( ini_get( 'display_errors' ) ) {
248
			ini_set( 'display_errors', 'stderr' );
249
		}
250
251
		$this->initProgress( $history );
252
253
		$db = $this->backupDb();
254
		$exporter = new WikiExporter( $db, $history, WikiExporter::STREAM, $text );
255
		$exporter->dumpUploads = $this->dumpUploads;
256
		$exporter->dumpUploadFileContents = $this->dumpUploadFileContents;
257
258
		$wrapper = new ExportProgressFilter( $this->sink, $this );
259
		$exporter->setOutputSink( $wrapper );
260
261
		if ( !$this->skipHeader ) {
262
			$exporter->openStream();
263
		}
264
		# Log item dumps: all or by range
265
		if ( $history & WikiExporter::LOGS ) {
266
			if ( $this->startId || $this->endId ) {
267
				$exporter->logsByRange( $this->startId, $this->endId );
268
			} else {
269
				$exporter->allLogs();
270
			}
271
		} elseif ( is_null( $this->pages ) ) {
272
			# Page dumps: all or by page ID range
273
			if ( $this->startId || $this->endId ) {
274
				$exporter->pagesByRange( $this->startId, $this->endId );
275
			} elseif ( $this->revStartId || $this->revEndId ) {
276
				$exporter->revsByRange( $this->revStartId, $this->revEndId );
277
			} else {
278
				$exporter->allPages();
279
			}
280
		} else {
281
			# Dump of specific pages
282
			$exporter->pagesByName( $this->pages );
283
		}
284
285
		if ( !$this->skipFooter ) {
286
			$exporter->closeStream();
287
		}
288
289
		$this->report( true );
290
	}
291
292
	/**
293
	 * Initialise starting time and maximum revision count.
294
	 * We'll make ETA calculations based an progress, assuming relatively
295
	 * constant per-revision rate.
296
	 * @param int $history WikiExporter::CURRENT or WikiExporter::FULL
297
	 */
298
	function initProgress( $history = WikiExporter::FULL ) {
299
		$table = ( $history == WikiExporter::CURRENT ) ? 'page' : 'revision';
300
		$field = ( $history == WikiExporter::CURRENT ) ? 'page_id' : 'rev_id';
301
302
		$dbr = $this->forcedDb;
303
		if ( $this->forcedDb === null ) {
304
			$dbr = wfGetDB( DB_SLAVE );
305
		}
306
		$this->maxCount = $dbr->selectField( $table, "MAX($field)", '', __METHOD__ );
0 ignored issues
show
Bug introduced by
The property maxCount does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
307
		$this->startTime = microtime( true );
0 ignored issues
show
Bug introduced by
The property startTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
308
		$this->lastTime = $this->startTime;
0 ignored issues
show
Documentation Bug introduced by
The property $lastTime was declared of type integer, but $this->startTime is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
309
		$this->ID = getmypid();
310
	}
311
312
	/**
313
	 * @todo Fixme: the --server parameter is currently not respected, as it
314
	 * doesn't seem terribly easy to ask the load balancer for a particular
315
	 * connection by name.
316
	 * @return DatabaseBase
317
	 */
318
	function backupDb() {
319
		if ( $this->forcedDb !== null ) {
320
			return $this->forcedDb;
321
		}
322
323
		$this->lb = wfGetLBFactory()->newMainLB();
0 ignored issues
show
Deprecated Code introduced by
The function wfGetLBFactory() has been deprecated with message: since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead.

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...
324
		$db = $this->lb->getConnection( DB_SLAVE, 'dump' );
325
326
		// Discourage the server from disconnecting us if it takes a long time
327
		// to read out the big ol' batch query.
328
		$db->setSessionOptions( [ 'connTimeout' => 3600 * 24 ] );
329
330
		return $db;
331
	}
332
333
	/**
334
	 * Force the dump to use the provided database connection for database
335
	 * operations, wherever possible.
336
	 *
337
	 * @param DatabaseBase|null $db (Optional) the database connection to use. If null, resort to
338
	 *   use the globally provided ways to get database connections.
339
	 */
340
	function setDB( IDatabase $db = null ) {
341
		parent::setDB( $db );
0 ignored issues
show
Bug introduced by
It seems like $db defined by parameter $db on line 340 can be null; however, Maintenance::setDB() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
342
		$this->forcedDb = $db;
0 ignored issues
show
Documentation Bug introduced by
It seems like $db can also be of type object<IDatabase>. However, the property $forcedDb is declared as type object<DatabaseBase>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
343
	}
344
345
	function __destruct() {
346
		if ( isset( $this->lb ) ) {
347
			$this->lb->closeAll();
348
		}
349
	}
350
351
	function backupServer() {
352
		global $wgDBserver;
353
354
		return $this->server
355
			? $this->server
356
			: $wgDBserver;
357
	}
358
359
	function reportPage() {
360
		$this->pageCount++;
361
	}
362
363
	function revCount() {
364
		$this->revCount++;
365
		$this->report();
366
	}
367
368
	function report( $final = false ) {
369
		if ( $final xor ( $this->revCount % $this->reportingInterval == 0 ) ) {
370
			$this->showReport();
371
		}
372
	}
373
374
	function showReport() {
375
		if ( $this->reporting ) {
376
			$now = wfTimestamp( TS_DB );
377
			$nowts = microtime( true );
378
			$deltaAll = $nowts - $this->startTime;
379
			$deltaPart = $nowts - $this->lastTime;
380
			$this->pageCountPart = $this->pageCount - $this->pageCountLast;
0 ignored issues
show
Bug introduced by
The property pageCountPart does not seem to exist. Did you mean pageCount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
381
			$this->revCountPart = $this->revCount - $this->revCountLast;
0 ignored issues
show
Bug introduced by
The property revCountPart does not seem to exist. Did you mean revCount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
382
383
			if ( $deltaAll ) {
384
				$portion = $this->revCount / $this->maxCount;
385
				$eta = $this->startTime + $deltaAll / $portion;
386
				$etats = wfTimestamp( TS_DB, intval( $eta ) );
387
				$pageRate = $this->pageCount / $deltaAll;
388
				$revRate = $this->revCount / $deltaAll;
389
			} else {
390
				$pageRate = '-';
391
				$revRate = '-';
392
				$etats = '-';
393
			}
394
			if ( $deltaPart ) {
395
				$pageRatePart = $this->pageCountPart / $deltaPart;
0 ignored issues
show
Bug introduced by
The property pageCountPart does not seem to exist. Did you mean pageCount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
396
				$revRatePart = $this->revCountPart / $deltaPart;
0 ignored issues
show
Bug introduced by
The property revCountPart does not seem to exist. Did you mean revCount?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
397
			} else {
398
				$pageRatePart = '-';
399
				$revRatePart = '-';
400
			}
401
			$this->progress( sprintf(
402
				"%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), "
403
					. "%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]",
404
				$now, wfWikiID(), $this->ID, $this->pageCount, $pageRate,
405
				$pageRatePart, $this->revCount, $revRate, $revRatePart, $etats,
406
				$this->maxCount
407
			) );
408
			$this->lastTime = $nowts;
0 ignored issues
show
Documentation Bug introduced by
The property $lastTime was declared of type integer, but $nowts is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
409
			$this->revCountLast = $this->revCount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->revCount can also be of type double. However, the property $revCountLast is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
410
		}
411
	}
412
413
	function progress( $string ) {
414
		if ( $this->reporting ) {
415
			fwrite( $this->stderr, $string . "\n" );
416
		}
417
	}
418
419
	function fatalError( $msg ) {
420
		$this->error( "$msg\n", 1 );
421
	}
422
}
423
424
class ExportProgressFilter extends DumpFilter {
425
	function __construct( &$sink, &$progress ) {
426
		parent::__construct( $sink );
427
		$this->progress = $progress;
0 ignored issues
show
Bug introduced by
The property progress does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
428
	}
429
430
	function writeClosePage( $string ) {
431
		parent::writeClosePage( $string );
432
		$this->progress->reportPage();
433
	}
434
435
	function writeRevision( $rev, $string ) {
436
		parent::writeRevision( $rev, $string );
437
		$this->progress->revCount();
438
	}
439
}
440