/**
  * @covers RequestContext::importScopedSession
  */
 public function testImportScopedSession()
 {
     $context = RequestContext::getMain();
     $oInfo = $context->exportSession();
     $this->assertEquals('127.0.0.1', $oInfo['ip'], "Correct initial IP address.");
     $this->assertEquals(0, $oInfo['userId'], "Correct initial user ID.");
     $user = User::newFromName('UnitTestContextUser');
     $user->addToDatabase();
     $sinfo = array('sessionId' => 'd612ee607c87e749ef14da4983a702cd', 'userId' => $user->getId(), 'ip' => '192.0.2.0', 'headers' => array('USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'));
     // importScopedSession() sets these variables
     $this->setMwGlobals(array('wgUser' => new User(), 'wgRequest' => new FauxRequest()));
     $sc = RequestContext::importScopedSession($sinfo);
     // load new context
     $info = $context->exportSession();
     $this->assertEquals($sinfo['ip'], $info['ip'], "Correct IP address.");
     $this->assertEquals($sinfo['headers'], $info['headers'], "Correct headers.");
     $this->assertEquals($sinfo['sessionId'], $info['sessionId'], "Correct session ID.");
     $this->assertEquals($sinfo['userId'], $info['userId'], "Correct user ID.");
     $this->assertEquals($sinfo['ip'], $context->getRequest()->getIP(), "Correct context IP address.");
     $this->assertEquals($sinfo['headers'], $context->getRequest()->getAllHeaders(), "Correct context headers.");
     $this->assertEquals($sinfo['sessionId'], session_id(), "Correct context session ID.");
     $this->assertEquals(true, $context->getUser()->isLoggedIn(), "Correct context user.");
     $this->assertEquals($sinfo['userId'], $context->getUser()->getId(), "Correct context user ID.");
     $this->assertEquals('UnitTestContextUser', $context->getUser()->getName(), "Correct context user name.");
     unset($sc);
     // restore previous context
     $info = $context->exportSession();
     $this->assertEquals($oInfo['ip'], $info['ip'], "Correct restored IP address.");
     $this->assertEquals($oInfo['headers'], $info['headers'], "Correct restored headers.");
     $this->assertEquals($oInfo['sessionId'], $info['sessionId'], "Correct restored session ID.");
     $this->assertEquals($oInfo['userId'], $info['userId'], "Correct restored user ID.");
 }
 /**
  * Try to create and attach the user.
  * @throws Exception
  * @return bool Success
  */
 public function run()
 {
     $username = $this->params['name'];
     $from = $this->params['from'];
     $wiki = wfWikiID();
     if (isset($this->params['session'])) {
         // restore IP and other request data
         $this->params['session']['userId'] = 0;
         $this->params['session']['sessionId'] = '';
         $callback = RequestContext::importScopedSession($this->params['session']);
     }
     $user = User::newFromName($username);
     $centralUser = CentralAuthUser::getInstance($user);
     if ($user->getId() !== 0) {
         wfDebugLog('CentralAuth', __CLASS__ . ": tried to create local account for {$username} " . "on {$wiki} from {$from} but one already exists\n");
         return true;
     } elseif (!$centralUser->exists()) {
         wfDebugLog('CentralAuth', __CLASS__ . ": tried to create local account for {$username} " . "on {$wiki} from {$from} but no global account exists\n");
         return true;
     } elseif ($centralUser->attachedOn($wiki)) {
         wfDebugLog('CentralAuth', __CLASS__ . ": tried to create local account for {$username} " . "on {$wiki} from {$from} but an attached local account already exists\n");
         return true;
     }
     $success = CentralAuthHooks::attemptAddUser($user);
     if ($success) {
         $centralUser->invalidateCache();
     }
     return true;
 }
 public function run()
 {
     $scope = RequestContext::importScopedSession($this->params['session']);
     $context = RequestContext::getMain();
     try {
         $user = $context->getUser();
         if (!$user->isLoggedIn()) {
             $this->setLastError("Could not load the author user from session.");
             return false;
         }
         if (count($_SESSION) === 0) {
             // Empty session probably indicates that we didn't associate
             // with the session correctly. Note that being able to load
             // the user does not necessarily mean the session was loaded.
             // Most likely cause by suhosin.session.encrypt = On.
             $this->setLastError("Error associating with user session. " . "Try setting suhosin.session.encrypt = Off");
             return false;
         }
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood()));
         $upload = new UploadFromStash($user);
         // @todo initialize() causes a GET, ideally we could frontload the antivirus
         // checks and anything else to the stash stage (which includes concatenation and
         // the local file is thus already there). That way, instead of GET+PUT, there could
         // just be a COPY operation from the stash to the public zone.
         $upload->initialize($this->params['filekey'], $this->params['filename']);
         // Check if the local file checks out (this is generally a no-op)
         $verification = $upload->verifyUpload();
         if ($verification['status'] !== UploadBase::OK) {
             $status = Status::newFatal('verification-error');
             $status->value = array('verification' => $verification);
             UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'publish', 'status' => $status));
             $this->setLastError("Could not verify upload.");
             return false;
         }
         // Upload the stashed file to a permanent location
         $status = $upload->performUpload($this->params['comment'], $this->params['text'], $this->params['watch'], $user);
         if (!$status->isGood()) {
             UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'publish', 'status' => $status));
             $this->setLastError($status->getWikiText());
             return false;
         }
         // Build the image info array while we have the local reference handy
         $apiMain = new ApiMain();
         // dummy object (XXX)
         $imageInfo = $upload->getImageInfo($apiMain->getResult());
         // Cleanup any temporary local file
         $upload->cleanupTempFile();
         // Cache the info so the user doesn't have to wait forever to get the final info
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Success', 'stage' => 'publish', 'filename' => $upload->getLocalFile()->getName(), 'imageinfo' => $imageInfo, 'status' => Status::newGood()));
     } catch (MWException $e) {
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'publish', 'status' => Status::newFatal('api-error-publishfailed')));
         $this->setLastError(get_class($e) . ": " . $e->getText());
         // To prevent potential database referential integrity issues.
         // See bug 32551.
         MWExceptionHandler::rollbackMasterChangesAndLog($e);
         return false;
     }
     return true;
 }
 public function run()
 {
     $scope = RequestContext::importScopedSession($this->params['session']);
     $this->addTeardownCallback(function () use(&$scope) {
         ScopedCallback::consume($scope);
         // T126450
     });
     $context = RequestContext::getMain();
     $user = $context->getUser();
     try {
         if (!$user->isLoggedIn()) {
             $this->setLastError("Could not load the author user from session.");
             return false;
         }
         UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood()]);
         $upload = new UploadFromStash($user);
         // @todo initialize() causes a GET, ideally we could frontload the antivirus
         // checks and anything else to the stash stage (which includes concatenation and
         // the local file is thus already there). That way, instead of GET+PUT, there could
         // just be a COPY operation from the stash to the public zone.
         $upload->initialize($this->params['filekey'], $this->params['filename']);
         // Check if the local file checks out (this is generally a no-op)
         $verification = $upload->verifyUpload();
         if ($verification['status'] !== UploadBase::OK) {
             $status = Status::newFatal('verification-error');
             $status->value = ['verification' => $verification];
             UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Failure', 'stage' => 'publish', 'status' => $status]);
             $this->setLastError("Could not verify upload.");
             return false;
         }
         // Upload the stashed file to a permanent location
         $status = $upload->performUpload($this->params['comment'], $this->params['text'], $this->params['watch'], $user, isset($this->params['tags']) ? $this->params['tags'] : []);
         if (!$status->isGood()) {
             UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Failure', 'stage' => 'publish', 'status' => $status]);
             $this->setLastError($status->getWikiText(false, false, 'en'));
             return false;
         }
         // Build the image info array while we have the local reference handy
         $apiMain = new ApiMain();
         // dummy object (XXX)
         $imageInfo = $upload->getImageInfo($apiMain->getResult());
         // Cleanup any temporary local file
         $upload->cleanupTempFile();
         // Cache the info so the user doesn't have to wait forever to get the final info
         UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Success', 'stage' => 'publish', 'filename' => $upload->getLocalFile()->getName(), 'imageinfo' => $imageInfo, 'status' => Status::newGood()]);
     } catch (Exception $e) {
         UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Failure', 'stage' => 'publish', 'status' => Status::newFatal('api-error-publishfailed')]);
         $this->setLastError(get_class($e) . ": " . $e->getMessage());
         // To prevent potential database referential integrity issues.
         // See bug 32551.
         MWExceptionHandler::rollbackMasterChangesAndLog($e);
         return false;
     }
     return true;
 }
 public function run()
 {
     $scope = RequestContext::importScopedSession($this->params['session']);
     $context = RequestContext::getMain();
     try {
         $user = $context->getUser();
         if (!$user->isLoggedIn()) {
             $this->setLastError("Could not load the author user from session.");
             return false;
         }
         if (count($_SESSION) === 0) {
             // Empty session probably indicates that we didn't associate
             // with the session correctly. Note that being able to load
             // the user does not necessarily mean the session was loaded.
             // Most likely cause by suhosin.session.encrypt = On.
             $this->setLastError("Error associating with user session. " . "Try setting suhosin.session.encrypt = Off");
             return false;
         }
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood()));
         $upload = new UploadFromChunks($user);
         $upload->continueChunks($this->params['filename'], $this->params['filekey'], $context->getRequest());
         // Combine all of the chunks into a local file and upload that to a new stash file
         $status = $upload->concatenateChunks();
         if (!$status->isGood()) {
             UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'assembling', 'status' => $status));
             $this->setLastError($status->getWikiText());
             return false;
         }
         // We have a new filekey for the fully concatenated file
         $newFileKey = $upload->getLocalFile()->getFileKey();
         // Remove the old stash file row and first chunk file
         $upload->stash->removeFileNoAuth($this->params['filekey']);
         // Build the image info array while we have the local reference handy
         $apiMain = new ApiMain();
         // dummy object (XXX)
         $imageInfo = $upload->getImageInfo($apiMain->getResult());
         // Cleanup any temporary local file
         $upload->cleanupTempFile();
         // Cache the info so the user doesn't have to wait forever to get the final info
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Success', 'stage' => 'assembling', 'filekey' => $newFileKey, 'imageinfo' => $imageInfo, 'status' => Status::newGood()));
     } catch (MWException $e) {
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'assembling', 'status' => Status::newFatal('api-error-stashfailed')));
         $this->setLastError(get_class($e) . ": " . $e->getText());
         // To be extra robust.
         MWExceptionHandler::rollbackMasterChangesAndLog($e);
         return false;
     }
     return true;
 }
 public function run()
 {
     $scope = RequestContext::importScopedSession($this->params['session']);
     $this->addTeardownCallback(function () use(&$scope) {
         ScopedCallback::consume($scope);
         // T126450
     });
     $context = RequestContext::getMain();
     $user = $context->getUser();
     try {
         if (!$user->isLoggedIn()) {
             $this->setLastError("Could not load the author user from session.");
             return false;
         }
         UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood()]);
         $upload = new UploadFromChunks($user);
         $upload->continueChunks($this->params['filename'], $this->params['filekey'], new WebRequestUpload($context->getRequest(), 'null'));
         // Combine all of the chunks into a local file and upload that to a new stash file
         $status = $upload->concatenateChunks();
         if (!$status->isGood()) {
             UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Failure', 'stage' => 'assembling', 'status' => $status]);
             $this->setLastError($status->getWikiText(false, false, 'en'));
             return false;
         }
         // We can only get warnings like 'duplicate' after concatenating the chunks
         $status = Status::newGood();
         $status->value = ['warnings' => $upload->checkWarnings()];
         // We have a new filekey for the fully concatenated file
         $newFileKey = $upload->getStashFile()->getFileKey();
         // Remove the old stash file row and first chunk file
         $upload->stash->removeFileNoAuth($this->params['filekey']);
         // Build the image info array while we have the local reference handy
         $apiMain = new ApiMain();
         // dummy object (XXX)
         $imageInfo = $upload->getImageInfo($apiMain->getResult());
         // Cleanup any temporary local file
         $upload->cleanupTempFile();
         // Cache the info so the user doesn't have to wait forever to get the final info
         UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Success', 'stage' => 'assembling', 'filekey' => $newFileKey, 'imageinfo' => $imageInfo, 'status' => $status]);
     } catch (Exception $e) {
         UploadBase::setSessionStatus($user, $this->params['filekey'], ['result' => 'Failure', 'stage' => 'assembling', 'status' => Status::newFatal('api-error-stashfailed')]);
         $this->setLastError(get_class($e) . ": " . $e->getMessage());
         // To be extra robust.
         MWExceptionHandler::rollbackMasterChangesAndLog($e);
         return false;
     }
     return true;
 }
 public function run()
 {
     if (isset($this->params['session'])) {
         $callback = RequestContext::importScopedSession($this->params['session']);
     }
     $this->user = User::newFromName($this->params['renamer']);
     if (isset($this->params['pages'])) {
         // Old calling style for b/c
         foreach ($this->params['pages'] as $current => $target) {
             $this->movePage(Title::newFromText($current), Title::newFromText($target));
         }
     } else {
         $oldTitle = Title::makeTitle($this->params['old'][0], $this->params['old'][1]);
         $newTitle = Title::makeTitle($this->params['new'][0], $this->params['new'][1]);
         $this->movePage($oldTitle, $newTitle);
     }
 }
 /**
  * @covers RequestContext::importScopedSession
  */
 public function testImportScopedSession()
 {
     // Make sure session handling is started
     if (!MediaWiki\Session\PHPSessionHandler::isInstalled()) {
         MediaWiki\Session\PHPSessionHandler::install(MediaWiki\Session\SessionManager::singleton());
     }
     $oldSessionId = session_id();
     $context = RequestContext::getMain();
     $oInfo = $context->exportSession();
     $this->assertEquals('127.0.0.1', $oInfo['ip'], "Correct initial IP address.");
     $this->assertEquals(0, $oInfo['userId'], "Correct initial user ID.");
     $this->assertFalse(MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(), 'Global session isn\'t persistent to start');
     $user = User::newFromName('UnitTestContextUser');
     $user->addToDatabase();
     $sinfo = array('sessionId' => 'd612ee607c87e749ef14da4983a702cd', 'userId' => $user->getId(), 'ip' => '192.0.2.0', 'headers' => array('USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'));
     // importScopedSession() sets these variables
     $this->setMwGlobals(array('wgUser' => new User(), 'wgRequest' => new FauxRequest()));
     $sc = RequestContext::importScopedSession($sinfo);
     // load new context
     $info = $context->exportSession();
     $this->assertEquals($sinfo['ip'], $info['ip'], "Correct IP address.");
     $this->assertEquals($sinfo['headers'], $info['headers'], "Correct headers.");
     $this->assertEquals($sinfo['sessionId'], $info['sessionId'], "Correct session ID.");
     $this->assertEquals($sinfo['userId'], $info['userId'], "Correct user ID.");
     $this->assertEquals($sinfo['ip'], $context->getRequest()->getIP(), "Correct context IP address.");
     $this->assertEquals($sinfo['headers'], $context->getRequest()->getAllHeaders(), "Correct context headers.");
     $this->assertEquals($sinfo['sessionId'], MediaWiki\Session\SessionManager::getGlobalSession()->getId(), "Correct context session ID.");
     if (\MediaWiki\Session\PhpSessionHandler::isEnabled()) {
         $this->assertEquals($sinfo['sessionId'], session_id(), "Correct context session ID.");
     } else {
         $this->assertEquals($oldSessionId, session_id(), "Unchanged PHP session ID.");
     }
     $this->assertEquals(true, $context->getUser()->isLoggedIn(), "Correct context user.");
     $this->assertEquals($sinfo['userId'], $context->getUser()->getId(), "Correct context user ID.");
     $this->assertEquals('UnitTestContextUser', $context->getUser()->getName(), "Correct context user name.");
     unset($sc);
     // restore previous context
     $info = $context->exportSession();
     $this->assertEquals($oInfo['ip'], $info['ip'], "Correct restored IP address.");
     $this->assertEquals($oInfo['headers'], $info['headers'], "Correct restored headers.");
     $this->assertEquals($oInfo['sessionId'], $info['sessionId'], "Correct restored session ID.");
     $this->assertEquals($oInfo['userId'], $info['userId'], "Correct restored user ID.");
     $this->assertFalse(MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(), 'Global session isn\'t persistent after restoring the context');
 }
 public function run()
 {
     $this->setRenameUserStatus(new GlobalRenameUserStatus($this->params['to']));
     if (isset($this->params['session'])) {
         // Don't carry over users or sessions because it's going to be wrong
         // across wikis
         $this->params['session']['userId'] = 0;
         $this->params['session']['sessionId'] = '';
         $callback = RequestContext::importScopedSession($this->params['session']);
     }
     try {
         $this->doRun();
     } catch (Exception $e) {
         // This will lock the user out of their account
         // until a sysadmin intervenes
         $this->updateStatus('failed');
         throw $e;
     }
     return true;
 }
 public function run()
 {
     $scope = RequestContext::importScopedSession($this->params['session']);
     $context = RequestContext::getMain();
     try {
         $user = $context->getUser();
         if (!$user->isLoggedIn()) {
             $this->setLastError("Could not load the author user from session.");
             return false;
         }
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood()));
         $upload = new UploadFromChunks($user);
         $upload->continueChunks($this->params['filename'], $this->params['filekey'], $context->getRequest());
         // Combine all of the chunks into a local file and upload that to a new stash file
         $status = $upload->concatenateChunks();
         if (!$status->isGood()) {
             UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'assembling', 'status' => $status));
             $this->setLastError($status->getWikiText());
             return false;
         }
         // We have a new filekey for the fully concatenated file
         $newFileKey = $upload->getLocalFile()->getFileKey();
         // Remove the old stash file row and first chunk file
         $upload->stash->removeFileNoAuth($this->params['filekey']);
         // Build the image info array while we have the local reference handy
         $apiMain = new ApiMain();
         // dummy object (XXX)
         $imageInfo = $upload->getImageInfo($apiMain->getResult());
         // Cleanup any temporary local file
         $upload->cleanupTempFile();
         // Cache the info so the user doesn't have to wait forever to get the final info
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Success', 'stage' => 'assembling', 'filekey' => $newFileKey, 'imageinfo' => $imageInfo, 'status' => Status::newGood()));
     } catch (MWException $e) {
         UploadBase::setSessionStatus($this->params['filekey'], array('result' => 'Failure', 'stage' => 'assembling', 'status' => Status::newFatal('api-error-stashfailed')));
         $this->setLastError(get_class($e) . ": " . $e->getText());
         return false;
     }
     return true;
 }