This commit is contained in:
Daniel Hensby 2016-08-22 18:07:07 +01:00
parent 4aca9ac145
commit 59be597004
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E

View File

@ -38,37 +38,38 @@ class OldPageRedirector extends Extension {
* Attempt to find an old/renamed page from some given the URL as an array * 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 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 * @param boolean $redirect Whether we've found an old page worthy of a redirect
* *
* @return string|boolean False, or the new URL * @return string|boolean False, or the new URL
*/ */
static public function find_old_page($params, $parent = null, $redirect = false) { 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; $params = (array)$params;
$URL = rawurlencode(array_shift($params)); $URL = rawurlencode(array_shift($params));
if (empty($URL)) { return false; } if (empty($URL)) { return false; }
$pages = SiteTree::get()->filter(array(
'URLSegment' => $URL,
));
if ($parent) { if ($parent) {
$page = SiteTree::get()->filter(array('ParentID' => $parent->ID, 'URLSegment' => $URL))->First(); $pages = $pages->filter(array(
} else { 'ParentID' => $parent->ID,
$page = SiteTree::get()->filter(array('URLSegment' => $URL))->First(); ));
} }
$page = $pages->first();
if (!$page) { if (!$page) {
// If we haven't found a candidate, resort to finding a previously published page version with this URL segment // If we haven't found a candidate, lets resort to finding an old page with this URL segment
$oldRecords = DataList::create('SiteTree') $pages = $pages
->filter(array( ->filter(array(
'URLSegment' => $URL, 'WasPublished' => true,
'WasPublished' => 1
)) ))
->sort('"LastEdited" DESC') ->sort('LastEdited', 'DESC')
->setDataQueryParam("Versioned.mode", 'all_versions'); ->setDataQueryParam("Versioned.mode", 'all_versions');
if($parent) $oldRecords->filter(array('ParentID' => $parent->ID)); $record = $pages->first();
if ($record) {
$page = $oldRecords->first(); $page = SiteTree::get()->byID($record->RecordID);
if ($page) {
$redirect = true; $redirect = true;
} }
} }