‪TYPO3CMS  ‪main
XmlSitemapRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
28 use TYPO3Fluid\Fluid\View\TemplateView;
29 
36 {
37  public function ‪__construct(
38  private readonly ‪TypoScriptService $typoScriptService,
39  private readonly ‪RenderingContextFactory $renderingContextFactory,
40  ) {}
41 
47  public function ‪render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request): string
48  {
49  $settingsTree = $request->getAttribute('frontend.typoscript')->getSetupTree()->getChildByName('plugin')->getChildByName('tx_seo');
50  $configurationArrayWithoutDots = $this->typoScriptService->convertTypoScriptArrayToPlainArray($settingsTree->toArray());
51  $viewConfiguration = $configurationArrayWithoutDots['view'] ?? [];
52  $renderingContext = $this->renderingContextFactory->create();
53  $templatePaths = $renderingContext->getTemplatePaths();
54  $templatePaths->setTemplateRootPaths($viewConfiguration['templateRootPaths'] ?? []);
55  $templatePaths->setLayoutRootPaths($viewConfiguration['layoutRootPaths'] ?? []);
56  $templatePaths->setPartialRootPaths($viewConfiguration['partialRootPaths'] ?? []);
57  $templatePaths->setFormat('xml');
58  $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
59  $view = GeneralUtility::makeInstance(TemplateView::class, $renderingContext);
60  $view->assign('type', $request->getAttribute('routing')->getPageType());
61  $view->assign('sitemapType', $sitemapType);
62  $configConfiguration = $configurationArrayWithoutDots['config'] ?? [];
63  if (!empty($sitemapName = ($request->getQueryParams()['sitemap'] ?? null))) {
64  $view->assign('xslFile', $this->‪getXslFilePath($configConfiguration, $sitemapType, $sitemapName));
65  return $this->‪renderSitemap($request, $view, $configConfiguration, $sitemapType, $sitemapName);
66  }
67  $view->assign('xslFile', $this->‪getXslFilePath($configConfiguration, $sitemapType));
68  return $this->‪renderIndex($request, $view, $configConfiguration, $sitemapType);
69  }
70 
71  private function ‪renderIndex(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType): string
72  {
73  $sitemaps = [];
74  foreach ($configConfiguration[$sitemapType]['sitemaps'] as $sitemapName => $sitemapConfig) {
75  $sitemapProvider = $sitemapConfig['provider'] ?? null;
76  if (is_string($sitemapName)
77  && is_string($sitemapProvider)
78  && class_exists($sitemapProvider)
79  && is_subclass_of($sitemapProvider, XmlSitemapDataProviderInterface::class)
80  ) {
82  $provider = GeneralUtility::makeInstance($sitemapProvider, $request, $sitemapName, $sitemapConfig['config'] ?? []);
83  $pages = $provider->getNumberOfPages();
84  for ($page = 0; $page < $pages; $page++) {
85  $sitemaps[] = [
86  'key' => $sitemapName,
87  'page' => $page,
88  'lastMod' => $provider->getLastModified(),
89  ];
90  }
91  }
92  }
93  $view->assign('sitemaps', $sitemaps);
94  return $view->render('Index');
95  }
96 
97  private function ‪renderSitemap(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType, string $sitemapName): string
98  {
99  $sitemapConfig = $configConfiguration[$sitemapType]['sitemaps'][$sitemapName] ?? null;
100  if ($sitemapConfig) {
101  $sitemapProvider = $sitemapConfig['provider'] ?? null;
102  if (is_string($sitemapProvider)
103  && class_exists($sitemapProvider)
104  && is_subclass_of($sitemapProvider, XmlSitemapDataProviderInterface::class)
105  ) {
107  $provider = GeneralUtility::makeInstance($sitemapProvider, $request, $sitemapName, $sitemapConfig['config'] ?? []);
108  $items = $provider->getItems();
109  $view->assign('items', $items);
110  $template = $sitemapConfig['template'] ?? 'Sitemap';
111  return $view->render($template);
112  }
113  throw new ‪InvalidConfigurationException('No valid provider set for ' . $sitemapName, 1535578522);
114  }
116  GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
117  $request,
118  'No valid configuration found for sitemap ' . $sitemapName
119  ),
120  1535578569
121  );
122  }
123 
124  private function ‪getXslFilePath(array $configConfiguration, string $sitemapType, string $sitemapName = null): string
125  {
126  $path = $configConfiguration[$sitemapType]['sitemaps'][$sitemapName]['config']['xslFile']
127  ?? $configConfiguration[$sitemapType]['sitemaps']['xslFile']
128  ?? $configConfiguration['xslFile']
129  ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
130  return ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
131  }
132 }
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getXslFilePath
‪getXslFilePath(array $configConfiguration, string $sitemapType, string $sitemapName=null)
Definition: XmlSitemapRenderer.php:124
‪TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException
Definition: InvalidConfigurationException.php:22
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:38
‪TYPO3\CMS\Seo\XmlSitemap
Definition: AbstractXmlSitemapDataProvider.php:18
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Core\Http\PropagateResponseException
Definition: PropagateResponseException.php:47
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:27
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\render
‪render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request)
Definition: XmlSitemapRenderer.php:47
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\__construct
‪__construct(private readonly TypoScriptService $typoScriptService, private readonly RenderingContextFactory $renderingContextFactory,)
Definition: XmlSitemapRenderer.php:37
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer
Definition: XmlSitemapRenderer.php:36
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderSitemap
‪renderSitemap(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType, string $sitemapName)
Definition: XmlSitemapRenderer.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderIndex
‪renderIndex(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType)
Definition: XmlSitemapRenderer.php:71