MediaWiki master
reassignEdits.php
Go to the documentation of this file.
1<?php
28use Wikimedia\IPUtils;
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
41 public function __construct() {
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' );
49 }
50
51 public function execute() {
52 # Set up the users involved
53 $from = $this->initialiseUser( $this->getArg( 0 ) );
54 $to = $this->initialiseUser( $this->getArg( 1 ) );
55
56 // Reject attempts to re-assign to an IP address. This is done because the script does not
57 // populate ip_changes and it breaks if temporary accounts are enabled (T373914).
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.' );
62 }
63
64 # If the target doesn't exist, and --force is not set, stop here
65 if ( $to->getId() || $this->hasOption( 'force' ) ) {
66 # Reassign the edits
67 $report = $this->hasOption( 'report' );
68 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
69 # If reporting, and there were items, advise the user to run without --report
70 if ( $report ) {
71 $this->output( "Run the script again without --report to update.\n" );
72 }
73 } else {
74 $this->fatalError( "User '{$to->getName()}' not found." );
75 }
76 }
77
87 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
88 $dbw = $this->getPrimaryDB();
89 $this->beginTransaction( $dbw, __METHOD__ );
90 $actorNormalization = $this->getServiceContainer()->getActorNormalization();
91 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
92
93 # Count things
94 $this->output( "Checking current edits..." );
95
96 $revisionRows = $dbw->newSelectQueryBuilder()
97 ->select( '*' )
98 ->from( 'revision' )
99 ->where( [ 'rev_actor' => $fromActorId ] )
100 ->caller( __METHOD__ )
101 ->fetchRowCount();
102
103 $this->output( "found {$revisionRows}.\n" );
104
105 $this->output( "Checking deleted edits..." );
106 $archiveRows = $dbw->newSelectQueryBuilder()
107 ->select( '*' )
108 ->from( 'archive' )
109 ->where( [ 'ar_actor' => $fromActorId ] )
110 ->caller( __METHOD__ )->fetchRowCount();
111 $this->output( "found {$archiveRows}.\n" );
112
113 # Don't count recent changes if we're not supposed to
114 if ( $updateRC ) {
115 $this->output( "Checking recent changes..." );
116 $recentChangesRows = $dbw->newSelectQueryBuilder()
117 ->select( '*' )
118 ->from( 'recentchanges' )
119 ->where( [ 'rc_actor' => $fromActorId ] )
120 ->caller( __METHOD__ )->fetchRowCount();
121 $this->output( "found {$recentChangesRows}.\n" );
122 } else {
123 $recentChangesRows = 0;
124 }
125
126 $total = $revisionRows + $archiveRows + $recentChangesRows;
127 $this->output( "\nTotal entries to change: {$total}\n" );
128
129 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
130 if ( !$report && $total ) {
131 $this->output( "\n" );
132 if ( $revisionRows ) {
133 # Reassign edits
134 $this->output( "Reassigning current edits..." );
135
136 $dbw->newUpdateQueryBuilder()
137 ->update( 'revision' )
138 ->set( [ 'rev_actor' => $toActorId ] )
139 ->where( [ 'rev_actor' => $fromActorId ] )
140 ->caller( __METHOD__ )->execute();
141
142 $this->output( "done.\n" );
143 }
144
145 if ( $archiveRows ) {
146 $this->output( "Reassigning deleted edits..." );
147
148 $dbw->newUpdateQueryBuilder()
149 ->update( 'archive' )
150 ->set( [ 'ar_actor' => $toActorId ] )
151 ->where( [ 'ar_actor' => $fromActorId ] )
152 ->caller( __METHOD__ )->execute();
153
154 $this->output( "done.\n" );
155 }
156 # Update recent changes if required
157 if ( $recentChangesRows ) {
158 $this->output( "Updating recent changes..." );
159
160 $dbw->newUpdateQueryBuilder()
161 ->update( 'recentchanges' )
162 ->set( [ 'rc_actor' => $toActorId ] )
163 ->where( [ 'rc_actor' => $fromActorId ] )
164 ->caller( __METHOD__ )->execute();
165
166 $this->output( "done.\n" );
167 }
168
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..." );
173
174 $dbw->newDeleteQueryBuilder()
175 ->deleteFrom( 'ip_changes' )
176 ->where( [ 'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
177 ->caller( __METHOD__ )->execute();
178
179 $this->output( "done.\n" );
180 }
181 }
182
183 $this->commitTransaction( $dbw, __METHOD__ );
184
185 return $total;
186 }
187
194 private function initialiseUser( $username ) {
195 $services = $this->getServiceContainer();
196 if ( $services->getUserNameUtils()->isIP( $username ) ) {
197 $user = User::newFromName( $username, false );
198 $user->getActorId();
199 } else {
200 $user = User::newFromName( $username );
201 if ( !$user ) {
202 $this->fatalError( "Invalid username" );
203 }
204 }
205 $user->load();
206
207 return $user;
208 }
209}
210
211// @codeCoverageIgnoreStart
212$maintClass = ReassignEdits::class;
213require_once RUN_MAINTENANCE_IF_MAIN;
214// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
User class for the MediaWiki software.
Definition User.php:123
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
$maintClass