RollbackEdits   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
D execute() 0 42 9
A getRollbackTitles() 0 15 2
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 33 and the first side effect is on line 25.

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
 * Rollback all edits by a given user or IP provided they're the most
4
 * recent edit (just like real rollback)
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 * http://www.gnu.org/copyleft/gpl.html
20
 *
21
 * @file
22
 * @ingroup Maintenance
23
 */
24
25
require_once __DIR__ . '/Maintenance.php';
26
27
/**
28
 * Maintenance script to rollback all edits by a given user or IP provided
29
 * they're the most recent edit.
30
 *
31
 * @ingroup Maintenance
32
 */
33
class RollbackEdits extends Maintenance {
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addDescription(
37
			"Rollback all edits by a given user or IP provided they're the most recent edit" );
38
		$this->addOption(
39
			'titles',
40
			'A list of titles, none means all titles where the given user is the most recent',
41
			false,
42
			true
43
		);
44
		$this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
45
		$this->addOption( 'summary', 'Edit summary to use', false, true );
46
		$this->addOption( 'bot', 'Mark the edits as bot' );
47
	}
48
49
	public function execute() {
50
		$user = $this->getOption( 'user' );
51
		$username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
52
		if ( !$username ) {
53
			$this->error( 'Invalid username', true );
54
		}
55
56
		$bot = $this->hasOption( 'bot' );
57
		$summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
58
		$titles = [];
59
		$results = [];
60
		if ( $this->hasOption( 'titles' ) ) {
61
			foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
62
				$t = Title::newFromText( $title );
63
				if ( !$t ) {
64
					$this->error( 'Invalid title, ' . $title );
65
				} else {
66
					$titles[] = $t;
67
				}
68
			}
69
		} else {
70
			$titles = $this->getRollbackTitles( $user );
71
		}
72
73
		if ( !$titles ) {
74
			$this->output( 'No suitable titles to be rolled back' );
75
76
			return;
77
		}
78
79
		$doer = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
80
81
		foreach ( $titles as $t ) {
82
			$page = WikiPage::factory( $t );
83
			$this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
84
			if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
0 ignored issues
show
Bug introduced by
It seems like $doer defined by \User::newSystemUser('Ma...array('steal' => true)) on line 79 can be null; however, WikiPage::commitRollback() 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...
85
				$this->output( "done\n" );
86
			} else {
87
				$this->output( "failed\n" );
88
			}
89
		}
90
	}
91
92
	/**
93
	 * Get all pages that should be rolled back for a given user
94
	 * @param string $user A name to check against rev_user_text
95
	 * @return array
96
	 */
97
	private function getRollbackTitles( $user ) {
98
		$dbr = $this->getDB( DB_REPLICA );
99
		$titles = [];
100
		$results = $dbr->select(
101
			[ 'page', 'revision' ],
102
			[ 'page_namespace', 'page_title' ],
103
			[ 'page_latest = rev_id', 'rev_user_text' => $user ],
104
			__METHOD__
105
		);
106
		foreach ( $results as $row ) {
107
			$titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
108
		}
109
110
		return $titles;
111
	}
112
}
113
114
$maintClass = 'RollbackEdits';
115
require_once RUN_MAINTENANCE_IF_MAIN;
116