2014-12-02 20:40:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-09-19 16:59:47 +00:00
|
|
|
use MediaWiki\Parser\Sanitizer;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2014-12-02 20:40:23 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2014-12-02 20:40:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A script to remove emails that are invalid from
|
|
|
|
|
* the user_email column of the user table. Emails
|
|
|
|
|
* are validated before users can add them, but
|
|
|
|
|
* this was not always the case so older users may
|
|
|
|
|
* have invalid ones.
|
|
|
|
|
*
|
|
|
|
|
* By default it does a dry-run, pass --commit
|
|
|
|
|
* to actually update the database.
|
|
|
|
|
*/
|
|
|
|
|
class RemoveInvalidEmails extends Maintenance {
|
|
|
|
|
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var bool */
|
2014-12-02 20:40:23 +00:00
|
|
|
private $commit = false;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addOption( 'commit', 'Whether to actually update the database', false, false );
|
|
|
|
|
$this->setBatchSize( 500 );
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2014-12-02 20:40:23 +00:00
|
|
|
public function execute() {
|
|
|
|
|
$this->commit = $this->hasOption( 'commit' );
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
|
|
|
|
$dbw = $this->getPrimaryDB();
|
2014-12-02 20:40:23 +00:00
|
|
|
$lastId = 0;
|
|
|
|
|
do {
|
2023-07-18 22:56:37 +00:00
|
|
|
$rows = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'user_id', 'user_email' ] )
|
|
|
|
|
->from( 'user' )
|
|
|
|
|
->where( [
|
2024-01-17 17:48:40 +00:00
|
|
|
$dbr->expr( 'user_id', '>', $lastId ),
|
|
|
|
|
$dbr->expr( 'user_email', '!=', '' ),
|
2023-07-17 20:17:57 +00:00
|
|
|
'user_email_authenticated' => null,
|
2023-07-18 22:56:37 +00:00
|
|
|
] )
|
|
|
|
|
->limit( $this->getBatchSize() )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2014-12-02 20:40:23 +00:00
|
|
|
$count = $rows->numRows();
|
2016-02-17 09:09:32 +00:00
|
|
|
$badIds = [];
|
2014-12-02 20:40:23 +00:00
|
|
|
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;
|
2014-12-04 18:40:25 +00:00
|
|
|
}
|
|
|
|
|
if ( $row->user_id > $lastId ) {
|
|
|
|
|
$lastId = $row->user_id;
|
2014-12-02 20:40:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $badIds ) {
|
|
|
|
|
$badCount = count( $badIds );
|
|
|
|
|
if ( $this->commit ) {
|
|
|
|
|
$this->output( "Removing $badCount emails from the database.\n" );
|
2024-04-14 18:36:13 +00:00
|
|
|
$dbw->newUpdateQueryBuilder()
|
|
|
|
|
->update( 'user' )
|
|
|
|
|
->set( [ 'user_email' => '' ] )
|
|
|
|
|
->where( [ 'user_id' => $badIds ] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
2014-12-02 20:40:23 +00:00
|
|
|
foreach ( $badIds as $badId ) {
|
|
|
|
|
User::newFromId( $badId )->invalidateCache();
|
|
|
|
|
}
|
2022-10-24 18:31:49 +00:00
|
|
|
$this->waitForReplication();
|
2014-12-02 20:40:23 +00:00
|
|
|
} else {
|
|
|
|
|
$this->output( "Would have removed $badCount emails from the database.\n" );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while ( $count !== 0 );
|
|
|
|
|
$this->output( "Done.\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = RemoveInvalidEmails::class;
|
2014-12-02 20:40:23 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|