4 * Support functions for the reassignEdits script
8 * @author Rob Church <robchur@gmail.com>
9 * @licence GNU General Public Licence 2.0 or later
13 * Reassign edits from one user to another
15 * @param $from User to take edits from
16 * @param $to User to assign edits to
17 * @param $rc Update the recent changes table
18 * @param $report Don't change things; just echo numbers
19 * @return integer Number of entries changed, or that would be changed
21 function reassignEdits( &$from, &$to, $rc = false, $report = false ) {
22 $dbw = wfGetDB( DB_MASTER
);
23 $dbw->immediateBegin();
24 $fname = 'reassignEdits';
27 out( "Checking current edits..." );
28 $res = $dbw->select( 'revision', 'COUNT(*) AS count', userConditions( $from, 'rev_user', 'rev_user_text' ), $fname );
29 $row = $dbw->fetchObject( $res );
31 out( "found {$cur}.\n" );
33 out( "Checking deleted edits..." );
34 $res = $dbw->select( 'archive', 'COUNT(*) AS count', userConditions( $from, 'ar_user', 'ar_user_text' ), $fname );
35 $row = $dbw->fetchObject( $res );
37 out( "found {$del}.\n" );
39 # Don't count recent changes if we're not supposed to
41 out( "Checking recent changes..." );
42 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', userConditions( $from, 'rc_user', 'rc_user_text' ), $fname );
43 $row = $dbw->fetchObject( $res );
45 out( "found {$rec}.\n" );
50 $total = $cur +
$del +
$rec;
51 out( "\nTotal entries to change: {$total}\n" );
56 out( "\nReassigning current edits..." );
57 $res = $dbw->update( 'revision', userSpecification( $to, 'rev_user', 'rev_user_text' ), userConditions( $from, 'rev_user', 'rev_user_text' ), $fname );
58 out( "done.\nReassigning deleted edits..." );
59 $res = $dbw->update( 'archive', userSpecification( $to, 'ar_user', 'ar_user_text' ), userConditions( $from, 'ar_user', 'ar_user_text' ), $fname );
61 # Update recent changes if required
63 out( "Updating recent changes..." );
64 $res = $dbw->update( 'recentchanges', userSpecification( $to, 'rc_user', 'rc_user_text' ), userConditions( $from, 'rc_user', 'rc_user_text' ), $fname );
70 $dbw->immediateCommit();
75 * Return the most efficient set of user conditions
76 * i.e. a user => id mapping, or a user_text => text mapping
78 * @param $user User for the condition
79 * @param $idfield Field name containing the identifier
80 * @param $utfield Field name containing the user text
83 function userConditions( &$user, $idfield, $utfield ) {
84 return $user->getId() ?
array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
88 * Return user specifications
89 * i.e. user => id, user_text => text
91 * @param $user User for the spec
92 * @param $idfield Field name containing the identifier
93 * @param $utfield Field name containing the user text
96 function userSpecification( &$user, $idfield, $utfield ) {
97 return array( $idfield => $user->getId(), $utfield => $user->getName() );
101 * Echo output if $wgSilent is off
103 * @param $output Output to echo
104 * @return bool True if the output was echoed
106 function out( $output ) {
117 * Mutator for $wgSilent
119 * @param $silent Switch on $wgSilent
121 function silent( $silent = true ) {
127 * Initialise the user object
129 * @param $username Username or IP address
132 function initialiseUser( $username ) {
133 if( User
::isIP( $username ) ) {
136 $user->setName( $username );
138 $user = User
::newFromName( $username );