* Double-escaping, checkLabel, fixed another label
[mediawiki.git] / maintenance / reassignEdits.inc.php
blobda05016373d63cfb1cd1a668726ceb36e6b5081a
1 <?php
3 /**
4 * Support functions for the reassignEdits script
6 * @addtogroup Maintenance
7 * @author Rob Church <robchur@gmail.com>
8 * @licence GNU General Public Licence 2.0 or later
9 */
11 /**
12 * Reassign edits from one user to another
14 * @param $from User to take edits from
15 * @param $to User to assign edits to
16 * @param $rc Update the recent changes table
17 * @param $report Don't change things; just echo numbers
18 * @return integer Number of entries changed, or that would be changed
20 function reassignEdits( &$from, &$to, $rc = false, $report = false ) {
21 $dbw = wfGetDB( DB_MASTER );
22 $dbw->immediateBegin();
23 $fname = 'reassignEdits';
25 # Count things
26 out( "Checking current edits..." );
27 $res = $dbw->select( 'revision', 'COUNT(*) AS count', userConditions( $from, 'rev_user', 'rev_user_text' ), $fname );
28 $row = $dbw->fetchObject( $res );
29 $cur = $row->count;
30 out( "found {$cur}.\n" );
32 out( "Checking deleted edits..." );
33 $res = $dbw->select( 'archive', 'COUNT(*) AS count', userConditions( $from, 'ar_user', 'ar_user_text' ), $fname );
34 $row = $dbw->fetchObject( $res );
35 $del = $row->count;
36 out( "found {$del}.\n" );
38 # Don't count recent changes if we're not supposed to
39 if( $rc ) {
40 out( "Checking recent changes..." );
41 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', userConditions( $from, 'rc_user', 'rc_user_text' ), $fname );
42 $row = $dbw->fetchObject( $res );
43 $rec = $row->count;
44 out( "found {$rec}.\n" );
45 } else {
46 $rec = 0;
49 $total = $cur + $del + $rec;
50 out( "\nTotal entries to change: {$total}\n" );
52 if( !$report ) {
53 if( $total ) {
54 # Reassign edits
55 out( "\nReassigning current edits..." );
56 $res = $dbw->update( 'revision', userSpecification( $to, 'rev_user', 'rev_user_text' ), userConditions( $from, 'rev_user', 'rev_user_text' ), $fname );
57 out( "done.\nReassigning deleted edits..." );
58 $res = $dbw->update( 'archive', userSpecification( $to, 'ar_user', 'ar_user_text' ), userConditions( $from, 'ar_user', 'ar_user_text' ), $fname );
59 out( "done.\n" );
60 # Update recent changes if required
61 if( $rc ) {
62 out( "Updating recent changes..." );
63 $res = $dbw->update( 'recentchanges', userSpecification( $to, 'rc_user', 'rc_user_text' ), userConditions( $from, 'rc_user', 'rc_user_text' ), $fname );
64 out( "done.\n" );
69 $dbw->immediateCommit();
70 return (int)$total;
73 /**
74 * Return the most efficient set of user conditions
75 * i.e. a user => id mapping, or a user_text => text mapping
77 * @param $user User for the condition
78 * @param $idfield Field name containing the identifier
79 * @param $utfield Field name containing the user text
80 * @return array
82 function userConditions( &$user, $idfield, $utfield ) {
83 return $user->getId() ? array( $idfield => $user->getID() ) : array( $utfield => $user->getName() );
86 /**
87 * Return user specifications
88 * i.e. user => id, user_text => text
90 * @param $user User for the spec
91 * @param $idfield Field name containing the identifier
92 * @param $utfield Field name containing the user text
93 * @return array
95 function userSpecification( &$user, $idfield, $utfield ) {
96 return array( $idfield => $user->getId(), $utfield => $user->getName() );
99 /**
100 * Echo output if $wgSilent is off
102 * @param $output Output to echo
103 * @return bool True if the output was echoed
105 function out( $output ) {
106 global $wgSilent;
107 if( !$wgSilent ) {
108 echo( $output );
109 return true;
110 } else {
111 return false;
116 * Mutator for $wgSilent
118 * @param $silent Switch on $wgSilent
120 function silent( $silent = true ) {
121 global $wgSilent;
122 $wgSilent = $silent;
126 * Initialise the user object
128 * @param $username Username or IP address
129 * @return User
131 function initialiseUser( $username ) {
132 if( User::isIP( $username ) ) {
133 $user = new User();
134 $user->setId( 0 );
135 $user->setName( $username );
136 } else {
137 $user = User::newFromName( $username );
139 $user->load();
140 return $user;