| 1 | <?php |
| 2 | /** |
| 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 |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 21 | * |
| 22 | * @file |
| 23 | * @author Samuel Guebo <[email protected]> |
| 24 | * @see https://phabricator.wikimedia.org/T290099 |
| 25 | * @ingroup Maintenance |
| 26 | */ |
| 27 | |
| 28 | // @codeCoverageIgnoreStart |
| 29 | require_once __DIR__ . '/Maintenance.php'; |
| 30 | // @codeCoverageIgnoreEnd |
| 31 | |
| 32 | /** |
| 33 | * |
| 34 | * @since 1.38 |
| 35 | * @author Samuel Guebo |
| 36 | */ |
| 37 | class DeleteUserEmail extends Maintenance { |
| 38 | public function __construct() { |
| 39 | parent::__construct(); |
| 40 | $this->addDescription( "Delete a user's email" ); |
| 41 | $this->addArg( 'user', 'Username or user ID, if starts with #', true ); |
| 42 | } |
| 43 | |
| 44 | public function execute() { |
| 45 | $userFactory = $this->getServiceContainer()->getUserFactory(); |
| 46 | $userName = $this->getArg( 0 ); |
| 47 | if ( preg_match( '/^#\d+$/', $userName ) ) { |
| 48 | $user = $userFactory->newFromId( (int)substr( $userName, 1 ) ); |
| 49 | } else { |
| 50 | $user = $userFactory->newFromName( $userName ); |
| 51 | } |
| 52 | |
| 53 | // Checking whether User object is valid and has an actual id |
| 54 | if ( !$user || !$user->isRegistered() || !$user->loadFromId() ) { |
| 55 | $this->fatalError( "Error: user '$userName' could not be loaded" ); |
| 56 | } |
| 57 | |
| 58 | // Blank the email address |
| 59 | $user->invalidateEmail(); |
| 60 | $user->saveSettings(); |
| 61 | $this->output( "Done!\n" ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // @codeCoverageIgnoreStart |
| 66 | $maintClass = DeleteUserEmail::class; |
| 67 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 68 | // @codeCoverageIgnoreEnd |