BUGFIX: Fix legacy URL redirection for pre-nestedurls URLs, after it has been enabled.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@104463 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-05-10 06:24:50 +00:00
parent f405d71645
commit 2bc1af1752
2 changed files with 32 additions and 8 deletions

View File

@ -147,14 +147,16 @@ class ModelAsController extends Controller implements NestedController {
* @param int $parentID The ID of the parent of the page the URLSegment belongs to.
* @return SiteTree
*/
static function find_old_page($URLSegment,$parentID = 0) {
static function find_old_page($URLSegment,$parentID = 0, $ignoreNestedURLs = false) {
$URLSegment = Convert::raw2sql($URLSegment);
$useParentIDFilter = SiteTree::nested_urls() && !$ignoreNestedURLs;
// First look for a non-nested page that has a unique URLSegment and can be redirected to.
if(SiteTree::nested_urls()) {
$pages = DataObject::get(
'SiteTree',
"\"URLSegment\" = '$URLSegment'" . ((SiteTree::nested_urls()) ? ' AND "ParentID" = ' . (int)$parentID : '')
"\"URLSegment\" = '$URLSegment'" . ($useParentIDFilter ? ' AND "ParentID" = ' . (int)$parentID : '')
);
if($pages && $pages->Count() == 1) return $pages->First();
}
@ -163,20 +165,24 @@ class ModelAsController extends Controller implements NestedController {
$query = new SQLQuery (
'"RecordID"',
'"SiteTree_versions"',
"\"URLSegment\" = '$URLSegment' AND \"WasPublished\" = 1" . (SiteTree::nested_urls() ? ' AND "ParentID" = ' . (int)$parentID : ''),
"\"URLSegment\" = '$URLSegment' AND \"WasPublished\" = 1" . ($useParentIDFilter ? ' AND "ParentID" = ' . (int)$parentID : ''),
'"LastEdited" DESC',
null,
null,
1
);
$record = $query->execute()->first();
if(!$record) return false;
if($oldPage = DataObject::get_by_id('SiteTree', $record['RecordID'])) {
if($record && ($oldPage = DataObject::get_by_id('SiteTree', $record['RecordID']))) {
// Run the page through an extra filter to ensure that all decorators are applied.
if(SiteTree::get_by_link($oldPage->RelativeLink())) return $oldPage;
}
// Otherwise, if nested URLs is enabled, look for a page anywhere in the site that may be
// left from pre-nested-URLs days
if(SiteTree::nested_urls() && !$ignoreNestedURLs && $parentID == 0) {
return self::find_old_page($URLSegment, $parentID, true);
}
}
}

View File

@ -107,6 +107,24 @@ class ModelAsControllerTest extends FunctionalTest {
Controller::join_links(Director::baseURL() . 'newlevel1/newlevel2/newlevel3/'),
$response->getHeader('Location')
);
$response = $this->get('newlevel1/newlevel2/level3');
}
public function testRedirectionForPreNestedurlsBookmarks(){
$this->generateNestedPagesFixture();
// Up-to-date URLs will be redirected to the appropriate subdirectory
$response = $this->get('newlevel3');
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(Director::baseURL() . 'newlevel1/newlevel2/newlevel3/',
$response->getHeader("Location"));
// So will the legacy ones
$response = $this->get('level3');
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals(Director::baseURL() . 'newlevel1/newlevel2/newlevel3/',
$response->getHeader("Location"));
}
function testDoesntRedirectToNestedChildrenOutsideOfOwnHierarchy() {