Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FixUserRegistration
97.37% covered (success)
97.37%
37 / 38
50.00% covered (danger)
50.00%
1 / 2
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
97.14% covered (success)
97.14%
34 / 35
0.00% covered (danger)
0.00%
0 / 1
4
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\Maintenance\Maintenance;
26use MediaWiki\User\User;
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
32/**
33 * Maintenance script that fixes the user_registration field.
34 *
35 * @ingroup Maintenance
36 */
37class FixUserRegistration extends Maintenance {
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