BUGFIX: Prevent infinite redirection when the homepage isn't available, when using modules such as subsites.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@72200 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-02-25 01:53:16 +00:00
parent a2ffe586a1
commit c931eb10fb

View File

@ -39,11 +39,11 @@ class ModelAsController extends Controller implements NestedController {
$url = Controller::join_links( $url = Controller::join_links(
Director::baseURL(), Director::baseURL(),
$child->URLSegment, $child->URLSegment,
$this->urlParams['Action'], isset($this->urlParams['Action']) ? $this->urlParams['Action'] : null,
$this->urlParams['ID'], isset($this->urlParams['ID']) ? $this->urlParams['ID'] : null,
$this->urlParams['OtherID'] isset($this->urlParams['OtherID']) ? $this->urlParams['OtherID'] : null
); );
$response = new HTTPResponse(); $response = new HTTPResponse();
$response->redirect($url, 301); $response->redirect($url, 301);
return $response; return $response;
@ -78,17 +78,32 @@ class ModelAsController extends Controller implements NestedController {
} }
protected function findOldPage($urlSegment) { protected function findOldPage($urlSegment) {
$versionedQuery = new SQLQuery ( // Build the query by replacing `SiteTree` with `SiteTree_versions` in a regular query.
'RecordID', 'SiteTree_versions', // Note that this should *really* be handled by a more full-featured data mapper; as it stands
"`WasPublished` = 1 AND `URLSegment` = '$urlSegment'", // this is a bit of a hack.
'`LastEdited` DESC, `WasPublished`', $origStage = Versioned::current_stage();
null, null, 1 Versioned::reading_stage('Stage');
); $versionedQuery = singleton('SiteTree')->extendedSQL('');
Versioned::reading_stage($origStage);
foreach($versionedQuery->from as $k => $v) {
$versionedQuery->renameTable($k, $k . '_versions');
}
$versionedQuery->select = array("`SiteTree_versions`.RecordID");
$versionedQuery->where[] = "`SiteTree_versions`.`WasPublished` = 1 AND `URLSegment` = '$urlSegment'";
$versionedQuery->orderby = '`LastEdited` DESC, `SiteTree_versions`.`WasPublished`';
$versionedQuery->limit = 1;
$result = $versionedQuery->execute(); $result = $versionedQuery->execute();
if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) { if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
if($redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID'])) return $redirectObj; $redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID']);
if($redirectObj) {
// Double-check by querying this page in the same way that getNestedController() does. This
// will prevent query muck-ups from modules such as subsites
$doubleCheck = SiteTree::get_by_url($redirectObj->URLSegment);
if($doubleCheck) return $redirectObj;
}
} }
return false; return false;