aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/rollbackEdits.php
blob: 688741bc174a785fdecc81a661d32bd00ecbc9f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
 * Rollback all edits by a given user or IP provided they're the most
 * recent edit (just like real rollback)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

use MediaWiki\Maintenance\Maintenance;
use MediaWiki\Title\Title;
use MediaWiki\User\User;

// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd

/**
 * Maintenance script to rollback all edits by a given user or IP provided
 * they're the most recent edit.
 *
 * @ingroup Maintenance
 */
class RollbackEdits extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription(
			"Rollback all edits by a given user or IP provided they're the most recent edit" );
		$this->addOption(
			'titles',
			'A list of titles, none means all titles where the given user is the most recent',
			false,
			true
		);
		$this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
		$this->addOption( 'summary', 'Edit summary to use', false, true );
		$this->addOption( 'bot', 'Mark the edits as bot' );
		$this->setBatchSize( 10 );
	}

	public function execute() {
		$user = $this->getOption( 'user' );
		$services = $this->getServiceContainer();
		$userNameUtils = $services->getUserNameUtils();
		$user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
		if ( !$user ) {
			$this->fatalError( 'Invalid username' );
		}

		$bot = $this->hasOption( 'bot' );
		$summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
		$titles = [];
		if ( $this->hasOption( 'titles' ) ) {
			foreach ( explode( '|', $this->getOption( 'titles' ) ) as $text ) {
				$title = Title::newFromText( $text );
				if ( !$title ) {
					$this->error( 'Invalid title, ' . $text );
				} else {
					$titles[] = $title;
				}
			}
		} else {
			$titles = $this->getRollbackTitles( $user );
		}

		if ( !$titles ) {
			$this->output( 'No suitable titles to be rolled back.' );

			return;
		}

		$doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
		$byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user );

		if ( !$byUser ) {
			$this->fatalError( 'Unknown user.' );
		}

		$wikiPageFactory = $services->getWikiPageFactory();
		$rollbackPageFactory = $services->getRollbackPageFactory();

		/** @var iterable<Title[]> $titleBatches */
		$titleBatches = $this->newBatchIterator( $titles );

		foreach ( $titleBatches as $titleBatch ) {
			foreach ( $titleBatch as $title ) {
				$page = $wikiPageFactory->newFromTitle( $title );
				$this->output( 'Processing ' . $title->getPrefixedText() . '...' );

				$this->beginTransactionRound( __METHOD__ );
				$rollbackResult = $rollbackPageFactory
					->newRollbackPage( $page, $doer, $byUser )
					->markAsBot( $bot )
					->setSummary( $summary )
					->rollback();
				$this->commitTransactionRound( __METHOD__ );

				if ( $rollbackResult->isGood() ) {
					$this->output( "Done!\n" );
				} else {
					$this->output( "Failed!\n" );
				}
			}
		}
	}

	/**
	 * Get all pages that should be rolled back for a given user
	 * @param string $user A name to check against
	 * @return Title[]
	 */
	private function getRollbackTitles( $user ) {
		$dbr = $this->getReplicaDB();
		$titles = [];

		$results = $dbr->newSelectQueryBuilder()
			->select( [ 'page_namespace', 'page_title' ] )
			->from( 'page' )
			->join( 'revision', null, 'page_latest = rev_id' )
			->join( 'actor', null, 'rev_actor = actor_id' )
			->where( [ 'actor_name' => $user ] )
			->caller( __METHOD__ )->fetchResultSet();
		foreach ( $results as $row ) {
			$titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
		}

		return $titles;
	}
}

// @codeCoverageIgnoreStart
$maintClass = RollbackEdits::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd