function execute($subpage)
 {
     global $wgContLang;
     $this->setHeaders();
     $this->mCanUnmerge = $this->getUser()->isAllowed('centralauth-unmerge');
     $this->mCanLock = $this->getUser()->isAllowed('centralauth-lock');
     $this->mCanOversight = $this->getUser()->isAllowed('centralauth-oversight');
     $this->mCanEdit = $this->mCanUnmerge || $this->mCanLock || $this->mCanOversight;
     $this->getOutput()->addModules('ext.centralauth');
     $this->getOutput()->addModuleStyles('ext.centralauth.noflash');
     $this->addMergeMethodDescriptions();
     $this->mUserName = trim(str_replace('_', ' ', $this->getRequest()->getText('target', $subpage)));
     $this->mUserName = $wgContLang->ucfirst($this->mUserName);
     $this->mPosted = $this->getRequest()->wasPosted();
     $this->mMethod = $this->getRequest()->getVal('wpMethod');
     $this->mWikis = (array) $this->getRequest()->getArray('wpWikis');
     // Possible demo states
     // success, all accounts merged
     // successful login, some accounts merged, others left
     // successful login, others left
     // not account owner, others left
     // is owner / is not owner
     // did / did not merge some accounts
     // do / don't have more accounts to merge
     if ($this->mUserName === '') {
         # First time through
         $this->getOutput()->addWikiMsg('centralauth-admin-intro');
         $this->showUsernameForm();
         return;
     }
     $this->mGlobalUser = $globalUser = new CentralAuthUser($this->mUserName);
     if (!$globalUser->exists() || $globalUser->isOversighted() && !$this->mCanOversight) {
         $this->showError('centralauth-admin-nonexistent', $this->mUserName);
         $this->showUsernameForm();
         return;
     }
     $continue = true;
     if ($this->mCanEdit && $this->mPosted) {
         $continue = $this->doSubmit();
     }
     $this->mAttachedLocalAccounts = $this->mGlobalUser->queryAttached();
     $this->mUnattachedLocalAccounts = $this->mGlobalUser->queryUnattached();
     $this->showUsernameForm();
     if ($continue) {
         $this->showInfo();
         if ($this->mCanLock) {
             $this->showStatusForm();
         }
         if ($this->mCanUnmerge) {
             $this->showActionForm('delete');
         }
         if ($this->mCanEdit) {
             $this->showLogExtract();
         }
         $this->showWikiLists();
     }
 }
 public function execute()
 {
     $dbw = CentralAuthUser::getCentralDB();
     $databaseUpdates = new UsersToRenameDatabaseUpdates($dbw);
     // CentralAuthUser::chooseHomeWiki is expensive and called
     // multiple times, so lets cache it.
     $cache = new MapCacheLRU($this->mBatchSize);
     do {
         $rows = $this->doQuery();
         $insertRows = array();
         foreach ($rows as $row) {
             $this->lName = $row->name;
             $this->lWiki = $row->wiki;
             if ($cache->has($row->name)) {
                 $attachableWikis = $cache->get($row->name);
             } else {
                 $ca = new CentralAuthUser($row->name);
                 $attachableWikis = array();
                 $unattached = $ca->queryUnattached();
                 if ($ca->exists()) {
                     $home = $ca->getHomeWiki();
                     $attachableWikis[] = $home;
                     foreach ($unattached as $wiki => $info) {
                         if ($ca->getEmailAuthenticationTimestamp() && $info['email'] === $ca->getEmail() && !is_null($info['emailAuthenticated'])) {
                             $attachableWikis[] = $wiki;
                         }
                     }
                 } else {
                     $home = $ca->chooseHomeWiki($unattached);
                     $attachableWikis[] = $home;
                     if ($unattached[$home]['email'] && isset($unattached[$home]['emailAuthenticated'])) {
                         foreach ($unattached as $wiki => $info) {
                             if ($wiki !== $home && $unattached[$home]['email'] === $info['email'] && isset($info['emailAuthenticated'])) {
                                 $attachableWikis[] = $wiki;
                             }
                         }
                     }
                 }
                 $cache->set($row->name, $attachableWikis);
             }
             if (!in_array($row->wiki, $attachableWikis)) {
                 // Unattached account which is not attachable,
                 // so they're getting renamed :(
                 $this->output("{$row->name}@{$row->wiki} is going to be renamed.\n");
                 $insertRows[] = (array) $row;
             }
         }
         $databaseUpdates->batchInsert($insertRows);
         $count = $dbw->affectedRows();
         $this->output("Inserted {$count} users who we will rename\n");
         $this->output("Waiting for slaves...\n");
         CentralAuthUser::waitForSlaves();
     } while ($count !== 0);
 }
 function process($username)
 {
     $central = new CentralAuthUser($username);
     if (!$central->exists()) {
         $this->output("ERROR: [{$username}] Central account does not exist. So how'd we find it?\n");
         return;
     }
     try {
         $unattached = $central->queryUnattached();
     } catch (Exception $e) {
         // This might happen due to localnames inconsistencies (bug 67350)
         $this->output("ERROR: [{$username}] Fetching unattached accounts failed.\n");
         return;
     }
     foreach ($unattached as $wiki => $local) {
         if ($local['email'] === '' && $local['password'] === '') {
             $this->output("SKIP: [{$username}] Account on {$wiki} has no password or email\n");
             return;
         }
     }
     if ($this->fix) {
         $reason = wfMessage('centralauth-delete-empty-account')->inContentLanguage()->text();
         $status = $central->adminDelete($reason);
         if (!$status->isGood()) {
             $msg = $status->errors[0]['message'];
             if ($msg instanceof Message) {
                 $msg = $msg->getKey();
             }
             $this->output("ERROR: [{$username}] Delete failed ({$msg})\n");
             return;
         }
         $this->output("DELETE: [{$username}] Deleted\n");
     } else {
         $this->output("DELETE: [{$username}] Would delete\n");
     }
     if (count($unattached) !== 0 && $this->migrate) {
         if ($this->fix) {
             $central = CentralAuthUser::newUnattached($username, true);
             if ($central->storeAndMigrate(array(), !$this->suppressRC, $this->safe)) {
                 $unattachedAfter = count($central->queryUnattached());
                 $this->output("MIGRATE: [{$username}] Success; {$unattachedAfter} left unattached\n");
             } else {
                 $this->output("MIGRATE: [{$username}] Fail\n");
             }
         } else {
             $this->output("MIGRATE: [{$username}] Would attempt\n");
         }
     }
 }
 public function execute()
 {
     $params = $this->extractRequestParams();
     $prop = array_flip((array) $params['prop']);
     if (is_null($params['user'])) {
         $params['user'] = $this->getUser()->getName();
     }
     $user = new CentralAuthUser($params['user']);
     // Add basic info
     $result = $this->getResult();
     $data = array();
     $userExists = $user->exists();
     if ($userExists) {
         $data['home'] = $user->getHomeWiki();
         $data['id'] = $user->getId();
         $data['registration'] = wfTimestamp(TS_ISO_8601, $user->getRegistration());
         if ($user->isLocked()) {
             $data['locked'] = '';
         }
         if ($user->isHidden()) {
             $data['hidden'] = '';
         }
     } else {
         $data['missing'] = '';
     }
     $result->addValue('query', $this->getModuleName(), $data);
     // Add requested info
     if ($userExists && isset($prop['groups'])) {
         $groups = $user->getGlobalGroups();
         $result->setIndexedTagName($groups, 'g');
         $result->addValue(array('query', $this->getModuleName()), 'groups', $groups);
     }
     if ($userExists && isset($prop['rights'])) {
         $rights = $user->getGlobalRights();
         $result->setIndexedTagName($rights, 'r');
         $result->addValue(array('query', $this->getModuleName()), 'rights', $rights);
     }
     if ($userExists && isset($prop['merged'])) {
         $accounts = $user->queryAttached();
         foreach ($accounts as $account) {
             $dbname = $account['wiki'];
             $a = array('wiki' => $dbname, 'url' => $this->getUrl($dbname), 'timestamp' => wfTimestamp(TS_ISO_8601, $account['attachedTimestamp']), 'method' => $account['attachedMethod'], 'editcount' => $account['editCount']);
             if ($account['blocked']) {
                 $a['blocked'] = array('expiry' => $this->getLanguage()->formatExpiry($account['block-expiry'], TS_ISO_8601), 'reason' => $account['block-reason']);
             }
             $result->addValue(array('query', $this->getModuleName(), 'merged'), null, $a);
         }
         $result->setIndexedTagName_internal(array('query', $this->getModuleName(), 'merged'), 'account');
     }
     if (isset($prop['unattached'])) {
         $accounts = $user->queryUnattached();
         foreach ($accounts as $account) {
             $a = array('wiki' => $account['wiki'], 'editcount' => $account['editCount']);
             if ($account['blocked']) {
                 $a['blocked'] = array('expiry' => $this->getLanguage()->formatExpiry($account['block-expiry'], TS_ISO_8601), 'reason' => $account['block-reason']);
             }
             $result->addValue(array('query', $this->getModuleName(), 'unattached'), null, $a);
         }
         $result->setIndexedTagName_internal(array('query', $this->getModuleName(), 'unattached'), 'account');
     }
 }
 public function execute($subpage)
 {
     global $wgContLang;
     $this->setHeaders();
     $this->mCanUnmerge = $this->getUser()->isAllowed('centralauth-unmerge');
     $this->mCanLock = $this->getUser()->isAllowed('centralauth-lock');
     $this->mCanOversight = $this->getUser()->isAllowed('centralauth-oversight');
     $this->mCanEdit = $this->mCanUnmerge || $this->mCanLock || $this->mCanOversight;
     $this->getOutput()->setPageTitle($this->msg($this->mCanEdit ? 'centralauth' : 'centralauth-ro'));
     $this->getOutput()->addModules('ext.centralauth');
     $this->getOutput()->addModules('ext.centralauth.globaluserautocomplete');
     $this->getOutput()->addModuleStyles('ext.centralauth.noflash');
     $this->getOutput()->addJsConfigVars('wgMergeMethodDescriptions', $this->getMergeMethodDescriptions());
     $this->mUserName = trim(str_replace('_', ' ', $this->getRequest()->getText('target', $subpage)));
     $this->mUserName = $wgContLang->ucfirst($this->mUserName);
     $this->mPosted = $this->getRequest()->wasPosted();
     $this->mMethod = $this->getRequest()->getVal('wpMethod');
     $this->mWikis = (array) $this->getRequest()->getArray('wpWikis');
     // Possible demo states
     // success, all accounts merged
     // successful login, some accounts merged, others left
     // successful login, others left
     // not account owner, others left
     // is owner / is not owner
     // did / did not merge some accounts
     // do / don't have more accounts to merge
     if ($this->mUserName === '') {
         # First time through
         $this->getOutput()->addWikiMsg('centralauth-admin-intro');
         $this->showUsernameForm();
         return;
     }
     // per bug 47991
     $this->getOutput()->setHTMLTitle($this->msg('pagetitle', $this->msg($this->mCanEdit ? 'centralauth-admin-title' : 'centralauth-admin-title-ro', $this->mUserName)->plain()));
     $this->mGlobalUser = $globalUser = new CentralAuthUser($this->mUserName);
     if (($globalUser->isOversighted() || $globalUser->isHidden()) && !$this->mCanOversight) {
         // Claim that there's nothing if the global account is hidden and the user is not
         // allowed to see it.
         $this->showNonexistentError();
         return;
     }
     $continue = true;
     if ($this->mCanEdit && $this->mPosted) {
         $continue = $this->doSubmit();
     }
     // Show just a user friendly message when a rename is in progress
     try {
         $this->mAttachedLocalAccounts = $globalUser->queryAttached();
     } catch (Exception $e) {
         if ($globalUser->renameInProgress()) {
             $this->showRenameInProgressError();
             return;
         }
         // Rethrow
         throw $e;
     }
     $this->mUnattachedLocalAccounts = $globalUser->queryUnattached();
     if (!$globalUser->exists() && !count($this->mUnattachedLocalAccounts)) {
         // Nothing to see here
         $this->showNonexistentError();
         return;
     }
     $this->showUsernameForm();
     if ($continue && $globalUser->exists()) {
         $this->showInfo();
         if ($this->mCanLock) {
             $this->showStatusForm();
         }
         if ($this->mCanUnmerge) {
             $this->showActionForm('delete');
         }
         $this->showLogExtract();
         $this->showWikiLists();
     } elseif ($continue && !$globalUser->exists()) {
         // No global account, but we can still list the local ones
         $this->showError('centralauth-admin-nonexistent', $this->mUserName);
         $this->showWikiLists();
     }
 }
 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();
     }
 }
 public function execute()
 {
     $params = $this->extractRequestParams();
     $prop = array_flip((array) $params['prop']);
     if (is_null($params['user'])) {
         $params['user'] = $this->getUser()->getName();
     }
     $user = new CentralAuthUser($params['user']);
     // Add basic info
     $result = $this->getResult();
     $data = array();
     $userExists = $user->exists();
     if ($userExists && ($user->getHiddenLevel() === CentralAuthUser::HIDDEN_NONE || $this->getUser()->isAllowed('centralauth-oversight'))) {
         // The global user exists and it's not hidden or the current user is allowed to see it
         $data['home'] = $user->getHomeWiki();
         $data['id'] = $user->getId();
         $data['registration'] = wfTimestamp(TS_ISO_8601, $user->getRegistration());
         $data['name'] = $user->getName();
         if ($user->isLocked()) {
             $data['locked'] = '';
         }
         if ($user->isHidden()) {
             $data['hidden'] = '';
         }
     } else {
         // The user doesn't exist or we pretend it doesn't if it's hidden
         $data['missing'] = '';
     }
     $result->addValue('query', $this->getModuleName(), $data);
     // Add requested info
     if ($userExists && isset($prop['groups'])) {
         $groups = $user->getGlobalGroups();
         $result->setIndexedTagName($groups, 'g');
         $result->addValue(array('query', $this->getModuleName()), 'groups', $groups);
     }
     if ($userExists && isset($prop['rights'])) {
         $rights = $user->getGlobalRights();
         $result->setIndexedTagName($rights, 'r');
         $result->addValue(array('query', $this->getModuleName()), 'rights', $rights);
     }
     $attachedAccounts = null;
     if ($userExists && (isset($prop['merged']) || isset($prop['editcount']))) {
         $attachedAccounts = $user->queryAttached();
     }
     if ($userExists && isset($prop['merged'])) {
         foreach ($attachedAccounts as $account) {
             $dbname = $account['wiki'];
             $wiki = WikiMap::getWiki($dbname);
             $a = array('wiki' => $dbname, 'url' => $wiki->getCanonicalServer(), 'timestamp' => wfTimestamp(TS_ISO_8601, $account['attachedTimestamp']), 'method' => $account['attachedMethod'], 'editcount' => $account['editCount']);
             if ($account['blocked']) {
                 $a['blocked'] = array('expiry' => $this->getLanguage()->formatExpiry($account['block-expiry'], TS_ISO_8601), 'reason' => $account['block-reason']);
             }
             $result->addValue(array('query', $this->getModuleName(), 'merged'), null, $a);
         }
         if (defined('ApiResult::META_CONTENT')) {
             $result->addIndexedTagName(array('query', $this->getModuleName(), 'merged'), 'account');
         } else {
             $result->setIndexedTagName_internal(array('query', $this->getModuleName(), 'merged'), 'account');
         }
     }
     if ($userExists && isset($prop['editcount'])) {
         $editcount = 0;
         foreach ($attachedAccounts as $account) {
             $editcount += $account['editCount'];
         }
         $result->addValue('query', $this->getModuleName(), array('editcount' => $editcount));
     }
     if (isset($prop['unattached'])) {
         $accounts = $user->queryUnattached();
         foreach ($accounts as $account) {
             $a = array('wiki' => $account['wiki'], 'editcount' => $account['editCount']);
             if ($account['blocked']) {
                 $a['blocked'] = array('expiry' => $this->getLanguage()->formatExpiry($account['block-expiry'], TS_ISO_8601), 'reason' => $account['block-reason']);
             }
             $result->addValue(array('query', $this->getModuleName(), 'unattached'), null, $a);
         }
         if (defined('ApiResult::META_CONTENT')) {
             $result->addIndexedTagName(array('query', $this->getModuleName(), 'unattached'), 'account');
         } else {
             $result->setIndexedTagName_internal(array('query', $this->getModuleName(), 'unattached'), 'account');
         }
     }
 }