MakeTestEdits::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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
 * Make test edits for a user to populate a test wiki
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
 * Make test edits for a user to populate a test wiki
27
 *
28
 * @ingroup Maintenance
29
 */
30
class MakeTestEdits extends Maintenance {
31
	public function __construct() {
32
		parent::__construct();
33
		$this->addDescription( 'Make test edits for a user' );
34
		$this->addOption( 'user', 'User name', true, true );
35
		$this->addOption( 'count', 'Number of edits', true, true );
36
		$this->addOption( 'namespace', 'Namespace number', false, true );
37
		$this->setBatchSize( 100 );
38
	}
39
40
	public function execute() {
41
		$user = User::newFromName( $this->getOption( 'user' ) );
42
		if ( !$user->getId() ) {
43
			$this->error( "No such user exists.", 1 );
44
		}
45
46
		$count = $this->getOption( 'count' );
47
		$namespace = (int)$this->getOption( 'namespace', 0 );
48
49
		for ( $i = 0; $i < $count; ++$i ) {
50
			$title = Title::makeTitleSafe( $namespace, "Page " . wfRandomString( 2 ) );
51
			$page = WikiPage::factory( $title );
0 ignored issues
show
Bug introduced by
It seems like $title defined by \Title::makeTitleSafe($n... ' . wfRandomString(2)) on line 50 can be null; however, WikiPage::factory() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52
			$content = ContentHandler::makeContent( wfRandomString(), $title );
53
			$summary = "Change " . wfRandomString( 6 );
54
55
			$page->doEditContent( $content, $summary, 0, false, $user );
0 ignored issues
show
Security Bug introduced by
It seems like $user defined by \User::newFromName($this->getOption('user')) on line 41 can also be of type false; however, WikiPage::doEditContent() does only seem to accept null|object<User>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
56
57
			$this->output( "Edited $title\n" );
58
			if ( $i && ( $i % $this->mBatchSize ) == 0 ) {
59
				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...
60
			}
61
		}
62
63
		$this->output( "Done\n" );
64
	}
65
}
66
67
$maintClass = "MakeTestEdits";
68
require_once RUN_MAINTENANCE_IF_MAIN;
69