MediaWiki master
fixUserRegistration.php
Go to the documentation of this file.
1<?php
26
27require_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->getPrimaryDB();
43
44 $lastId = 0;
45 do {
46 // Get user IDs which need fixing
47 $res = $dbw->newSelectQueryBuilder()
48 ->select( 'user_id' )
49 ->from( 'user' )
50 ->where( [ $dbw->expr( 'user_id', '>', $lastId ), 'user_registration' => null ] )
51 ->orderBy( 'user_id' )
52 ->limit( $this->getBatchSize() )
53 ->caller( __METHOD__ )->fetchResultSet();
54
55 foreach ( $res as $row ) {
56 $id = $row->user_id;
57 $lastId = $id;
58
59 // Get first edit time
60 $actorStore = $this->getServiceContainer()->getActorStore();
61 $userIdentity = $actorStore->getUserIdentityByUserId( $id );
62 if ( !$userIdentity ) {
63 continue;
64 }
65
66 $timestamp = $dbw->newSelectQueryBuilder()
67 ->select( 'MIN(rev_timestamp)' )
68 ->from( 'revision' )
69 ->where( [ 'rev_actor' => $userIdentity->getId() ] )
70 ->caller( __METHOD__ )->fetchField();
71
72 // Update
73 if ( $timestamp !== null ) {
74 $dbw->newUpdateQueryBuilder()
75 ->update( 'user' )
76 ->set( [ 'user_registration' => $timestamp ] )
77 ->where( [ 'user_id' => $id ] )
78 ->caller( __METHOD__ )->execute();
79
80 $user = User::newFromId( $id );
81 $user->invalidateCache();
82 $this->output( "Set registration for #$id to $timestamp\n" );
83 } else {
84 $this->output( "Could not find registration for #$id NULL\n" );
85 }
86 }
87 $this->output( "Waiting for replica DBs..." );
88 $this->waitForReplication();
89 $this->output( " done.\n" );
90 } while ( $res->numRows() >= $this->getBatchSize() );
91 }
92}
93
94$maintClass = FixUserRegistration::class;
95require_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...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
internal since 1.36
Definition User.php:93