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 * @licence GNU General Public Licence 2.0 or later
26 use Wikimedia\Rdbms\IDatabase
;
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 $rc 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, $rc = false, $report = false ) {
79 global $wgActorTableSchemaMigrationStage;
81 $dbw = $this->getDB( DB_MASTER
);
82 $this->beginTransaction( $dbw, __METHOD__
);
85 $this->output( "Checking current edits..." );
86 $revQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'rev_user', $from );
88 [ 'revision' ] +
$revQueryInfo['tables'],
90 $revQueryInfo['conds'],
93 $revQueryInfo['joins']
95 $row = $dbw->fetchObject( $res );
97 $this->output( "found {$cur}.\n" );
99 $this->output( "Checking deleted edits..." );
100 $arQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'ar_user', $from, false );
102 [ 'archive' ] +
$arQueryInfo['tables'],
104 $arQueryInfo['conds'],
107 $arQueryInfo['joins']
109 $row = $dbw->fetchObject( $res );
111 $this->output( "found {$del}.\n" );
113 # Don't count recent changes if we're not supposed to
115 $this->output( "Checking recent changes..." );
116 $rcQueryInfo = ActorMigration
::newMigration()->getWhere( $dbw, 'rc_user', $from, false );
118 [ 'recentchanges' ] +
$rcQueryInfo['tables'],
120 $rcQueryInfo['conds'],
123 $rcQueryInfo['joins']
125 $row = $dbw->fetchObject( $res );
127 $this->output( "found {$rec}.\n" );
132 $total = $cur +
$del +
$rec;
133 $this->output( "\nTotal entries to change: {$total}\n" );
138 $this->output( "\nReassigning current edits..." );
139 if ( $wgActorTableSchemaMigrationStage < MIGRATION_NEW
) {
143 'rev_user' => $to->getId(),
145 $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ?
$to->getName() : ''
148 ?
[ 'rev_user' => $from->getId() ] : [ 'rev_user_text' => $from->getName() ],
152 if ( $wgActorTableSchemaMigrationStage > MIGRATION_OLD
) {
154 'revision_actor_temp',
155 [ 'revactor_actor' => $to->getActorId( $dbw ) ],
156 [ 'revactor_actor' => $from->getActorId() ],
160 $this->output( "done.\nReassigning deleted edits..." );
161 $dbw->update( 'archive',
162 $this->userSpecification( $dbw, $to, 'ar_user', 'ar_user_text', 'ar_actor' ),
163 [ $arQueryInfo['conds'] ], __METHOD__
);
164 $this->output( "done.\n" );
165 # Update recent changes if required
167 $this->output( "Updating recent changes..." );
168 $dbw->update( 'recentchanges',
169 $this->userSpecification( $dbw, $to, 'rc_user', 'rc_user_text', 'rc_actor' ),
170 [ $rcQueryInfo['conds'] ], __METHOD__
);
171 $this->output( "done.\n" );
176 $this->commitTransaction( $dbw, __METHOD__
);
182 * Return user specifications
183 * i.e. user => id, user_text => text
185 * @param IDatabase $dbw Database handle
186 * @param User $user User for the spec
187 * @param string $idfield Field name containing the identifier
188 * @param string $utfield Field name containing the user text
189 * @param string $acfield Field name containing the actor ID
192 private function userSpecification( IDatabase
$dbw, &$user, $idfield, $utfield, $acfield ) {
193 global $wgActorTableSchemaMigrationStage;
196 if ( $wgActorTableSchemaMigrationStage < MIGRATION_NEW
) {
198 $idfield => $user->getId(),
199 $utfield => $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ?
$user->getName() : '',
202 if ( $wgActorTableSchemaMigrationStage > MIGRATION_OLD
) {
203 $ret +
= [ $acfield => $user->getActorId( $dbw ) ];
209 * Initialise the user object
211 * @param string $username Username or IP address
214 private function initialiseUser( $username ) {
215 if ( User
::isIP( $username ) ) {
218 $user->setName( $username );
220 $user = User
::newFromName( $username );
222 $this->fatalError( "Invalid username" );
231 $maintClass = ReassignEdits
::class;
232 require_once RUN_MAINTENANCE_IF_MAIN
;