3 * Reassign edits from a user or IP address to another user
6 * @author Rob Church <robchur@gmail.com>
7 * @licence GNU General Public Licence 2.0 or later
10 require_once( "Maintenance.php" );
12 class ReassignEdits
extends Maintenance
{
13 public function __construct() {
14 parent
::__construct();
15 $this->mDescription
= "Reassign edits from one user to another";
16 $this->addParam( "force", "Reassign even if the target user doesn't exist" );
17 $this->addParam( "norc", "Don't update the recent changes table" );
18 $this->addParam( "report", "Print out details of what would be changed, but don't update it" );
19 $this->addArgs( array( 'from', 'to' ) );
22 public function execute() {
23 if( $this->hasArg(0) && $this->hasArg(1) ) {
24 # Set up the users involved
25 $from =& $this->initialiseUser( $this->getArg(0) );
26 $to =& $this->initialiseUser( $this->getArg(1) );
28 # If the target doesn't exist, and --force is not set, stop here
29 if( $to->getId() ||
$this->hasOption('force') ) {
31 $report = $this->hasOption('report');
32 $count = $this->reassignEdits( $from, $to, !$this->hasOption('norc'), $report );
33 # If reporting, and there were items, advise the user to run without --report
35 $this->output( "Run the script again without --report to update.\n" );
37 $ton = $to->getName();
38 $this->error( "User '{$ton}' not found.\n" );
44 * Reassign edits from one user to another
46 * @param $from User to take edits from
47 * @param $to User to assign edits to
48 * @param $rc Update the recent changes table
49 * @param $report Don't change things; just echo numbers
50 * @return integer Number of entries changed, or that would be changed
52 private function reassignEdits( &$from, &$to, $rc = false, $report = false ) {
53 $dbw = wfGetDB( DB_MASTER
);
54 $dbw->immediateBegin();
57 $this->output( "Checking current edits..." );
58 $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__
);
59 $row = $dbw->fetchObject( $res );
61 $this->output( "found {$cur}.\n" );
63 $this->output( "Checking deleted edits..." );
64 $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__
);
65 $row = $dbw->fetchObject( $res );
67 $this->output( "found {$del}.\n" );
69 # Don't count recent changes if we're not supposed to
71 $this->output( "Checking recent changes..." );
72 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__
);
73 $row = $dbw->fetchObject( $res );
75 $this->output( "found {$rec}.\n" );
80 $total = $cur +
$del +
$rec;
81 $this->output( "\nTotal entries to change: {$total}\n" );
86 $this->output( "\nReassigning current edits..." );
87 $res = $dbw->update( 'revision', userSpecification( $to, 'rev_user', 'rev_user_text' ), $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__
);
88 $this->output( "done.\nReassigning deleted edits..." );
89 $res = $dbw->update( 'archive', userSpecification( $to, 'ar_user', 'ar_user_text' ), $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__
);
90 $this->output( "done.\n" );
91 # Update recent changes if required
93 $this->output( "Updating recent changes..." );
94 $res = $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ), $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__
);
95 $this->output( "done.\n" );
100 $dbw->immediateCommit();
105 * Return the most efficient set of user conditions
106 * i.e. a user => id mapping, or a user_text => text mapping
108 * @param $user User for the condition
109 * @param $idfield Field name containing the identifier
110 * @param $utfield Field name containing the user text
113 private function userConditions( &$user, $idfield, $utfield ) {
114 return $user->getId() ?
array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
118 * Return user specifications
119 * i.e. user => id, user_text => text
121 * @param $user User for the spec
122 * @param $idfield Field name containing the identifier
123 * @param $utfield Field name containing the user text
126 private function userSpecification( &$user, $idfield, $utfield ) {
127 return array( $idfield => $user->getId(), $utfield => $user->getName() );
131 * Initialise the user object
133 * @param $username Username or IP address
136 private function initialiseUser( $username ) {
137 if( User
::isIP( $username ) ) {
140 $user->setName( $username );
142 $user = User
::newFromName( $username );
151 $maintClass = "ReassignEdits";
152 require_once( DO_MAINTENANCE
);