Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResetUserEmail
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3 * Reset user email.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24require_once __DIR__ . '/Maintenance.php';
25
26use MediaWiki\Parser\Sanitizer;
27use MediaWiki\Status\Status;
28use MediaWiki\User\User;
29
30/**
31 * Maintenance script that resets user email.
32 *
33 * @since 1.27
34 * @ingroup Maintenance
35 */
36class ResetUserEmail extends Maintenance {
37    public function __construct() {
38        parent::__construct();
39
40        $this->addDescription( "Resets a user's email" );
41
42        $this->addArg( 'user', 'Username or user ID, if starts with #' );
43        $this->addArg( 'email', 'Email to assign' );
44
45        $this->addOption( 'no-reset-password', 'Don\'t reset the user\'s password' );
46        $this->addOption( 'email-password', 'Send a temporary password to the user\'s new email address' );
47    }
48
49    public function execute() {
50        $userName = $this->getArg( 0 );
51        if ( preg_match( '/^#\d+$/', $userName ) ) {
52            $user = User::newFromId( (int)substr( $userName, 1 ) );
53        } else {
54            $user = User::newFromName( $userName );
55        }
56        if ( !$user || !$user->isRegistered() || !$user->loadFromId() ) {
57            $this->fatalError( "Error: user '$userName' does not exist\n" );
58        }
59
60        $email = $this->getArg( 1, '' );
61        if ( $email !== '' && !Sanitizer::validateEmail( $email ) ) {
62            $this->fatalError( "Error: email '$email' is not valid\n" );
63        }
64
65        // Code from https://wikitech.wikimedia.org/wiki/Password_reset
66        $user->setEmail( $email );
67        $user->setEmailAuthenticationTimestamp( wfTimestampNow() );
68        $user->saveSettings();
69
70        if ( !$this->hasOption( 'no-reset-password' ) ) {
71            // Kick whomever is currently controlling the account off if possible
72            $password = PasswordFactory::generateRandomPasswordString( 128 );
73            $status = $user->changeAuthenticationData( [
74                'username' => $user->getName(),
75                'password' => $password,
76                'retype' => $password,
77            ] );
78            if ( !$status->isGood() ) {
79                $this->error( "Password couldn't be reset because:\n"
80                    . $status->getMessage( false, false, 'en' )->text() );
81            }
82        }
83
84        if ( $this->hasOption( 'email-password' ) ) {
85            $passReset = $this->getServiceContainer()->getPasswordReset();
86            $sysUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
87            $status = Status::wrap( $passReset->execute( $sysUser, $user->getName(), $email ) );
88            if ( !$status->isGood() ) {
89                $this->error( "Email couldn't be sent because:\n"
90                    . $status->getMessage( false, false, 'en' )->text() );
91            }
92        }
93        $this->output( "Done!\n" );
94    }
95}
96
97$maintClass = ResetUserEmail::class;
98require_once RUN_MAINTENANCE_IF_MAIN;