Debian Codegraph @

maintenance/reassignEdits.php

raw symbols SCIP
1 <?php
2 /**
3 * Reassign edits from a user or IP address to another user
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <[email protected]>
23 * @license GPL-2.0-or-later
24 */
25
26 use MediaWiki\User\User;
27 use Wikimedia\IPUtils;
28
29 // @codeCoverageIgnoreStart
30 require_once __DIR__ . '/Maintenance.php';
31 // @codeCoverageIgnoreEnd
32
33 /**
34 * Maintenance script that reassigns edits from a user or IP address
35 * to another user.
36 *
37 * @ingroup Maintenance
38 */
39 class ReassignEdits extends Maintenance {
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Reassign edits from one user to another' );
43 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
44 $this->addOption( "norc", "Don't update the recent changes table" );
45 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
46 $this->addArg( 'from', 'Old user to take edits from' );
47 $this->addArg( 'to', 'New user to give edits to' );
48 }
49
50 public function execute() {
51 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
52 # Set up the users involved
53 $from = $this->initialiseUser( $this->getArg( 0 ) );
54 $to = $this->initialiseUser( $this->getArg( 1 ) );
55
56 # If the target doesn't exist, and --force is not set, stop here
57 if ( $to->getId() || $this->hasOption( 'force' ) ) {
58 # Reassign the edits
59 $report = $this->hasOption( 'report' );
60 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
61 # If reporting, and there were items, advise the user to run without --report
62 if ( $report ) {
63 $this->output( "Run the script again without --report to update.\n" );
64 }
65 } else {
66 $ton = $to->getName();
67 $this->error( "User '{$ton}' not found." );
68 }
69 }
70 }
71
72 /**
73 * Reassign edits from one user to another
74 *
75 * @param User &$from User to take edits from
76 * @param User &$to User to assign edits to
77 * @param bool $updateRC Update the recent changes table
78 * @param bool $report Don't change things; just echo numbers
79 * @return int The number of entries changed, or that would be changed
80 */
81 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
82 $dbw = $this->getPrimaryDB();
83 $this->beginTransaction( $dbw, __METHOD__ );
84 $actorNormalization = $this->getServiceContainer()->getActorNormalization();
85 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
86
87 # Count things
88 $this->output( "Checking current edits..." );
89
90 $revisionRows = $dbw->newSelectQueryBuilder()
91 ->select( '*' )
92 ->from( 'revision' )
93 ->where( [ 'rev_actor' => $fromActorId ] )
94 ->caller( __METHOD__ )
95 ->fetchRowCount();
96
97 $this->output( "found {$revisionRows}.\n" );
98
99 $this->output( "Checking deleted edits..." );
100 $archiveRows = $dbw->newSelectQueryBuilder()
101 ->select( '*' )
102 ->from( 'archive' )
103 ->where( [ 'ar_actor' => $fromActorId ] )
104 ->caller( __METHOD__ )->fetchRowCount();
105 $this->output( "found {$archiveRows}.\n" );
106
107 # Don't count recent changes if we're not supposed to
108 if ( $updateRC ) {
109 $this->output( "Checking recent changes..." );
110 $recentChangesRows = $dbw->newSelectQueryBuilder()
111 ->select( '*' )
112 ->from( 'recentchanges' )
113 ->where( [ 'rc_actor' => $fromActorId ] )
114 ->caller( __METHOD__ )->fetchRowCount();
115 $this->output( "found {$recentChangesRows}.\n" );
116 } else {
117 $recentChangesRows = 0;
118 }
119
120 $total = $revisionRows + $archiveRows + $recentChangesRows;
121 $this->output( "\nTotal entries to change: {$total}\n" );
122
123 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
124 if ( !$report && $total ) {
125 $this->output( "\n" );
126 if ( $revisionRows ) {
127 # Reassign edits
128 $this->output( "Reassigning current edits..." );
129
130 $dbw->newUpdateQueryBuilder()
131 ->update( 'revision' )
132 ->set( [ 'rev_actor' => $toActorId ] )
133 ->where( [ 'rev_actor' => $fromActorId ] )
134 ->caller( __METHOD__ )->execute();
135
136 $this->output( "done.\n" );
137 }
138
139 if ( $archiveRows ) {
140 $this->output( "Reassigning deleted edits..." );
141
142 $dbw->newUpdateQueryBuilder()
143 ->update( 'archive' )
144 ->set( [ 'ar_actor' => $toActorId ] )
145 ->where( [ 'ar_actor' => $fromActorId ] )
146 ->caller( __METHOD__ )->execute();
147
148 $this->output( "done.\n" );
149 }
150 # Update recent changes if required
151 if ( $recentChangesRows ) {
152 $this->output( "Updating recent changes..." );
153
154 $dbw->newUpdateQueryBuilder()
155 ->update( 'recentchanges' )
156 ->set( [ 'rc_actor' => $toActorId ] )
157 ->where( [ 'rc_actor' => $fromActorId ] )
158 ->caller( __METHOD__ )->execute();
159
160 $this->output( "done.\n" );
161 }
162
163 # If $from is an IP, delete any relevant rows from the
164 # ip_changes. No update needed, as $to cannot be an IP.
165 if ( !$from->isRegistered() ) {
166 $this->output( "Deleting ip_changes..." );
167
168 $dbw->newDeleteQueryBuilder()
169 ->deleteFrom( 'ip_changes' )
170 ->where( [ 'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
171 ->caller( __METHOD__ )->execute();
172
173 $this->output( "done.\n" );
174 }
175 }
176
177 $this->commitTransaction( $dbw, __METHOD__ );
178
179 return $total;
180 }
181
182 /**
183 * Initialise the user object
184 *
185 * @param string $username Username or IP address
186 * @return User
187 */
188 private function initialiseUser( $username ) {
189 $services = $this->getServiceContainer();
190 if ( $services->getUserNameUtils()->isIP( $username ) ) {
191 $user = User::newFromName( $username, false );
192 $user->getActorId();
193 } else {
194 $user = User::newFromName( $username );
195 if ( !$user ) {
196 $this->fatalError( "Invalid username" );
197 }
198 }
199 $user->load();
200
201 return $user;
202 }
203 }
204
205 // @codeCoverageIgnoreStart
206 $maintClass = ReassignEdits::class;
207 require_once RUN_MAINTENANCE_IF_MAIN;
208 // @codeCoverageIgnoreEnd