MediaWiki master
fixUserRegistration.php
Go to the documentation of this file.
1<?php
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/Maintenance.php';
16// @codeCoverageIgnoreEnd
17
24 public function __construct() {
25 parent::__construct();
26 $this->addDescription( 'Fix the user_registration field' );
27 $this->setBatchSize( 1000 );
28 }
29
30 public function execute() {
31 $dbw = $this->getPrimaryDB();
32
33 $lastId = 0;
34 do {
35 // Get user IDs which need fixing
36 $res = $dbw->newSelectQueryBuilder()
37 ->select( 'user_id' )
38 ->from( 'user' )
39 ->where( [ $dbw->expr( 'user_id', '>', $lastId ), 'user_registration' => null ] )
40 ->orderBy( 'user_id' )
41 ->limit( $this->getBatchSize() )
42 ->caller( __METHOD__ )->fetchResultSet();
43
44 foreach ( $res as $row ) {
45 $id = $row->user_id;
46 $lastId = $id;
47
48 // Get first edit time
49 $actorStore = $this->getServiceContainer()->getActorStore();
50 $userIdentity = $actorStore->getUserIdentityByUserId( $id );
51 if ( !$userIdentity ) {
52 continue;
53 }
54
55 $timestamp = $dbw->newSelectQueryBuilder()
56 ->select( 'MIN(rev_timestamp)' )
57 ->from( 'revision' )
58 ->where( [ 'rev_actor' => $userIdentity->getId() ] )
59 ->caller( __METHOD__ )->fetchField();
60
61 // Update
62 if ( $timestamp !== null ) {
63 $dbw->newUpdateQueryBuilder()
64 ->update( 'user' )
65 ->set( [ 'user_registration' => $timestamp ] )
66 ->where( [ 'user_id' => $id ] )
67 ->caller( __METHOD__ )->execute();
68
69 $user = User::newFromId( $id );
70 $user->invalidateCache();
71 $this->output( "Set registration for #$id to $timestamp\n" );
72 } else {
73 $this->output( "Could not find registration for #$id NULL\n" );
74 }
75 }
76 $this->output( "Waiting for replica DBs..." );
77 $this->waitForReplication();
78 $this->output( " done.\n" );
79 } while ( $res->numRows() >= $this->getBatchSize() );
80 }
81}
82
83// @codeCoverageIgnoreStart
84$maintClass = FixUserRegistration::class;
85require_once RUN_MAINTENANCE_IF_MAIN;
86// @codeCoverageIgnoreEnd
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...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DB servers to catch up.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
User class for the MediaWiki software.
Definition User.php:130