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

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. public function __construct(
  29. private ContentRepositoryInterface $contentRepository,
  30. private WebspaceManagerInterface $webspaceManager,
  31. private string $environment,
  32. private ?AccessControlManagerInterface $accessControlManager = null,
  33. ) {
  34. }
  35. public function build($page, $scheme, $host)
  36. {
  37. $portalInformations = $this->webspaceManager->findPortalInformationsByHostIncludingSubdomains(
  38. $host, $this->environment
  39. );
  40. $result = [];
  41. foreach ($portalInformations as $portalInformation) {
  42. $localization = $portalInformation->getLocalization();
  43. if (!$localization) {
  44. continue;
  45. }
  46. $pages = $this->contentRepository->findAllByPortal(
  47. $portalInformation->getLocalization()->getLocale(),
  48. $portalInformation->getPortalKey(),
  49. MappingBuilder::create()
  50. ->addProperties(['changed', 'seo-hideInSitemap', 'lastModified'])
  51. ->setResolveUrl(true)
  52. ->setHydrateGhost(false)
  53. ->getMapping()
  54. );
  55. foreach ($pages as $contentPage) {
  56. if (!$contentPage->getUrl()
  57. || true === $contentPage['seo-hideInSitemap']
  58. || RedirectType::NONE !== $contentPage->getNodeType()
  59. ) {
  60. continue;
  61. }
  62. if ($this->accessControlManager) {
  63. $userPermissions = $this->accessControlManager->getUserPermissionByArray(
  64. $contentPage->getLocale(),
  65. PageAdmin::SECURITY_CONTEXT_PREFIX . $contentPage->getWebspaceKey(),
  66. $contentPage->getPermissions(),
  67. null
  68. );
  69. if (isset($userPermissions['view']) && !$userPermissions['view']) {
  70. continue;
  71. }
  72. }
  73. $sitemapUrl = $this->generateSitemapUrl($contentPage, $portalInformation, $host, $scheme);
  74. if (!$sitemapUrl) {
  75. continue;
  76. }
  77. $result[] = $sitemapUrl;
  78. }
  79. }
  80. return $result;
  81. }
  82. private function generateSitemapUrl(
  83. Content $contentPage,
  84. PortalInformation $portalInformation,
  85. string $host,
  86. string $scheme
  87. ) {
  88. $changed = $contentPage['changed'];
  89. if (!empty($contentPage['lastModified'])) {
  90. $changed = $contentPage['lastModified'];
  91. }
  92. if (\is_string($changed)) {
  93. $changed = new \DateTime($changed);
  94. }
  95. $url = $this->webspaceManager->findUrlByResourceLocator(
  96. $contentPage->getUrl(),
  97. $this->environment,
  98. $contentPage->getLocale(),
  99. $portalInformation->getWebspaceKey(),
  100. $host,
  101. $scheme
  102. );
  103. if (!$url) {
  104. return null;
  105. }
  106. $defaultLocale = $portalInformation
  107. ->getWebspace()
  108. ->getDefaultLocalization()
  109. ->getLocale(Localization::DASH);
  110. $sitemapUrl = new SitemapUrl(
  111. $url,
  112. $contentPage->getLocale(),
  113. $defaultLocale,
  114. $changed
  115. );
  116. foreach ($contentPage->getUrls() as $urlLocale => $href) {
  117. if (null === $href) {
  118. continue;
  119. }
  120. $url = $this->webspaceManager->findUrlByResourceLocator(
  121. $href,
  122. $this->environment,
  123. $urlLocale,
  124. $portalInformation->getWebspaceKey(),
  125. $host,
  126. $scheme
  127. );
  128. if (!$url) {
  129. continue;
  130. }
  131. $sitemapUrl->addAlternateLink(new SitemapAlternateLink($url, $urlLocale));
  132. }
  133. return $sitemapUrl;
  134. }
  135. public function getAlias(): string
  136. {
  137. return 'pages';
  138. }
  139. }