/**
  * Performs the request.
  * - bad titles
  * - read restriction
  * - local interwiki redirects
  * - redirect loop
  * - special pages
  * - normal pages
  *
  * @throws MWException|PermissionsError|BadTitleError|HttpError
  * @return void
  */
 private function performRequest()
 {
     global $wgTitle;
     $request = $this->context->getRequest();
     $requestTitle = $title = $this->context->getTitle();
     $output = $this->context->getOutput();
     $user = $this->context->getUser();
     ### <SANDSTORM>
     global $wgAuth;
     $name = array_key_exists('HTTP_X_SANDSTORM_USERNAME', $_SERVER) ? $_SERVER['HTTP_X_SANDSTORM_USERNAME'] : '******';
     $name = urldecode($name);
     $original_name = $name;
     $id = array_key_exists('HTTP_X_SANDSTORM_USER_ID', $_SERVER) ? $_SERVER['HTTP_X_SANDSTORM_USER_ID'] : '';
     $count = 2;
     $isNew = false;
     if ($user->isAnon()) {
         $u = null;
         do {
             $u = User::newFromName($name, 'creatable');
             // TODO
             if (!is_object($u)) {
                 wfLogWarning('no name for ' . $name);
                 throw new BadTitleError();
             }
             if (0 != $u->idForName() && $u->getEmail() !== $id && !empty($id)) {
                 $name = $original_name . $count;
             }
             $count++;
         } while (0 != $u->idForName() && $u->getEmail() !== $id && !empty($id));
         if (0 == $u->idForName()) {
             $permissions = array_key_exists('HTTP_X_SANDSTORM_PERMISSIONS', $_SERVER) ? $_SERVER['HTTP_X_SANDSTORM_PERMISSIONS'] : '';
             $pass = sha1(mt_rand());
             $u->setRealName($name);
             $u->setEmail($id);
             if (!$wgAuth->addUser($u, $pass, "", $name)) {
                 // TODO
                 wfLogWarning('externaldberror');
             }
             $this->initUser($u, $pass, false);
             if (strpos($permissions, 'admin') !== false) {
                 wfLogWarning('setting user as admin: ' . $name);
                 $u->addGroup('sysop');
                 $u->addGroup('bureaucrat');
                 $u->saveSettings();
             }
             if (!empty($id)) {
                 $u->addGroup('editor');
                 $u->saveSettings();
             }
             $isNew = true;
         }
         // wfSetupSession();
         // Not sure why, but I manually have to set session cookie
         setcookie(session_name(), MWCryptRand::generateHex(32), 0, '/');
         $u->setCookies();
         wfResetSessionID();
         $this->context->setUser($u);
         $user = $this->context->getUser();
         $wgUser = $u;
     }
     if ($isNew) {
         wfRunHooks('AddNewAccount', array($u, false));
         $u->addNewUserLogEntry('create');
         $injected_html = '';
         wfRunHooks('UserLoginComplete', array(&$u, &$injected_html));
         $welcome_creation_msg = 'welcomecreation-msg';
         wfRunHooks('BeforeWelcomeCreation', array(&$welcome_creation_msg, &$injected_html));
     } else {
         $injected_html = '';
         wfRunHooks('UserLoginComplete', array(&$u, &$injected_html));
     }
     # </SANDSTORM>
     if ($request->getVal('printable') === 'yes') {
         $output->setPrintable();
     }
     $unused = null;
     // To pass it by reference
     Hooks::run('BeforeInitialize', array(&$title, &$unused, &$output, &$user, $request, $this));
     // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
     if (is_null($title) || $title->getDBkey() == '' && !$title->isExternal() || $title->isSpecial('Badtitle')) {
         $this->context->setTitle(SpecialPage::getTitleFor('Badtitle'));
         throw new BadTitleError();
     }
     // Check user's permissions to read this page.
     // We have to check here to catch special pages etc.
     // We will check again in Article::view().
     $permErrors = $title->isSpecial('RunJobs') ? array() : $title->getUserPermissionsErrors('read', $user);
     if (count($permErrors)) {
         // Bug 32276: allowing the skin to generate output with $wgTitle or
         // $this->context->title set to the input title would allow anonymous users to
         // determine whether a page exists, potentially leaking private data. In fact, the
         // curid and oldid request  parameters would allow page titles to be enumerated even
         // when they are not guessable. So we reset the title to Special:Badtitle before the
         // permissions error is displayed.
         //
         // The skin mostly uses $this->context->getTitle() these days, but some extensions
         // still use $wgTitle.
         $badTitle = SpecialPage::getTitleFor('Badtitle');
         $this->context->setTitle($badTitle);
         $wgTitle = $badTitle;
         throw new PermissionsError('read', $permErrors);
     }
     // Interwiki redirects
     if ($title->isExternal()) {
         $rdfrom = $request->getVal('rdfrom');
         if ($rdfrom) {
             $url = $title->getFullURL(array('rdfrom' => $rdfrom));
         } else {
             $query = $request->getValues();
             unset($query['title']);
             $url = $title->getFullURL($query);
         }
         // Check for a redirect loop
         if (!preg_match('/^' . preg_quote($this->config->get('Server'), '/') . '/', $url) && $title->isLocal()) {
             // 301 so google et al report the target as the actual url.
             $output->redirect($url, 301);
         } else {
             $this->context->setTitle(SpecialPage::getTitleFor('Badtitle'));
             throw new BadTitleError();
         }
         // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
     } elseif ($request->getVal('action', 'view') == 'view' && !$request->wasPosted() && ($request->getVal('title') === null || $title->getPrefixedDBkey() != $request->getVal('title')) && !count($request->getValueNames(array('action', 'title'))) && Hooks::run('TestCanonicalRedirect', array($request, $title, $output))) {
         if ($title->isSpecialPage()) {
             list($name, $subpage) = SpecialPageFactory::resolveAlias($title->getDBkey());
             if ($name) {
                 $title = SpecialPage::getTitleFor($name, $subpage);
             }
         }
         $targetUrl = wfExpandUrl($title->getFullURL(), PROTO_CURRENT);
         // Redirect to canonical url, make it a 301 to allow caching
         if ($targetUrl == $request->getFullRequestURL()) {
             $message = "Redirect loop detected!\n\n" . "This means the wiki got confused about what page was " . "requested; this sometimes happens when moving a wiki " . "to a new server or changing the server configuration.\n\n";
             if ($this->config->get('UsePathInfo')) {
                 $message .= "The wiki is trying to interpret the page " . "title from the URL path portion (PATH_INFO), which " . "sometimes fails depending on the web server. Try " . "setting \"\$wgUsePathInfo = false;\" in your " . "LocalSettings.php, or check that \$wgArticlePath " . "is correct.";
             } else {
                 $message .= "Your web server was detected as possibly not " . "supporting URL path components (PATH_INFO) correctly; " . "check your LocalSettings.php for a customized " . "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " . "to true.";
             }
             throw new HttpError(500, $message);
         } else {
             $output->setSquidMaxage(1200);
             $output->redirect($targetUrl, '301');
         }
         // Special pages
     } elseif (NS_SPECIAL == $title->getNamespace()) {
         // Actions that need to be made when we have a special pages
         SpecialPageFactory::executePath($title, $this->context);
     } else {
         // ...otherwise treat it as an article view. The article
         // may be a redirect to another article or URL.
         $article = $this->initializeArticle();
         if (is_object($article)) {
             $this->performAction($article, $requestTitle);
         } elseif (is_string($article)) {
             $output->redirect($article);
         } else {
             throw new MWException("Shouldn't happen: MediaWiki::initializeArticle()" . " returned neither an object nor a URL");
         }
     }
 }