42 parent::__construct();
43 $this->
addDescription(
'Reassign edits from one user to another' );
44 $this->
addOption(
"force",
"Reassign even if the target user doesn't exist" );
45 $this->
addOption(
"norc",
"Don't update the recent changes table" );
46 $this->
addOption(
"report",
"Print out details of what would be changed, but don't update it" );
47 $this->
addArg(
'from',
'Old user to take edits from' );
48 $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 ) );
58 if ( IPUtils::isIPAddress( $to->getName() ) ) {
59 $this->
fatalError(
'Script does not support re-assigning to another IP.' );
60 } elseif ( $from->equals( $to ) ) {
61 $this->
fatalError(
'The from and to user cannot be the same.' );
64 # If the target doesn't exist, and --force is not set, stop here
65 if ( $to->getId() || $this->hasOption(
'force' ) ) {
68 $this->doReassignEdits( $from, $to, !$this->
hasOption(
'norc' ), $report );
69 # If reporting, and there were items, advise the user to run without --report
71 $this->
output(
"Run the script again without --report to update.\n" );
74 $this->
fatalError(
"User '{$to->getName()}' not found." );
87 private function doReassignEdits( &$from, &$to, $updateRC =
false, $report =
false ) {
91 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
94 $this->
output(
"Checking current edits..." );
96 $revisionRows = $dbw->newSelectQueryBuilder()
99 ->where( [
'rev_actor' => $fromActorId ] )
100 ->caller( __METHOD__ )
103 $this->
output(
"found {$revisionRows}.\n" );
105 $this->
output(
"Checking deleted edits..." );
106 $archiveRows = $dbw->newSelectQueryBuilder()
109 ->where( [
'ar_actor' => $fromActorId ] )
110 ->caller( __METHOD__ )->fetchRowCount();
111 $this->
output(
"found {$archiveRows}.\n" );
113 # Don't count recent changes if we're not supposed to
115 $this->
output(
"Checking recent changes..." );
116 $recentChangesRows = $dbw->newSelectQueryBuilder()
118 ->from(
'recentchanges' )
119 ->where( [
'rc_actor' => $fromActorId ] )
120 ->caller( __METHOD__ )->fetchRowCount();
121 $this->
output(
"found {$recentChangesRows}.\n" );
123 $recentChangesRows = 0;
126 $total = $revisionRows + $archiveRows + $recentChangesRows;
127 $this->
output(
"\nTotal entries to change: {$total}\n" );
129 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
130 if ( !$report && $total ) {
132 if ( $revisionRows ) {
134 $this->
output(
"Reassigning current edits..." );
136 $dbw->newUpdateQueryBuilder()
137 ->update(
'revision' )
138 ->set( [
'rev_actor' => $toActorId ] )
139 ->where( [
'rev_actor' => $fromActorId ] )
140 ->caller( __METHOD__ )->execute();
142 $this->
output(
"done.\n" );
145 if ( $archiveRows ) {
146 $this->
output(
"Reassigning deleted edits..." );
148 $dbw->newUpdateQueryBuilder()
149 ->update(
'archive' )
150 ->set( [
'ar_actor' => $toActorId ] )
151 ->where( [
'ar_actor' => $fromActorId ] )
152 ->caller( __METHOD__ )->execute();
154 $this->
output(
"done.\n" );
156 # Update recent changes if required
157 if ( $recentChangesRows ) {
158 $this->
output(
"Updating recent changes..." );
160 $dbw->newUpdateQueryBuilder()
161 ->update(
'recentchanges' )
162 ->set( [
'rc_actor' => $toActorId ] )
163 ->where( [
'rc_actor' => $fromActorId ] )
164 ->caller( __METHOD__ )->execute();
166 $this->
output(
"done.\n" );
169 # If $from is an IP, delete any relevant rows from the
170 # ip_changes. No update needed, as $to cannot be an IP.
171 if ( !$from->isRegistered() ) {
172 $this->
output(
"Deleting ip_changes..." );
174 $dbw->newDeleteQueryBuilder()
175 ->deleteFrom(
'ip_changes' )
176 ->where( [
'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
177 ->caller( __METHOD__ )->execute();
179 $this->
output(
"done.\n" );
194 private function initialiseUser( $username ) {
196 if ( $services->getUserNameUtils()->isIP( $username ) ) {
197 $user = User::newFromName( $username,
false );
200 $user = User::newFromName( $username );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.