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