/**
  * Try to determine the controller for the current request.
  *
  * @return type Controller
  */
 public function getNestedController()
 {
     $URLSegment = $this->request->param('URLSegment');
     if (empty($URLSegment)) {
         PrefixRootURLController::set_is_at_root();
         // get the homepage from the defaul homepage Translation Group
         $URLSegment = LanguagePrefix::get_homepage_link_by_locale($this->locale);
         // if no homepage is found in the default translation group for this locale
         // use the first page in the tree instead
         if (empty($URLSegment)) {
             //@TODO: make 3.0
             $sitetree = SiteTree::get()->filter(array('ParentID' => '0', 'Locale' => $this->locale))->sort('Sort')->First();
             if ($sitetree) {
                 $URLSegment = $sitetree->URLSegment;
             } else {
                 // last resort
                 $URLSegment = self::$default_homepage_link;
             }
         }
     }
     // We have an URLSegment: find the page with this segment - within the current locale
     // In the original ModelAsController the locale filter is disabled for this query,
     // meaning /nl/englishHomePage/ will be found and be redirected later on
     // to /en/englishHomePage/ where I'd rather have a 404!!
     Translatable::enable_locale_filter();
     // make sure multibyte urls are supported
     $sitetree = DataObject::get_one('SiteTree', sprintf('"SiteTree"."URLSegment" = \'%s\' %s', Convert::raw2sql(rawurlencode($URLSegment)), SiteTree::config()->nested_urls ? 'AND "SiteTree"."ParentID" = 0' : null));
     // As in the original ModelAsController: if no page can be found, check if it
     // has been renamed or relocated - again: within the current locale!!!
     // If the current $URLSegment refers to an 'old page', do a 302 redirect to the
     // current version (this works for bookmarked pages)
     // Note: for this the find_old_page() function needs to be localized as well to
     // find_old_page_localized()
     if (empty($sitetree)) {
         if ($redirect = self::find_old_page_localized($URLSegment)) {
             $params = $this->request->getVars();
             if (isset($params['url'])) {
                 unset($params['url']);
             }
             $this->response = new SS_HTTPResponse();
             $this->response->redirect(Controller::join_links($redirect->Link(Controller::join_links($this->request->param('Action'), $this->request->param('ID'), $this->request->param('OtherID'))), $params ? '?' . http_build_query($params) : null), 301);
             return $this->response;
         }
         // all is now lost!
         return $this->showPageNotFound();
     }
     // This we don't really need anymore...
     // Enforce current locale setting to the loaded SiteTree object
     // if($sitetree->Locale) Translatable::set_current_locale($sitetree->Locale);
     if (isset($_REQUEST['debug'])) {
         Debug::message("Using record #{$sitetree->ID} of type {$sitetree->class} with link {$sitetree->Link()}");
     }
     return self::controller_for($sitetree, $this->request->param('Action'));
 }
 function testRootUrlDefaultsToTranslatedLink()
 {
     $origPage = $this->objFromFixture('Page', 'homepage_en');
     $origPage->publish('Stage', 'Live');
     $translationDe = $origPage->createTranslation('de_DE');
     $translationDe->URLSegment = 'heim';
     $translationDe->write();
     $translationDe->publish('Stage', 'Live');
     // test with translatable
     Translatable::set_current_locale('de_DE');
     $this->assertEquals(LanguagePrefix::get_homepage_link_by_locale('de_DE'), 'heim', 'Homepage with different URLSegment in non-default language is found');
     // @todo Fix add/remove extension
     // test with translatable disabled
     // Object::remove_extension('Page', 'Translatable');
     // 		$_SERVER['HTTP_HOST'] = '/';
     // 		$this->assertEquals(
     // 			RootURLController::get_homepage_urlsegment(),
     // 			'home',
     // 			'Homepage is showing in default language if ?lang GET variable is left out'
     // 		);
     // 		Object::add_extension('Page', 'Translatable');
     // setting back to default
     Translatable::set_current_locale('en_US');
 }