Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
65 / 65 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
RollbackEdits | |
100.00% |
65 / 65 |
|
100.00% |
3 / 3 |
14 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
40 / 40 |
|
100.00% |
1 / 1 |
11 | |||
getRollbackTitles | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 |
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\Maintenance\Maintenance; |
26 | use MediaWiki\Title\Title; |
27 | use MediaWiki\User\User; |
28 | |
29 | // @codeCoverageIgnoreStart |
30 | require_once __DIR__ . '/Maintenance.php'; |
31 | // @codeCoverageIgnoreEnd |
32 | |
33 | /** |
34 | * Maintenance script to rollback all edits by a given user or IP provided |
35 | * they're the most recent edit. |
36 | * |
37 | * @ingroup Maintenance |
38 | */ |
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 |
49 | ); |
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 ); |
54 | } |
55 | |
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' ); |
63 | } |
64 | |
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; |
75 | } |
76 | } |
77 | } else { |
78 | $titles = $this->getRollbackTitles( $user ); |
79 | } |
80 | |
81 | if ( !$titles ) { |
82 | $this->output( 'No suitable titles to be rolled back.' ); |
83 | |
84 | return; |
85 | } |
86 | |
87 | $doer = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
88 | $byUser = $services->getUserIdentityLookup()->getUserIdentityByName( $user ); |
89 | |
90 | if ( !$byUser ) { |
91 | $this->fatalError( 'Unknown user.' ); |
92 | } |
93 | |
94 | $wikiPageFactory = $services->getWikiPageFactory(); |
95 | $rollbackPageFactory = $services->getRollbackPageFactory(); |
96 | |
97 | /** @var iterable<Title[]> $titleBatches */ |
98 | $titleBatches = $this->newBatchIterator( $titles ); |
99 | |
100 | foreach ( $titleBatches as $titleBatch ) { |
101 | foreach ( $titleBatch as $title ) { |
102 | $page = $wikiPageFactory->newFromTitle( $title ); |
103 | $this->output( 'Processing ' . $title->getPrefixedText() . '...' ); |
104 | |
105 | $this->beginTransactionRound( __METHOD__ ); |
106 | $rollbackResult = $rollbackPageFactory |
107 | ->newRollbackPage( $page, $doer, $byUser ) |
108 | ->markAsBot( $bot ) |
109 | ->setSummary( $summary ) |
110 | ->rollback(); |
111 | $this->commitTransactionRound( __METHOD__ ); |
112 | |
113 | if ( $rollbackResult->isGood() ) { |
114 | $this->output( "Done!\n" ); |
115 | } else { |
116 | $this->output( "Failed!\n" ); |
117 | } |
118 | } |
119 | } |
120 | } |
121 | |
122 | /** |
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[] |
126 | */ |
127 | private function getRollbackTitles( $user ) { |
128 | $dbr = $this->getReplicaDB(); |
129 | $titles = []; |
130 | |
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 ); |
140 | } |
141 | |
142 | return $titles; |
143 | } |
144 | } |
145 | |
146 | // @codeCoverageIgnoreStart |
147 | $maintClass = RollbackEdits::class; |
148 | require_once RUN_MAINTENANCE_IF_MAIN; |
149 | // @codeCoverageIgnoreEnd |