MediaWiki  master
fixUserRegistration.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Fix the user_registration field' );
38  $this->setBatchSize( 1000 );
39  }
40 
41  public function execute() {
42  $dbw = $this->getDB( DB_PRIMARY );
43 
44  $lastId = 0;
45  do {
46  // Get user IDs which need fixing
47  $res = $dbw->select(
48  'user',
49  'user_id',
50  [
51  'user_id > ' . $dbw->addQuotes( $lastId ),
52  'user_registration IS NULL'
53  ],
54  __METHOD__,
55  [
56  'LIMIT' => $this->getBatchSize(),
57  'ORDER BY' => 'user_id',
58  ]
59  );
60  foreach ( $res as $row ) {
61  $id = $row->user_id;
62  $lastId = $id;
63  // Get first edit time
64  $actorQuery = ActorMigration::newMigration()
65  ->getWhere( $dbw, 'rev_user', User::newFromId( $id ) );
66  $timestamp = $dbw->selectField(
67  [ 'revision' ] + $actorQuery['tables'],
68  'MIN(rev_timestamp)',
69  $actorQuery['conds'],
70  __METHOD__,
71  [],
72  $actorQuery['joins']
73  );
74  // Update
75  if ( $timestamp !== null ) {
76  $dbw->update(
77  'user',
78  [ 'user_registration' => $timestamp ],
79  [ 'user_id' => $id ],
80  __METHOD__
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 $maintClass = FixUserRegistration::class;
97 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that fixes the user_registration field.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:632
const DB_PRIMARY
Definition: defines.php:28