| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
use MediaWiki\User\User; |
| 26 |
|
| 27 |
|
| 28 |
require_once __DIR__ . '/Maintenance.php'; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class FixUserRegistration extends Maintenance { |
| 37 |
public function __construct() { |
| 38 |
parent::__construct(); |
| 39 |
$this->addDescription( 'Fix the user_registration field' ); |
| 40 |
$this->setBatchSize( 1000 ); |
| 41 |
} |
| 42 |
|
| 43 |
public function execute() { |
| 44 |
$dbw = $this->getPrimaryDB(); |
| 45 |
|
| 46 |
$lastId = 0; |
| 47 |
do { |
| 48 |
|
| 49 |
$res = $dbw->newSelectQueryBuilder() |
| 50 |
->select( 'user_id' ) |
| 51 |
->from( 'user' ) |
| 52 |
->where( [ $dbw->expr( 'user_id', '>', $lastId ), 'user_registration' => null ] ) |
| 53 |
->orderBy( 'user_id' ) |
| 54 |
->limit( $this->getBatchSize() ) |
| 55 |
->caller( __METHOD__ )->fetchResultSet(); |
| 56 |
|
| 57 |
foreach ( $res as $row ) { |
| 58 |
$id = $row->user_id; |
| 59 |
$lastId = $id; |
| 60 |
|
| 61 |
|
| 62 |
$actorStore = $this->getServiceContainer()->getActorStore(); |
| 63 |
$userIdentity = $actorStore->getUserIdentityByUserId( $id ); |
| 64 |
if ( !$userIdentity ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$timestamp = $dbw->newSelectQueryBuilder() |
| 69 |
->select( 'MIN(rev_timestamp)' ) |
| 70 |
->from( 'revision' ) |
| 71 |
->where( [ 'rev_actor' => $userIdentity->getId() ] ) |
| 72 |
->caller( __METHOD__ )->fetchField(); |
| 73 |
|
| 74 |
|
| 75 |
if ( $timestamp !== null ) { |
| 76 |
$dbw->newUpdateQueryBuilder() |
| 77 |
->update( 'user' ) |
| 78 |
->set( [ 'user_registration' => $timestamp ] ) |
| 79 |
->where( [ 'user_id' => $id ] ) |
| 80 |
->caller( __METHOD__ )->execute(); |
| 81 |
|
| 82 |
$user = User::newFromId( $id ); |
| 83 |
$user->invalidateCache(); |
| 84 |
$this->output( "Set registration for #$id to $timestamp\n" ); |
| 85 |
} else { |
| 86 |
$this->output( "Could not find registration for #$id NULL\n" ); |
| 87 |
} |
| 88 |
} |
| 89 |
$this->output( "Waiting for replica DBs..." ); |
| 90 |
$this->waitForReplication(); |
| 91 |
$this->output( " done.\n" ); |
| 92 |
} while ( $res->numRows() >= $this->getBatchSize() ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
$maintClass = FixUserRegistration::class; |
| 98 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 99 |
|