From 26f5fc2853038316b68c22c92e5efeb793974e67 Mon Sep 17 00:00:00 2001 From: Dan Meeking Date: Tue, 15 Jul 2014 22:44:44 -0600 Subject: [PATCH 1/3] Issue #1051: fix so that OldPageRedirector::find_old_page uses ORM call --- code/controllers/OldPageRedirector.php | 27 +++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/code/controllers/OldPageRedirector.php b/code/controllers/OldPageRedirector.php index b8f6fb00..1d77aaa9 100644 --- a/code/controllers/OldPageRedirector.php +++ b/code/controllers/OldPageRedirector.php @@ -7,7 +7,6 @@ class OldPageRedirector extends Extension { * find an old URL that it should be redirecting to. * * @param SS_HTTPResponse $request The request object - * @throws SS_HTTPResponse_Exception */ public function onBeforeHTTPError404($request) { // Build up the request parameters @@ -48,20 +47,20 @@ class OldPageRedirector extends Extension { } if (!$page) { - // If we haven't found a candidate, lets resort to finding an old page with this URL segment - // TODO: Rewrite using ORM syntax - $query = new SQLQuery ( - '"RecordID"', - '"SiteTree_versions"', - "\"URLSegment\" = '$URL' AND \"WasPublished\" = 1" . ($parent ? ' AND "ParentID" = ' . $parent->ID : ''), - '"LastEdited" DESC', - null, - null, - 1 - ); - $record = $query->execute()->first(); + // If we haven't found a candidate, resort to finding a previously published page version with this URL segment + $oldRecords = DataList::create('SiteTree') + ->filter(array( + 'URLSegment' => $URL, + 'WasPublished' => 1)) + ->sort('"LastEdited" DESC') + ->setDataQueryParam("Versioned.mode", 'all_versions'); + + if($parent) $oldRecords->filter(array('ParentID' => $parent->ID)); + + $record = $oldRecords->first(); + if ($record) { - $page = SiteTree::get()->byID($record['RecordID']); + $page = SiteTree::get()->byID($record->RecordID); $redirect = true; } } From 59be597004da21064e51c6237fbb451628bebf66 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 22 Aug 2016 18:07:07 +0100 Subject: [PATCH 2/3] FIX #1052 --- code/controllers/OldPageRedirector.php | 31 +++++++++++++------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/code/controllers/OldPageRedirector.php b/code/controllers/OldPageRedirector.php index 9eba5399..6ab6e997 100644 --- a/code/controllers/OldPageRedirector.php +++ b/code/controllers/OldPageRedirector.php @@ -38,37 +38,38 @@ class OldPageRedirector extends Extension { * Attempt to find an old/renamed page from some given the URL as an array * * @param array $params The array of URL, e.g. /foo/bar as array('foo', 'bar') - * @param SiteTree $parent The current parent in the recursive flow + * @param SiteTree|null $parent The current parent in the recursive flow * @param boolean $redirect Whether we've found an old page worthy of a redirect * * @return string|boolean False, or the new URL */ static public function find_old_page($params, $parent = null, $redirect = false) { - $parent = is_numeric($parent) ? SiteTree::get()->byId($parent) : $parent; + $parent = is_numeric($parent) ? SiteTree::get()->byID($parent) : $parent; $params = (array)$params; $URL = rawurlencode(array_shift($params)); if (empty($URL)) { return false; } + $pages = SiteTree::get()->filter(array( + 'URLSegment' => $URL, + )); if ($parent) { - $page = SiteTree::get()->filter(array('ParentID' => $parent->ID, 'URLSegment' => $URL))->First(); - } else { - $page = SiteTree::get()->filter(array('URLSegment' => $URL))->First(); + $pages = $pages->filter(array( + 'ParentID' => $parent->ID, + )); } + $page = $pages->first(); if (!$page) { - // If we haven't found a candidate, resort to finding a previously published page version with this URL segment - $oldRecords = DataList::create('SiteTree') + // If we haven't found a candidate, lets resort to finding an old page with this URL segment + $pages = $pages ->filter(array( - 'URLSegment' => $URL, - 'WasPublished' => 1 + 'WasPublished' => true, )) - ->sort('"LastEdited" DESC') + ->sort('LastEdited', 'DESC') ->setDataQueryParam("Versioned.mode", 'all_versions'); - if($parent) $oldRecords->filter(array('ParentID' => $parent->ID)); - - $page = $oldRecords->first(); - - if ($page) { + $record = $pages->first(); + if ($record) { + $page = SiteTree::get()->byID($record->RecordID); $redirect = true; } } From 3820a26abdc1f96b7fe680d7010392492ed97034 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 22 Aug 2016 19:33:17 +0100 Subject: [PATCH 3/3] Simplifying ModelAsControllerTest setUp --- tests/controller/ModelAsControllerTest.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/controller/ModelAsControllerTest.php b/tests/controller/ModelAsControllerTest.php index b1fae3d2..e095c333 100644 --- a/tests/controller/ModelAsControllerTest.php +++ b/tests/controller/ModelAsControllerTest.php @@ -11,8 +11,6 @@ class ModelAsControllerTest extends FunctionalTest { protected $autoFollowRedirection = false; - protected $orig = array(); - /** * New tests require nested urls to be enabled, but the site might not * support nested URLs. @@ -22,24 +20,9 @@ class ModelAsControllerTest extends FunctionalTest { public function setUp() { parent::setUp(); - $this->orig['nested_urls'] = SiteTree::config()->nested_urls; Config::inst()->update('SiteTree', 'nested_urls', true); } - /** - * New tests require nested urls to be enabled, but the site might not - * support nested URLs. - * This setup will enable nested-urls for this test and resets the state - * after the tests have been performed. - */ - public function tearDown() { - - if (isset($this->orig['nested_urls']) && !$this->orig['nested_urls']) { - SiteTree::config()->nested_urls = false; - } - parent::tearDown(); - } - protected function generateNestedPagesFixture() { $level1 = new Page();