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 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that reassigns edits from a user or IP address
32 * @ingroup Maintenance
34 class ReassignEdits
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Reassign edits from one user to another' );
38 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
39 $this->addOption( "norc", "Don't update the recent changes table" );
40 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
41 $this->addArg( 'from', 'Old user to take edits from' );
42 $this->addArg( 'to', 'New user to give edits to' );
45 public function execute() {
46 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
47 # Set up the users involved
48 $from = $this->initialiseUser( $this->getArg( 0 ) );
49 $to = $this->initialiseUser( $this->getArg( 1 ) );
51 # If the target doesn't exist, and --force is not set, stop here
52 if ( $to->getId() ||
$this->hasOption( 'force' ) ) {
54 $report = $this->hasOption( 'report' );
55 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
56 # If reporting, and there were items, advise the user to run without --report
58 $this->output( "Run the script again without --report to update.\n" );
61 $ton = $to->getName();
62 $this->error( "User '{$ton}' not found." );
68 * Reassign edits from one user to another
70 * @param User $from User to take edits from
71 * @param User $to User to assign edits to
72 * @param bool $rc Update the recent changes table
73 * @param bool $report Don't change things; just echo numbers
74 * @return int Number of entries changed, or that would be changed
76 private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
77 $dbw = $this->getDB( DB_MASTER
);
78 $this->beginTransaction( $dbw, __METHOD__
);
81 $this->output( "Checking current edits..." );
82 $revQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'rev_user', $from );
84 [ 'revision' ] +
$revQueryInfo['tables'],
86 $revQueryInfo['conds'],
89 $revQueryInfo['joins']
91 $row = $dbw->fetchObject( $res );
93 $this->output( "found {$cur}.\n" );
95 $this->output( "Checking deleted edits..." );
96 $arQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'ar_user', $from, false );
98 [ 'archive' ] +
$arQueryInfo['tables'],
100 $arQueryInfo['conds'],
103 $arQueryInfo['joins']
105 $row = $dbw->fetchObject( $res );
107 $this->output( "found {$del}.\n" );
109 # Don't count recent changes if we're not supposed to
111 $this->output( "Checking recent changes..." );
112 $rcQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'rc_user', $from, false );
114 [ 'recentchanges' ] +
$rcQueryInfo['tables'],
116 $rcQueryInfo['conds'],
119 $rcQueryInfo['joins']
121 $row = $dbw->fetchObject( $res );
123 $this->output( "found {$rec}.\n" );
128 $total = $cur +
$del +
$rec;
129 $this->output( "\nTotal entries to change: {$total}\n" );
134 $this->output( "\nReassigning current edits..." );
136 'revision_actor_temp',
137 [ 'revactor_actor' => $to->getActorId( $dbw ) ],
138 [ 'revactor_actor' => $from->getActorId() ],
141 $this->output( "done.\nReassigning deleted edits..." );
142 $dbw->update( 'archive',
143 [ 'ar_actor' => $to->getActorId( $dbw ) ],
144 [ $arQueryInfo['conds'] ], __METHOD__
);
145 $this->output( "done.\n" );
146 # Update recent changes if required
148 $this->output( "Updating recent changes..." );
149 $dbw->update( 'recentchanges',
150 [ 'rc_actor' => $to->getActorId( $dbw ) ],
151 [ $rcQueryInfo['conds'] ], __METHOD__
);
152 $this->output( "done.\n" );
157 $this->commitTransaction( $dbw, __METHOD__
);
163 * Initialise the user object
165 * @param string $username Username or IP address
168 private function initialiseUser( $username ) {
169 if ( User
::isIP( $username ) ) {
172 $user->setName( $username );
174 $user = User
::newFromName( $username );
176 $this->fatalError( "Invalid username" );
185 $maintClass = ReassignEdits
::class;
186 require_once RUN_MAINTENANCE_IF_MAIN
;