41 parent::__construct();
42 $this->
addDescription(
'Reassign edits from one user to another' );
43 $this->
addOption(
"force",
"Reassign even if the target user doesn't exist" );
44 $this->
addOption(
"norc",
"Don't update the recent changes table" );
45 $this->
addOption(
"report",
"Print out details of what would be changed, but don't update it" );
46 $this->
addArg(
'from',
'Old user to take edits from' );
47 $this->
addArg(
'to',
'New user to give edits to' );
52 # Set up the users involved
53 $from = $this->initialiseUser( $this->
getArg( 0 ) );
54 $to = $this->initialiseUser( $this->
getArg( 1 ) );
56 # If the target doesn't exist, and --force is not set, stop here
57 if ( $to->getId() || $this->hasOption(
'force' ) ) {
60 $this->doReassignEdits( $from, $to, !$this->
hasOption(
'norc' ), $report );
61 # If reporting, and there were items, advise the user to run without --report
63 $this->
output(
"Run the script again without --report to update.\n" );
66 $ton = $to->getName();
67 $this->
error(
"User '{$ton}' not found." );
81 private function doReassignEdits( &$from, &$to, $updateRC =
false, $report =
false ) {
85 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
88 $this->
output(
"Checking current edits..." );
90 $revisionRows = $dbw->newSelectQueryBuilder()
93 ->where( [
'rev_actor' => $fromActorId ] )
94 ->caller( __METHOD__ )
97 $this->
output(
"found {$revisionRows}.\n" );
99 $this->
output(
"Checking deleted edits..." );
100 $archiveRows = $dbw->newSelectQueryBuilder()
103 ->where( [
'ar_actor' => $fromActorId ] )
104 ->caller( __METHOD__ )->fetchRowCount();
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->newSelectQueryBuilder()
112 ->from(
'recentchanges' )
113 ->where( [
'rc_actor' => $fromActorId ] )
114 ->caller( __METHOD__ )->fetchRowCount();
115 $this->
output(
"found {$recentChangesRows}.\n" );
117 $recentChangesRows = 0;
120 $total = $revisionRows + $archiveRows + $recentChangesRows;
121 $this->
output(
"\nTotal entries to change: {$total}\n" );
123 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
124 if ( !$report && $total ) {
126 if ( $revisionRows ) {
128 $this->
output(
"Reassigning current edits..." );
130 $dbw->newUpdateQueryBuilder()
131 ->update(
'revision' )
132 ->set( [
'rev_actor' => $toActorId ] )
133 ->where( [
'rev_actor' => $fromActorId ] )
134 ->caller( __METHOD__ )->execute();
136 $this->
output(
"done.\n" );
139 if ( $archiveRows ) {
140 $this->
output(
"Reassigning deleted edits..." );
142 $dbw->newUpdateQueryBuilder()
143 ->update(
'archive' )
144 ->set( [
'ar_actor' => $toActorId ] )
145 ->where( [
'ar_actor' => $fromActorId ] )
146 ->caller( __METHOD__ )->execute();
148 $this->
output(
"done.\n" );
150 # Update recent changes if required
151 if ( $recentChangesRows ) {
152 $this->
output(
"Updating recent changes..." );
154 $dbw->newUpdateQueryBuilder()
155 ->update(
'recentchanges' )
156 ->set( [
'rc_actor' => $toActorId ] )
157 ->where( [
'rc_actor' => $fromActorId ] )
158 ->caller( __METHOD__ )->execute();
160 $this->
output(
"done.\n" );
163 # If $from is an IP, delete any relevant rows from the
164 # ip_changes. No update needed, as $to cannot be an IP.
165 if ( !$from->isRegistered() ) {
166 $this->
output(
"Deleting ip_changes..." );
168 $dbw->newDeleteQueryBuilder()
169 ->deleteFrom(
'ip_changes' )
170 ->where( [
'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
171 ->caller( __METHOD__ )->execute();
173 $this->
output(
"done.\n" );
188 private function initialiseUser( $username ) {
190 if ( $services->getUserNameUtils()->isIP( $username ) ) {
191 $user = User::newFromName( $username,
false );
194 $user = User::newFromName( $username );
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.