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