MediaWiki master
removeInvalidEmails.php
Go to the documentation of this file.
1<?php
2
6
7// @codeCoverageIgnoreStart
8require_once __DIR__ . '/Maintenance.php';
9// @codeCoverageIgnoreEnd
10
22
24 private $commit = false;
25
26 public function __construct() {
27 parent::__construct();
28 $this->addOption( 'commit', 'Whether to actually update the database', false, false );
29 $this->setBatchSize( 500 );
30 }
31
32 public function execute() {
33 $this->commit = $this->hasOption( 'commit' );
34 $dbr = $this->getReplicaDB();
35 $dbw = $this->getPrimaryDB();
36 $lastId = 0;
37 do {
38 $rows = $dbr->newSelectQueryBuilder()
39 ->select( [ 'user_id', 'user_email' ] )
40 ->from( 'user' )
41 ->where( [
42 $dbr->expr( 'user_id', '>', $lastId ),
43 $dbr->expr( 'user_email', '!=', '' ),
44 'user_email_authenticated' => null,
45 ] )
46 ->limit( $this->getBatchSize() )
47 ->caller( __METHOD__ )->fetchResultSet();
48 $count = $rows->numRows();
49 $badIds = [];
50 foreach ( $rows as $row ) {
51 if ( !Sanitizer::validateEmail( trim( $row->user_email ) ) ) {
52 $this->output( "Found bad email: {$row->user_email} for user #{$row->user_id}\n" );
53 $badIds[] = $row->user_id;
54 }
55 if ( $row->user_id > $lastId ) {
56 $lastId = $row->user_id;
57 }
58 }
59
60 if ( $badIds ) {
61 $badCount = count( $badIds );
62 if ( $this->commit ) {
63 $this->output( "Removing $badCount emails from the database.\n" );
64 $dbw->newUpdateQueryBuilder()
65 ->update( 'user' )
66 ->set( [ 'user_email' => '' ] )
67 ->where( [ 'user_id' => $badIds ] )
68 ->caller( __METHOD__ )
69 ->execute();
70 foreach ( $badIds as $badId ) {
71 User::newFromId( $badId )->invalidateCache();
72 }
73 $this->waitForReplication();
74 } else {
75 $this->output( "Would have removed $badCount emails from the database.\n" );
76
77 }
78 }
79 } while ( $count !== 0 );
80 $this->output( "Done.\n" );
81 }
82}
83
84// @codeCoverageIgnoreStart
85$maintClass = RemoveInvalidEmails::class;
86require_once RUN_MAINTENANCE_IF_MAIN;
87// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
User class for the MediaWiki software.
Definition User.php:121
A script to remove emails that are invalid from the user_email column of the user table.
execute()
Do the actual work.
__construct()
Default constructor.