MediaWiki 1.41.2
reassignEdits.php
Go to the documentation of this file.
1<?php
28use Wikimedia\IPUtils;
29
30require_once __DIR__ . '/Maintenance.php';
31
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Reassign edits from one user to another' );
42 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
43 $this->addOption( "norc", "Don't update the recent changes table" );
44 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
45 $this->addArg( 'from', 'Old user to take edits from' );
46 $this->addArg( 'to', 'New user to give edits to' );
47 }
48
49 public function execute() {
50 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
51 # Set up the users involved
52 $from = $this->initialiseUser( $this->getArg( 0 ) );
53 $to = $this->initialiseUser( $this->getArg( 1 ) );
54
55 # If the target doesn't exist, and --force is not set, stop here
56 if ( $to->getId() || $this->hasOption( 'force' ) ) {
57 # Reassign the edits
58 $report = $this->hasOption( 'report' );
59 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
60 # If reporting, and there were items, advise the user to run without --report
61 if ( $report ) {
62 $this->output( "Run the script again without --report to update.\n" );
63 }
64 } else {
65 $ton = $to->getName();
66 $this->error( "User '{$ton}' not found." );
67 }
68 }
69 }
70
80 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
81 $dbw = $this->getDB( DB_PRIMARY );
82 $this->beginTransaction( $dbw, __METHOD__ );
83 $actorNormalization = $this->getServiceContainer()->getActorNormalization();
84 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
85
86 # Count things
87 $this->output( "Checking current edits..." );
88 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
89 $revisionRows = $dbw->selectRowCount(
90 [ 'revision' ] + $revQueryInfo['tables'],
91 '*',
92 $revQueryInfo['conds'],
93 __METHOD__,
94 [],
95 $revQueryInfo['joins']
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 $dbw->update(
130 'revision',
131 [ 'rev_actor' => $toActorId ],
132 [ 'rev_actor' => $fromActorId ],
133 __METHOD__
134 );
135 $this->output( "done.\n" );
136 }
137
138 if ( $archiveRows ) {
139 $this->output( "Reassigning deleted edits..." );
140 $dbw->update( 'archive',
141 [ 'ar_actor' => $toActorId ],
142 [ 'ar_actor' => $fromActorId ],
143 __METHOD__
144 );
145 $this->output( "done.\n" );
146 }
147 # Update recent changes if required
148 if ( $recentChangesRows ) {
149 $this->output( "Updating recent changes..." );
150 $dbw->update( 'recentchanges',
151 [ 'rc_actor' => $toActorId ],
152 [ 'rc_actor' => $fromActorId ],
153 __METHOD__
154 );
155 $this->output( "done.\n" );
156 }
157
158 # If $from is an IP, delete any relevant rows from the
159 # ip_changes. No update needed, as $to cannot be an IP.
160 if ( !$from->isRegistered() ) {
161 $this->output( "Deleting ip_changes..." );
162 $dbw->delete(
163 'ip_changes',
164 [
165 'ipc_hex' => IPUtils::toHex( $from->getName() )
166 ],
167 __METHOD__
168 );
169 $this->output( "done.\n" );
170 }
171 }
172
173 $this->commitTransaction( $dbw, __METHOD__ );
174
175 return $total;
176 }
177
184 private function initialiseUser( $username ) {
185 $services = $this->getServiceContainer();
186 if ( $services->getUserNameUtils()->isIP( $username ) ) {
187 $user = User::newFromName( $username, false );
188 $user->getActorId();
189 } else {
190 $user = User::newFromName( $username );
191 if ( !$user ) {
192 $this->fatalError( "Invalid username" );
193 }
194 }
195 $user->load();
196
197 return $user;
198 }
199}
200
201$maintClass = ReassignEdits::class;
202require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
internal since 1.36
Definition User.php:98
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
const DB_PRIMARY
Definition defines.php:28
$maintClass