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
22 * @ingroup Maintenance
25 use MediaWiki\Maintenance\Maintenance
;
26 use MediaWiki\User\User
;
28 // @codeCoverageIgnoreStart
29 require_once __DIR__
. '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
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();
49 // Get user IDs which need fixing
50 $res = $dbw->newSelectQueryBuilder()
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 ) {
62 // Get first edit time
63 $actorStore = $this->getServiceContainer()->getActorStore();
64 $userIdentity = $actorStore->getUserIdentityByUserId( $id );
65 if ( !$userIdentity ) {
69 $timestamp = $dbw->newSelectQueryBuilder()
70 ->select( 'MIN(rev_timestamp)' )
72 ->where( [ 'rev_actor' => $userIdentity->getId() ] )
73 ->caller( __METHOD__
)->fetchField();
76 if ( $timestamp !== null ) {
77 $dbw->newUpdateQueryBuilder()
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" );
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