3
* Maintenance script to clean up after incomplete user renames
4
* Sometimes user edits are left lying around under the old name,
5
* check for that and assign them to the new username
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License along
18
* with this program; if not, write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
* http://www.gnu.org/copyleft/gpl.html
22
* @ingroup Maintenance
23
* @author Ariel Glenn <ariel@wikimedia.org>
26
$IP = getenv( 'MW_INSTALL_PATH' );
27
if ( $IP === false ) {
28
$IP = dirname( __FILE__ ) . '/../..';
30
require_once( "$IP/maintenance/Maintenance.php" );
32
class RenameUserCleanup extends Maintenance {
33
public function __construct() {
34
parent::__construct();
35
$this->mDescription = "Maintenance script to finish incomplete rename user, in particular to reassign edits that were missed";
36
$this->addOption( 'olduser', 'Old user name', true, true );
37
$this->addOption( 'newuser', 'New user name', true, true );
38
$this->addOption( 'olduid', 'Old user id in revision records (DANGEROUS)', false, true );
39
$this->mBatchSize = 1000;
42
public function execute() {
43
$this->output( "Rename User Cleanup starting...\n\n" );
44
$olduser = User::newFromName( $this->getOption( 'olduser' ) );
45
$newuser = User::newFromName( $this->getOption( 'newuser' ) );
46
$olduid = $this->getOption( 'olduid' );
48
$this->checkUserExistence( $olduser, $newuser );
49
$this->checkRenameLog( $olduser, $newuser );
52
$this->doUpdates( $olduser, $newuser, $olduid );
54
$this->doUpdates( $olduser, $newuser, $newuser->getId() );
55
$this->doUpdates( $olduser, $newuser, 0 );
62
* @param $olduser User
63
* @param $newuser User
65
public function checkUserExistence( $olduser, $newuser ) {
66
if ( !$newuser->getId() ) {
67
$this->error( "No such user: " . $this->getOption( 'newuser' ), true );
70
if ($olduser->getId() ) {
71
print "WARNING!!: Old user still exists: " . $this->getOption( 'olduser' ) . "\n";
72
print "proceed anyways? We'll only re-attribute edits that have the new user uid (or 0)";
73
print " or the uid specified by the caller, and the old user name. [N/y] ";
74
$stdin = fopen ("php://stdin","rt");
75
$line = fgets($stdin);
77
if ( $line[0] != "Y" && $line[0] != "y" ) {
78
print "Exiting at user's request\n";
85
* @param $olduser User
86
* @param $newuser User
88
public function checkRenameLog( $olduser, $newuser ) {
89
$dbr = wfGetDB( DB_SLAVE );
91
$oldTitle = Title::makeTitle( NS_USER, $olduser->getName() );
93
$result = $dbr->select( 'logging', '*',
94
array( 'log_type' => 'renameuser',
95
'log_action' => 'renameuser',
96
'log_namespace' => NS_USER,
97
'log_title' => $oldTitle->getDBkey(),
98
'log_params' => $newuser->getName()
102
if (! $result || ! $result->numRows() ) {
103
// try the old format
104
$result = $dbr->select( 'logging', '*',
105
array( 'log_type' => 'renameuser',
106
'log_action' => 'renameuser',
107
'log_namespace' => NS_USER,
108
'log_title' => $olduser->getName(),
112
if (! $result || ! $result->numRows() ) {
113
print "No log entry found for a rename of ".$olduser->getName()." to ".$newuser->getName().", proceed anyways??? [N/y] ";
114
$stdin = fopen ("php://stdin","rt");
115
$line = fgets($stdin);
117
if ( $line[0] != "Y" && $line[0] != "y" ) {
118
print "Exiting at user's request\n";
122
foreach ( $result as $row ) {
123
print "Found possible log entry of the rename, please check: ".$row->log_title." with comment ".$row->log_comment." on $row->log_timestamp\n";
127
foreach ( $result as $row ) {
128
print "Found log entry of the rename: ".$olduser->getName()." to ".$newuser->getName()." on $row->log_timestamp\n";
131
if ($result && $result->numRows() > 1) {
132
print "More than one rename entry found in the log, not sure what to do. Continue anyways? [N/y] ";
133
$stdin = fopen ("php://stdin","rt");
134
$line = fgets($stdin);
136
if ( $line[0] != "Y" && $line[0] != "y" ) {
137
print "Exiting at user's request\n";
144
* @param $olduser User
145
* @param $newuser User
148
public function doUpdates( $olduser, $newuser, $uid ) {
149
$this->updateTable( 'revision', 'rev_user_text', 'rev_user', 'rev_timestamp', $olduser, $newuser, $uid );
150
$this->updateTable( 'archive', 'ar_user_text', 'ar_user', 'ar_timestamp', $olduser, $newuser, $uid );
151
$this->updateTable( 'logging', 'log_user_text', 'log_user', 'log_timestamp', $olduser, $newuser, $uid );
152
$this->updateTable( 'image', 'img_user_text', 'img_user', 'img_timestamp', $olduser, $newuser, $uid );
153
$this->updateTable( 'oldimage', 'oi_user_text', 'oi_user', 'oi_timestamp', $olduser, $newuser, $uid );
154
$this->updateTable( 'filearchive', 'fa_user_text','fa_user', 'fa_timestamp', $olduser, $newuser, $uid );
159
* @param $usernamefield
160
* @param $useridfield
161
* @param $timestampfield
162
* @param $olduser User
163
* @param $newuser User
167
public function updateTable( $table, $usernamefield, $useridfield, $timestampfield, $olduser, $newuser, $uid ) {
168
$dbw = wfGetDB( DB_MASTER );
170
$contribs = $dbw->selectField( $table, 'count(*)',
171
array( $usernamefield => $olduser->getName(), $useridfield => $uid ), __METHOD__ );
173
if ( $contribs == 0 ) {
174
print "No edits to be re-attributed from table $table for uid $uid\n" ;
178
print "Found $contribs edits to be re-attributed from table $table for uid $uid\n";
179
if ( $uid != $newuser->getId() ) {
180
print "If you proceed, the uid field will be set to that of the new user name (i.e. ".$newuser->getId().") in these rows.\n";
183
print "Proceed? [N/y] ";
184
$stdin = fopen ("php://stdin","rt");
185
$line = fgets($stdin);
187
if ( $line[0] != "Y" && $line[0] != "y" ) {
188
print "Skipping at user's request\n";
192
$selectConds = array( $usernamefield => $olduser->getName(), $useridfield => $uid );
193
$updateFields = array( $usernamefield => $newuser->getName(), $useridfield => $newuser->getId() );
195
while ( $contribs > 0 ) {
196
print "Doing batch of up to approximately ".$this->mBatchSize."\n";
197
print "Do this batch? [N/y] ";
198
$stdin = fopen ("php://stdin","rt");
199
$line = fgets($stdin);
201
if ( $line[0] != "Y" && $line[0] != "y" ) {
202
print "Skipping at user's request\n";
206
$result = $dbw->select( $table, $timestampfield, $selectConds , __METHOD__,
207
array( 'ORDER BY' => $timestampfield.' DESC', 'LIMIT' => $this->mBatchSize ) );
209
print "There were rows for updating but now they are gone. Skipping.\n";
213
$result->seek($result->numRows() -1 );
214
$row = $result->fetchObject();
215
$timestamp = $row->$timestampfield;
216
$updateCondsWithTime = array_merge( $selectConds, array ("$timestampfield >= $timestamp") );
217
$success = $dbw->update( $table, $updateFields, $updateCondsWithTime, __METHOD__ );
219
$rowsDone = $dbw->affectedRows();
222
print "Problem with the update, rolling back and exiting\n";
226
//$contribs = User::edits( $olduser->getId() );
227
$contribs = $dbw->selectField( $table, 'count(*)', $selectConds, __METHOD__ );
228
print "Updated $rowsDone edits; $contribs edits remaining to be re-attributed\n";
235
$maintClass = "RenameUserCleanup";
236
require_once( RUN_MAINTENANCE_IF_MAIN );