28 parent::__construct();
29 $this->
addDescription(
'Reassign edits from one user to another' );
30 $this->
addOption(
"force",
"Reassign even if the target user doesn't exist" );
31 $this->
addOption(
"norc",
"Don't update the recent changes table" );
32 $this->
addOption(
"report",
"Print out details of what would be changed, but don't update it" );
33 $this->
addArg(
'from',
'Old user to take edits from' );
34 $this->
addArg(
'to',
'New user to give edits to' );
38 # Set up the users involved
39 $from = $this->initialiseUser( $this->
getArg( 0 ) );
40 $to = $this->initialiseUser( $this->
getArg( 1 ) );
44 if ( IPUtils::isIPAddress( $to->getName() ) ) {
45 $this->
fatalError(
'Script does not support re-assigning to another IP.' );
46 } elseif ( $from->equals( $to ) ) {
47 $this->
fatalError(
'The from and to user cannot be the same.' );
50 # If the target doesn't exist, and --force is not set, stop here
51 if ( $to->getId() || $this->hasOption(
'force' ) ) {
54 $this->doReassignEdits( $from, $to, !$this->
hasOption(
'norc' ), $report );
55 # If reporting, and there were items, advise the user to run without --report
57 $this->
output(
"Run the script again without --report to update.\n" );
60 $this->
fatalError(
"User '{$to->getName()}' not found." );
73 private function doReassignEdits( &$from, &$to, $updateRC =
false, $report =
false ) {
77 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
80 $this->
output(
"Checking current edits..." );
82 $revisionRows = $dbw->newSelectQueryBuilder()
85 ->where( [
'rev_actor' => $fromActorId ] )
86 ->caller( __METHOD__ )
89 $this->
output(
"found {$revisionRows}.\n" );
91 $this->
output(
"Checking deleted edits..." );
92 $archiveRows = $dbw->newSelectQueryBuilder()
95 ->where( [
'ar_actor' => $fromActorId ] )
96 ->caller( __METHOD__ )->fetchRowCount();
97 $this->
output(
"found {$archiveRows}.\n" );
99 # Don't count recent changes if we're not supposed to
101 $this->
output(
"Checking recent changes..." );
102 $recentChangesRows = $dbw->newSelectQueryBuilder()
104 ->from(
'recentchanges' )
105 ->where( [
'rc_actor' => $fromActorId ] )
106 ->caller( __METHOD__ )->fetchRowCount();
107 $this->
output(
"found {$recentChangesRows}.\n" );
109 $recentChangesRows = 0;
112 $total = $revisionRows + $archiveRows + $recentChangesRows;
113 $this->
output(
"\nTotal entries to change: {$total}\n" );
115 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
116 if ( !$report && $total ) {
118 if ( $revisionRows ) {
120 $this->
output(
"Reassigning current edits..." );
122 $dbw->newUpdateQueryBuilder()
123 ->update(
'revision' )
124 ->set( [
'rev_actor' => $toActorId ] )
125 ->where( [
'rev_actor' => $fromActorId ] )
126 ->caller( __METHOD__ )->execute();
128 $this->
output(
"done.\n" );
131 if ( $archiveRows ) {
132 $this->
output(
"Reassigning deleted edits..." );
134 $dbw->newUpdateQueryBuilder()
135 ->update(
'archive' )
136 ->set( [
'ar_actor' => $toActorId ] )
137 ->where( [
'ar_actor' => $fromActorId ] )
138 ->caller( __METHOD__ )->execute();
140 $this->
output(
"done.\n" );
142 # Update recent changes if required
143 if ( $recentChangesRows ) {
144 $this->
output(
"Updating recent changes..." );
146 $dbw->newUpdateQueryBuilder()
147 ->update(
'recentchanges' )
148 ->set( [
'rc_actor' => $toActorId ] )
149 ->where( [
'rc_actor' => $fromActorId ] )
150 ->caller( __METHOD__ )->execute();
152 $this->
output(
"done.\n" );
155 # If $from is an IP, delete any relevant rows from the
156 # ip_changes. No update needed, as $to cannot be an IP.
157 if ( !$from->isRegistered() ) {
158 $this->
output(
"Deleting ip_changes..." );
160 $dbw->newDeleteQueryBuilder()
161 ->deleteFrom(
'ip_changes' )
162 ->where( [
'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
163 ->caller( __METHOD__ )->execute();
165 $this->
output(
"done.\n" );
180 private function initialiseUser( $username ) {
182 if ( $services->getUserNameUtils()->isIP( $username ) ) {
183 $user = User::newFromName( $username,
false );
186 $user = User::newFromName( $username );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.