‪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  }
42 
48  public function ‪render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request): string
49  {
50  $settingsTree = $request->getAttribute('frontend.typoscript')->getSetupTree()->getChildByName('plugin')->getChildByName('tx_seo');
51  $configurationArrayWithoutDots = $this->typoScriptService->convertTypoScriptArrayToPlainArray($settingsTree->toArray());
52  $viewConfiguration = $configurationArrayWithoutDots['view'] ?? [];
53  $renderingContext = $this->renderingContextFactory->create();
54  $templatePaths = $renderingContext->getTemplatePaths();
55  $templatePaths->setTemplateRootPaths($viewConfiguration['templateRootPaths'] ?? []);
56  $templatePaths->setLayoutRootPaths($viewConfiguration['layoutRootPaths'] ?? []);
57  $templatePaths->setPartialRootPaths($viewConfiguration['partialRootPaths'] ?? []);
58  $templatePaths->setFormat('xml');
59  $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
60  $view = GeneralUtility::makeInstance(TemplateView::class, $renderingContext);
61  $view->assign('type', $request->getAttribute('frontend.controller')->getPageArguments()->getPageType());
62  $view->assign('sitemapType', $sitemapType);
63  $configConfiguration = $configurationArrayWithoutDots['config'] ?? [];
64  if (!empty($sitemapName = ($request->getQueryParams()['sitemap'] ?? null))) {
65  $view->assign('xslFile', $this->‪getXslFilePath($configConfiguration, $sitemapType, $sitemapName));
66  return $this->‪renderSitemap($request, $view, $configConfiguration, $sitemapType, $sitemapName);
67  }
68  $view->assign('xslFile', $this->‪getXslFilePath($configConfiguration, $sitemapType));
69  return $this->‪renderIndex($request, $view, $configConfiguration, $sitemapType);
70  }
71 
72  private function ‪renderIndex(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType): string
73  {
74  $sitemaps = [];
75  foreach ($configConfiguration[$sitemapType]['sitemaps'] as $sitemapName => $sitemapConfig) {
76  $sitemapProvider = $sitemapConfig['provider'] ?? null;
77  if (is_string($sitemapName)
78  && is_string($sitemapProvider)
79  && class_exists($sitemapProvider)
80  && is_subclass_of($sitemapProvider, XmlSitemapDataProviderInterface::class)
81  ) {
83  $provider = GeneralUtility::makeInstance($sitemapProvider, $request, $sitemapName, $sitemapConfig['config'] ?? []);
84  $pages = $provider->getNumberOfPages();
85  for ($page = 0; $page < $pages; $page++) {
86  $sitemaps[] = [
87  'key' => $sitemapName,
88  'page' => $page,
89  'lastMod' => $provider->getLastModified(),
90  ];
91  }
92  }
93  }
94  $view->assign('sitemaps', $sitemaps);
95  return $view->render('Index');
96  }
97 
98  private function ‪renderSitemap(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType, string $sitemapName): string
99  {
100  $sitemapConfig = $configConfiguration[$sitemapType]['sitemaps'][$sitemapName] ?? null;
101  if ($sitemapConfig) {
102  $sitemapProvider = $sitemapConfig['provider'] ?? null;
103  if (is_string($sitemapProvider)
104  && class_exists($sitemapProvider)
105  && is_subclass_of($sitemapProvider, XmlSitemapDataProviderInterface::class)
106  ) {
108  $provider = GeneralUtility::makeInstance($sitemapProvider, $request, $sitemapName, $sitemapConfig['config'] ?? []);
109  $items = $provider->getItems();
110  $view->assign('items', $items);
111  $template = $sitemapConfig['template'] ?? 'Sitemap';
112  return $view->render($template);
113  }
114  throw new ‪InvalidConfigurationException('No valid provider set for ' . $sitemapName, 1535578522);
115  }
117  GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
118  $request,
119  'No valid configuration found for sitemap ' . $sitemapName
120  ),
121  1535578569
122  );
123  }
124 
125  private function ‪getXslFilePath(array $configConfiguration, string $sitemapType, string $sitemapName = null): string
126  {
127  $path = $configConfiguration[$sitemapType]['sitemaps'][$sitemapName]['config']['xslFile']
128  ?? $configConfiguration[$sitemapType]['sitemaps']['xslFile']
129  ?? $configConfiguration['xslFile']
130  ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
131  return ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
132  }
133 }
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getXslFilePath
‪getXslFilePath(array $configConfiguration, string $sitemapType, string $sitemapName=null)
Definition: XmlSitemapRenderer.php:125
‪TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException
Definition: InvalidConfigurationException.php:23
‪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:48
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:27
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\render
‪render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request)
Definition: XmlSitemapRenderer.php:48
‪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:98
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderIndex
‪renderIndex(ServerRequestInterface $request, TemplateView $view, array $configConfiguration, string $sitemapType)
Definition: XmlSitemapRenderer.php:72