| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Parser\Sanitizer; |
| 4 |
use MediaWiki\User\User; |
| 5 |
|
| 6 |
|
| 7 |
require_once __DIR__ . '/Maintenance.php'; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class RemoveInvalidEmails extends Maintenance { |
| 21 |
|
| 22 |
|
| 23 |
private $commit = false; |
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
parent::__construct(); |
| 27 |
$this->addOption( 'commit', 'Whether to actually update the database', false, false ); |
| 28 |
$this->setBatchSize( 500 ); |
| 29 |
} |
| 30 |
|
| 31 |
public function execute() { |
| 32 |
$this->commit = $this->hasOption( 'commit' ); |
| 33 |
$dbr = $this->getReplicaDB(); |
| 34 |
$dbw = $this->getPrimaryDB(); |
| 35 |
$lastId = 0; |
| 36 |
do { |
| 37 |
$rows = $dbr->newSelectQueryBuilder() |
| 38 |
->select( [ 'user_id', 'user_email' ] ) |
| 39 |
->from( 'user' ) |
| 40 |
->where( [ |
| 41 |
$dbr->expr( 'user_id', '>', $lastId ), |
| 42 |
$dbr->expr( 'user_email', '!=', '' ), |
| 43 |
'user_email_authenticated' => null, |
| 44 |
] ) |
| 45 |
->limit( $this->getBatchSize() ) |
| 46 |
->caller( __METHOD__ )->fetchResultSet(); |
| 47 |
$count = $rows->numRows(); |
| 48 |
$badIds = []; |
| 49 |
foreach ( $rows as $row ) { |
| 50 |
if ( !Sanitizer::validateEmail( trim( $row->user_email ) ) ) { |
| 51 |
$this->output( "Found bad email: {$row->user_email} for user #{$row->user_id}\n" ); |
| 52 |
$badIds[] = $row->user_id; |
| 53 |
} |
| 54 |
if ( $row->user_id > $lastId ) { |
| 55 |
$lastId = $row->user_id; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ( $badIds ) { |
| 60 |
$badCount = count( $badIds ); |
| 61 |
if ( $this->commit ) { |
| 62 |
$this->output( "Removing $badCount emails from the database.\n" ); |
| 63 |
$dbw->newUpdateQueryBuilder() |
| 64 |
->update( 'user' ) |
| 65 |
->set( [ 'user_email' => '' ] ) |
| 66 |
->where( [ 'user_id' => $badIds ] ) |
| 67 |
->caller( __METHOD__ ) |
| 68 |
->execute(); |
| 69 |
foreach ( $badIds as $badId ) { |
| 70 |
User::newFromId( $badId )->invalidateCache(); |
| 71 |
} |
| 72 |
$this->waitForReplication(); |
| 73 |
} else { |
| 74 |
$this->output( "Would have removed $badCount emails from the database.\n" ); |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
} while ( $count !== 0 ); |
| 79 |
$this->output( "Done.\n" ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
$maintClass = RemoveInvalidEmails::class; |
| 85 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 86 |
|