Exemplo n.º 1
0
 /**
  * Checks if the request provided to the constructor is valid.
  *
  * @return bool True if all prerequisites are met
  */
 protected function setup()
 {
     wfProfileIn(__METHOD__);
     global $wgContLang, $wgCapitalLinks;
     //Sanitize input data
     $oldnamePar = trim(str_replace('_', ' ', $this->mRequestData->oldUsername));
     $oldTitle = Title::makeTitle(NS_USER, $oldnamePar);
     // Force uppercase of newusername, otherwise wikis with wgCapitalLinks=false can create lc usernames
     $newTitle = Title::makeTitleSafe(NS_USER, $wgContLang->ucfirst($this->mRequestData->newUsername));
     $oun = is_object($oldTitle) ? $oldTitle->getText() : '';
     $nun = is_object($newTitle) ? $newTitle->getText() : '';
     $this->addInternalLog("title: old={$oun} new={$nun}");
     //AntiSpoof test
     if (class_exists('SpoofUser')) {
         $oNewSpoofUser = new SpoofUser($nun);
         if (!$oNewSpoofUser->isLegal()) {
             $this->addWarning(wfMessage('userrenametool-error-antispoof-conflict', $nun));
         }
     } else {
         $this->addError(wfMessage('userrenametool-error-antispoof-notinstalled'));
     }
     //Phalanx test
     $warning = RenameUserHelper::testBlock($oun);
     if (!empty($warning)) {
         $this->addWarning($warning);
     }
     $warning = RenameUserHelper::testBlock($nun);
     if (!empty($warning)) {
         $this->addWarning($warning);
     }
     //Invalid old user name entered
     if (!$oun) {
         $this->addError(wfMessage('userrenametool-errorinvalid', $this->mRequestData->oldUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     }
     //Invalid new user name entered
     if (!$nun) {
         $this->addError(wfMessage('userrenametool-errorinvalidnew', $this->mRequestData->newUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     }
     //Old username is the same as new username
     if ($oldTitle->getText() === $newTitle->getText()) {
         $this->addError(wfMessage('userrenametool-error-same-user')->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     }
     //validate new username and disable validation for old username
     $olduser = User::newFromName($oldTitle->getText(), false);
     $newuser = User::newFromName($newTitle->getText(), 'creatable');
     // It won't be an object if for instance "|" is supplied as a value
     if (!is_object($olduser)) {
         $this->addError(wfMessage('userrenametool-errorinvalid', $this->mRequestData->oldUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     }
     if (!is_object($newuser) || !User::isCreatableName($newuser->getName())) {
         $this->addError(wfMessage('userrenametool-errorinvalid', $this->mRequestData->newUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     }
     $this->addInternalLog("user: old={$olduser->getName()}:{$olduser->getId()} new={$newuser->getName()}:{$newuser->getId()}");
     // Check for the existence of lowercase oldusername in database.
     // Until r19631 it was possible to rename a user to a name with first character as lowercase
     if ($oldTitle->getText() !== $wgContLang->ucfirst($oldTitle->getText())) {
         // oldusername was entered as lowercase -> check for existence in table 'user'
         $dbr = WikiFactory::db(DB_SLAVE);
         $uid = $dbr->selectField('`user`', 'user_id', array('user_name' => $oldTitle->getText()), __METHOD__);
         $this->addLog('Running query: ' . $dbr->lastQuery() . " resulted in " . $dbr->affectedRows() . " row(s) being affected.");
         if ($uid === false) {
             if (!$wgCapitalLinks) {
                 $uid = 0;
                 // We are on a lowercase wiki but lowercase username does not exists
             } else {
                 // We are on a standard uppercase wiki, use normal
                 $uid = $olduser->idForName();
                 $oldTitle = Title::makeTitleSafe(NS_USER, $olduser->getName());
             }
         }
     } else {
         // oldusername was entered as upperase -> standard procedure
         $uid = $olduser->idForName();
     }
     $this->addInternalLog("id: uid={$uid} old={$olduser->getName()}:{$olduser->getId()} new={$newuser->getName()}:{$newuser->getId()}");
     //If old user name does not exist:
     if ($uid == 0) {
         $this->addError(wfMessage('userrenametool-errordoesnotexist', $this->mRequestData->oldUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     } elseif ($olduser->isLocked()) {
         $this->addError(wfMessage('userrenametool-errorlocked', $this->mRequestData->oldUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     } elseif ($olduser->isAllowed('bot')) {
         $this->addError(wfMessage('userrenametool-errorbot', $this->mRequestData->oldUsername)->inContentLanguage()->text());
         wfProfileOut(__METHOD__);
         return false;
     }
     $fakeuid = 0;
     //If new user name does exist (we have a special case - repeating rename process)
     if ($newuser->idForName() != 0) {
         $repeating = false;
         $processing = false;
         //invalidate properties cache and reload to get updated data
         //needed here, if the cache is wrong bad things happen
         $this->addInternalLog("pre-invalidate: titletext={$oldTitle->getText()} old={$olduser->getName()}");
         $olduser->invalidateCache();
         $olduser = User::newFromName($oldTitle->getText(), false);
         $renameData = $olduser->getGlobalAttribute('renameData', '');
         $this->addInternalLog("post-invalidate: titletext={$oldTitle->getText()} old={$olduser->getName()}:{$olduser->getId()}");
         $this->addLog("Scanning user option renameData for process data: {$renameData}");
         if (stripos($renameData, self::RENAME_TAG) !== false) {
             $tokens = explode(';', $renameData, 3);
             if (!empty($tokens[0])) {
                 $nameTokens = explode('=', $tokens[0], 2);
                 $repeating = count($nameTokens) == 2 && $nameTokens[0] === self::RENAME_TAG && $nameTokens[1] === $newuser->getName();
             }
             if (!empty($tokens[1])) {
                 $statusTokens = explode('=', $tokens[1], 2);
                 $processing = count($statusTokens) == 2 && $statusTokens[0] === self::PROCESS_TAG && (int) $statusTokens[1] === 1;
             }
             if (!empty($tokens[2])) {
                 $blockTokens = explode('=', $tokens[2], 2);
                 if (count($blockTokens) == 2 && $blockTokens[0] === self::PHALANX_BLOCK_TAG && is_numeric($blockTokens[1])) {
                     $this->mPhalanxBlockId = (int) $blockTokens[1];
                 }
             }
         }
         /**
         			 * Not needed, process must be resumable even in case of fatal errors, if 2 processes are run nothing bad happens
         			//if the process is already running throw an error
         			if($processing){
         				$this->addError( wfMessage( 'userrenametool-errorprocessing', $olduser->getName(), $newuser->getName())->inContentLanguage()->text() );
         				wfProfileOut(__METHOD__);
         				return false;
         			}*/
         if ($repeating) {
             $this->addWarning(wfMessage('userrenametool-warn-repeat', $this->mRequestData->oldUsername, $this->mRequestData->newUsername)->inContentLanguage()->text());
             //Swap the uids because the real user ID is the new user ID in this special case
             $fakeuid = $uid;
             $uid = $newuser->idForName();
         } else {
             //In the case other than repeating the process drop an error
             $this->addError(wfMessage('userrenametool-errorexists', $newuser->getName())->inContentLanguage()->text());
             wfProfileOut(__METHOD__);
             return false;
         }
     }
     //Execute Warning hook (arguments the same as in the original Renameuser extension)
     if (!$this->mActionConfirmed) {
         wfRunHooks('UserRename::Warning', array($this->mRequestData->oldUsername, $this->mRequestData->newUsername, &$this->mWarnings));
     }
     $this->mOldUsername = $olduser->getName();
     $this->mNewUsername = $newuser->getName();
     $this->mUserId = (int) $uid;
     $this->mFakeUserId = $fakeuid;
     $this->addInternalLog("setup: uid={$this->mUserId} fakeuid={$this->mFakeUserId} old={$this->mOldUsername} new={$this->mNewUsername}");
     //If there are only warnings and user confirmed that, do not show them again
     //on success page ;-)
     if ($this->mActionConfirmed) {
         $this->mWarnings = array();
     } elseif (count($this->mWarnings)) {
         //in case action is not confirmed and there are warnings display them and wait for confirmation before running the process
         wfProfileOut(__METHOD__);
         return false;
     }
     wfProfileOut(__METHOD__);
     return empty($this->mErrors);
 }