3 * Deletes a given user's associated email address
4 * Usage: php deleteUserEmail.php <user>
5 * where <user> can be either the username or user ID
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @author Samuel Guebo <sguebo@wikimedia.org>
24 * @see https://phabricator.wikimedia.org/T290099
25 * @ingroup Maintenance
28 use MediaWiki\Maintenance\Maintenance
;
30 // @codeCoverageIgnoreStart
31 require_once __DIR__
. '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
37 * @author Samuel Guebo
39 class DeleteUserEmail
extends Maintenance
{
40 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( "Delete a user's email" );
43 $this->addArg( 'user', 'Username or user ID, if starts with #', true );
46 public function execute() {
47 $userFactory = $this->getServiceContainer()->getUserFactory();
48 $userName = $this->getArg( 0 );
49 if ( preg_match( '/^#\d+$/', $userName ) ) {
50 $user = $userFactory->newFromId( (int)substr( $userName, 1 ) );
52 $user = $userFactory->newFromName( $userName );
55 // Checking whether User object is valid and has an actual id
56 if ( !$user ||
!$user->isRegistered() ||
!$user->loadFromId() ) {
57 $this->fatalError( "Error: user '$userName' could not be loaded" );
60 // Blank the email address
61 $user->invalidateEmail();
62 $user->saveSettings();
63 $this->output( "Done!\n" );
67 // @codeCoverageIgnoreStart
68 $maintClass = DeleteUserEmail
::class;
69 require_once RUN_MAINTENANCE_IF_MAIN
;
70 // @codeCoverageIgnoreEnd