Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / maintenance / reassignEdits.inc.php
blobe68b4cf51233bc23af10d3f7c5312df9aec89b45
1 <?php
3 /**
4 * Support functions for the reassignEdits script
6 * @file
7 * @ingroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 * @licence GNU General Public Licence 2.0 or later
12 /**
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';
26 # Count things
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 );
30 $cur = $row->count;
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 );
36 $del = $row->count;
37 out( "found {$del}.\n" );
39 # Don't count recent changes if we're not supposed to
40 if( $rc ) {
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 );
44 $rec = $row->count;
45 out( "found {$rec}.\n" );
46 } else {
47 $rec = 0;
50 $total = $cur + $del + $rec;
51 out( "\nTotal entries to change: {$total}\n" );
53 if( !$report ) {
54 if( $total ) {
55 # Reassign edits
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 );
60 out( "done.\n" );
61 # Update recent changes if required
62 if( $rc ) {
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 );
65 out( "done.\n" );
70 $dbw->immediateCommit();
71 return (int)$total;
74 /**
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
81 * @return array
83 function userConditions( &$user, $idfield, $utfield ) {
84 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
87 /**
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
94 * @return array
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 ) {
107 global $wgSilent;
108 if( !$wgSilent ) {
109 echo( $output );
110 return true;
111 } else {
112 return false;
117 * Mutator for $wgSilent
119 * @param $silent Switch on $wgSilent
121 function silent( $silent = true ) {
122 global $wgSilent;
123 $wgSilent = $silent;
127 * Initialise the user object
129 * @param $username Username or IP address
130 * @return User
132 function initialiseUser( $username ) {
133 if( User::isIP( $username ) ) {
134 $user = new User();
135 $user->setId( 0 );
136 $user->setName( $username );
137 } else {
138 $user = User::newFromName( $username );
140 $user->load();
141 return $user;