Revert r54244 which was stupid and fix this properly. Require commandLine.inc/Mainten...
[lhc/web/wiklou.git] / maintenance / reassignEdits.php
1 <?php
2 /**
3 * Reassign edits from a user or IP address to another user
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 * @author Rob Church <robchur@gmail.com>
22 * @licence GNU General Public Licence 2.0 or later
23 */
24
25 require_once( dirname(__FILE__) . '/Maintenance.php' );
26
27 class ReassignEdits extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Reassign edits from one user to another";
31 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
32 $this->addOption( "norc", "Don't update the recent changes table" );
33 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
34 $this->addArgs( array( 'from', 'to' ) );
35 }
36
37 public function execute() {
38 if( $this->hasArg(0) && $this->hasArg(1) ) {
39 # Set up the users involved
40 $from =& $this->initialiseUser( $this->getArg(0) );
41 $to =& $this->initialiseUser( $this->getArg(1) );
42
43 # If the target doesn't exist, and --force is not set, stop here
44 if( $to->getId() || $this->hasOption('force') ) {
45 # Reassign the edits
46 $report = $this->hasOption('report');
47 $count = $this->doReassignEdits( $from, $to, !$this->hasOption('norc'), $report );
48 # If reporting, and there were items, advise the user to run without --report
49 if( $report )
50 $this->output( "Run the script again without --report to update.\n" );
51 } else {
52 $ton = $to->getName();
53 $this->error( "User '{$ton}' not found." );
54 }
55 }
56 }
57
58 /**
59 * Reassign edits from one user to another
60 *
61 * @param $from User to take edits from
62 * @param $to User to assign edits to
63 * @param $rc Update the recent changes table
64 * @param $report Don't change things; just echo numbers
65 * @return integer Number of entries changed, or that would be changed
66 */
67 private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
68 $dbw = wfGetDB( DB_MASTER );
69 $dbw->immediateBegin();
70
71 # Count things
72 $this->output( "Checking current edits..." );
73 $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
74 $row = $dbw->fetchObject( $res );
75 $cur = $row->count;
76 $this->output( "found {$cur}.\n" );
77
78 $this->output( "Checking deleted edits..." );
79 $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
80 $row = $dbw->fetchObject( $res );
81 $del = $row->count;
82 $this->output( "found {$del}.\n" );
83
84 # Don't count recent changes if we're not supposed to
85 if( $rc ) {
86 $this->output( "Checking recent changes..." );
87 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
88 $row = $dbw->fetchObject( $res );
89 $rec = $row->count;
90 $this->output( "found {$rec}.\n" );
91 } else {
92 $rec = 0;
93 }
94
95 $total = $cur + $del + $rec;
96 $this->output( "\nTotal entries to change: {$total}\n" );
97
98 if( !$report ) {
99 if( $total ) {
100 # Reassign edits
101 $this->output( "\nReassigning current edits..." );
102 $res = $dbw->update( 'revision', userSpecification( $to, 'rev_user', 'rev_user_text' ), $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
103 $this->output( "done.\nReassigning deleted edits..." );
104 $res = $dbw->update( 'archive', userSpecification( $to, 'ar_user', 'ar_user_text' ), $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
105 $this->output( "done.\n" );
106 # Update recent changes if required
107 if( $rc ) {
108 $this->output( "Updating recent changes..." );
109 $res = $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ), $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
110 $this->output( "done.\n" );
111 }
112 }
113 }
114
115 $dbw->immediateCommit();
116 return (int)$total;
117 }
118
119 /**
120 * Return the most efficient set of user conditions
121 * i.e. a user => id mapping, or a user_text => text mapping
122 *
123 * @param $user User for the condition
124 * @param $idfield Field name containing the identifier
125 * @param $utfield Field name containing the user text
126 * @return array
127 */
128 private function userConditions( &$user, $idfield, $utfield ) {
129 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
130 }
131
132 /**
133 * Return user specifications
134 * i.e. user => id, user_text => text
135 *
136 * @param $user User for the spec
137 * @param $idfield Field name containing the identifier
138 * @param $utfield Field name containing the user text
139 * @return array
140 */
141 private function userSpecification( &$user, $idfield, $utfield ) {
142 return array( $idfield => $user->getId(), $utfield => $user->getName() );
143 }
144
145 /**
146 * Initialise the user object
147 *
148 * @param $username Username or IP address
149 * @return User
150 */
151 private function initialiseUser( $username ) {
152 if( User::isIP( $username ) ) {
153 $user = new User();
154 $user->setId( 0 );
155 $user->setName( $username );
156 } else {
157 $user = User::newFromName( $username );
158 }
159 $user->load();
160 return $user;
161 }
162
163
164 }
165
166 $maintClass = "ReassignEdits";
167 require_once( DO_MAINTENANCE );
168