29require_once __DIR__ .
'/Maintenance.php';
39 parent::__construct();
40 $this->
addDescription(
'Reassign edits from one user to another' );
41 $this->
addOption(
"force",
"Reassign even if the target user doesn't exist" );
42 $this->
addOption(
"norc",
"Don't update the recent changes table" );
43 $this->
addOption(
"report",
"Print out details of what would be changed, but don't update it" );
44 $this->
addArg(
'from',
'Old user to take edits from' );
45 $this->
addArg(
'to',
'New user to give edits to' );
50 # Set up the users involved
51 $from = $this->initialiseUser( $this->
getArg( 0 ) );
52 $to = $this->initialiseUser( $this->
getArg( 1 ) );
54 # If the target doesn't exist, and --force is not set, stop here
55 if ( $to->getId() || $this->hasOption(
'force' ) ) {
58 $this->doReassignEdits( $from, $to, !$this->
hasOption(
'norc' ), $report );
59 # If reporting, and there were items, advise the user to run without --report
61 $this->
output(
"Run the script again without --report to update.\n" );
64 $ton = $to->getName();
65 $this->
error(
"User '{$ton}' not found." );
79 private function doReassignEdits( &$from, &$to, $updateRC =
false, $report =
false ) {
82 $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
83 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
86 $this->
output(
"Checking current edits..." );
87 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw,
'rev_user', $from );
88 $revisionRows = $dbw->selectRowCount(
89 [
'revision' ] + $revQueryInfo[
'tables'],
91 $revQueryInfo[
'conds'],
94 $revQueryInfo[
'joins']
96 $this->
output(
"found {$revisionRows}.\n" );
98 $this->
output(
"Checking deleted edits..." );
99 $archiveRows = $dbw->selectRowCount(
102 [
'ar_actor' => $fromActorId ],
105 $this->
output(
"found {$archiveRows}.\n" );
107 # Don't count recent changes if we're not supposed to
109 $this->
output(
"Checking recent changes..." );
110 $recentChangesRows = $dbw->selectRowCount(
113 [
'rc_actor' => $fromActorId ],
116 $this->
output(
"found {$recentChangesRows}.\n" );
118 $recentChangesRows = 0;
121 $total = $revisionRows + $archiveRows + $recentChangesRows;
122 $this->
output(
"\nTotal entries to change: {$total}\n" );
124 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
125 if ( !$report && $total ) {
127 if ( $revisionRows ) {
129 $this->
output(
"Reassigning current edits..." );
132 [
'rev_actor' => $toActorId ],
133 [
'rev_actor' => $fromActorId ],
136 $this->
output(
"done.\n" );
139 if ( $archiveRows ) {
140 $this->
output(
"Reassigning deleted edits..." );
141 $dbw->update(
'archive',
142 [
'ar_actor' => $toActorId ],
143 [
'ar_actor' => $fromActorId ],
146 $this->
output(
"done.\n" );
148 # Update recent changes if required
149 if ( $recentChangesRows ) {
150 $this->
output(
"Updating recent changes..." );
151 $dbw->update(
'recentchanges',
152 [
'rc_actor' => $toActorId ],
153 [
'rc_actor' => $fromActorId ],
156 $this->
output(
"done.\n" );
159 # If $from is an IP, delete any relevant rows from the
160 # ip_changes. No update needed, as $to cannot be an IP.
161 if ( !$from->isRegistered() ) {
162 $this->
output(
"Deleting ip_changes..." );
166 'ipc_hex' => IPUtils::toHex( $from->getName() )
170 $this->
output(
"done.\n" );
185 private function initialiseUser( $username ) {
186 $services = MediaWikiServices::getInstance();
187 if ( $services->getUserNameUtils()->isIP( $username ) ) {
203require_once RUN_MAINTENANCE_IF_MAIN;
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)
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.
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.
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
static newFromName( $name, $validate='valid')