Whitelist the <wbr> element.
[mediawiki.git] / maintenance / reassignEdits.php
blob7e15c09eb864e6df4636f4eb4c9c40dac48b4faa
1 <?php
2 /**
3 * Reassign edits from a user or IP address to another user
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.
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.
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
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @licence GNU General Public Licence 2.0 or later
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that reassigns edits from a user or IP address
30 * to another user.
32 * @ingroup Maintenance
34 class ReassignEdits extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->mDescription = "Reassign edits from one user to another";
38 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
39 $this->addOption( "norc", "Don't update the recent changes table" );
40 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
41 $this->addArg( 'from', 'Old user to take edits from' );
42 $this->addArg( 'to', 'New user to give edits to' );
45 public function execute() {
46 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
47 # Set up the users involved
48 $from = $this->initialiseUser( $this->getArg( 0 ) );
49 $to = $this->initialiseUser( $this->getArg( 1 ) );
51 # If the target doesn't exist, and --force is not set, stop here
52 if ( $to->getId() || $this->hasOption( 'force' ) ) {
53 # Reassign the edits
54 $report = $this->hasOption( 'report' );
55 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
56 # If reporting, and there were items, advise the user to run without --report
57 if ( $report ) {
58 $this->output( "Run the script again without --report to update.\n" );
60 } else {
61 $ton = $to->getName();
62 $this->error( "User '{$ton}' not found." );
67 /**
68 * Reassign edits from one user to another
70 * @param $from User to take edits from
71 * @param $to User to assign edits to
72 * @param $rc bool Update the recent changes table
73 * @param $report bool Don't change things; just echo numbers
74 * @return integer Number of entries changed, or that would be changed
76 private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
77 $dbw = wfGetDB( DB_MASTER );
78 $dbw->begin( __METHOD__ );
80 # Count things
81 $this->output( "Checking current edits..." );
82 $res = $dbw->select( 'revision', 'COUNT(*) AS count', $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
83 $row = $dbw->fetchObject( $res );
84 $cur = $row->count;
85 $this->output( "found {$cur}.\n" );
87 $this->output( "Checking deleted edits..." );
88 $res = $dbw->select( 'archive', 'COUNT(*) AS count', $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
89 $row = $dbw->fetchObject( $res );
90 $del = $row->count;
91 $this->output( "found {$del}.\n" );
93 # Don't count recent changes if we're not supposed to
94 if ( $rc ) {
95 $this->output( "Checking recent changes..." );
96 $res = $dbw->select( 'recentchanges', 'COUNT(*) AS count', $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
97 $row = $dbw->fetchObject( $res );
98 $rec = $row->count;
99 $this->output( "found {$rec}.\n" );
100 } else {
101 $rec = 0;
104 $total = $cur + $del + $rec;
105 $this->output( "\nTotal entries to change: {$total}\n" );
107 if ( !$report ) {
108 if ( $total ) {
109 # Reassign edits
110 $this->output( "\nReassigning current edits..." );
111 $dbw->update( 'revision', $this->userSpecification( $to, 'rev_user', 'rev_user_text' ),
112 $this->userConditions( $from, 'rev_user', 'rev_user_text' ), __METHOD__ );
113 $this->output( "done.\nReassigning deleted edits..." );
114 $dbw->update( 'archive', $this->userSpecification( $to, 'ar_user', 'ar_user_text' ),
115 $this->userConditions( $from, 'ar_user', 'ar_user_text' ), __METHOD__ );
116 $this->output( "done.\n" );
117 # Update recent changes if required
118 if ( $rc ) {
119 $this->output( "Updating recent changes..." );
120 $dbw->update( 'recentchanges', $this->userSpecification( $to, 'rc_user', 'rc_user_text' ),
121 $this->userConditions( $from, 'rc_user', 'rc_user_text' ), __METHOD__ );
122 $this->output( "done.\n" );
127 $dbw->commit( __METHOD__ );
128 return (int)$total;
132 * Return the most efficient set of user conditions
133 * i.e. a user => id mapping, or a user_text => text mapping
135 * @param $user User for the condition
136 * @param $idfield string Field name containing the identifier
137 * @param $utfield string Field name containing the user text
138 * @return array
140 private function userConditions( &$user, $idfield, $utfield ) {
141 return $user->getId() ? array( $idfield => $user->getId() ) : array( $utfield => $user->getName() );
145 * Return user specifications
146 * i.e. user => id, user_text => text
148 * @param $user User for the spec
149 * @param $idfield string Field name containing the identifier
150 * @param $utfield string Field name containing the user text
151 * @return array
153 private function userSpecification( &$user, $idfield, $utfield ) {
154 return array( $idfield => $user->getId(), $utfield => $user->getName() );
158 * Initialise the user object
160 * @param $username string Username or IP address
161 * @return User
163 private function initialiseUser( $username ) {
164 if ( User::isIP( $username ) ) {
165 $user = new User();
166 $user->setId( 0 );
167 $user->setName( $username );
168 } else {
169 $user = User::newFromName( $username );
170 if ( !$user ) {
171 $this->error( "Invalid username", true );
174 $user->load();
175 return $user;
181 $maintClass = "ReassignEdits";
182 require_once RUN_MAINTENANCE_IF_MAIN;