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 ) {
83 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
86 $this->
output(
"Checking current edits..." );
88 $revisionRows = $dbw->newSelectQueryBuilder()
91 ->where( [
'rev_actor' => $fromActorId ] )
92 ->caller( __METHOD__ )
95 $this->
output(
"found {$revisionRows}.\n" );
97 $this->
output(
"Checking deleted edits..." );
98 $archiveRows = $dbw->newSelectQueryBuilder()
101 ->where( [
'ar_actor' => $fromActorId ] )
102 ->caller( __METHOD__ )->fetchRowCount();
103 $this->
output(
"found {$archiveRows}.\n" );
105 # Don't count recent changes if we're not supposed to
107 $this->
output(
"Checking recent changes..." );
108 $recentChangesRows = $dbw->newSelectQueryBuilder()
110 ->from(
'recentchanges' )
111 ->where( [
'rc_actor' => $fromActorId ] )
112 ->caller( __METHOD__ )->fetchRowCount();
113 $this->
output(
"found {$recentChangesRows}.\n" );
115 $recentChangesRows = 0;
118 $total = $revisionRows + $archiveRows + $recentChangesRows;
119 $this->
output(
"\nTotal entries to change: {$total}\n" );
121 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
122 if ( !$report && $total ) {
124 if ( $revisionRows ) {
126 $this->
output(
"Reassigning current edits..." );
128 $dbw->newUpdateQueryBuilder()
129 ->update(
'revision' )
130 ->set( [
'rev_actor' => $toActorId ] )
131 ->where( [
'rev_actor' => $fromActorId ] )
132 ->caller( __METHOD__ )->execute();
134 $this->
output(
"done.\n" );
137 if ( $archiveRows ) {
138 $this->
output(
"Reassigning deleted edits..." );
140 $dbw->newUpdateQueryBuilder()
141 ->update(
'archive' )
142 ->set( [
'ar_actor' => $toActorId ] )
143 ->where( [
'ar_actor' => $fromActorId ] )
144 ->caller( __METHOD__ )->execute();
146 $this->
output(
"done.\n" );
148 # Update recent changes if required
149 if ( $recentChangesRows ) {
150 $this->
output(
"Updating recent changes..." );
152 $dbw->newUpdateQueryBuilder()
153 ->update(
'recentchanges' )
154 ->set( [
'rc_actor' => $toActorId ] )
155 ->where( [
'rc_actor' => $fromActorId ] )
156 ->caller( __METHOD__ )->execute();
158 $this->
output(
"done.\n" );
161 # If $from is an IP, delete any relevant rows from the
162 # ip_changes. No update needed, as $to cannot be an IP.
163 if ( !$from->isRegistered() ) {
164 $this->
output(
"Deleting ip_changes..." );
166 $dbw->newDeleteQueryBuilder()
167 ->deleteFrom(
'ip_changes' )
168 ->where( [
'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
169 ->caller( __METHOD__ )->execute();
171 $this->
output(
"done.\n" );
186 private function initialiseUser( $username ) {
188 if ( $services->getUserNameUtils()->isIP( $username ) ) {
189 $user = User::newFromName( $username,
false );
192 $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.