MediaWiki REL1_39
reassignEdits.php
Go to the documentation of this file.
1<?php
27use Wikimedia\IPUtils;
28
29require_once __DIR__ . '/Maintenance.php';
30
38 public function __construct() {
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' );
46 }
47
48 public function execute() {
49 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
50 # Set up the users involved
51 $from = $this->initialiseUser( $this->getArg( 0 ) );
52 $to = $this->initialiseUser( $this->getArg( 1 ) );
53
54 # If the target doesn't exist, and --force is not set, stop here
55 if ( $to->getId() || $this->hasOption( 'force' ) ) {
56 # Reassign the edits
57 $report = $this->hasOption( 'report' );
58 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
59 # If reporting, and there were items, advise the user to run without --report
60 if ( $report ) {
61 $this->output( "Run the script again without --report to update.\n" );
62 }
63 } else {
64 $ton = $to->getName();
65 $this->error( "User '{$ton}' not found." );
66 }
67 }
68 }
69
79 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
80 $dbw = $this->getDB( DB_PRIMARY );
81 $this->beginTransaction( $dbw, __METHOD__ );
82 $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
83 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
84
85 # Count things
86 $this->output( "Checking current edits..." );
87 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
88 $revisionRows = $dbw->selectRowCount(
89 [ 'revision' ] + $revQueryInfo['tables'],
90 '*',
91 $revQueryInfo['conds'],
92 __METHOD__,
93 [],
94 $revQueryInfo['joins']
95 );
96 $this->output( "found {$revisionRows}.\n" );
97
98 $this->output( "Checking deleted edits..." );
99 $archiveRows = $dbw->selectRowCount(
100 [ 'archive' ],
101 '*',
102 [ 'ar_actor' => $fromActorId ],
103 __METHOD__
104 );
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->selectRowCount(
111 [ 'recentchanges' ],
112 '*',
113 [ 'rc_actor' => $fromActorId ],
114 __METHOD__
115 );
116 $this->output( "found {$recentChangesRows}.\n" );
117 } else {
118 $recentChangesRows = 0;
119 }
120
121 $total = $revisionRows + $archiveRows + $recentChangesRows;
122 $this->output( "\nTotal entries to change: {$total}\n" );
123
124 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
125 if ( !$report && $total ) {
126 $this->output( "\n" );
127 if ( $revisionRows ) {
128 # Reassign edits
129 $this->output( "Reassigning current edits..." );
130 $dbw->update(
131 'revision',
132 [ 'rev_actor' => $toActorId ],
133 [ 'rev_actor' => $fromActorId ],
134 __METHOD__
135 );
136 $this->output( "done.\n" );
137 }
138
139 if ( $archiveRows ) {
140 $this->output( "Reassigning deleted edits..." );
141 $dbw->update( 'archive',
142 [ 'ar_actor' => $toActorId ],
143 [ 'ar_actor' => $fromActorId ],
144 __METHOD__
145 );
146 $this->output( "done.\n" );
147 }
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 ],
154 __METHOD__
155 );
156 $this->output( "done.\n" );
157 }
158
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..." );
163 $dbw->delete(
164 'ip_changes',
165 [
166 'ipc_hex' => IPUtils::toHex( $from->getName() )
167 ],
168 __METHOD__
169 );
170 $this->output( "done.\n" );
171 }
172 }
173
174 $this->commitTransaction( $dbw, __METHOD__ );
175
176 return $total;
177 }
178
185 private function initialiseUser( $username ) {
186 $services = MediaWikiServices::getInstance();
187 if ( $services->getUserNameUtils()->isIP( $username ) ) {
188 $user = User::newFromName( $username, false );
189 $user->getActorId();
190 } else {
191 $user = User::newFromName( $username );
192 if ( !$user ) {
193 $this->fatalError( "Invalid username" );
194 }
195 }
196 $user->load();
197
198 return $user;
199 }
200}
201
202$maintClass = ReassignEdits::class;
203require_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)
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.
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:598
const DB_PRIMARY
Definition defines.php:28
$maintClass