vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Sitemap/PagesSitemapProvider.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\PageBundle\Sitemap;
  11. use Sulu\Bundle\PageBundle\Admin\PageAdmin;
  12. use Sulu\Bundle\WebsiteBundle\Sitemap\AbstractSitemapProvider;
  13. use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapAlternateLink;
  14. use Sulu\Bundle\WebsiteBundle\Sitemap\SitemapUrl;
  15. use Sulu\Component\Content\Document\RedirectType;
  16. use Sulu\Component\Content\Repository\Content;
  17. use Sulu\Component\Content\Repository\ContentRepositoryInterface;
  18. use Sulu\Component\Content\Repository\Mapping\MappingBuilder;
  19. use Sulu\Component\Localization\Localization;
  20. use Sulu\Component\Security\Authorization\AccessControl\AccessControlManagerInterface;
  21. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  22. use Sulu\Component\Webspace\PortalInformation;
  23. /**
  24. * Provides sitemap for webspaces.
  25. */
  26. class PagesSitemapProvider extends AbstractSitemapProvider
  27. {
  28. /**
  29. * @var ContentRepositoryInterface
  30. */
  31. private $contentRepository;
  32. /**
  33. * @var WebspaceManagerInterface
  34. */
  35. private $webspaceManager;
  36. /**
  37. * @var string
  38. */
  39. private $environment;
  40. /**
  41. * @var ?AccessControlManagerInterface
  42. */
  43. private $accessControlManager;
  44. public function __construct(
  45. ContentRepositoryInterface $contentRepository,
  46. WebspaceManagerInterface $webspaceManager,
  47. string $environment,
  48. ?AccessControlManagerInterface $accessControlManager = null
  49. ) {
  50. $this->contentRepository = $contentRepository;
  51. $this->webspaceManager = $webspaceManager;
  52. $this->environment = $environment;
  53. $this->accessControlManager = $accessControlManager;
  54. }
  55. public function build($page, $scheme, $host)
  56. {
  57. $portalInformations = $this->webspaceManager->findPortalInformationsByHostIncludingSubdomains(
  58. $host, $this->environment
  59. );
  60. $result = [];
  61. foreach ($portalInformations as $portalInformation) {
  62. $localization = $portalInformation->getLocalization();
  63. if (!$localization) {
  64. continue;
  65. }
  66. $pages = $this->contentRepository->findAllByPortal(
  67. $portalInformation->getLocalization()->getLocale(),
  68. $portalInformation->getPortalKey(),
  69. MappingBuilder::create()
  70. ->addProperties(['changed', 'seo-hideInSitemap'])
  71. ->setResolveUrl(true)
  72. ->setHydrateGhost(false)
  73. ->getMapping()
  74. );
  75. foreach ($pages as $contentPage) {
  76. if (!$contentPage->getUrl()
  77. || true === $contentPage['seo-hideInSitemap']
  78. || RedirectType::NONE !== $contentPage->getNodeType()
  79. ) {
  80. continue;
  81. }
  82. if ($this->accessControlManager) {
  83. $userPermissions = $this->accessControlManager->getUserPermissionByArray(
  84. $contentPage->getLocale(),
  85. PageAdmin::SECURITY_CONTEXT_PREFIX . $contentPage->getWebspaceKey(),
  86. $contentPage->getPermissions(),
  87. null
  88. );
  89. if (isset($userPermissions['view']) && !$userPermissions['view']) {
  90. continue;
  91. }
  92. }
  93. $sitemapUrl = $this->generateSitemapUrl($contentPage, $portalInformation, $host, $scheme);
  94. if (!$sitemapUrl) {
  95. continue;
  96. }
  97. $result[] = $sitemapUrl;
  98. }
  99. }
  100. return $result;
  101. }
  102. private function generateSitemapUrl(
  103. Content $contentPage,
  104. PortalInformation $portalInformation,
  105. string $host,
  106. string $scheme
  107. ) {
  108. $changed = $contentPage['changed'];
  109. if (\is_string($changed)) {
  110. $changed = new \DateTime($changed);
  111. }
  112. $url = $this->webspaceManager->findUrlByResourceLocator(
  113. $contentPage->getUrl(),
  114. $this->environment,
  115. $contentPage->getLocale(),
  116. $portalInformation->getWebspaceKey(),
  117. $host,
  118. $scheme
  119. );
  120. if (!$url) {
  121. return null;
  122. }
  123. $defaultLocale = $portalInformation
  124. ->getWebspace()
  125. ->getDefaultLocalization()
  126. ->getLocale(Localization::DASH);
  127. $sitemapUrl = new SitemapUrl(
  128. $url,
  129. $contentPage->getLocale(),
  130. $defaultLocale,
  131. $changed
  132. );
  133. foreach ($contentPage->getUrls() as $urlLocale => $href) {
  134. if (null === $href) {
  135. continue;
  136. }
  137. $url = $this->webspaceManager->findUrlByResourceLocator(
  138. $href,
  139. $this->environment,
  140. $urlLocale,
  141. $portalInformation->getWebspaceKey(),
  142. $host,
  143. $scheme
  144. );
  145. if (!$url) {
  146. continue;
  147. }
  148. $sitemapUrl->addAlternateLink(new SitemapAlternateLink($url, $urlLocale));
  149. }
  150. return $sitemapUrl;
  151. }
  152. public function getAlias(): string
  153. {
  154. return 'pages';
  155. }
  156. }