2006-01-26 21:41:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Reassign edits from a user or IP address to another user
|
|
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-09 16:06:18 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2006-01-26 21:41:40 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
2018-05-23 23:23:42 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2006-01-26 21:41:40 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-02-15 22:26:08 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2009-08-02 19:35:17 +00:00
|
|
|
|
2012-08-09 16:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that reassigns edits from a user or IP address
|
|
|
|
|
* to another user.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class ReassignEdits extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Reassign edits from one user to another' );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->addOption( "force", "Reassign even if the target user doesn't exist" );
|
|
|
|
|
$this->addOption( "norc", "Don't update the recent changes table" );
|
|
|
|
|
$this->addOption( "report", "Print out details of what would be changed, but don't update it" );
|
2009-08-18 23:06:24 +00:00
|
|
|
$this->addArg( 'from', 'Old user to take edits from' );
|
|
|
|
|
$this->addArg( 'to', 'New user to give edits to' );
|
2009-06-24 02:02:37 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
# Set up the users involved
|
2010-05-22 16:50:39 +00:00
|
|
|
$from = $this->initialiseUser( $this->getArg( 0 ) );
|
2013-04-18 18:48:44 +00:00
|
|
|
$to = $this->initialiseUser( $this->getArg( 1 ) );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# If the target doesn't exist, and --force is not set, stop here
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $to->getId() || $this->hasOption( 'force' ) ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
# Reassign the edits
|
2010-05-22 16:50:39 +00:00
|
|
|
$report = $this->hasOption( 'report' );
|
2010-11-30 18:44:50 +00:00
|
|
|
$this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
|
2010-12-04 03:20:14 +00:00
|
|
|
# If reporting, and there were items, advise the user to run without --report
|
2010-09-20 14:32:38 +00:00
|
|
|
if ( $report ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Run the script again without --report to update.\n" );
|
2010-09-20 14:32:38 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
} else {
|
|
|
|
|
$ton = $to->getName();
|
2009-08-02 21:55:10 +00:00
|
|
|
$this->error( "User '{$ton}' not found." );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reassign edits from one user to another
|
|
|
|
|
*
|
2019-11-23 22:28:57 +00:00
|
|
|
* @param User &$from User to take edits from
|
|
|
|
|
* @param User &$to User to assign edits to
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param bool $rc Update the recent changes table
|
|
|
|
|
* @param bool $report Don't change things; just echo numbers
|
|
|
|
|
* @return int Number of entries changed, or that would be changed
|
2009-08-02 19:35:17 +00:00
|
|
|
*/
|
|
|
|
|
private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
|
2021-05-03 05:13:41 +00:00
|
|
|
$actorTableSchemaMigrationStage = $this->getConfig()->get( 'ActorTableSchemaMigrationStage' );
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->beginTransaction( $dbw, __METHOD__ );
|
2021-04-19 01:04:30 +00:00
|
|
|
$actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
|
|
|
|
|
$fromActorId = $actorNormalization->findActorId( $from, $dbw );
|
2009-08-02 19:35:17 +00:00
|
|
|
|
|
|
|
|
# Count things
|
|
|
|
|
$this->output( "Checking current edits..." );
|
2017-09-12 17:12:29 +00:00
|
|
|
$revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
|
2014-04-22 21:07:08 +00:00
|
|
|
$res = $dbw->select(
|
2017-09-12 17:12:29 +00:00
|
|
|
[ 'revision' ] + $revQueryInfo['tables'],
|
2014-04-22 21:07:08 +00:00
|
|
|
'COUNT(*) AS count',
|
2017-09-12 17:12:29 +00:00
|
|
|
$revQueryInfo['conds'],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
[],
|
|
|
|
|
$revQueryInfo['joins']
|
2014-04-22 21:07:08 +00:00
|
|
|
);
|
2009-08-02 19:35:17 +00:00
|
|
|
$row = $dbw->fetchObject( $res );
|
|
|
|
|
$cur = $row->count;
|
|
|
|
|
$this->output( "found {$cur}.\n" );
|
|
|
|
|
|
|
|
|
|
$this->output( "Checking deleted edits..." );
|
2014-04-22 21:07:08 +00:00
|
|
|
$res = $dbw->select(
|
2021-04-19 01:04:30 +00:00
|
|
|
[ 'archive' ],
|
2014-04-22 21:07:08 +00:00
|
|
|
'COUNT(*) AS count',
|
2021-04-19 01:04:30 +00:00
|
|
|
[ 'ar_actor' => $fromActorId ],
|
|
|
|
|
__METHOD__
|
2014-04-22 21:07:08 +00:00
|
|
|
);
|
2009-08-02 19:35:17 +00:00
|
|
|
$row = $dbw->fetchObject( $res );
|
|
|
|
|
$del = $row->count;
|
|
|
|
|
$this->output( "found {$del}.\n" );
|
|
|
|
|
|
|
|
|
|
# Don't count recent changes if we're not supposed to
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $rc ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Checking recent changes..." );
|
2014-04-22 21:07:08 +00:00
|
|
|
$res = $dbw->select(
|
2021-04-19 01:04:30 +00:00
|
|
|
[ 'recentchanges' ],
|
2014-04-22 21:07:08 +00:00
|
|
|
'COUNT(*) AS count',
|
2021-04-19 01:04:30 +00:00
|
|
|
[ 'rc_actor' => $fromActorId ],
|
|
|
|
|
__METHOD__
|
2014-04-22 21:07:08 +00:00
|
|
|
);
|
2009-08-02 19:35:17 +00:00
|
|
|
$row = $dbw->fetchObject( $res );
|
|
|
|
|
$rec = $row->count;
|
|
|
|
|
$this->output( "found {$rec}.\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$rec = 0;
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$total = $cur + $del + $rec;
|
|
|
|
|
$this->output( "\nTotal entries to change: {$total}\n" );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2021-04-19 01:04:30 +00:00
|
|
|
$toActorId = $actorNormalization->acquireActorId( $to, $dbw );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$report ) {
|
|
|
|
|
if ( $total ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
# Reassign edits
|
|
|
|
|
$this->output( "\nReassigning current edits..." );
|
2021-05-03 05:13:41 +00:00
|
|
|
if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_TEMP ) {
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'revision_actor_temp',
|
|
|
|
|
[ 'revactor_actor' => $toActorId ],
|
|
|
|
|
[ 'revactor_actor' => $fromActorId ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( $actorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'revision',
|
|
|
|
|
[ 'rev_actor' => $toActorId ],
|
|
|
|
|
[ 'rev_actor' => $fromActorId ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "done.\nReassigning deleted edits..." );
|
2017-09-12 17:12:29 +00:00
|
|
|
$dbw->update( 'archive',
|
2021-02-15 22:26:08 +00:00
|
|
|
[ 'ar_actor' => $toActorId ],
|
2021-04-19 01:04:30 +00:00
|
|
|
[ 'ar_actor' => $fromActorId ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "done.\n" );
|
|
|
|
|
# Update recent changes if required
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $rc ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Updating recent changes..." );
|
2017-09-12 17:12:29 +00:00
|
|
|
$dbw->update( 'recentchanges',
|
2021-02-15 22:26:08 +00:00
|
|
|
[ 'rc_actor' => $toActorId ],
|
2021-04-19 01:04:30 +00:00
|
|
|
[ 'rc_actor' => $fromActorId ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "done.\n" );
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
return (int)$total;
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise the user object
|
|
|
|
|
*
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param string $username Username or IP address
|
2009-08-02 19:35:17 +00:00
|
|
|
* @return User
|
|
|
|
|
*/
|
|
|
|
|
private function initialiseUser( $username ) {
|
2021-04-12 14:45:26 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
if ( $services->getUserNameUtils()->isIP( $username ) ) {
|
2020-07-24 16:28:48 +00:00
|
|
|
$user = User::newFromName( $username, false );
|
|
|
|
|
$user->getActorId();
|
2009-08-02 19:35:17 +00:00
|
|
|
} else {
|
|
|
|
|
$user = User::newFromName( $username );
|
2011-11-20 10:55:58 +00:00
|
|
|
if ( !$user ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid username" );
|
2011-11-20 10:55:58 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
$user->load();
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
return $user;
|
|
|
|
|
}
|
2009-06-24 02:49:24 +00:00
|
|
|
}
|
2009-06-24 02:02:37 +00:00
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = ReassignEdits::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|