Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RemoveInvalidEmails
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
72
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 / 38
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3use MediaWiki\Parser\Sanitizer;
4use MediaWiki\User\User;
5
6require_once __DIR__ . '/Maintenance.php';
7
8/**
9 * A script to remove emails that are invalid from
10 * the user_email column of the user table. Emails
11 * are validated before users can add them, but
12 * this was not always the case so older users may
13 * have invalid ones.
14 *
15 * By default it does a dry-run, pass --commit
16 * to actually update the database.
17 */
18class RemoveInvalidEmails extends Maintenance {
19
20    private $commit = false;
21
22    public function __construct() {
23        parent::__construct();
24        $this->addOption( 'commit', 'Whether to actually update the database', false, false );
25        $this->setBatchSize( 500 );
26    }
27
28    public function execute() {
29        $this->commit = $this->hasOption( 'commit' );
30        $dbr = $this->getReplicaDB();
31        $dbw = $this->getPrimaryDB();
32        $lastId = 0;
33        do {
34            $rows = $dbr->newSelectQueryBuilder()
35                ->select( [ 'user_id', 'user_email' ] )
36                ->from( 'user' )
37                ->where( [
38                    $dbr->expr( 'user_id', '>', $lastId ),
39                    $dbr->expr( 'user_email', '!=', '' ),
40                    'user_email_authenticated' => null,
41                ] )
42                ->limit( $this->getBatchSize() )
43                ->caller( __METHOD__ )->fetchResultSet();
44            $count = $rows->numRows();
45            $badIds = [];
46            foreach ( $rows as $row ) {
47                if ( !Sanitizer::validateEmail( trim( $row->user_email ) ) ) {
48                    $this->output( "Found bad email: {$row->user_email} for user #{$row->user_id}\n" );
49                    $badIds[] = $row->user_id;
50                }
51                if ( $row->user_id > $lastId ) {
52                    $lastId = $row->user_id;
53                }
54            }
55
56            if ( $badIds ) {
57                $badCount = count( $badIds );
58                if ( $this->commit ) {
59                    $this->output( "Removing $badCount emails from the database.\n" );
60                    $dbw->newUpdateQueryBuilder()
61                        ->update( 'user' )
62                        ->set( [ 'user_email' => '' ] )
63                        ->where( [ 'user_id' => $badIds ] )
64                        ->caller( __METHOD__ )
65                        ->execute();
66                    foreach ( $badIds as $badId ) {
67                        User::newFromId( $badId )->invalidateCache();
68                    }
69                    $this->waitForReplication();
70                } else {
71                    $this->output( "Would have removed $badCount emails from the database.\n" );
72
73                }
74            }
75        } while ( $count !== 0 );
76        $this->output( "Done.\n" );
77    }
78}
79
80$maintClass = RemoveInvalidEmails::class;
81require_once RUN_MAINTENANCE_IF_MAIN;