Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / rollbackEdits.php
blob688741bc174a785fdecc81a661d32bd00ecbc9f3
1 <?php
2 /**
3 * Rollback all edits by a given user or IP provided they're the most
4 * recent edit (just like real rollback)
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.
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.
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
21 * @file
22 * @ingroup Maintenance
25 use MediaWiki\Maintenance\Maintenance;
26 use MediaWiki\Title\Title;
27 use MediaWiki\User\User;
29 // @codeCoverageIgnoreStart
30 require_once __DIR__ . '/Maintenance.php';
31 // @codeCoverageIgnoreEnd
33 /**
34 * Maintenance script to rollback all edits by a given user or IP provided
35 * they're the most recent edit.
37 * @ingroup Maintenance
39 class RollbackEdits extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription(
43 "Rollback all edits by a given user or IP provided they're the most recent edit" );
44 $this->addOption(
45 'titles',
46 'A list of titles, none means all titles where the given user is the most recent',
47 false,
48 true
50 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
51 $this->addOption( 'summary', 'Edit summary to use', false, true );
52 $this->addOption( 'bot', 'Mark the edits as bot' );
53 $this->setBatchSize( 10 );
56 public function execute() {
57 $user = $this->getOption( 'user' );
58 $services = $this->getServiceContainer();
59 $userNameUtils = $services->getUserNameUtils();
60 $user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
61 if ( !$user ) {
62 $this->fatalError( 'Invalid username' );
65 $bot = $this->hasOption( 'bot' );
66 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
67 $titles = [];
68 if ( $this->hasOption( 'titles' ) ) {
69 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $text ) {
70 $title = Title::newFromText( $text );
71 if ( !$title ) {
72 $this->error( 'Invalid title, ' . $text );
73 } else {
74 $titles[] = $title;
77 } else {
78 $titles = $this->getRollbackTitles( $user );
81 if ( !$titles ) {
82 $this->output( 'No suitable titles to be rolled back.' );
84 return;
87 $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
88 $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user );
90 if ( !$byUser ) {
91 $this->fatalError( 'Unknown user.' );
94 $wikiPageFactory = $services->getWikiPageFactory();
95 $rollbackPageFactory = $services->getRollbackPageFactory();
97 /** @var iterable<Title[]> $titleBatches */
98 $titleBatches = $this->newBatchIterator( $titles );
100 foreach ( $titleBatches as $titleBatch ) {
101 foreach ( $titleBatch as $title ) {
102 $page = $wikiPageFactory->newFromTitle( $title );
103 $this->output( 'Processing ' . $title->getPrefixedText() . '...' );
105 $this->beginTransactionRound( __METHOD__ );
106 $rollbackResult = $rollbackPageFactory
107 ->newRollbackPage( $page, $doer, $byUser )
108 ->markAsBot( $bot )
109 ->setSummary( $summary )
110 ->rollback();
111 $this->commitTransactionRound( __METHOD__ );
113 if ( $rollbackResult->isGood() ) {
114 $this->output( "Done!\n" );
115 } else {
116 $this->output( "Failed!\n" );
123 * Get all pages that should be rolled back for a given user
124 * @param string $user A name to check against
125 * @return Title[]
127 private function getRollbackTitles( $user ) {
128 $dbr = $this->getReplicaDB();
129 $titles = [];
131 $results = $dbr->newSelectQueryBuilder()
132 ->select( [ 'page_namespace', 'page_title' ] )
133 ->from( 'page' )
134 ->join( 'revision', null, 'page_latest = rev_id' )
135 ->join( 'actor', null, 'rev_actor = actor_id' )
136 ->where( [ 'actor_name' => $user ] )
137 ->caller( __METHOD__ )->fetchResultSet();
138 foreach ( $results as $row ) {
139 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
142 return $titles;
146 // @codeCoverageIgnoreStart
147 $maintClass = RollbackEdits::class;
148 require_once RUN_MAINTENANCE_IF_MAIN;
149 // @codeCoverageIgnoreEnd