Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F2752399
removeInvalidEmails.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
removeInvalidEmails.php
View Options
<?php
use
MediaWiki\Parser\Sanitizer
;
use
MediaWiki\User\User
;
// @codeCoverageIgnoreStart
require_once
__DIR__
.
'/Maintenance.php'
;
// @codeCoverageIgnoreEnd
/**
* 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
{
/** @var bool */
private
$commit
=
false
;
public
function
__construct
()
{
parent
::
__construct
();
$this
->
addOption
(
'commit'
,
'Whether to actually update the database'
,
false
,
false
);
$this
->
setBatchSize
(
500
);
}
public
function
execute
()
{
$this
->
commit
=
$this
->
hasOption
(
'commit'
);
$dbr
=
$this
->
getReplicaDB
();
$dbw
=
$this
->
getPrimaryDB
();
$lastId
=
0
;
do
{
$rows
=
$dbr
->
newSelectQueryBuilder
()
->
select
(
[
'user_id'
,
'user_email'
]
)
->
from
(
'user'
)
->
where
(
[
$dbr
->
expr
(
'user_id'
,
'>'
,
$lastId
),
$dbr
->
expr
(
'user_email'
,
'!='
,
''
),
'user_email_authenticated'
=>
null
,
]
)
->
limit
(
$this
->
getBatchSize
()
)
->
caller
(
__METHOD__
)->
fetchResultSet
();
$count
=
$rows
->
numRows
();
$badIds
=
[];
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
->
newUpdateQueryBuilder
()
->
update
(
'user'
)
->
set
(
[
'user_email'
=>
''
]
)
->
where
(
[
'user_id'
=>
$badIds
]
)
->
caller
(
__METHOD__
)
->
execute
();
foreach
(
$badIds
as
$badId
)
{
User
::
newFromId
(
$badId
)->
invalidateCache
();
}
$this
->
waitForReplication
();
}
else
{
$this
->
output
(
"Would have removed $badCount emails from the database.
\n
"
);
}
}
}
while
(
$count
!==
0
);
$this
->
output
(
"Done.
\n
"
);
}
}
// @codeCoverageIgnoreStart
$maintClass
=
RemoveInvalidEmails
::
class
;
require_once
RUN_MAINTENANCE_IF_MAIN
;
// @codeCoverageIgnoreEnd
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Jul 3, 19:57 (1 d, 6 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
3e/e6/9b64e0ebad0c7a34c8dccc3fa7c0
Default Alt Text
removeInvalidEmails.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment