MediaWiki REL1_37
removeInvalidEmails.php
Go to the documentation of this file.
1<?php
2
3require_once __DIR__ . '/Maintenance.php';
4
6
18
19 private $commit = false;
20
21 public function __construct() {
22 parent::__construct();
23 $this->addOption( 'commit', 'Whether to actually update the database', false, false );
24 $this->setBatchSize( 500 );
25 }
26
27 public function execute() {
28 $this->commit = $this->hasOption( 'commit' );
29 $dbr = $this->getDB( DB_REPLICA );
30 $dbw = $this->getDB( DB_PRIMARY );
31 $lastId = 0;
32 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
33 do {
34 $rows = $dbr->select(
35 'user',
36 [ 'user_id', 'user_email' ],
37 [
38 'user_id > ' . $dbr->addQuotes( $lastId ),
39 'user_email != ' . $dbr->addQuotes( '' ),
40 'user_email_authenticated IS NULL'
41 ],
42 __METHOD__,
43 [ 'LIMIT' => $this->getBatchSize() ]
44 );
45 $count = $rows->numRows();
46 $badIds = [];
47 foreach ( $rows as $row ) {
48 if ( !Sanitizer::validateEmail( trim( $row->user_email ) ) ) {
49 $this->output( "Found bad email: {$row->user_email} for user #{$row->user_id}\n" );
50 $badIds[] = $row->user_id;
51 }
52 if ( $row->user_id > $lastId ) {
53 $lastId = $row->user_id;
54 }
55 }
56
57 if ( $badIds ) {
58 $badCount = count( $badIds );
59 if ( $this->commit ) {
60 $this->output( "Removing $badCount emails from the database.\n" );
61 $dbw->update(
62 'user',
63 [ 'user_email' => '' ],
64 [ 'user_id' => $badIds ],
65 __METHOD__
66 );
67 foreach ( $badIds as $badId ) {
68 User::newFromId( $badId )->invalidateCache();
69 }
70 $lbFactory->waitForReplication();
71 } else {
72 $this->output( "Would have removed $badCount emails from the database.\n" );
73
74 }
75 }
76 } while ( $count !== 0 );
77 $this->output( "Done.\n" );
78 }
79}
80
81$maintClass = RemoveInvalidEmails::class;
82require_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.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
setBatchSize( $s=0)
MediaWikiServices is the service locator for the application scope of MediaWiki.
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.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:648
const DB_REPLICA
Definition defines.php:25
const DB_PRIMARY
Definition defines.php:27