Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixUserRegistration
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
20
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
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25use MediaWiki\User\User;
26
27require_once __DIR__ . '/Maintenance.php';
28
29/**
30 * Maintenance script that fixes the user_registration field.
31 *
32 * @ingroup Maintenance
33 */
34class FixUserRegistration extends Maintenance {
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;