From 6002ee6d4bbe64d8623a3696a47c72b9ae431e79 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Tue, 2 Dec 2014 12:40:23 -0800 Subject: [PATCH] Add removeInvalidEmails.php maintenance script The script scans the user table and removes emails that are technically invalid which don't pass Sanitizer::validateEmail(), and wouldn't be allowed today. Confirmed emails are skipped entirely since they had to be valid at some point. Bug: T76512 Change-Id: I3cc6396ff6d8b738846b7716b4b0cddc9bf9e1a4 --- maintenance/removeInvalidEmails.php | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 maintenance/removeInvalidEmails.php diff --git a/maintenance/removeInvalidEmails.php b/maintenance/removeInvalidEmails.php new file mode 100644 index 0000000000..7ff69a1fec --- /dev/null +++ b/maintenance/removeInvalidEmails.php @@ -0,0 +1,78 @@ +addOption( 'commit', 'Whether to actually update the database', false, false ); + $this->setBatchSize( 500 ); + } + public function execute() { + $this->commit = $this->hasOption( 'commit' ); + $dbr = $this->getDB( DB_SLAVE ); + $dbw = $this->getDB( DB_MASTER ); + $lastId = 0; + do { + $rows = $dbr->select( + 'user', + array( 'user_id', 'user_email' ), + array( + 'user_id > ' . $dbr->addQuotes( $lastId ), + 'user_email != ""', + 'user_email_authenticated IS NULL' + ), + __METHOD__, + array( 'LIMIT' => $this->mBatchSize ) + ); + $count = $rows->numRows(); + $badIds = array(); + foreach ( $rows as $row ) { + if ( !Sanitizer::validateEmail( trim( $row->user_email ) ) ) { + $this->output( "Found bad email: {$row->user_email} for user #{$row->user_id}\n" ); + $badIds[] = $row->user_id; + if ( $row->user_id > $lastId ) { + $lastId = $row->user_id; + } + } + } + + if ( $badIds ) { + $badCount = count( $badIds ); + if ( $this->commit ) { + $this->output( "Removing $badCount emails from the database.\n" ); + $dbw->update( + 'user', + array( 'user_email' => '' ), + array( 'user_id' => $badIds ), + __METHOD__ + ); + foreach ( $badIds as $badId ) { + User::newFromId( $badId )->invalidateCache(); + } + wfWaitForSlaves(); + } else { + $this->output( "Would have removed $badCount emails from the database.\n" ); + + } + } + } while ( $count !== 0 ); + $this->output( "Done.\n" ); + } +} + +$maintClass = 'RemoveInvalidEmails'; +require_once RUN_MAINTENANCE_IF_MAIN; -- 2.20.1