Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.87% covered (success)
97.87%
92 / 94
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReassignEdits
97.87% covered (success)
97.87%
92 / 94
50.00% covered (danger)
50.00%
2 / 4
18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 execute
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
6.02
 doReassignEdits
98.48% covered (success)
98.48%
65 / 66
0.00% covered (danger)
0.00%
0 / 1
8
 initialiseUser
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Reassign edits from a user or IP address to another user
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 * @license GPL-2.0-or-later
10 */
11
12use MediaWiki\Maintenance\Maintenance;
13use MediaWiki\User\User;
14use Wikimedia\IPUtils;
15
16// @codeCoverageIgnoreStart
17require_once __DIR__ . '/Maintenance.php';
18// @codeCoverageIgnoreEnd
19
20/**
21 * Maintenance script that reassigns edits from a user or IP address
22 * to another user.
23 *
24 * @ingroup Maintenance
25 */
26class ReassignEdits extends Maintenance {
27    public function __construct() {
28        parent::__construct();
29        $this->addDescription( 'Reassign edits from one user to another' );
30        $this->addOption( "force", "Reassign even if the target user doesn't exist" );
31        $this->addOption( "norc", "Don't update the recent changes table" );
32        $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
33        $this->addArg( 'from', 'Old user to take edits from' );
34        $this->addArg( 'to', 'New user to give edits to' );
35    }
36
37    public function execute() {
38        # Set up the users involved
39        $from = $this->initialiseUser( $this->getArg( 0 ) );
40        $to = $this->initialiseUser( $this->getArg( 1 ) );
41
42        // Reject attempts to re-assign to an IP address. This is done because the script does not
43        // populate ip_changes and it breaks if temporary accounts are enabled (T373914).
44        if ( IPUtils::isIPAddress( $to->getName() ) ) {
45            $this->fatalError( 'Script does not support re-assigning to another IP.' );
46        } elseif ( $from->equals( $to ) ) {
47            $this->fatalError( 'The from and to user cannot be the same.' );
48        }
49
50        # If the target doesn't exist, and --force is not set, stop here
51        if ( $to->getId() || $this->hasOption( 'force' ) ) {
52            # Reassign the edits
53            $report = $this->hasOption( 'report' );
54            $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
55            # If reporting, and there were items, advise the user to run without --report
56            if ( $report ) {
57                $this->output( "Run the script again without --report to update.\n" );
58            }
59        } else {
60            $this->fatalError( "User '{$to->getName()}' not found." );
61        }
62    }
63
64    /**
65     * Reassign edits from one user to another
66     *
67     * @param User &$from User to take edits from
68     * @param User &$to User to assign edits to
69     * @param bool $updateRC Update the recent changes table
70     * @param bool $report Don't change things; just echo numbers
71     * @return int The number of entries changed, or that would be changed
72     */
73    private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
74        $dbw = $this->getPrimaryDB();
75        $this->beginTransactionRound( __METHOD__ );
76        $actorNormalization = $this->getServiceContainer()->getActorNormalization();
77        $fromActorId = $actorNormalization->findActorId( $from, $dbw );
78
79        # Count things
80        $this->output( "Checking current edits..." );
81
82        $revisionRows = $dbw->newSelectQueryBuilder()
83            ->select( '*' )
84            ->from( 'revision' )
85            ->where( [ 'rev_actor' => $fromActorId ] )
86            ->caller( __METHOD__ )
87            ->fetchRowCount();
88
89        $this->output( "found {$revisionRows}.\n" );
90
91        $this->output( "Checking deleted edits..." );
92        $archiveRows = $dbw->newSelectQueryBuilder()
93            ->select( '*' )
94            ->from( 'archive' )
95            ->where( [ 'ar_actor' => $fromActorId ] )
96            ->caller( __METHOD__ )->fetchRowCount();
97        $this->output( "found {$archiveRows}.\n" );
98
99        # Don't count recent changes if we're not supposed to
100        if ( $updateRC ) {
101            $this->output( "Checking recent changes..." );
102            $recentChangesRows = $dbw->newSelectQueryBuilder()
103                ->select( '*' )
104                ->from( 'recentchanges' )
105                ->where( [ 'rc_actor' => $fromActorId ] )
106                ->caller( __METHOD__ )->fetchRowCount();
107            $this->output( "found {$recentChangesRows}.\n" );
108        } else {
109            $recentChangesRows = 0;
110        }
111
112        $total = $revisionRows + $archiveRows + $recentChangesRows;
113        $this->output( "\nTotal entries to change: {$total}\n" );
114
115        $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
116        if ( !$report && $total ) {
117            $this->output( "\n" );
118            if ( $revisionRows ) {
119                # Reassign edits
120                $this->output( "Reassigning current edits..." );
121
122                $dbw->newUpdateQueryBuilder()
123                    ->update( 'revision' )
124                    ->set( [ 'rev_actor' => $toActorId ] )
125                    ->where( [ 'rev_actor' => $fromActorId ] )
126                    ->caller( __METHOD__ )->execute();
127
128                $this->output( "done.\n" );
129            }
130
131            if ( $archiveRows ) {
132                $this->output( "Reassigning deleted edits..." );
133
134                $dbw->newUpdateQueryBuilder()
135                    ->update( 'archive' )
136                    ->set( [ 'ar_actor' => $toActorId ] )
137                    ->where( [ 'ar_actor' => $fromActorId ] )
138                    ->caller( __METHOD__ )->execute();
139
140                $this->output( "done.\n" );
141            }
142            # Update recent changes if required
143            if ( $recentChangesRows ) {
144                $this->output( "Updating recent changes..." );
145
146                $dbw->newUpdateQueryBuilder()
147                    ->update( 'recentchanges' )
148                    ->set( [ 'rc_actor' => $toActorId ] )
149                    ->where( [ 'rc_actor' => $fromActorId ] )
150                    ->caller( __METHOD__ )->execute();
151
152                $this->output( "done.\n" );
153            }
154
155            # If $from is an IP, delete any relevant rows from the
156            # ip_changes. No update needed, as $to cannot be an IP.
157            if ( !$from->isRegistered() ) {
158                $this->output( "Deleting ip_changes..." );
159
160                $dbw->newDeleteQueryBuilder()
161                    ->deleteFrom( 'ip_changes' )
162                    ->where( [ 'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
163                    ->caller( __METHOD__ )->execute();
164
165                $this->output( "done.\n" );
166            }
167        }
168
169        $this->commitTransactionRound( __METHOD__ );
170
171        return $total;
172    }
173
174    /**
175     * Initialise the user object
176     *
177     * @param string $username Username or IP address
178     * @return User
179     */
180    private function initialiseUser( $username ) {
181        $services = $this->getServiceContainer();
182        if ( $services->getUserNameUtils()->isIP( $username ) ) {
183            $user = User::newFromName( $username, false );
184            $user->getActorId();
185        } else {
186            $user = User::newFromName( $username );
187            if ( !$user ) {
188                $this->fatalError( "Invalid username" );
189            }
190        }
191        $user->load();
192
193        return $user;
194    }
195}
196
197// @codeCoverageIgnoreStart
198$maintClass = ReassignEdits::class;
199require_once RUN_MAINTENANCE_IF_MAIN;
200// @codeCoverageIgnoreEnd