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' );
53 # Set up the users involved
54 $from = $this->initialiseUser( $this->
getArg( 0 ) );
55 $to = $this->initialiseUser( $this->
getArg( 1 ) );
57 # If the target doesn't exist, and --force is not set, stop here
58 if ( $to->getId() || $this->hasOption(
'force' ) ) {
61 $this->doReassignEdits( $from, $to, !$this->
hasOption(
'norc' ), $report );
62 # If reporting, and there were items, advise the user to run without --report
64 $this->
output(
"Run the script again without --report to update.\n" );
67 $ton = $to->getName();
68 $this->
error(
"User '{$ton}' not found." );
82 private function doReassignEdits( &$from, &$to, $updateRC =
false, $report =
false ) {
86 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
89 $this->
output(
"Checking current edits..." );
91 $revisionRows = $dbw->newSelectQueryBuilder()
94 ->where( [
'rev_actor' => $fromActorId ] )
95 ->caller( __METHOD__ )
98 $this->
output(
"found {$revisionRows}.\n" );
100 $this->
output(
"Checking deleted edits..." );
101 $archiveRows = $dbw->newSelectQueryBuilder()
104 ->where( [
'ar_actor' => $fromActorId ] )
105 ->caller( __METHOD__ )->fetchRowCount();
106 $this->
output(
"found {$archiveRows}.\n" );
108 # Don't count recent changes if we're not supposed to
110 $this->
output(
"Checking recent changes..." );
111 $recentChangesRows = $dbw->newSelectQueryBuilder()
113 ->from(
'recentchanges' )
114 ->where( [
'rc_actor' => $fromActorId ] )
115 ->caller( __METHOD__ )->fetchRowCount();
116 $this->
output(
"found {$recentChangesRows}.\n" );
118 $recentChangesRows = 0;
121 $total = $revisionRows + $archiveRows + $recentChangesRows;
122 $this->
output(
"\nTotal entries to change: {$total}\n" );
124 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
125 if ( !$report && $total ) {
127 if ( $revisionRows ) {
129 $this->
output(
"Reassigning current edits..." );
131 $dbw->newUpdateQueryBuilder()
132 ->update(
'revision' )
133 ->set( [
'rev_actor' => $toActorId ] )
134 ->where( [
'rev_actor' => $fromActorId ] )
135 ->caller( __METHOD__ )->execute();
137 $this->
output(
"done.\n" );
140 if ( $archiveRows ) {
141 $this->
output(
"Reassigning deleted edits..." );
143 $dbw->newUpdateQueryBuilder()
144 ->update(
'archive' )
145 ->set( [
'ar_actor' => $toActorId ] )
146 ->where( [
'ar_actor' => $fromActorId ] )
147 ->caller( __METHOD__ )->execute();
149 $this->
output(
"done.\n" );
151 # Update recent changes if required
152 if ( $recentChangesRows ) {
153 $this->
output(
"Updating recent changes..." );
155 $dbw->newUpdateQueryBuilder()
156 ->update(
'recentchanges' )
157 ->set( [
'rc_actor' => $toActorId ] )
158 ->where( [
'rc_actor' => $fromActorId ] )
159 ->caller( __METHOD__ )->execute();
161 $this->
output(
"done.\n" );
164 # If $from is an IP, delete any relevant rows from the
165 # ip_changes. No update needed, as $to cannot be an IP.
166 if ( !$from->isRegistered() ) {
167 $this->
output(
"Deleting ip_changes..." );
169 $dbw->newDeleteQueryBuilder()
170 ->deleteFrom(
'ip_changes' )
171 ->where( [
'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
172 ->caller( __METHOD__ )->execute();
174 $this->
output(
"done.\n" );
189 private function initialiseUser( $username ) {
191 if ( $services->getUserNameUtils()->isIP( $username ) ) {
192 $user = User::newFromName( $username,
false );
195 $user = User::newFromName( $username );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.