Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / fixUserRegistration.php
blobea149ca39f83e0f264b5f58f60f21018bc6a0d3c
1 <?php
2 /**
3 * Fix the user_registration field.
4 * In particular, for values which are NULL, set them to the date of the first edit
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
25 use MediaWiki\Maintenance\Maintenance;
26 use MediaWiki\User\User;
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
32 /**
33 * Maintenance script that fixes the user_registration field.
35 * @ingroup Maintenance
37 class FixUserRegistration extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Fix the user_registration field' );
41 $this->setBatchSize( 1000 );
44 public function execute() {
45 $dbw = $this->getPrimaryDB();
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();
58 foreach ( $res as $row ) {
59 $id = $row->user_id;
60 $lastId = $id;
62 // Get first edit time
63 $actorStore = $this->getServiceContainer()->getActorStore();
64 $userIdentity = $actorStore->getUserIdentityByUserId( $id );
65 if ( !$userIdentity ) {
66 continue;
69 $timestamp = $dbw->newSelectQueryBuilder()
70 ->select( 'MIN(rev_timestamp)' )
71 ->from( 'revision' )
72 ->where( [ 'rev_actor' => $userIdentity->getId() ] )
73 ->caller( __METHOD__ )->fetchField();
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();
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" );
90 $this->output( "Waiting for replica DBs..." );
91 $this->waitForReplication();
92 $this->output( " done.\n" );
93 } while ( $res->numRows() >= $this->getBatchSize() );
97 // @codeCoverageIgnoreStart
98 $maintClass = FixUserRegistration::class;
99 require_once RUN_MAINTENANCE_IF_MAIN;
100 // @codeCoverageIgnoreEnd