]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/reassignEdits.php
MediaWiki 1.17.1-scripts
[autoinstalls/mediawiki.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->addArg( 'from', 'Old user to take edits from' );
35                 $this->addArg( 'to', 'New user to give edits to' );
36         }
37
38         public function execute() {
39                 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
40                         # Set up the users involved
41                         $from = $this->initialiseUser( $this->getArg( 0 ) );
42                         $to   = $this->initialiseUser( $this->getArg( 1 ) );
43
44                         # If the target doesn't exist, and --force is not set, stop here
45                         if ( $to->getId() || $this->hasOption( 'force' ) ) {
46                                 # Reassign the edits
47                                 $report = $this->hasOption( 'report' );
48                                 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
49                                 # If reporting, and there were items, advise the user to run without --report
50                                 if ( $report ) {
51                                         $this->output( "Run the script again without --report to update.\n" );
52                                 }
53                         } else {
54                                 $ton = $to->getName();
55                                 $this->error( "User '{$ton}' not found." );
56                         }
57                 }
58         }
59
60         /**
61          * Reassign edits from one user to another
62          *
63          * @param $from User to take edits from
64          * @param $to User to assign edits to
65          * @param $rc Update the recent changes table
66          * @param $report Don't change things; just echo numbers
67          * @return integer Number of entries changed, or that would be changed
68          */
69         private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
70                 $dbw = wfGetDB( DB_MASTER );
71                 $dbw->begin();
72
73                 # Count things
74                 $this->output( "Checking current edits..." );
75                 $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
76                 $row = $dbw->fetchObject( $res );
77                 $cur = $row->count;
78                 $this->output( "found {$cur}.\n" );
79
80                 $this->output( "Checking deleted edits..." );
81                 $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
82                 $row = $dbw->fetchObject( $res );
83                 $del = $row->count;
84                 $this->output( "found {$del}.\n" );
85
86                 # Don't count recent changes if we're not supposed to
87                 if ( $rc ) {
88                         $this->output( "Checking recent changes..." );
89                         $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
90                         $row = $dbw->fetchObject( $res );
91                         $rec = $row->count;
92                         $this->output( "found {$rec}.\n" );
93                 } else {
94                         $rec = 0;
95                 }
96
97                 $total = $cur + $del + $rec;
98                 $this->output( "\nTotal entries to change: {$total}\n" );
99
100                 if ( !$report ) {
101                         if ( $total ) {
102                                 # Reassign edits
103                                 $this->output( "\nReassigning current edits..." );
104                                 $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
105                                         $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
106                                 $this->output( "done.\nReassigning deleted edits..." );
107                                 $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
108                                         $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
109                                 $this->output( "done.\n" );
110                                 # Update recent changes if required
111                                 if ( $rc ) {
112                                         $this->output( "Updating recent changes..." );
113                                         $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
114                                                 $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
115                                         $this->output( "done.\n" );
116                                 }
117                         }
118                 }
119
120                 $dbw->commit();
121                 return (int)$total;
122         }
123
124         /**
125          * Return the most efficient set of user conditions
126          * i.e. a user => id mapping, or a user_text => text mapping
127          *
128          * @param $user User for the condition
129          * @param $idfield Field name containing the identifier
130          * @param $utfield Field name containing the user text
131          * @return array
132          */
133         private function userConditions( &$user, $idfield, $utfield ) {
134                 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
135         }
136
137         /**
138          * Return user specifications
139          * i.e. user => id, user_text => text
140          *
141          * @param $user User for the spec
142          * @param $idfield Field name containing the identifier
143          * @param $utfield Field name containing the user text
144          * @return array
145          */
146         private function userSpecification( &$user, $idfield, $utfield ) {
147                 return array( $idfield => $user->getId(), $utfield => $user->getName() );
148         }
149
150         /**
151          * Initialise the user object
152          *
153          * @param $username Username or IP address
154          * @return User
155          */
156         private function initialiseUser( $username ) {
157                 if ( User::isIP( $username ) ) {
158                         $user = new User();
159                         $user->setId( 0 );
160                         $user->setName( $username );
161                 } else {
162                         $user = User::newFromName( $username );
163                 }
164                 $user->load();
165                 return $user;
166         }
167
168
169 }
170
171 $maintClass = "ReassignEdits";
172 require_once( RUN_MAINTENANCE_IF_MAIN );
173