Skip to content

Commit

Permalink
Fix sitemap provider (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored and wachterjohannes committed Oct 24, 2019
1 parent 73e5fc2 commit 9c1818e
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
<argument type="service" id="sulu_article.view_document.factory"/>
<argument type="service" id="sulu_core.webspace.webspace_manager"/>

<tag name="sulu.sitemap.provider" alias="articles"/>
<tag name="sulu.sitemap.provider"/>
</service>

<!-- preview -->
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Resources/doc/default.xml → Resources/doc/article.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<tag name="sulu_article.article_title"/>
</property>

<property name="routePath" type="resource_locator">
<property name="routePath" type="route">
<meta>
<title lang="en">Resourcelocator</title>
<title lang="de">Adresse</title>
Expand Down
8 changes: 4 additions & 4 deletions Resources/doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ More information about this topic can be found in the section [multi-webspaces](
Add xml template for structure in configured folder:
```
%kernel.project_dir%/config/templates/articles/default.xml
%kernel.project_dir%/config/templates/articles/article.xml
```

Example is located in Bundle
[default.xml](default.xml).
[article.xml](article.xml).

Add template for article type in configured folder:

```
%kernel.project_dir%/templates/articles/default.html.twig
%kernel.project_dir%/templates/articles/article.html.twig
```

Example is located in Bundle
[default.html.twig](default.html.twig).
[article.html.twig](article.html.twig).

## Initialize bundle

Expand Down
103 changes: 79 additions & 24 deletions Sitemap/ArticleSitemapProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,44 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function build($page, $portalKey)
public function build($page, $scheme, $host)
{
$repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
$portal = $this->webspaceManager->findPortalByKey($portalKey);

if (!$portal) {
throw new \InvalidArgumentException('Portal with key "' . $portalKey . '" not found');
}
$webspaceKeys = $this->getWebspaceKeysByHost($host);

$result = [];

$from = 0;
$size = 1000;

do {
$bulk = $this->getBulk($repository, $portal->getWebspace()->getKey(), $from, $size);
$bulk = $this->getBulk($repository, $webspaceKeys, $from, $size);
/** @var SitemapUrl[] $alternatives */
$sitemapUrlListByUuid = [];

/** @var ArticleViewDocumentInterface $item */
foreach ($bulk as $item) {
$result[] = $url = $this->buildUrl($item);
// Get all webspace keys which are for the current document and current selected webspaces
$itemWebspaceKeys = array_intersect(
array_merge([$item->getMainWebspace()], $item->getAdditionalWebspaces()),
$webspaceKeys
);

if (!isset($sitemapUrlListByUuid[$item->getUuid()])) {
$sitemapUrlListByUuid[$item->getUuid()] = [];
}
foreach ($itemWebspaceKeys as $itemWebspaceKey) {
$url = $this->buildUrl($item, $scheme, $host, $itemWebspaceKey);

$sitemapUrlListByUuid[$item->getUuid()] = $this->setAlternatives(
$sitemapUrlListByUuid[$item->getUuid()],
$url
);
$result[] = $url;

$alternativeUrlsKey = $itemWebspaceKey . '__' . $item->getUuid();
if (!isset($sitemapUrlListByUuid[$alternativeUrlsKey])) {
$sitemapUrlListByUuid[$alternativeUrlsKey] = [];
}

$sitemapUrlListByUuid[$alternativeUrlsKey] = $this->setAlternatives(
$sitemapUrlListByUuid[$alternativeUrlsKey],
$url
);
}
}

$from += $size;
Expand All @@ -94,12 +102,28 @@ public function build($page, $portalKey)
return $result;
}

protected function buildUrl(ArticleViewDocumentInterface $articleView, $scheme, $host, $webspaceKey)
{
return new SitemapUrl(
$this->findUrl($articleView, $scheme, $host, $webspaceKey),
$articleView->getLocale(),
$articleView->getChanged()
);
}

/**
* @return SitemapUrl
* @return string
*/
protected function buildUrl(ArticleViewDocumentInterface $articleView)
private function findUrl(ArticleViewDocumentInterface $articleView, $scheme, $host, $webspaceKey)
{
return new SitemapUrl($articleView->getRoutePath(), $articleView->getLocale(), $articleView->getChanged());
return $this->webspaceManager->findUrlByResourceLocator(
$articleView->getRoutePath(),
null,
$articleView->getLocale(),
$webspaceKey,
$host,
$scheme
);
}

/**
Expand Down Expand Up @@ -129,16 +153,19 @@ private function setAlternatives(array $sitemapUrlList, SitemapUrl $sitemapUrl)
return $sitemapUrlList;
}

private function getBulk(Repository $repository, $webspaceKey, $from, $size)
private function getBulk(Repository $repository, $webspaceKeys, $from, $size)
{
$search = $repository->createSearch()
->addQuery(new TermQuery('seo.hide_in_sitemap', 'false'))
->setFrom($from)
->setSize($size);

$webspaceQuery = new BoolQuery();
$webspaceQuery->add(new TermQuery('main_webspace', $webspaceKey), BoolQuery::SHOULD);
$webspaceQuery->add(new TermQuery('additional_webspaces', $webspaceKey), BoolQuery::SHOULD);
foreach ($webspaceKeys as $webspaceKey) {
$webspaceQuery->add(new TermQuery('main_webspace', $webspaceKey), BoolQuery::SHOULD);
$webspaceQuery->add(new TermQuery('additional_webspaces', $webspaceKey), BoolQuery::SHOULD);
}

$search->addQuery($webspaceQuery);

return $repository->findDocuments($search);
Expand All @@ -147,20 +174,48 @@ private function getBulk(Repository $repository, $webspaceKey, $from, $size)
/**
* {@inheritdoc}
*/
public function createSitemap($alias)
public function createSitemap($scheme, $host)
{
return new Sitemap($alias, $this->getMaxPage());
return new Sitemap($this->getAlias(), $this->getMaxPage($scheme, $host));
}

/**
* {@inheritdoc}
*/
public function getMaxPage()
public function getMaxPage($schema, $host)
{
$repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
$search = $repository->createSearch()
->addQuery(new TermQuery('seo.hide_in_sitemap', 'false'));

$webspaceKeys = $this->getWebspaceKeysByHost($host);

$webspaceQuery = new BoolQuery();
foreach ($webspaceKeys as $webspaceKey) {
$webspaceQuery->add(new TermQuery('main_webspace', $webspaceKey), BoolQuery::SHOULD);
$webspaceQuery->add(new TermQuery('additional_webspaces', $webspaceKey), BoolQuery::SHOULD);
}

return ceil($repository->count($search) / static::PAGE_SIZE);
}

/**
* @return string[]
*/
private function getWebspaceKeysByHost(string $host): array
{
$portalInformations = $this->webspaceManager->findPortalInformationsByHostIncludingSubdomains($host);

$webspaceKeys = [];
foreach ($portalInformations as $portalInformation) {
$webspaceKeys[] = $portalInformation->getWebspaceKey();
}

return $webspaceKeys;
}

public function getAlias()
{
return 'articles';
}
}
55 changes: 55 additions & 0 deletions Tests/Application/config/webspaces/sulu.io.blog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<webspace xmlns="http://schemas.sulu.io/webspace/webspace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/webspace/webspace http://schemas.sulu.io/webspace/webspace-1.1.xsd">

<name>Sulu Blog CMF</name>
<key>sulu_io_blog</key>

<localizations>
<localization language="de" default="true"/>
<localization language="en"/>
</localizations>

<theme>default</theme>

<default-templates>
<default-template type="homepage">overview</default-template>
<default-template type="page">overview</default-template>
</default-templates>

<navigation>
<contexts>
<context key="main">
<meta>
<title lang="en">Mainnavigation</title>
</meta>
</context>
</contexts>
</navigation>

<portals>
<portal>
<name>Sulu CMF AT</name>
<key>sulucmf_at</key>

<environments>
<environment type="prod">
<urls>
<url language="de">sulu_io.localhost/blog</url>
</urls>
</environment>
<environment type="dev">
<urls>
<url language="de">sulu_io.localhost/blog</url>
</urls>
</environment>
<environment type="test">
<urls>
<url language="de">sulu_io.localhost/blog</url>
</urls>
</environment>
</environments>
</portal>
</portals>
</webspace>
6 changes: 3 additions & 3 deletions Tests/Application/config/webspaces/sulu.io.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
<environments>
<environment type="prod">
<urls>
<url language="de">{host}</url>
<url language="de">sulu_io.localhost</url>
</urls>
</environment>
<environment type="dev">
<urls>
<url language="de">{host}</url>
<url language="de">sulu_io.localhost</url>
</urls>
</environment>
<environment type="test">
<urls>
<url language="de">{host}</url>
<url language="de">sulu_io.localhost</url>
</urls>
</environment>
</environments>
Expand Down
6 changes: 3 additions & 3 deletions Tests/Application/config/webspaces/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
<environments>
<environment type="prod">
<urls>
<url language="de">{host}</url>
<url language="de">test.localhost</url>
</urls>
</environment>
<environment type="dev">
<urls>
<url language="de">{host}</url>
<url language="de">test.localhost</url>
</urls>
</environment>
<environment type="test">
<urls>
<url language="de">{host}</url>
<url language="de">test.localhost</url>
</urls>
</environment>
</environments>
Expand Down
6 changes: 3 additions & 3 deletions Tests/Application/config/webspaces/test2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
<environments>
<environment type="prod">
<urls>
<url language="de">{host}</url>
<url language="de">test-2.localhost</url>
</urls>
</environment>
<environment type="dev">
<urls>
<url language="de">{host}</url>
<url language="de">test-2.localhost</url>
</urls>
</environment>
<environment type="test">
<urls>
<url language="de">{host}</url>
<url language="de">test-2.localhost</url>
</urls>
</environment>
</environments>
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Markup/ArticleLinkProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ function(array $data) {

$this->assertCount(2, $result);
$this->assertEquals($articles[0]['id'], $result[0]->getId());
$this->assertEquals('http://{host}' . $articles[0]['route'], $result[0]->getUrl());
$this->assertEquals('http://test.localhost' . $articles[0]['route'], $result[0]->getUrl());
$this->assertFalse($result[0]->isPublished());
$this->assertEquals($articles[1]['id'], $result[1]->getId());
$this->assertEquals('http://{host}' . $articles[1]['route'], $result[1]->getUrl());
$this->assertEquals('http://test.localhost' . $articles[1]['route'], $result[1]->getUrl());
$this->assertTrue($result[1]->isPublished());
}

Expand Down
21 changes: 12 additions & 9 deletions Tests/Functional/Sitemap/ArticleSitemapProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,26 @@ public function testBuild()
$article1 = $this->createArticle('1', 'default', 'sulu_io', []);
$article2 = $this->createArticle('2', 'simple', 'test', ['sulu_io']);
$article3 = $this->createArticle('3', 'default', 'test', ['test-2']);
$article4 = $this->createArticle('4', 'default', 'sulu_io_blog', []);

/** @var SitemapUrl[] $result */
$result = $this->articleSitemapProvider->build(0, 'sulucmf_at');
$this->assertCount(2, $result);
$this->assertEquals($article1['route'], $result[0]->getLoc());
$this->assertEquals($article2['route'], $result[1]->getLoc());
$result = $this->articleSitemapProvider->build(0, 'http', 'sulu_io.localhost');

$this->assertCount(3, $result);
$this->assertEquals('http://sulu_io.localhost' . $article1['route'], $result[0]->getLoc());
$this->assertEquals('http://sulu_io.localhost/blog' . $article4['route'], $result[1]->getLoc());
$this->assertEquals('http://sulu_io.localhost' . $article2['route'], $result[2]->getLoc());

/** @var SitemapUrl[] $result */
$result = $this->articleSitemapProvider->build(0, 'test');
$result = $this->articleSitemapProvider->build(0, 'http', 'test.localhost');
$this->assertCount(2, $result);
$this->assertEquals($article2['route'], $result[0]->getLoc());
$this->assertEquals($article3['route'], $result[1]->getLoc());
$this->assertEquals('http://test.localhost' . $article2['route'], $result[0]->getLoc());
$this->assertEquals('http://test.localhost' . $article3['route'], $result[1]->getLoc());

/** @var SitemapUrl[] $result */
$result = $this->articleSitemapProvider->build(0, 'test-2');
$result = $this->articleSitemapProvider->build(0, 'http', 'test-2.localhost');
$this->assertCount(1, $result);
$this->assertEquals($article3['route'], $result[0]->getLoc());
$this->assertEquals('http://test-2.localhost' . $article3['route'], $result[0]->getLoc());
}

private function createArticle(
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"jms/serializer-bundle": "^3.3",
"ongr/elasticsearch-bundle": "^5.2",
"ongr/elasticsearch-dsl": "~5.0 || ~6.0",
"sulu/sulu": "^2.0",
"sulu/sulu": "^2.0.1",
"symfony/config": "^4.3",
"symfony/dependency-injection": "^4.3",
"symfony/http-foundation": "^4.3",
Expand Down

0 comments on commit 9c1818e

Please sign in to comment.