function doAttachMerge()
 {
     global $wgCentralAuthDryRun;
     $globalUser = new CentralAuthUser($this->getUser()->getName());
     if (!$globalUser->exists()) {
         throw new MWException("User doesn't exist -- race condition?");
     }
     if ($globalUser->isAttached()) {
         throw new MWException("Already attached -- race condition?");
     }
     if ($wgCentralAuthDryRun) {
         $this->dryRunError();
         return;
     }
     $password = $this->getRequest()->getText('wpPassword');
     if ($globalUser->authenticate($password) == 'ok') {
         $globalUser->attach(wfWikiID(), 'password');
         $this->getOutput()->addWikiMsg('centralauth-attach-success');
         $this->showCleanupForm();
     } else {
         $this->getOutput()->addHTML('<div class="errorbox">' . wfMsg('wrongpassword') . '</div>' . $this->attachActionForm());
     }
 }
 /**
  * @covers CentralAuthUser::attach
  */
 public function testAttach()
 {
     $caUser = new CentralAuthUser('GlobalUser');
     $caUser->attach('anotherwiki', 'admin', false);
     $this->assertSame(true, $caUser->exists());
     $this->assertSame(true, in_array('anotherwiki', $caUser->listAttached()));
 }
 function migrate($username, $homewiki = null)
 {
     $this->total++;
     $this->output("CentralAuth account migration for: " . $username . "\n");
     $central = new CentralAuthUser($username);
     try {
         $unattached = $central->queryUnattached();
     } catch (Exception $e) {
         // This might happen due to localnames inconsistencies (bug 67350)
         $this->output("ERROR: Fetching unattached accounts for {$username} failed.");
         return;
     }
     /**
      * Migration with an existing global account
      */
     if ($central->exists()) {
         $this->output("INFO: A global account already exists for: {$username}\n");
         if ($this->getOption('attachmissing', false) && !is_null($central->getEmailAuthenticationTimestamp())) {
             foreach ($unattached as $wiki => $local) {
                 if ($central->getEmail() === $local['email'] && !is_null($local['emailAuthenticated'])) {
                     $this->output("ATTACHING: {$username}@{$wiki}\n");
                     $central->attach($wiki, 'mail', !$this->suppressRC);
                 }
             }
         }
         if ($this->getOption('attachbroken', false)) {
             // This option is for bug 61876 / bug 39996 where the account has
             // an empty password and email set, and became unattached.
             // Since there is no way an account can have an empty password manually
             // it has to be due to a CentralAuth bug. So just attach it then.
             // But just to be on the safe side, check that it also has 0 edits.
             foreach ($unattached as $wiki => $local) {
                 if ($local['email'] === '' && $local['password'] === '' && $local['editCount'] === '0') {
                     $this->output("ATTACHING: {$username}@{$wiki}\n");
                     // Ironically, the attachment is made due to lack of a password.
                     $central->attach($wiki, 'password', !$this->suppressRC);
                 }
             }
         }
     } else {
         if (count($unattached) == 0) {
             $this->output("ERROR: No local accounts found for: {$username}\n");
             return;
         }
         if ($this->safe && count($unattached) !== 1) {
             $this->output("ERROR: More than 1 local user account found for username: {$username}\n");
             foreach ($unattached as $local) {
                 $this->output("\t" . $central->getName() . "@" . $local['wiki'] . "\n");
             }
             return;
         }
         if ($homewiki !== null) {
             if (!array_key_exists($homewiki, $unattached)) {
                 $this->output("ERROR: Unattached user not found for {$username}@{$homewiki}\n");
                 return;
             }
             $this->output("INFO: Setting homewiki for '{$username}' to {$homewiki}\n");
             $central->mHomeWiki = $homewiki;
         }
         // Check that all unattached (i.e. ALL) accounts have a confirmed email
         // address and that the addresses are all the same. We are using this
         // to match accounts to the same user, since we can't use the password.
         $emailMatch = true;
         $email = null;
         foreach ($unattached as $local) {
             if (is_null($email)) {
                 $email = $local['email'];
             }
             if ($local['email'] === $email && !is_null($local['emailAuthenticated'])) {
                 continue;
             }
             $emailMatch = false;
             break;
         }
         // All of the emails are the same and confirmed? Merge all the accounts.
         // They aren't? Skip, or merge the winner if --auto was specified.
         if ($emailMatch) {
             $this->output("Email addresses match and are confirmed for: {$username}\n");
             $central->storeAndMigrate(array(), !$this->suppressRC);
         } else {
             if (isset($central->mHomeWiki) || $this->autoMigrate) {
                 $central->storeAndMigrate(array(), !$this->suppressRC);
             } else {
                 $this->output("ERROR: Auto migration is disabled and email addresses do not match for: {$username}\n");
             }
         }
     }
     $unattachedAfter = $central->queryUnattached();
     if (count($unattachedAfter) == 0) {
         $this->migrated++;
         return;
     } elseif (count($unattachedAfter) > 0 && count($unattachedAfter) < count($unattached)) {
         $this->partial++;
         $this->output("INFO: Incomplete migration for '{$username}'\n");
     }
     if ($this->resetToken) {
         $this->output("INFO: Resetting CentralAuth auth token for '{$username}'\n");
         $central->resetAuthToken();
     }
 }
 function doAttachMerge()
 {
     global $wgCentralAuthDryRun;
     $globalUser = new CentralAuthUser($this->getUser()->getName());
     if (!$globalUser->exists()) {
         throw new Exception("User doesn't exist -- race condition?");
     }
     if ($globalUser->isAttached()) {
         // Already attached - race condition
         $this->showCleanupForm();
         return;
     }
     if ($wgCentralAuthDryRun) {
         $this->dryRunError();
         return;
     }
     $password = $this->getRequest()->getText('wpPassword');
     if ($globalUser->authenticate($password) == 'ok') {
         $globalUser->attach(wfWikiID(), 'password');
         $this->getOutput()->addWikiMsg('centralauth-attach-success');
         $this->showCleanupForm();
     } else {
         $this->getOutput()->addHTML(Html::rawElement('div', array("class" => "errorbox"), $this->msg('wrongpassword')->escaped()) . $this->attachActionForm());
     }
 }