Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / reassignEdits.php
blob67c92c074c955ffd070365e1a3b00b17b269bc5b
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\Maintenance\Maintenance;
27 use MediaWiki\User\User;
28 use Wikimedia\IPUtils;
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
34 /**
35 * Maintenance script that reassigns edits from a user or IP address
36 * to another user.
38 * @ingroup Maintenance
40 class ReassignEdits extends Maintenance {
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' );
51 public function execute() {
52 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
53 # Set up the users involved
54 $from = $this->initialiseUser( $this->getArg( 0 ) );
55 $to = $this->initialiseUser( $this->getArg( 1 ) );
57 # If the target doesn't exist, and --force is not set, stop here
58 if ( $to->getId() || $this->hasOption( 'force' ) ) {
59 # Reassign the edits
60 $report = $this->hasOption( 'report' );
61 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
62 # If reporting, and there were items, advise the user to run without --report
63 if ( $report ) {
64 $this->output( "Run the script again without --report to update.\n" );
66 } else {
67 $ton = $to->getName();
68 $this->error( "User '{$ton}' not found." );
73 /**
74 * Reassign edits from one user to another
76 * @param User &$from User to take edits from
77 * @param User &$to User to assign edits to
78 * @param bool $updateRC Update the recent changes table
79 * @param bool $report Don't change things; just echo numbers
80 * @return int The number of entries changed, or that would be changed
82 private function doReassignEdits( &$from, &$to, $updateRC = false, $report = false ) {
83 $dbw = $this->getPrimaryDB();
84 $this->beginTransaction( $dbw, __METHOD__ );
85 $actorNormalization = $this->getServiceContainer()->getActorNormalization();
86 $fromActorId = $actorNormalization->findActorId( $from, $dbw );
88 # Count things
89 $this->output( "Checking current edits..." );
91 $revisionRows = $dbw->newSelectQueryBuilder()
92 ->select( '*' )
93 ->from( 'revision' )
94 ->where( [ 'rev_actor' => $fromActorId ] )
95 ->caller( __METHOD__ )
96 ->fetchRowCount();
98 $this->output( "found {$revisionRows}.\n" );
100 $this->output( "Checking deleted edits..." );
101 $archiveRows = $dbw->newSelectQueryBuilder()
102 ->select( '*' )
103 ->from( 'archive' )
104 ->where( [ 'ar_actor' => $fromActorId ] )
105 ->caller( __METHOD__ )->fetchRowCount();
106 $this->output( "found {$archiveRows}.\n" );
108 # Don't count recent changes if we're not supposed to
109 if ( $updateRC ) {
110 $this->output( "Checking recent changes..." );
111 $recentChangesRows = $dbw->newSelectQueryBuilder()
112 ->select( '*' )
113 ->from( 'recentchanges' )
114 ->where( [ 'rc_actor' => $fromActorId ] )
115 ->caller( __METHOD__ )->fetchRowCount();
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..." );
131 $dbw->newUpdateQueryBuilder()
132 ->update( 'revision' )
133 ->set( [ 'rev_actor' => $toActorId ] )
134 ->where( [ 'rev_actor' => $fromActorId ] )
135 ->caller( __METHOD__ )->execute();
137 $this->output( "done.\n" );
140 if ( $archiveRows ) {
141 $this->output( "Reassigning deleted edits..." );
143 $dbw->newUpdateQueryBuilder()
144 ->update( 'archive' )
145 ->set( [ 'ar_actor' => $toActorId ] )
146 ->where( [ 'ar_actor' => $fromActorId ] )
147 ->caller( __METHOD__ )->execute();
149 $this->output( "done.\n" );
151 # Update recent changes if required
152 if ( $recentChangesRows ) {
153 $this->output( "Updating recent changes..." );
155 $dbw->newUpdateQueryBuilder()
156 ->update( 'recentchanges' )
157 ->set( [ 'rc_actor' => $toActorId ] )
158 ->where( [ 'rc_actor' => $fromActorId ] )
159 ->caller( __METHOD__ )->execute();
161 $this->output( "done.\n" );
164 # If $from is an IP, delete any relevant rows from the
165 # ip_changes. No update needed, as $to cannot be an IP.
166 if ( !$from->isRegistered() ) {
167 $this->output( "Deleting ip_changes..." );
169 $dbw->newDeleteQueryBuilder()
170 ->deleteFrom( 'ip_changes' )
171 ->where( [ 'ipc_hex' => IPUtils::toHex( $from->getName() ) ] )
172 ->caller( __METHOD__ )->execute();
174 $this->output( "done.\n" );
178 $this->commitTransaction( $dbw, __METHOD__ );
180 return $total;
184 * Initialise the user object
186 * @param string $username Username or IP address
187 * @return User
189 private function initialiseUser( $username ) {
190 $services = $this->getServiceContainer();
191 if ( $services->getUserNameUtils()->isIP( $username ) ) {
192 $user = User::newFromName( $username, false );
193 $user->getActorId();
194 } else {
195 $user = User::newFromName( $username );
196 if ( !$user ) {
197 $this->fatalError( "Invalid username" );
200 $user->load();
202 return $user;
206 // @codeCoverageIgnoreStart
207 $maintClass = ReassignEdits::class;
208 require_once RUN_MAINTENANCE_IF_MAIN;
209 // @codeCoverageIgnoreEnd