Debian Codegraph @

maintenance/fixUserRegistration.php

raw symbols SCIP
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
25 use MediaWiki\User\User;
26
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
30
31 /**
32 * Maintenance script that fixes the user_registration field.
33 *
34 * @ingroup Maintenance
35 */
36 class FixUserRegistration extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Fix the user_registration field' );
40 $this->setBatchSize( 1000 );
41 }
42
43 public function execute() {
44 $dbw = $this->getPrimaryDB();
45
46 $lastId = 0;
47 do {
48 // Get user IDs which need fixing
49 $res = $dbw->newSelectQueryBuilder()
50 ->select( 'user_id' )
51 ->from( 'user' )
52 ->where( [ $dbw->expr( 'user_id', '>', $lastId ), 'user_registration' => null ] )
53 ->orderBy( 'user_id' )
54 ->limit( $this->getBatchSize() )
55 ->caller( __METHOD__ )->fetchResultSet();
56
57 foreach ( $res as $row ) {
58 $id = $row->user_id;
59 $lastId = $id;
60
61 // Get first edit time
62 $actorStore = $this->getServiceContainer()->getActorStore();
63 $userIdentity = $actorStore->getUserIdentityByUserId( $id );
64 if ( !$userIdentity ) {
65 continue;
66 }
67
68 $timestamp = $dbw->newSelectQueryBuilder()
69 ->select( 'MIN(rev_timestamp)' )
70 ->from( 'revision' )
71 ->where( [ 'rev_actor' => $userIdentity->getId() ] )
72 ->caller( __METHOD__ )->fetchField();
73
74 // Update
75 if ( $timestamp !== null ) {
76 $dbw->newUpdateQueryBuilder()
77 ->update( 'user' )
78 ->set( [ 'user_registration' => $timestamp ] )
79 ->where( [ 'user_id' => $id ] )
80 ->caller( __METHOD__ )->execute();
81
82 $user = User::newFromId( $id );
83 $user->invalidateCache();
84 $this->output( "Set registration for #$id to $timestamp\n" );
85 } else {
86 $this->output( "Could not find registration for #$id NULL\n" );
87 }
88 }
89 $this->output( "Waiting for replica DBs..." );
90 $this->waitForReplication();
91 $this->output( " done.\n" );
92 } while ( $res->numRows() >= $this->getBatchSize() );
93 }
94 }
95
96 // @codeCoverageIgnoreStart
97 $maintClass = FixUserRegistration::class;
98 require_once RUN_MAINTENANCE_IF_MAIN;
99 // @codeCoverageIgnoreEnd