Update git submodules
[mediawiki.git] / maintenance / fixUserRegistration.php
blob57390c8c0a18bd6cfcf8128090f4dc36f93d6537
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\User\ActorMigration;
26 use MediaWiki\User\User;
28 require_once __DIR__ . '/Maintenance.php';
30 /**
31 * Maintenance script that fixes the user_registration field.
33 * @ingroup Maintenance
35 class FixUserRegistration extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Fix the user_registration field' );
39 $this->setBatchSize( 1000 );
42 public function execute() {
43 $dbw = $this->getDB( DB_PRIMARY );
45 $lastId = 0;
46 do {
47 // Get user IDs which need fixing
48 $res = $dbw->newSelectQueryBuilder()
49 ->select( 'user_id' )
50 ->from( 'user' )
51 ->where( [ 'user_id > ' . $dbw->addQuotes( $lastId ), 'user_registration' => null ] )
52 ->orderBy( 'user_id' )
53 ->limit( $this->getBatchSize() )
54 ->caller( __METHOD__ )->fetchResultSet();
55 foreach ( $res as $row ) {
56 $id = $row->user_id;
57 $lastId = $id;
58 // Get first edit time
59 $actorQuery = ActorMigration::newMigration()
60 ->getWhere( $dbw, 'rev_user', User::newFromId( $id ) );
61 $timestamp = $dbw->selectField(
62 [ 'revision' ] + $actorQuery['tables'],
63 'MIN(rev_timestamp)',
64 $actorQuery['conds'],
65 __METHOD__,
66 [],
67 $actorQuery['joins']
69 // Update
70 if ( $timestamp !== null ) {
71 $dbw->update(
72 'user',
73 [ 'user_registration' => $timestamp ],
74 [ 'user_id' => $id ],
75 __METHOD__
77 $user = User::newFromId( $id );
78 $user->invalidateCache();
79 $this->output( "Set registration for #$id to $timestamp\n" );
80 } else {
81 $this->output( "Could not find registration for #$id NULL\n" );
84 $this->output( "Waiting for replica DBs..." );
85 $this->waitForReplication();
86 $this->output( " done.\n" );
87 } while ( $res->numRows() >= $this->getBatchSize() );
91 $maintClass = FixUserRegistration::class;
92 require_once RUN_MAINTENANCE_IF_MAIN;