30require_once __DIR__ .
'/Maintenance.php';
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' );
51 # Set up the users involved
52 $from = $this->initialiseUser( $this->
getArg( 0 ) );
53 $to = $this->initialiseUser( $this->
getArg( 1 ) );
55 # If the target doesn't exist, and --force is not set, stop here
56 if ( $to->getId() || $this->hasOption(
'force' ) ) {
59 $this->doReassignEdits( $from, $to, !$this->
hasOption(
'norc' ), $report );
60 # If reporting, and there were items, advise the user to run without --report
62 $this->
output(
"Run the script again without --report to update.\n" );
65 $ton = $to->getName();
66 $this->
error(
"User '{$ton}' not found." );
80 private function doReassignEdits( &$from, &$to, $updateRC =
false, $report =
false ) {
83 $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
84 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
87 $this->
output(
"Checking current edits..." );
88 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw,
'rev_user', $from );
89 $revisionRows = $dbw->selectRowCount(
90 [
'revision' ] + $revQueryInfo[
'tables'],
92 $revQueryInfo[
'conds'],
95 $revQueryInfo[
'joins']
97 $this->
output(
"found {$revisionRows}.\n" );
99 $this->
output(
"Checking deleted edits..." );
100 $archiveRows = $dbw->selectRowCount(
103 [
'ar_actor' => $fromActorId ],
106 $this->
output(
"found {$archiveRows}.\n" );
108 # Don't count recent changes if we're not supposed to
110 $this->
output(
"Checking recent changes..." );
111 $recentChangesRows = $dbw->selectRowCount(
114 [
'rc_actor' => $fromActorId ],
117 $this->
output(
"found {$recentChangesRows}.\n" );
119 $recentChangesRows = 0;
122 $total = $revisionRows + $archiveRows + $recentChangesRows;
123 $this->
output(
"\nTotal entries to change: {$total}\n" );
125 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
126 if ( !$report && $total ) {
128 if ( $revisionRows ) {
130 $this->
output(
"Reassigning current edits..." );
133 [
'rev_actor' => $toActorId ],
134 [
'rev_actor' => $fromActorId ],
137 $this->
output(
"done.\n" );
140 if ( $archiveRows ) {
141 $this->
output(
"Reassigning deleted edits..." );
142 $dbw->update(
'archive',
143 [
'ar_actor' => $toActorId ],
144 [
'ar_actor' => $fromActorId ],
147 $this->
output(
"done.\n" );
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 ],
157 $this->
output(
"done.\n" );
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..." );
167 'ipc_hex' => IPUtils::toHex( $from->getName() )
171 $this->
output(
"done.\n" );
186 private function initialiseUser( $username ) {
187 $services = MediaWikiServices::getInstance();
188 if ( $services->getUserNameUtils()->isIP( $username ) ) {
204require_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, $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.
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')