MediaWiki REL1_40
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 = MediaWikiServices::getInstance()->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->selectRowCount(
101 [ 'archive' ],
102 '*',
103 [ 'ar_actor' => $fromActorId ],
104 __METHOD__
105 );
106 $this->output( "found {$archiveRows}.\n" );
107
108 # Don't count recent changes if we're not supposed to
109 if ( $updateRC ) {
110 $this->output( "Checking recent changes..." );
111 $recentChangesRows = $dbw->selectRowCount(
112 [ 'recentchanges' ],
113 '*',
114 [ 'rc_actor' => $fromActorId ],
115 __METHOD__
116 );
117 $this->output( "found {$recentChangesRows}.\n" );
118 } else {
119 $recentChangesRows = 0;
120 }
121
122 $total = $revisionRows + $archiveRows + $recentChangesRows;
123 $this->output( "\nTotal entries to change: {$total}\n" );
124
125 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
126 if ( !$report && $total ) {
127 $this->output( "\n" );
128 if ( $revisionRows ) {
129 # Reassign edits
130 $this->output( "Reassigning current edits..." );
131 $dbw->update(
132 'revision',
133 [ 'rev_actor' => $toActorId ],
134 [ 'rev_actor' => $fromActorId ],
135 __METHOD__
136 );
137 $this->output( "done.\n" );
138 }
139
140 if ( $archiveRows ) {
141 $this->output( "Reassigning deleted edits..." );
142 $dbw->update( 'archive',
143 [ 'ar_actor' => $toActorId ],
144 [ 'ar_actor' => $fromActorId ],
145 __METHOD__
146 );
147 $this->output( "done.\n" );
148 }
149 # Update recent changes if required
150 if ( $recentChangesRows ) {
151 $this->output( "Updating recent changes..." );
152 $dbw->update( 'recentchanges',
153 [ 'rc_actor' => $toActorId ],
154 [ 'rc_actor' => $fromActorId ],
155 __METHOD__
156 );
157 $this->output( "done.\n" );
158 }
159
160 # If $from is an IP, delete any relevant rows from the
161 # ip_changes. No update needed, as $to cannot be an IP.
162 if ( !$from->isRegistered() ) {
163 $this->output( "Deleting ip_changes..." );
164 $dbw->delete(
165 'ip_changes',
166 [
167 'ipc_hex' => IPUtils::toHex( $from->getName() )
168 ],
169 __METHOD__
170 );
171 $this->output( "done.\n" );
172 }
173 }
174
175 $this->commitTransaction( $dbw, __METHOD__ );
176
177 return $total;
178 }
179
186 private function initialiseUser( $username ) {
187 $services = MediaWikiServices::getInstance();
188 if ( $services->getUserNameUtils()->isIP( $username ) ) {
189 $user = User::newFromName( $username, false );
190 $user->getActorId();
191 } else {
192 $user = User::newFromName( $username );
193 if ( !$user ) {
194 $this->fatalError( "Invalid username" );
195 }
196 }
197 $user->load();
198
199 return $user;
200 }
201}
202
203$maintClass = ReassignEdits::class;
204require_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.
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.
Service locator for MediaWiki core services.
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
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')
Definition User.php:592
const DB_PRIMARY
Definition defines.php:28
$maintClass