3 * Reassign edits from a user or IP address to another user
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @license GPL-2.0-or-later
26 use MediaWiki\MediaWikiServices
;
28 require_once __DIR__
. '/Maintenance.php';
31 * Maintenance script that reassigns edits from a user or IP address
34 * @ingroup Maintenance
36 class ReassignEdits
extends Maintenance
{
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' );
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 ) );
53 # If the target doesn't exist, and --force is not set, stop here
54 if ( $to->getId() ||
$this->hasOption( 'force' ) ) {
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
60 $this->output( "Run the script again without --report to update.\n" );
63 $ton = $to->getName();
64 $this->error( "User '{$ton}' not found." );
70 * Reassign edits from one user to another
72 * @param User &$from User to take edits from
73 * @param User &$to User to assign edits to
74 * @param bool $updateRC Update the recent changes table
75 * @param bool $report Don't change things; just echo numbers
76 * @return int Number of entries changed, or that would be changed
78 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
79 $actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' );
80 $dbw = $this->getDB( DB_PRIMARY
);
81 $this->beginTransaction( $dbw, __METHOD__
);
82 $actorNormalization = MediaWikiServices
::getInstance()->getActorNormalization();
83 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
86 $this->output( "Checking current edits..." );
87 $revQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'rev_user', $from );
88 $revisionRows = $dbw->selectRowCount(
89 [ 'revision' ] +
$revQueryInfo['tables'],
91 $revQueryInfo['conds'],
94 $revQueryInfo['joins']
96 $this->output( "found {$revisionRows}.\n" );
98 $this->output( "Checking deleted edits..." );
99 $archiveRows = $dbw->selectRowCount(
102 [ 'ar_actor' => $fromActorId ],
105 $this->output( "found {$archiveRows}.\n" );
107 # Don't count recent changes if we're not supposed to
109 $this->output( "Checking recent changes..." );
110 $recentChangesRows = $dbw->selectRowCount(
113 [ 'rc_actor' => $fromActorId ],
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 ) {
126 $this->output( "\n" );
127 if ( $revisionRows ) {
129 $this->output( "Reassigning current edits..." );
130 if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_TEMP
) {
132 'revision_actor_temp',
133 [ 'revactor_actor' => $toActorId ],
134 [ 'revactor_actor' => $fromActorId ],
138 if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
141 [ 'rev_actor' => $toActorId ],
142 [ 'rev_actor' => $fromActorId ],
146 $this->output( "done.\n" );
149 if ( $archiveRows ) {
150 $this->output( "Reassigning deleted edits..." );
151 $dbw->update( 'archive',
152 [ 'ar_actor' => $toActorId ],
153 [ 'ar_actor' => $fromActorId ],
156 $this->output( "done.\n" );
158 # Update recent changes if required
159 if ( $recentChangesRows ) {
160 $this->output( "Updating recent changes..." );
161 $dbw->update( 'recentchanges',
162 [ 'rc_actor' => $toActorId ],
163 [ 'rc_actor' => $fromActorId ],
166 $this->output( "done.\n" );
170 $this->commitTransaction( $dbw, __METHOD__
);
176 * Initialise the user object
178 * @param string $username Username or IP address
181 private function initialiseUser( $username ) {
182 $services = MediaWikiServices
::getInstance();
183 if ( $services->getUserNameUtils()->isIP( $username ) ) {
184 $user = User
::newFromName( $username, false );
187 $user = User
::newFromName( $username );
189 $this->fatalError( "Invalid username" );
198 $maintClass = ReassignEdits
::class;
199 require_once RUN_MAINTENANCE_IF_MAIN
;