MediaWiki  master
removeInvalidEmails.php
Go to the documentation of this file.
1 <?php
2 
3 require_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_MASTER );
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 != ""',
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;
82 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
RemoveInvalidEmails
A script to remove emails that are invalid from the user_email column of the user table.
Definition: removeInvalidEmails.php:17
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:584
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:162
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1711
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$dbr
$dbr
Definition: testCompression.php:54
RemoveInvalidEmails\$commit
$commit
Definition: removeInvalidEmails.php:19
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: removeInvalidEmails.php:81
RemoveInvalidEmails\__construct
__construct()
Default constructor.
Definition: removeInvalidEmails.php:21
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1370
RemoveInvalidEmails\execute
execute()
Do the actual work.
Definition: removeInvalidEmails.php:27
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:374