-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathArticleSitemapProvider.php
223 lines (185 loc) · 6.98 KB
/
ArticleSitemapProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sulu\Bundle\ArticleBundle\Sitemap;
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchBundle\Service\Repository;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Bundle\ArticleBundle\Document\Index\DocumentFactoryInterface;
use Sulu\Bundle\WebsiteBundle\Sitemap\Sitemap;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapAlternateLink;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapProviderInterface;
use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapUrl;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
/**
* Integrates articles into sitemap.
*/
class ArticleSitemapProvider implements SitemapProviderInterface
{
/**
* @var Manager
*/
private $manager;
/**
* @var DocumentFactoryInterface
*/
private $documentFactory;
/**
* @var WebspaceManagerInterface
*/
private $webspaceManager;
public function __construct(
Manager $manager,
DocumentFactoryInterface $documentFactory,
WebspaceManagerInterface $webspaceManager
) {
$this->manager = $manager;
$this->documentFactory = $documentFactory;
$this->webspaceManager = $webspaceManager;
}
public function build($page, $scheme, $host)
{
$repository = $this->manager->getRepository($this->documentFactory->getClass('article'));
$webspaceKeys = $this->getWebspaceKeysByHost($host);
$result = [];
$from = 0;
$size = 1000;
do {
$bulk = $this->getBulk($repository, $webspaceKeys, $from, $size);
/** @var SitemapUrl[] $sitemapUrlListByUuid */
$sitemapUrlListByUuid = [];
/** @var ArticleViewDocumentInterface $item */
foreach ($bulk as $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
);
foreach ($itemWebspaceKeys as $itemWebspaceKey) {
$url = $this->buildUrl($item, $scheme, $host, $itemWebspaceKey);
$result[] = $url;
$alternativeUrlsKey = $itemWebspaceKey . '__' . $item->getUuid();
if (!isset($sitemapUrlListByUuid[$alternativeUrlsKey])) {
$sitemapUrlListByUuid[$alternativeUrlsKey] = [];
}
$sitemapUrlListByUuid[$alternativeUrlsKey] = $this->setAlternatives(
$sitemapUrlListByUuid[$alternativeUrlsKey],
$url
);
}
}
$from += $size;
} while ($bulk->count() > $from && $from < static::PAGE_SIZE);
return $result;
}
protected function buildUrl(
ArticleViewDocumentInterface $articleView,
string $scheme,
string $host,
string $webspaceKey
): SitemapUrl {
return new SitemapUrl(
$this->findUrl($articleView, $scheme, $host, $webspaceKey),
$articleView->getLocale(),
null,
$articleView->getLastModified() ?? $articleView->getChanged()
);
}
private function findUrl(
ArticleViewDocumentInterface $articleView,
string $scheme,
string $host,
string $webspaceKey
): string {
return $this->webspaceManager->findUrlByResourceLocator(
$articleView->getRoutePath(),
null,
$articleView->getLocale(),
$webspaceKey,
$host,
$scheme
);
}
/**
* Set alternatives to sitemap url.
*
* @param SitemapUrl[] $sitemapUrlList
*
* @return SitemapUrl[]
*/
private function setAlternatives(array $sitemapUrlList, SitemapUrl $sitemapUrl): array
{
foreach ($sitemapUrlList as $sitemapUrlFromList) {
// Add current as alternative to exist.
$sitemapUrlFromList->addAlternateLink(
new SitemapAlternateLink($sitemapUrl->getLoc(), $sitemapUrl->getLocale())
);
// Add others as alternative to current.
$sitemapUrl->addAlternateLink(
new SitemapAlternateLink($sitemapUrlFromList->getLoc(), $sitemapUrlFromList->getLocale())
);
}
$sitemapUrlList[] = $sitemapUrl;
return $sitemapUrlList;
}
private function getBulk(Repository $repository, array $webspaceKeys, int $from, int $size): DocumentIterator
{
$search = $repository->createSearch()
->addQuery(new TermQuery('seo.hide_in_sitemap', 'false'))
->setFrom($from)
->setSize($size);
$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);
}
$search->addQuery($webspaceQuery);
if (\method_exists($search, 'setTrackTotalHits')) {
$search->setTrackTotalHits(true);
}
return $repository->findDocuments($search);
}
public function createSitemap($scheme, $host)
{
return new Sitemap($this->getAlias(), $this->getMaxPage($scheme, $host));
}
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);
}
$search->addQuery($webspaceQuery);
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';
}
}