MediaWiki fundraising/REL1_35
reassignEdits.php
Go to the documentation of this file.
1<?php
26use Wikimedia\IPUtils;
27
28require_once __DIR__ . '/Maintenance.php';
29
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Reassign edits from one user to another' );
40 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
41 $this->addOption( "norc", "Don't update the recent changes table" );
42 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
43 $this->addArg( 'from', 'Old user to take edits from' );
44 $this->addArg( 'to', 'New user to give edits to' );
45 }
46
47 public function execute() {
48 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
49 # Set up the users involved
50 $from = $this->initialiseUser( $this->getArg( 0 ) );
51 $to = $this->initialiseUser( $this->getArg( 1 ) );
52
53 # If the target doesn't exist, and --force is not set, stop here
54 if ( $to->getId() || $this->hasOption( 'force' ) ) {
55 # Reassign the edits
56 $report = $this->hasOption( 'report' );
57 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
58 # If reporting, and there were items, advise the user to run without --report
59 if ( $report ) {
60 $this->output( "Run the script again without --report to update.\n" );
61 }
62 } else {
63 $ton = $to->getName();
64 $this->error( "User '{$ton}' not found." );
65 }
66 }
67 }
68
78 private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
79 $dbw = $this->getDB( DB_MASTER );
80 $this->beginTransaction( $dbw, __METHOD__ );
81
82 # Count things
83 $this->output( "Checking current edits..." );
84 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
85 $res = $dbw->select(
86 [ 'revision' ] + $revQueryInfo['tables'],
87 'COUNT(*) AS count',
88 $revQueryInfo['conds'],
89 __METHOD__,
90 [],
91 $revQueryInfo['joins']
92 );
93 $row = $dbw->fetchObject( $res );
94 $cur = $row->count;
95 $this->output( "found {$cur}.\n" );
96
97 $this->output( "Checking deleted edits..." );
98 $arQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'ar_user', $from, false );
99 $res = $dbw->select(
100 [ 'archive' ] + $arQueryInfo['tables'],
101 'COUNT(*) AS count',
102 $arQueryInfo['conds'],
103 __METHOD__,
104 [],
105 $arQueryInfo['joins']
106 );
107 $row = $dbw->fetchObject( $res );
108 $del = $row->count;
109 $this->output( "found {$del}.\n" );
110
111 # Don't count recent changes if we're not supposed to
112 if ( $rc ) {
113 $this->output( "Checking recent changes..." );
114 $rcQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rc_user', $from, false );
115 $res = $dbw->select(
116 [ 'recentchanges' ] + $rcQueryInfo['tables'],
117 'COUNT(*) AS count',
118 $rcQueryInfo['conds'],
119 __METHOD__,
120 [],
121 $rcQueryInfo['joins']
122 );
123 $row = $dbw->fetchObject( $res );
124 $rec = $row->count;
125 $this->output( "found {$rec}.\n" );
126 } else {
127 $rec = 0;
128 }
129
130 $total = $cur + $del + $rec;
131 $this->output( "\nTotal entries to change: {$total}\n" );
132
133 if ( !$report ) {
134 if ( $total ) {
135 # Reassign edits
136 $this->output( "\nReassigning current edits..." );
137 $dbw->update(
138 'revision_actor_temp',
139 [ 'revactor_actor' => $to->getActorId( $dbw ) ],
140 [ 'revactor_actor' => $from->getActorId() ],
141 __METHOD__
142 );
143 $this->output( "done.\nReassigning deleted edits..." );
144 $dbw->update( 'archive',
145 [ 'ar_actor' => $to->getActorId( $dbw ) ],
146 [ $arQueryInfo['conds'] ], __METHOD__ );
147 $this->output( "done.\n" );
148 # Update recent changes if required
149 if ( $rc ) {
150 $this->output( "Updating recent changes..." );
151 $dbw->update( 'recentchanges',
152 [ 'rc_actor' => $to->getActorId( $dbw ) ],
153 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable False positive
154 [ $rcQueryInfo['conds'] ], __METHOD__ );
155 $this->output( "done.\n" );
156 }
157 }
158
159 # If $from is an IP, delete any relevant rows from the
160 # ip_changes. No update needed, as $to cannot be an IP.
161 if ( !$from->isRegistered() ) {
162 $this->output( "Deleting ip_changes..." );
163 $dbw->delete(
164 'ip_changes',
165 [
166 'ipc_hex' => IPUtils::toHex( $from->getName() )
167 ],
168 __METHOD__
169 );
170 $this->output( "done.\n" );
171 }
172 }
173
174 $this->commitTransaction( $dbw, __METHOD__ );
175
176 return (int)$total;
177 }
178
185 private function initialiseUser( $username ) {
186 if ( User::isIP( $username ) ) {
187 $user = User::newFromName( $username, false );
188 $user->getActorId();
189 } else {
190 $user = User::newFromName( $username );
191 if ( !$user ) {
192 $this->fatalError( "Invalid username" );
193 }
194 }
195 $user->load();
196
197 return $user;
198 }
199}
200
201$maintClass = ReassignEdits::class;
202require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
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)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation 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.
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.
Maintenance script that reassigns edits from a user or IP address to another user.
execute()
Do the actual work.
__construct()
Default constructor.
initialiseUser( $username)
Initialise the user object.
doReassignEdits(&$from, &$to, $rc=false, $report=false)
Reassign edits from one user to another.
const DB_MASTER
Definition defines.php:29
$maintClass