Merge pull request #152 from sminnee/fix-canonical-url

FIX: Ensure that canonical URLs don’t redirect elsewhere.
This commit is contained in:
Mikaela Young 2019-08-15 12:06:38 +12:00 committed by GitHub
commit 6681ced327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 20 deletions

View File

@ -588,10 +588,26 @@ class DocumentationViewer extends Controller implements PermissionProvider
*/ */
public function getCanonicalUrl() public function getCanonicalUrl()
{ {
if (!$this->getPage()) { $page = $this->getPage();
if (!$page) {
return ''; return '';
} }
return $this->getPage()->getCanonicalUrl();
// Build the basic canoncial URL, relative to link_base as is mapped in the manifest
$url = Controller::join_links(
$page->getEntity()->getLanguage(),
$page->getRelativeLink()
);
// If the canoncial URL is, in fact, a redirect, post that directly - redirecting canoncials are a bad thing.
// See https://github.com/silverstripe/doc.silverstripe.org/issues/181 for discussion
if ($potentialRedirect = $this->manifest->getRedirect($url)) {
$url = $potentialRedirect;
}
// Turn into an absolute URL
return Director::absoluteUrl(Controller::join_links(self::getDocumentationBaseHref(), $url));
} }
/** /**

View File

@ -270,6 +270,8 @@ class DocumentationPage extends ViewableData
/** /**
* Determine and set the canonical URL for the given record, for example: dev/docs/en/Path/To/Document * Determine and set the canonical URL for the given record, for example: dev/docs/en/Path/To/Document
*
* @deprecated 2.1.0 This method is no longer used.
*/ */
public function populateCanonicalUrl() public function populateCanonicalUrl()
{ {
@ -371,11 +373,14 @@ class DocumentationPage extends ViewableData
/** /**
* Set the canonical URL to use for this page * Set the canonical URL to use for this page
* *
* @deprecated 2.1.0 This method is no longer used.
* @param string $canonicalUrl * @param string $canonicalUrl
* @return $this * @return $this
*/ */
public function setCanonicalUrl($canonicalUrl) public function setCanonicalUrl($canonicalUrl)
{ {
user_error("DocumentationPage::setCanonicalUrl() is deprecated; it won't affect DocumentationViewer", E_USER_WARNING);
$this->canonicalUrl = $canonicalUrl; $this->canonicalUrl = $canonicalUrl;
return $this; return $this;
} }
@ -384,10 +389,16 @@ class DocumentationPage extends ViewableData
* Get the canonical URL to use for this page. Will trigger discovery * Get the canonical URL to use for this page. Will trigger discovery
* via {@link DocumentationPage::populateCanonicalUrl()} if none is already set. * via {@link DocumentationPage::populateCanonicalUrl()} if none is already set.
* *
* @deprecated 2.1.0 This method is no longer used.
* @return string * @return string
*/ */
public function getCanonicalUrl() public function getCanonicalUrl()
{ {
user_error(
"DocumentationPage::getCanonicalUrl() is deprecated; use DocumentationViewer::getCanonicalUrl() instead.",
E_USER_WARNING
);
if (!$this->canonicalUrl) { if (!$this->canonicalUrl) {
$this->populateCanonicalUrl(); $this->populateCanonicalUrl();
} }

View File

@ -90,22 +90,4 @@ class DocumentationPageTest extends SapphireTest
$this->assertEquals('Sort - Doctest', $page->getBreadcrumbTitle()); $this->assertEquals('Sort - Doctest', $page->getBreadcrumbTitle());
} }
public function testGetCanonicalUrl()
{
$page = new DocumentationPage(
$this->entity,
'file.md',
DOCSVIEWER_PATH . '/tests/docs/en/test/file.md'
);
$this->assertContains(
'dev/docs/en/test/file/',
$page->getCanonicalUrl(),
'Canonical URL is determined, set and returned'
);
$page->setCanonicalUrl('some-other-url');
$this->assertSame('some-other-url', $page->getCanonicalUrl(), 'Canonical URL can be adjusted via public API');
}
} }