MediaWiki master
removeInvalidEmails.php
Go to the documentation of this file.
1<?php
2
5
6// @codeCoverageIgnoreStart
7require_once __DIR__ . '/Maintenance.php';
8// @codeCoverageIgnoreEnd
9
20class RemoveInvalidEmails extends Maintenance {
21
23 private $commit = false;
24
25 public function __construct() {
26 parent::__construct();
27 $this->addOption( 'commit', 'Whether to actually update the database', false, false );
28 $this->setBatchSize( 500 );
29 }
30
31 public function execute() {
32 $this->commit = $this->hasOption( 'commit' );
33 $dbr = $this->getReplicaDB();
34 $dbw = $this->getPrimaryDB();
35 $lastId = 0;
36 do {
37 $rows = $dbr->newSelectQueryBuilder()
38 ->select( [ 'user_id', 'user_email' ] )
39 ->from( 'user' )
40 ->where( [
41 $dbr->expr( 'user_id', '>', $lastId ),
42 $dbr->expr( 'user_email', '!=', '' ),
43 'user_email_authenticated' => null,
44 ] )
45 ->limit( $this->getBatchSize() )
46 ->caller( __METHOD__ )->fetchResultSet();
47 $count = $rows->numRows();
48 $badIds = [];
49 foreach ( $rows as $row ) {
50 if ( !Sanitizer::validateEmail( trim( $row->user_email ) ) ) {
51 $this->output( "Found bad email: {$row->user_email} for user #{$row->user_id}\n" );
52 $badIds[] = $row->user_id;
53 }
54 if ( $row->user_id > $lastId ) {
55 $lastId = $row->user_id;
56 }
57 }
58
59 if ( $badIds ) {
60 $badCount = count( $badIds );
61 if ( $this->commit ) {
62 $this->output( "Removing $badCount emails from the database.\n" );
63 $dbw->newUpdateQueryBuilder()
64 ->update( 'user' )
65 ->set( [ 'user_email' => '' ] )
66 ->where( [ 'user_id' => $badIds ] )
67 ->caller( __METHOD__ )
68 ->execute();
69 foreach ( $badIds as $badId ) {
70 User::newFromId( $badId )->invalidateCache();
71 }
72 $this->waitForReplication();
73 } else {
74 $this->output( "Would have removed $badCount emails from the database.\n" );
75
76 }
77 }
78 } while ( $count !== 0 );
79 $this->output( "Done.\n" );
80 }
81}
82
83// @codeCoverageIgnoreStart
84$maintClass = RemoveInvalidEmails::class;
85require_once RUN_MAINTENANCE_IF_MAIN;
86// @codeCoverageIgnoreEnd
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
internal since 1.36
Definition User.php:93
A script to remove emails that are invalid from the user_email column of the user table.