[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> createAndPromote.php (source)

   1  <?php
   2  /**
   3   * Creates an account and grants it rights.
   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   * @author Rob Church <[email protected]>
  23   * @author Pablo Castellano <[email protected]>
  24   */
  25  
  26  require_once  __DIR__ . '/Maintenance.php';
  27  
  28  /**
  29   * Maintenance script to create an account and grant it rights.
  30   *
  31   * @ingroup Maintenance
  32   */
  33  class CreateAndPromote extends Maintenance {
  34      private static $permitRoles = array( 'sysop', 'bureaucrat', 'bot' );
  35  
  36  	public function __construct() {
  37          parent::__construct();
  38          $this->mDescription = "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.'
  42          );
  43          foreach ( self::$permitRoles as $role ) {
  44              $this->addOption( $role, "Add the account to the {$role} group" );
  45          }
  46          $this->addArg( "username", "Username of new user" );
  47          $this->addArg( "password", "Password to set (not required if --force is used)", false );
  48      }
  49  
  50  	public function execute() {
  51          $username = $this->getArg( 0 );
  52          $password = $this->getArg( 1 );
  53          $force = $this->hasOption( 'force' );
  54          $inGroups = array();
  55  
  56          $user = User::newFromName( $username );
  57          if ( !is_object( $user ) ) {
  58              $this->error( "invalid username.", true );
  59          }
  60  
  61          $exists = ( 0 !== $user->idForName() );
  62  
  63          if ( $exists && !$force ) {
  64              $this->error( "Account exists. Perhaps you want the --force option?", true );
  65          } elseif ( !$exists && !$password ) {
  66              $this->error( "Argument <password> required!", false );
  67              $this->maybeHelp( true );
  68          } elseif ( $exists ) {
  69              $inGroups = $user->getGroups();
  70          }
  71  
  72          $promotions = array_diff(
  73              array_filter( self::$permitRoles, array( $this, 'hasOption' ) ),
  74              $inGroups
  75          );
  76  
  77          if ( $exists && !$password && count( $promotions ) === 0 ) {
  78              $this->output( "Account exists and nothing to do.\n" );
  79  
  80              return;
  81          } elseif ( count( $promotions ) !== 0 ) {
  82              $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
  83              if ( $exists ) {
  84                  $this->output( wfWikiID() . ": Promoting $promoText" );
  85              } else {
  86                  $this->output( wfWikiID() . ": Creating and promoting $promoText" );
  87              }
  88          }
  89  
  90          if ( $password ) {
  91              # Try to set the password
  92              try {
  93                  $user->setPassword( $password );
  94                  if ( $exists ) {
  95                      $this->output( "Password set.\n" );
  96                      $user->saveSettings();
  97                  }
  98              } catch ( PasswordError $pwe ) {
  99                  $this->error( $pwe->getText(), true );
 100              }
 101          }
 102  
 103          if ( !$exists ) {
 104              # Insert the account into the database
 105              $user->addToDatabase();
 106              $user->saveSettings();
 107          }
 108  
 109          # Promote user
 110          array_map( array( $user, 'addGroup' ), $promotions );
 111  
 112          if ( !$exists ) {
 113              # Increment site_stats.ss_users
 114              $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
 115              $ssu->doUpdate();
 116          }
 117  
 118          $this->output( "done.\n" );
 119      }
 120  }
 121  
 122  $maintClass = "CreateAndPromote";
 123  require_once RUN_MAINTENANCE_IF_MAIN;


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1