MediaWiki REL1_41
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->getDB( DB_REPLICA );
31 $dbw = $this->getDB( DB_PRIMARY );
32 $lastId = 0;
33 do {
34 $rows = $dbr->newSelectQueryBuilder()
35 ->select( [ 'user_id', 'user_email' ] )
36 ->from( 'user' )
37 ->where( [
38 'user_id > ' . $dbr->addQuotes( $lastId ),
39 'user_email != ' . $dbr->addQuotes( '' ),
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->update(
61 'user',
62 [ 'user_email' => '' ],
63 [ 'user_id' => $badIds ],
64 __METHOD__
65 );
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;
getDB()
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:98
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.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28