Debian Codegraph @

maintenance/rollbackEdits.php

raw symbols SCIP
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)
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 use MediaWiki\Title\Title;
26 use MediaWiki\User\User;
27
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
31
32 /**
33 * Maintenance script to rollback all edits by a given user or IP provided
34 * they're the most recent edit.
35 *
36 * @ingroup Maintenance
37 */
38 class RollbackEdits extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription(
42 "Rollback all edits by a given user or IP provided they're the most recent edit" );
43 $this->addOption(
44 'titles',
45 'A list of titles, none means all titles where the given user is the most recent',
46 false,
47 true
48 );
49 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
50 $this->addOption( 'summary', 'Edit summary to use', false, true );
51 $this->addOption( 'bot', 'Mark the edits as bot' );
52 }
53
54 public function execute() {
55 $user = $this->getOption( 'user' );
56 $services = $this->getServiceContainer();
57 $userNameUtils = $services->getUserNameUtils();
58 $user = $userNameUtils->isIP( $user ) ? $user : $userNameUtils->getCanonical( $user );
59 if ( !$user ) {
60 $this->fatalError( 'Invalid username' );
61 }
62
63 $bot = $this->hasOption( 'bot' );
64 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
65 $titles = [];
66 if ( $this->hasOption( 'titles' ) ) {
67 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
68 $t = Title::newFromText( $title );
69 if ( !$t ) {
70 $this->error( 'Invalid title, ' . $title );
71 } else {
72 $titles[] = $t;
73 }
74 }
75 } else {
76 $titles = $this->getRollbackTitles( $user );
77 }
78
79 if ( !$titles ) {
80 $this->output( 'No suitable titles to be rolled back.' );
81
82 return;
83 }
84
85 $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
86 $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user );
87
88 if ( !$byUser ) {
89 $this->fatalError( 'Unknown user.' );
90 }
91
92 $wikiPageFactory = $services->getWikiPageFactory();
93 $rollbackPageFactory = $services->getRollbackPageFactory();
94 foreach ( $titles as $t ) {
95 $page = $wikiPageFactory->newFromTitle( $t );
96 $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
97 $rollbackResult = $rollbackPageFactory
98 ->newRollbackPage( $page, $doer, $byUser )
99 ->markAsBot( $bot )
100 ->setSummary( $summary )
101 ->rollback();
102 if ( $rollbackResult->isGood() ) {
103 $this->output( "Done!\n" );
104 } else {
105 $this->output( "Failed!\n" );
106 }
107 }
108 }
109
110 /**
111 * Get all pages that should be rolled back for a given user
112 * @param string $user A name to check against
113 * @return array
114 */
115 private function getRollbackTitles( $user ) {
116 $dbr = $this->getReplicaDB();
117 $titles = [];
118
119 $results = $dbr->newSelectQueryBuilder()
120 ->select( [ 'page_namespace', 'page_title' ] )
121 ->from( 'page' )
122 ->join( 'revision', null, 'page_latest = rev_id' )
123 ->join( 'actor', null, 'rev_actor = actor_id' )
124 ->where( [ 'actor_name' => $user ] )
125 ->caller( __METHOD__ )->fetchResultSet();
126 foreach ( $results as $row ) {
127 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
128 }
129
130 return $titles;
131 }
132 }
133
134 // @codeCoverageIgnoreStart
135 $maintClass = RollbackEdits::class;
136 require_once RUN_MAINTENANCE_IF_MAIN;
137 // @codeCoverageIgnoreEnd