26 $IP = getenv(
'MW_INSTALL_PATH' );
27 if (
$IP ===
false ) {
28 $IP = __DIR__ .
'/../..';
30 require_once
"$IP/maintenance/Maintenance.php";
34 parent::__construct();
35 $this->mDescription =
'Maintenance script to finish incomplete rename user, ' .
36 'in particular to reassign edits that were missed';
37 $this->
addOption(
'olduser',
'Old user name',
true,
true );
38 $this->
addOption(
'newuser',
'New user name',
true,
true );
39 $this->
addOption(
'olduid',
'Old user id in revision records (DANGEROUS)',
false,
true );
40 $this->mBatchSize = 1000;
44 $this->
output(
"Rename User Cleanup starting...\n\n" );
53 $this->
doUpdates( $olduser, $newuser, $olduid );
55 $this->
doUpdates( $olduser, $newuser, $newuser->getId() );
56 $this->
doUpdates( $olduser, $newuser, 0 );
67 if ( !$newuser->getId() ) {
68 $this->
error(
'No such user: ' . $this->
getOption(
'newuser' ),
true );
71 if ( $olduser->getId() ) {
72 print
'WARNING!!: Old user still exists: ' . $this->
getOption(
'olduser' ) .
"\n";
73 print
"proceed anyways? We'll only re-attribute edits that have the new user uid (or 0)";
74 print
' or the uid specified by the caller, and the old user name. [N/y] ';
75 $stdin = fopen(
'php://stdin',
'rt' );
76 $line = fgets( $stdin );
79 print
"Exiting at user's request\n";
95 [
'log_type' =>
'renameuser',
96 'log_action' =>
'renameuser',
99 'log_params' => $newuser->getName()
106 [
'log_type' =>
'renameuser',
107 'log_action' =>
'renameuser',
109 'log_title' => $olduser->getName(),
114 print
'No log entry found for a rename of ' . $olduser->getName() .
115 ' to ' . $newuser->getName() .
', proceed anyways??? [N/y] ';
116 $stdin = fopen(
'php://stdin',
'rt' );
117 $line = fgets( $stdin );
120 print
"Exiting at user's request\n";
125 print
'Found possible log entry of the rename, please check: ' .
126 $row->log_title .
' with comment ' . $row->log_comment .
127 " on $row->log_timestamp\n";
132 print
'Found log entry of the rename: ' . $olduser->getName() .
133 ' to ' . $newuser->getName() .
" on $row->log_timestamp\n";
137 print
'More than one rename entry found in the log, not sure ' .
138 'what to do. Continue anyways? [N/y] ';
139 $stdin = fopen(
'php://stdin',
'rt' );
140 $line = fgets( $stdin );
143 print
"Exiting at user's request\n";
155 $this->
updateTable(
'revision',
'rev_user_text',
'rev_user',
'rev_timestamp',
156 $olduser, $newuser, $uid );
157 $this->
updateTable(
'archive',
'ar_user_text',
'ar_user',
'ar_timestamp',
158 $olduser, $newuser, $uid );
159 $this->
updateTable(
'logging',
'log_user_text',
'log_user',
'log_timestamp',
160 $olduser, $newuser, $uid );
161 $this->
updateTable(
'image',
'img_user_text',
'img_user',
'img_timestamp',
162 $olduser, $newuser, $uid );
163 $this->
updateTable(
'oldimage',
'oi_user_text',
'oi_user',
'oi_timestamp',
164 $olduser, $newuser, $uid );
165 $this->
updateTable(
'filearchive',
'fa_user_text',
'fa_user',
'fa_timestamp',
166 $olduser, $newuser, $uid );
179 public function updateTable( $table, $usernamefield, $useridfield,
180 $timestampfield, $olduser, $newuser, $uid
184 $contribs = $dbw->selectField( $table,
'count(*)',
185 [ $usernamefield => $olduser->getName(), $useridfield => $uid ], __METHOD__ );
187 if ( $contribs === 0 ) {
188 print
"No edits to be re-attributed from table $table for uid $uid\n";
193 print
"Found $contribs edits to be re-attributed from table $table for uid $uid\n";
194 if ( $uid !== $newuser->getId() ) {
195 print
'If you proceed, the uid field will be set to that ' .
196 'of the new user name (i.e. ' . $newuser->getId() .
") in these rows.\n";
199 print
'Proceed? [N/y] ';
200 $stdin = fopen(
'php://stdin',
'rt' );
201 $line = fgets( $stdin );
204 print
"Skipping at user's request\n";
209 $selectConds = [ $usernamefield => $olduser->getName(), $useridfield => $uid ];
210 $updateFields = [ $usernamefield => $newuser->getName(), $useridfield => $newuser->getId() ];
212 while ( $contribs > 0 ) {
213 print
'Doing batch of up to approximately ' . $this->mBatchSize .
"\n";
214 print
'Do this batch? [N/y] ';
215 $stdin = fopen(
'php://stdin',
'rt' );
216 $line = fgets( $stdin );
219 print
"Skipping at user's request\n";
224 $result = $dbw->select( $table, $timestampfield, $selectConds, __METHOD__,
225 [
'ORDER BY' => $timestampfield .
' DESC',
'LIMIT' => $this->mBatchSize ] );
227 print
"There were rows for updating but now they are gone. Skipping.\n";
234 $timestamp = $row->$timestampfield;
235 $updateCondsWithTime = array_merge( $selectConds, [
"$timestampfield >= $timestamp" ] );
236 $success = $dbw->update( $table, $updateFields, $updateCondsWithTime, __METHOD__ );
238 $rowsDone = $dbw->affectedRows();
241 print
"Problem with the update, rolling back and exiting\n";
247 $contribs = $dbw->selectField( $table,
'count(*)', $selectConds, __METHOD__ );
248 print
"Updated $rowsDone edits; $contribs edits remaining to be re-attributed\n";