Example #1
0
 /**
  * Add a user to the database, return the user object
  *
  * @param string $name The user's name
  * @param array $params Associative array of non-default parameters to save to the database:
  *     password             The user's password. Password logins will be disabled if this is omitted.
  *     newpassword          A temporary password mailed to the user
  *     email                The user's email address
  *     email_authenticated  The email authentication timestamp
  *     real_name            The user's real name
  *     options              An associative array of non-default options
  *     token                Random authentication token. Do not set.
  *     registration         Registration timestamp. Do not set.
  *
  * @return User object, or null if the username already exists
  */
 static function createNew($name, $params = array())
 {
     $user = new User();
     $user->load();
     if (isset($params['options'])) {
         $user->mOptions = $params['options'] + $user->mOptions;
         unset($params['options']);
     }
     $dbw = wfGetDB(DB_MASTER);
     $seqVal = $dbw->nextSequenceValue('user_user_id_seq');
     $fields = array('user_id' => $seqVal, 'user_name' => $name, 'user_password' => $user->mPassword, 'user_newpassword' => $user->mNewpassword, 'user_newpass_time' => $dbw->timestamp($user->mNewpassTime), 'user_email' => $user->mEmail, 'user_email_authenticated' => $dbw->timestampOrNull($user->mEmailAuthenticated), 'user_real_name' => $user->mRealName, 'user_options' => $user->encodeOptions(), 'user_token' => $user->mToken, 'user_registration' => $dbw->timestamp($user->mRegistration), 'user_editcount' => 0);
     foreach ($params as $name => $value) {
         $fields["user_{$name}"] = $value;
     }
     $dbw->insert('user', $fields, __METHOD__, array('IGNORE'));
     if ($dbw->affectedRows()) {
         $newUser = User::newFromId($dbw->insertId());
     } else {
         $newUser = null;
     }
     return $newUser;
 }