Add batching support to fixUserRegistration.php

Also:
* Add wfWaitForSlaves() call
* Clear User cache after updating registration time
* Don't use empty()
* Use the master for everything

Bug: T92890
Change-Id: I88b97befdbd78ef12eda9a9571f6943c7b232207
This commit is contained in:
Kunal Mehta 2015-03-16 13:07:33 -07:00
parent 510dfa7d9a
commit ea406e42cd

View file

@ -33,37 +33,57 @@ class FixUserRegistration extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = "Fix the user_registration field";
$this->setBatchSize( 1000 );
}
public function execute() {
$dbr = wfGetDB( DB_SLAVE );
$dbw = wfGetDB( DB_MASTER );
// Get user IDs which need fixing
$res = $dbr->select( 'user', 'user_id', 'user_registration IS NULL', __METHOD__ );
foreach ( $res as $row ) {
$id = $row->user_id;
// Get first edit time
$timestamp = $dbr->selectField(
'revision',
'MIN(rev_timestamp)',
array( 'rev_user' => $id ),
__METHOD__
$lastId = 0;
do {
// Get user IDs which need fixing
$res = $dbw->select(
'user',
'user_id',
array(
'user_id >' . $dbw->addQuotes( $lastId ),
'user_registration IS NULL'
),
__METHOD__,
array(
'LIMIT' => $this->mBatchSize,
'ORDER BY user_id ASC',
)
);
// Update
if ( !empty( $timestamp ) ) {
$dbw->update(
'user',
array( 'user_registration' => $timestamp ),
array( 'user_id' => $id ),
foreach ( $res as $row ) {
$id = $row->user_id;
$lastId = $id;
// Get first edit time
$timestamp = $dbw->selectField(
'revision',
'MIN(rev_timestamp)',
array( 'rev_user' => $id ),
__METHOD__
);
$this->output( "$id $timestamp\n" );
} else {
$this->output( "$id NULL\n" );
// Update
if ( $timestamp !== false ) {
$dbw->update(
'user',
array( 'user_registration' => $timestamp ),
array( 'user_id' => $id ),
__METHOD__
);
$user = User::newFromId( $id );
$user->invalidateCache();
$this->output( "Set registration for #$id to $timestamp\n" );
} else {
$this->output( "Could not find registration for #$id NULL\n" );
}
}
}
$this->output( "\n" );
$this->output( "Waiting for slaves..." );
wfWaitForSlaves();
$this->output( " done.\n" );
} while ( $res->numRows() );
}
}