Remove Profiler specific code from fileOpPerfTest
[mediawiki.git] / maintenance / createAndPromote.php
blob3591b9ce2456f0728c3c058571e88120c2b327d3
1 <?php
2 /**
3 * Creates an account and grants it rights.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 * @author Pablo Castellano <pablo@anche.no>
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script to create an account and grant it rights.
31 * @ingroup Maintenance
33 class CreateAndPromote extends Maintenance {
34 private static $permitRoles = [ 'sysop', 'bureaucrat', 'bot' ];
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Create a new user account and/or grant it additional rights' );
39 $this->addOption(
40 'force',
41 'If acccount exists already, just grant it rights or change password.'
43 foreach ( self::$permitRoles as $role ) {
44 $this->addOption( $role, "Add the account to the {$role} group" );
47 $this->addOption(
48 'custom-groups',
49 'Comma-separated list of groups to add the user to',
50 false,
51 true
54 $this->addArg( "username", "Username of new user" );
55 $this->addArg( "password", "Password to set (not required if --force is used)", false );
58 public function execute() {
59 global $wgDisableAuthManager;
61 $username = $this->getArg( 0 );
62 $password = $this->getArg( 1 );
63 $force = $this->hasOption( 'force' );
64 $inGroups = [];
66 $user = User::newFromName( $username );
67 if ( !is_object( $user ) ) {
68 $this->error( "invalid username.", true );
71 $exists = ( 0 !== $user->idForName() );
73 if ( $exists && !$force ) {
74 $this->error( "Account exists. Perhaps you want the --force option?", true );
75 } elseif ( !$exists && !$password ) {
76 $this->error( "Argument <password> required!", false );
77 $this->maybeHelp( true );
78 } elseif ( $exists ) {
79 $inGroups = $user->getGroups();
82 $groups = array_filter( self::$permitRoles, [ $this, 'hasOption' ] );
83 if ( $this->hasOption( 'custom-groups' ) ) {
84 $allGroups = array_flip( User::getAllGroups() );
85 $customGroupsText = $this->getOption( 'custom-groups' );
86 if ( $customGroupsText !== '' ) {
87 $customGroups = explode( ',', $customGroupsText );
88 foreach ( $customGroups as $customGroup ) {
89 if ( isset( $allGroups[$customGroup] ) ) {
90 $groups[] = trim( $customGroup );
91 } else {
92 $this->output( "$customGroup is not a valid group, ignoring!\n" );
98 $promotions = array_diff(
99 $groups,
100 $inGroups
103 if ( $exists && !$password && count( $promotions ) === 0 ) {
104 $this->output( "Account exists and nothing to do.\n" );
106 return;
107 } elseif ( count( $promotions ) !== 0 ) {
108 $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
109 if ( $exists ) {
110 $this->output( wfWikiID() . ": Promoting $promoText" );
111 } else {
112 $this->output( wfWikiID() . ": Creating and promoting $promoText" );
116 if ( !$exists ) {
117 # Insert the account into the database
118 $user->addToDatabase();
119 $user->saveSettings();
122 if ( $password ) {
123 # Try to set the password
124 try {
125 if ( $wgDisableAuthManager ) {
126 $user->setPassword( $password );
127 } else {
128 $status = $user->changeAuthenticationData( [
129 'username' => $user->getName(),
130 'password' => $password,
131 'retype' => $password,
132 ] );
133 if ( !$status->isGood() ) {
134 throw new PasswordError( $status->getWikiText( null, null, 'en' ) );
137 if ( $exists ) {
138 $this->output( "Password set.\n" );
139 $user->saveSettings();
141 } catch ( PasswordError $pwe ) {
142 $this->error( $pwe->getText(), true );
146 # Promote user
147 array_map( [ $user, 'addGroup' ], $promotions );
149 if ( !$exists ) {
150 # Increment site_stats.ss_users
151 $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
152 $ssu->doUpdate();
155 $this->output( "done.\n" );
159 $maintClass = "CreateAndPromote";
160 require_once RUN_MAINTENANCE_IF_MAIN;