Permalink
Browse files

#2679: Auto-redirect renamed pages

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65781 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
sminnee committed Nov 13, 2008
1 parent 2f25dda commit f11d7cc960391846e49960e7f95ff9708a56cdcd
Showing with 35 additions and 5 deletions.
  1. +35 −5 core/control/ModelAsController.php
@@ -13,11 +13,10 @@ public function handleRequest($request) {
$this->urlParams = $request->allParams();
$this->init();
- $nested = $this->getNestedController();
- if(is_object($nested)) {
- $result = $nested->handleRequest($request);
- } else {
- $result = $nested;
+ $result = $this->getNestedController();
+
+ if(is_object($result) && $result instanceOf RequestHandler) {
+ $result = $result->handleRequest($request);
}
$this->popCurrent();
@@ -39,6 +38,20 @@ public function getNestedController() {
$child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'", false);
}
if(!$child) {
+ if($child = $this->findOldPage($SQL_URLSegment)) {
+ $url = Controller::join_links(
+ Director::baseURL(),
+ $child->URLSegment,
+ $this->urlParams['Action'],
+ $this->urlParams['ID'],
+ $this->urlParams['OtherID']
+ );
+
+ $response = new HTTPResponse();
+ $response->redirect($url, 301);
+ return $response;
+ }
+
$child = $this->get404Page();
}
@@ -67,6 +80,23 @@ public function getNestedController() {
}
}
+ protected function findOldPage($urlSegment) {
+ $versionedQuery = new SQLQuery (
+ 'RecordID', 'SiteTree_versions',
+ "`WasPublished` = 1 AND `URLSegment` = '$urlSegment'",
+ '`LastEdited` DESC, `WasPublished`',
+ null, null, 1
+ );
+
+ $result = $versionedQuery->execute();
+
+ if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
+ if($redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID'])) return $redirectObj;
+ }
+
+ return false;
+ }
+
protected function get404Page() {
if($page = DataObject::get_one("ErrorPage", "ErrorCode = '404'")) return $page;
else return DataObject::get_one("SiteTree", "URLSegment = '404'");

0 comments on commit f11d7cc

Please sign in to comment.