MediaWiki  REL1_24
createAndPromote.php
Go to the documentation of this file.
00001 <?php
00026 require_once __DIR__ . '/Maintenance.php';
00027 
00033 class CreateAndPromote extends Maintenance {
00034     private static $permitRoles = array( 'sysop', 'bureaucrat', 'bot' );
00035 
00036     public function __construct() {
00037         parent::__construct();
00038         $this->mDescription = "Create a new user account and/or grant it additional rights";
00039         $this->addOption(
00040             'force',
00041             'If acccount exists already, just grant it rights or change password.'
00042         );
00043         foreach ( self::$permitRoles as $role ) {
00044             $this->addOption( $role, "Add the account to the {$role} group" );
00045         }
00046         $this->addArg( "username", "Username of new user" );
00047         $this->addArg( "password", "Password to set (not required if --force is used)", false );
00048     }
00049 
00050     public function execute() {
00051         $username = $this->getArg( 0 );
00052         $password = $this->getArg( 1 );
00053         $force = $this->hasOption( 'force' );
00054         $inGroups = array();
00055 
00056         $user = User::newFromName( $username );
00057         if ( !is_object( $user ) ) {
00058             $this->error( "invalid username.", true );
00059         }
00060 
00061         $exists = ( 0 !== $user->idForName() );
00062 
00063         if ( $exists && !$force ) {
00064             $this->error( "Account exists. Perhaps you want the --force option?", true );
00065         } elseif ( !$exists && !$password ) {
00066             $this->error( "Argument <password> required!", false );
00067             $this->maybeHelp( true );
00068         } elseif ( $exists ) {
00069             $inGroups = $user->getGroups();
00070         }
00071 
00072         $promotions = array_diff(
00073             array_filter( self::$permitRoles, array( $this, 'hasOption' ) ),
00074             $inGroups
00075         );
00076 
00077         if ( $exists && !$password && count( $promotions ) === 0 ) {
00078             $this->output( "Account exists and nothing to do.\n" );
00079 
00080             return;
00081         } elseif ( count( $promotions ) !== 0 ) {
00082             $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
00083             if ( $exists ) {
00084                 $this->output( wfWikiID() . ": Promoting $promoText" );
00085             } else {
00086                 $this->output( wfWikiID() . ": Creating and promoting $promoText" );
00087             }
00088         }
00089 
00090         if ( $password ) {
00091             # Try to set the password
00092             try {
00093                 $user->setPassword( $password );
00094                 if ( $exists ) {
00095                     $this->output( "Password set.\n" );
00096                     $user->saveSettings();
00097                 }
00098             } catch ( PasswordError $pwe ) {
00099                 $this->error( $pwe->getText(), true );
00100             }
00101         }
00102 
00103         if ( !$exists ) {
00104             # Insert the account into the database
00105             $user->addToDatabase();
00106             $user->saveSettings();
00107         }
00108 
00109         # Promote user
00110         array_map( array( $user, 'addGroup' ), $promotions );
00111 
00112         if ( !$exists ) {
00113             # Increment site_stats.ss_users
00114             $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
00115             $ssu->doUpdate();
00116         }
00117 
00118         $this->output( "done.\n" );
00119     }
00120 }
00121 
00122 $maintClass = "CreateAndPromote";
00123 require_once RUN_MAINTENANCE_IF_MAIN;