MediaWiki master
reassignEdits.php
Go to the documentation of this file.
1<?php
27use Wikimedia\IPUtils;
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Reassign edits from one user to another' );
43 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
44 $this->addOption( "norc", "Don't update the recent changes table" );
45 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
46 $this->addArg( 'from', 'Old user to take edits from' );
47 $this->addArg( 'to', 'New user to give edits to' );
48 }
49
50 public function execute() {
51 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
52 # Set up the users involved
53 $from = $this->initialiseUser( $this->getArg( 0 ) );
54 $to = $this->initialiseUser( $this->getArg( 1 ) );
55
56 # If the target doesn't exist, and --force is not set, stop here
57 if ( $to->getId() || $this->hasOption( 'force' ) ) {
58 # Reassign the edits
59 $report = $this->hasOption( 'report' );
60 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
61 # If reporting, and there were items, advise the user to run without --report
62 if ( $report ) {
63 $this->output( "Run the script again without --report to update.\n" );
64 }
65 } else {
66 $ton = $to->getName();
67 $this->error( "User '{$ton}' not found." );
68 }
69 }
70 }
71
81 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
82 $dbw = $this->getPrimaryDB();
83 $this->beginTransaction( $dbw, __METHOD__ );
84 $actorNormalization = $this->getServiceContainer()->getActorNormalization();
85 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
86
87 # Count things
88 $this->output( "Checking current edits..." );
89
90 $revisionRows = $dbw->newSelectQueryBuilder()
91 ->select( '*' )
92 ->from( 'revision' )
93 ->where( [ 'rev_actor' => $fromActorId ] )
94 ->caller( __METHOD__ )
95 ->fetchRowCount();
96
97 $this->output( "found {$revisionRows}.\n" );
98
99 $this->output( "Checking deleted edits..." );
100 $archiveRows = $dbw->newSelectQueryBuilder()
101 ->select( '*' )
102 ->from( 'archive' )
103 ->where( [ 'ar_actor' => $fromActorId ] )
104 ->caller( __METHOD__ )->fetchRowCount();
105 $this->output( "found {$archiveRows}.\n" );
106
107 # Don't count recent changes if we're not supposed to
108 if ( $updateRC ) {
109 $this->output( "Checking recent changes..." );
110 $recentChangesRows = $dbw->newSelectQueryBuilder()
111 ->select( '*' )
112 ->from( 'recentchanges' )
113 ->where( [ 'rc_actor' => $fromActorId ] )
114 ->caller( __METHOD__ )->fetchRowCount();
115 $this->output( "found {$recentChangesRows}.\n" );
116 } else {
117 $recentChangesRows = 0;
118 }
119
120 $total = $revisionRows + $archiveRows + $recentChangesRows;
121 $this->output( "\nTotal entries to change: {$total}\n" );
122
123 $toActorId = $actorNormalization->acquireActorId( $to, $dbw );
124 if ( !$report && $total ) {
125 $this->output( "\n" );
126 if ( $revisionRows ) {
127 # Reassign edits
128 $this->output( "Reassigning current edits..." );
129
130 $dbw->newUpdateQueryBuilder()
131 ->update( 'revision' )
132 ->set( [ 'rev_actor' => $toActorId ] )
133 ->where( [ 'rev_actor' => $fromActorId ] )
134 ->caller( __METHOD__ )->execute();
135
136 $this->output( "done.\n" );
137 }
138
139 if ( $archiveRows ) {
140 $this->output( "Reassigning deleted edits..." );
141
142 $dbw->newUpdateQueryBuilder()
143 ->update( 'archive' )
144 ->set( [ 'ar_actor' => $toActorId ] )
145 ->where( [ 'ar_actor' => $fromActorId ] )
146 ->caller( __METHOD__ )->execute();
147
148 $this->output( "done.\n" );
149 }
150 # Update recent changes if required
151 if ( $recentChangesRows ) {
152 $this->output( "Updating recent changes..." );
153
154 $dbw->newUpdateQueryBuilder()
155 ->update( 'recentchanges' )
156 ->set( [ 'rc_actor' => $toActorId ] )
157 ->where( [ 'rc_actor' => $fromActorId ] )
158 ->caller( __METHOD__ )->execute();
159
160 $this->output( "done.\n" );
161 }
162
163 # If $from is an IP, delete any relevant rows from the
164 # ip_changes. No update needed, as $to cannot be an IP.
165 if ( !$from->isRegistered() ) {
166 $this->output( "Deleting ip_changes..." );
167
168 $dbw->newDeleteQueryBuilder()
169 ->deleteFrom( 'ip_changes' )
170 ->where( [ 'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
171 ->caller( __METHOD__ )->execute();
172
173 $this->output( "done.\n" );
174 }
175 }
176
177 $this->commitTransaction( $dbw, __METHOD__ );
178
179 return $total;
180 }
181
188 private function initialiseUser( $username ) {
189 $services = $this->getServiceContainer();
190 if ( $services->getUserNameUtils()->isIP( $username ) ) {
191 $user = User::newFromName( $username, false );
192 $user->getActorId();
193 } else {
194 $user = User::newFromName( $username );
195 if ( !$user ) {
196 $this->fatalError( "Invalid username" );
197 }
198 }
199 $user->load();
200
201 return $user;
202 }
203}
204
205// @codeCoverageIgnoreStart
206$maintClass = ReassignEdits::class;
207require_once RUN_MAINTENANCE_IF_MAIN;
208// @codeCoverageIgnoreEnd
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.
internal since 1.36
Definition User.php:93
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
$maintClass