/** * @deprecated 4.0 Use OldPageRedirector::find_old_page instead * * @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment. * @param int $parent The ID of the parent of the page the URLSegment belongs to. * @param bool $ignoreNestedURLs * @return SiteTree */ public static function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) { Deprecation::notice('4.0', 'Use OldPageRedirector::find_old_page instead'); if ($parent) { $parent = SiteTree::get()->byId($parent); } $url = OldPageRedirector::find_old_page(array($URLSegment), $parent); return SiteTree::get_by_link($url); }
/** * * NOTE: This test requires nested_urls * */ public function testFindOldPage() { $page = new Page(); $page->Title = 'First Level'; $page->URLSegment = 'oldurl'; $page->write(); $page->publish('Stage', 'Live'); $page->URLSegment = 'newurl'; $page->write(); $page->publish('Stage', 'Live'); $url = OldPageRedirector::find_old_page('oldurl'); $matchedPage = SiteTree::get_by_link($url); $this->assertEquals('First Level', $matchedPage->Title); $page2 = new Page(); $page2->Title = 'Second Level Page'; $page2->URLSegment = 'oldpage2'; $page2->ParentID = $page->ID; $page2->write(); $page2->publish('Stage', 'Live'); $page2->URLSegment = 'newpage2'; $page2->write(); $page2->publish('Stage', 'Live'); $url = OldPageRedirector::find_old_page('oldpage2', $page2->ParentID); $matchedPage = SiteTree::get_by_link($url); $this->assertEquals('Second Level Page', $matchedPage->Title); $url = OldPageRedirector::find_old_page('oldpage2', $page2->ID); $matchedPage = SiteTree::get_by_link($url); $this->assertEquals(false, $matchedPage); }
/** * Basically works like SiteTree::get_by_link but is capable of accounting for Multisites. * * @return SiteTree */ public function getByLink($link, $findOldPageFallback = true) { $prefix = ''; if (class_exists('Multisites')) { $site = Multisites::inst()->getCurrentSite(); if ($site) { $prefix = $site->URLSegment . '/'; } } $link = trim($link, '/'); $linkDirParts = explode('/', $link); $result = SiteTree::get_by_link($prefix . $link . '/'); if ($result) { // Check if URLSegment matches the last part of the URL, as get_by_link // will return the parent page if there's no match. if ($result->URLSegment && $result->URLSegment === end($linkDirParts)) { return $result; } return false; } if ($findOldPageFallback) { $url = OldPageRedirector::find_old_page($linkDirParts); if ($url) { $result = $this->getByLink($url, false); return $result; } } return false; }
/** * Overrides ModelAsController->getNestedController to find the nested controller * on a per-site basis **/ public function getNestedController() { $request = $this->request; $segment = $request->param('URLSegment'); $site = Multisites::inst()->getCurrentSiteId(); if (!$site) { return $this->httpError(404); } if (class_exists('Translatable')) { Translatable::disable_locale_filter(); } $page = SiteTree::get()->filter(array('ParentID' => $site, 'URLSegment' => rawurlencode($segment))); $page = $page->first(); if (class_exists('Translatable')) { Translatable::enable_locale_filter(); } if (!$page) { // Check to see if linkmapping module is installed and if so, if there a map for this request. if (class_exists('LinkMapping')) { if ($request->requestVars()) { $queryString = '?'; foreach ($request->requestVars() as $key => $value) { if ($key != 'url') { $queryString .= $key . '=' . $value . '&'; } } $queryString = rtrim($queryString, '&'); } $link = $queryString != '?' ? $request->getURL() . $queryString : $request->getURL(); $link = trim(Director::makeRelative($link)); $map = LinkMapping::get()->filter('MappedLink', $link)->first(); if ($map) { $this->response = new SS_HTTPResponse(); $this->response->redirect($map->getLink(), 301); return $this->response; } } // use OldPageRedirector if it exists, to find old page if (class_exists('OldPageRedirector')) { if ($redirect = OldPageRedirector::find_old_page(array($segment), Multisites::inst()->getCurrentSite())) { $redirect = SiteTree::get_by_link($redirect); } } else { $redirect = self::find_old_page($segment, $site); } if ($redirect) { $getVars = $request->getVars(); //remove the url var as it confuses the routing unset($getVars['url']); $url = Controller::join_links($redirect->Link(Controller::join_links($request->param('Action'), $request->param('ID'), $request->param('OtherID')))); if (!empty($getVars)) { $url .= '?' . http_build_query($getVars); } $this->response->redirect($url, 301); return $this->response; } return $this->httpError(404); } if (class_exists('Translatable') && $page->Locale) { Translatable::set_current_locale($page->Locale); } return self::controller_for($page, $request->param('Action')); }