Update git submodules
[mediawiki.git] / maintenance / rollbackEdits.php
blob934ae287f0c1843e86ade58309b1c6a7ff059915
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\Title\Title;
26 use MediaWiki\User\ActorMigration;
27 use MediaWiki\User\User;
29 require_once __DIR__ . '/Maintenance.php';
31 /**
32 * Maintenance script to rollback all edits by a given user or IP provided
33 * they're the most recent edit.
35 * @ingroup Maintenance
37 class RollbackEdits extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription(
41 "Rollback all edits by a given user or IP provided they're the most recent edit" );
42 $this->addOption(
43 'titles',
44 'A list of titles, none means all titles where the given user is the most recent',
45 false,
46 true
48 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
49 $this->addOption( 'summary', 'Edit summary to use', false, true );
50 $this->addOption( 'bot', 'Mark the edits as bot' );
53 public function execute() {
54 $user = $this->getOption( 'user' );
55 $services = $this->getServiceContainer();
56 $userNameUtils = $services->getUserNameUtils();
57 $username = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
58 if ( !$username ) {
59 $this->fatalError( 'Invalid username' );
62 $bot = $this->hasOption( 'bot' );
63 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
64 $titles = [];
65 if ( $this->hasOption( 'titles' ) ) {
66 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
67 $t = Title::newFromText( $title );
68 if ( !$t ) {
69 $this->error( 'Invalid title, ' . $title );
70 } else {
71 $titles[] = $t;
74 } else {
75 $titles = $this->getRollbackTitles( $user );
78 if ( !$titles ) {
79 $this->output( 'No suitable titles to be rolled back.' );
81 return;
84 $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
85 $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $username );
87 if ( !$byUser ) {
88 $this->fatalError( 'Unknown user.' );
91 $wikiPageFactory = $services->getWikiPageFactory();
92 $rollbackPageFactory = $services->getRollbackPageFactory();
93 foreach ( $titles as $t ) {
94 $page = $wikiPageFactory->newFromTitle( $t );
95 $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
96 $rollbackResult = $rollbackPageFactory
97 ->newRollbackPage( $page, $doer, $byUser )
98 ->markAsBot( $bot )
99 ->setSummary( $summary )
100 ->rollback();
101 if ( $rollbackResult->isGood() ) {
102 $this->output( "Done!\n" );
103 } else {
104 $this->output( "Failed!\n" );
110 * Get all pages that should be rolled back for a given user
111 * @param string $user A name to check against
112 * @return array
114 private function getRollbackTitles( $user ) {
115 $dbr = $this->getDB( DB_REPLICA );
116 $titles = [];
117 $actorQuery = ActorMigration::newMigration()
118 ->getWhere( $dbr, 'rev_user', User::newFromName( $user, false ) );
119 $results = $dbr->select(
120 [ 'page', 'revision' ] + $actorQuery['tables'],
121 [ 'page_namespace', 'page_title' ],
122 $actorQuery['conds'],
123 __METHOD__,
125 [ 'revision' => [ 'JOIN', 'page_latest = rev_id' ] ] + $actorQuery['joins']
127 foreach ( $results as $row ) {
128 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
131 return $titles;
135 $maintClass = RollbackEdits::class;
136 require_once RUN_MAINTENANCE_IF_MAIN;