MediaWiki master
reassignEdits.php
Go to the documentation of this file.
1<?php
14use Wikimedia\IPUtils;
15
16// @codeCoverageIgnoreStart
17require_once __DIR__ . '/Maintenance.php';
18// @codeCoverageIgnoreEnd
19
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
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
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
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
User class for the MediaWiki software.
Definition User.php:130
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
$maintClass