Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / reassignEdits.php
blobbe70c37ec6dce05daf984ea59c0e4487ce87f72f
1 <?php
2 /**
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
20 * @file
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';
30 /**
31 * Maintenance script that reassigns edits from a user or IP address
32 * to another user.
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' ) ) {
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" );
62 } else {
63 $ton = $to->getName();
64 $this->error( "User '{$ton}' not found." );
69 /**
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 );
85 # Count things
86 $this->output( "Checking current edits..." );
87 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
88 $revisionRows = $dbw->selectRowCount(
89 [ 'revision' ] + $revQueryInfo['tables'],
90 '*',
91 $revQueryInfo['conds'],
92 __METHOD__,
93 [],
94 $revQueryInfo['joins']
96 $this->output( "found {$revisionRows}.\n" );
98 $this->output( "Checking deleted edits..." );
99 $archiveRows = $dbw->selectRowCount(
100 [ 'archive' ],
101 '*',
102 [ 'ar_actor' => $fromActorId ],
103 __METHOD__
105 $this->output( "found {$archiveRows}.\n" );
107 # Don't count recent changes if we're not supposed to
108 if ( $updateRC ) {
109 $this->output( "Checking recent changes..." );
110 $recentChangesRows = $dbw->selectRowCount(
111 [ 'recentchanges' ],
112 '*',
113 [ 'rc_actor' => $fromActorId ],
114 __METHOD__
116 $this->output( "found {$recentChangesRows}.\n" );
117 } else {
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 ) {
128 # Reassign edits
129 $this->output( "Reassigning current edits..." );
130 if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_TEMP ) {
131 $dbw->update(
132 'revision_actor_temp',
133 [ 'revactor_actor' => $toActorId ],
134 [ 'revactor_actor' => $fromActorId ],
135 __METHOD__
138 if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
139 $dbw->update(
140 'revision',
141 [ 'rev_actor' => $toActorId ],
142 [ 'rev_actor' => $fromActorId ],
143 __METHOD__
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 ],
154 __METHOD__
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 ],
164 __METHOD__
166 $this->output( "done.\n" );
170 $this->commitTransaction( $dbw, __METHOD__ );
172 return $total;
176 * Initialise the user object
178 * @param string $username Username or IP address
179 * @return User
181 private function initialiseUser( $username ) {
182 $services = MediaWikiServices::getInstance();
183 if ( $services->getUserNameUtils()->isIP( $username ) ) {
184 $user = User::newFromName( $username, false );
185 $user->getActorId();
186 } else {
187 $user = User::newFromName( $username );
188 if ( !$user ) {
189 $this->fatalError( "Invalid username" );
192 $user->load();
194 return $user;
198 $maintClass = ReassignEdits::class;
199 require_once RUN_MAINTENANCE_IF_MAIN;