Add ability to override mb_strtoupper in Language::ucfirst
[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 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @license GPL-2.0-or-later
24 */
25
26 use Wikimedia\Rdbms\IDatabase;
27
28 require_once __DIR__ . '/Maintenance.php';
29
30 /**
31 * Maintenance script that reassigns edits from a user or IP address
32 * to another user.
33 *
34 * @ingroup Maintenance
35 */
36 class ReassignEdits extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Reassign edits from one user to another' );
40 $this->addOption( "force", "Reassign even if the target user doesn't exist" );
41 $this->addOption( "norc", "Don't update the recent changes table" );
42 $this->addOption( "report", "Print out details of what would be changed, but don't update it" );
43 $this->addArg( 'from', 'Old user to take edits from' );
44 $this->addArg( 'to', 'New user to give edits to' );
45 }
46
47 public function execute() {
48 if ( $this->hasArg( 0 ) && $this->hasArg( 1 ) ) {
49 # Set up the users involved
50 $from = $this->initialiseUser( $this->getArg( 0 ) );
51 $to = $this->initialiseUser( $this->getArg( 1 ) );
52
53 # If the target doesn't exist, and --force is not set, stop here
54 if ( $to->getId() || $this->hasOption( 'force' ) ) {
55 # Reassign the edits
56 $report = $this->hasOption( 'report' );
57 $this->doReassignEdits( $from, $to, !$this->hasOption( 'norc' ), $report );
58 # If reporting, and there were items, advise the user to run without --report
59 if ( $report ) {
60 $this->output( "Run the script again without --report to update.\n" );
61 }
62 } else {
63 $ton = $to->getName();
64 $this->error( "User '{$ton}' not found." );
65 }
66 }
67 }
68
69 /**
70 * Reassign edits from one user to another
71 *
72 * @param User $from User to take edits from
73 * @param User $to User to assign edits to
74 * @param bool $rc Update the recent changes table
75 * @param bool $report Don't change things; just echo numbers
76 * @return int Number of entries changed, or that would be changed
77 */
78 private function doReassignEdits( &$from, &$to, $rc = false, $report = false ) {
79 global $wgActorTableSchemaMigrationStage;
80
81 $dbw = $this->getDB( DB_MASTER );
82 $this->beginTransaction( $dbw, __METHOD__ );
83
84 # Count things
85 $this->output( "Checking current edits..." );
86 $revQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rev_user', $from );
87 $res = $dbw->select(
88 [ 'revision' ] + $revQueryInfo['tables'],
89 'COUNT(*) AS count',
90 $revQueryInfo['conds'],
91 __METHOD__,
92 [],
93 $revQueryInfo['joins']
94 );
95 $row = $dbw->fetchObject( $res );
96 $cur = $row->count;
97 $this->output( "found {$cur}.\n" );
98
99 $this->output( "Checking deleted edits..." );
100 $arQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'ar_user', $from, false );
101 $res = $dbw->select(
102 [ 'archive' ] + $arQueryInfo['tables'],
103 'COUNT(*) AS count',
104 $arQueryInfo['conds'],
105 __METHOD__,
106 [],
107 $arQueryInfo['joins']
108 );
109 $row = $dbw->fetchObject( $res );
110 $del = $row->count;
111 $this->output( "found {$del}.\n" );
112
113 # Don't count recent changes if we're not supposed to
114 if ( $rc ) {
115 $this->output( "Checking recent changes..." );
116 $rcQueryInfo = ActorMigration::newMigration()->getWhere( $dbw, 'rc_user', $from, false );
117 $res = $dbw->select(
118 [ 'recentchanges' ] + $rcQueryInfo['tables'],
119 'COUNT(*) AS count',
120 $rcQueryInfo['conds'],
121 __METHOD__,
122 [],
123 $rcQueryInfo['joins']
124 );
125 $row = $dbw->fetchObject( $res );
126 $rec = $row->count;
127 $this->output( "found {$rec}.\n" );
128 } else {
129 $rec = 0;
130 }
131
132 $total = $cur + $del + $rec;
133 $this->output( "\nTotal entries to change: {$total}\n" );
134
135 if ( !$report ) {
136 if ( $total ) {
137 # Reassign edits
138 $this->output( "\nReassigning current edits..." );
139 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
140 $dbw->update(
141 'revision',
142 [
143 'rev_user' => $to->getId(),
144 'rev_user_text' => $to->getName(),
145 ],
146 $from->isLoggedIn()
147 ? [ 'rev_user' => $from->getId() ] : [ 'rev_user_text' => $from->getName() ],
148 __METHOD__
149 );
150 }
151 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
152 $dbw->update(
153 'revision_actor_temp',
154 [ 'revactor_actor' => $to->getActorId( $dbw ) ],
155 [ 'revactor_actor' => $from->getActorId() ],
156 __METHOD__
157 );
158 }
159 $this->output( "done.\nReassigning deleted edits..." );
160 $dbw->update( 'archive',
161 $this->userSpecification( $dbw, $to, 'ar_user', 'ar_user_text', 'ar_actor' ),
162 [ $arQueryInfo['conds'] ], __METHOD__ );
163 $this->output( "done.\n" );
164 # Update recent changes if required
165 if ( $rc ) {
166 $this->output( "Updating recent changes..." );
167 $dbw->update( 'recentchanges',
168 $this->userSpecification( $dbw, $to, 'rc_user', 'rc_user_text', 'rc_actor' ),
169 [ $rcQueryInfo['conds'] ], __METHOD__ );
170 $this->output( "done.\n" );
171 }
172 }
173 }
174
175 $this->commitTransaction( $dbw, __METHOD__ );
176
177 return (int)$total;
178 }
179
180 /**
181 * Return user specifications for an UPDATE
182 * i.e. user => id, user_text => text
183 *
184 * @param IDatabase $dbw Database handle
185 * @param User $user User for the spec
186 * @param string $idfield Field name containing the identifier
187 * @param string $utfield Field name containing the user text
188 * @param string $acfield Field name containing the actor ID
189 * @return array
190 */
191 private function userSpecification( IDatabase $dbw, &$user, $idfield, $utfield, $acfield ) {
192 global $wgActorTableSchemaMigrationStage;
193
194 $ret = [];
195 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
196 $ret += [
197 $idfield => $user->getId(),
198 $utfield => $user->getName(),
199 ];
200 }
201 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
202 $ret += [ $acfield => $user->getActorId( $dbw ) ];
203 }
204 return $ret;
205 }
206
207 /**
208 * Initialise the user object
209 *
210 * @param string $username Username or IP address
211 * @return User
212 */
213 private function initialiseUser( $username ) {
214 if ( User::isIP( $username ) ) {
215 $user = new User();
216 $user->setId( 0 );
217 $user->setName( $username );
218 } else {
219 $user = User::newFromName( $username );
220 if ( !$user ) {
221 $this->fatalError( "Invalid username" );
222 }
223 }
224 $user->load();
225
226 return $user;
227 }
228 }
229
230 $maintClass = ReassignEdits::class;
231 require_once RUN_MAINTENANCE_IF_MAIN;