Merge maintenance-work branch:
[lhc/web/wiklou.git] / maintenance / reassignEdits.php
1 <?php
2 /**
3 * Reassign edits from a user or IP address to another user
4 *
5 * @ingroup Maintenance
6 * @author Rob Church <robchur@gmail.com>
7 * @licence GNU General Public Licence 2.0 or later
8 */
9
10 require_once( "Maintenance.php" );
11
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' ) );
20 }
21
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) );
27
28 # If the target doesn't exist, and --force is not set, stop here
29 if( $to->getId() || $this->hasOption('force') ) {
30 # Reassign the edits
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
34 if( $report )
35 $this->output( "Run the script again without --report to update.\n" );
36 } else {
37 $ton = $to->getName();
38 $this->error( "User '{$ton}' not found.\n" );
39 }
40 }
41 }
42
43 /**
44 * Reassign edits from one user to another
45 *
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
51 */
52 private function reassignEdits( &$from, &$to, $rc = false, $report = false ) {
53 $dbw = wfGetDB( DB_MASTER );
54 $dbw->immediateBegin();
55
56 # Count things
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 );
60 $cur = $row->count;
61 $this->output( "found {$cur}.\n" );
62
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 );
66 $del = $row->count;
67 $this->output( "found {$del}.\n" );
68
69 # Don't count recent changes if we're not supposed to
70 if( $rc ) {
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 );
74 $rec = $row->count;
75 $this->output( "found {$rec}.\n" );
76 } else {
77 $rec = 0;
78 }
79
80 $total = $cur + $del + $rec;
81 $this->output( "\nTotal entries to change: {$total}\n" );
82
83 if( !$report ) {
84 if( $total ) {
85 # Reassign edits
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
92 if( $rc ) {
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" );
96 }
97 }
98 }
99
100 $dbw->immediateCommit();
101 return (int)$total;
102 }
103
104 /**
105 * Return the most efficient set of user conditions
106 * i.e. a user => id mapping, or a user_text => text mapping
107 *
108 * @param $user User for the condition
109 * @param $idfield Field name containing the identifier
110 * @param $utfield Field name containing the user text
111 * @return array
112 */
113 private function userConditions( &$user, $idfield, $utfield ) {
114 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
115 }
116
117 /**
118 * Return user specifications
119 * i.e. user => id, user_text => text
120 *
121 * @param $user User for the spec
122 * @param $idfield Field name containing the identifier
123 * @param $utfield Field name containing the user text
124 * @return array
125 */
126 private function userSpecification( &$user, $idfield, $utfield ) {
127 return array( $idfield => $user->getId(), $utfield => $user->getName() );
128 }
129
130 /**
131 * Initialise the user object
132 *
133 * @param $username Username or IP address
134 * @return User
135 */
136 private function initialiseUser( $username ) {
137 if( User::isIP( $username ) ) {
138 $user = new User();
139 $user->setId( 0 );
140 $user->setName( $username );
141 } else {
142 $user = User::newFromName( $username );
143 }
144 $user->load();
145 return $user;
146 }
147
148
149 }
150
151 $maintClass = "ReassignEdits";
152 require_once( DO_MAINTENANCE );
153