MediaWiki master
removeInvalidEmails.php
Go to the documentation of this file.
1<?php
2
5
6require_once __DIR__ . '/Maintenance.php';
7
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;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
setBatchSize( $s=0)
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.
execute()
Do the actual work.
__construct()
Default constructor.