‪TYPO3CMS  11.5
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 
34 {
38  protected ‪$configuration;
39 
43  protected ‪$view;
44 
46 
48  {
49  $this->typoScriptService = ‪$typoScriptService;
50  }
51 
52  protected function ‪initialize(array $fullConfiguration)
53  {
54  $this->configuration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($fullConfiguration['plugin.']['tx_seo.'] ?? []);
55  $this->view = $this->‪getStandaloneView();
56  }
57 
65  public function ‪render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request): string
66  {
67  $this->‪initialize(‪$GLOBALS['TSFE']->tmpl->setup);
68  $this->view->assign('type', ‪$GLOBALS['TSFE']->type);
69  $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
70  $this->view->assign('xslFile', $this->‪getXslFilePath($sitemapType));
71  if (!empty($sitemap = ($request->getQueryParams()['sitemap'] ?? null))) {
72  return $this->‪renderSitemap($request, $sitemap, $sitemapType);
73  }
74 
75  return $this->‪renderIndex($request, $sitemapType);
76  }
77 
83  protected function ‪renderIndex(ServerRequestInterface $request, string $sitemapType): string
84  {
85  $sitemaps = [];
86  foreach ($this->configuration['config'][$sitemapType]['sitemaps'] ?? [] as $sitemap => $config) {
87  if (!empty($config['provider']) && is_string($config['provider'])
88  && class_exists($config['provider'])
89  && is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)
90  ) {
92  $provider = GeneralUtility::makeInstance(
93  $config['provider'],
94  $request,
95  $sitemap,
96  (array)($config['config'] ?? [])
97  );
98 
99  $pages = $provider->getNumberOfPages();
100 
101  for ($page = 0; $page < $pages; $page++) {
102  $sitemaps[] = [
103  'key' => $sitemap,
104  'page' => $page,
105  'lastMod' => $provider->getLastModified(),
106  ];
107  }
108  }
109  }
110 
111  $this->view->assign('sitemapType', $sitemapType);
112  $this->view->assign('sitemaps', $sitemaps);
113  $this->view->setTemplate('Index');
114 
115  return $this->view->render();
116  }
117 
125  protected function ‪renderSitemap(ServerRequestInterface $request, string $sitemap, string $sitemapType): string
126  {
127  if (!empty($sitemapConfig = $this->configuration['config'][$sitemapType]['sitemaps'][$sitemap] ?? null)) {
128  if (class_exists($sitemapConfig['provider']) &&
129  is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) {
131  $provider = GeneralUtility::makeInstance(
132  $sitemapConfig['provider'],
133  $request,
134  $sitemap,
135  (array)($sitemapConfig['config'] ?? [])
136  );
137 
138  $items = $provider->getItems();
139 
140  $template = ($sitemapConfig['config']['template'] ?? false) ?: 'Sitemap';
141  $this->view->setTemplate($template);
142  $this->view->assign('xslFile', $this->‪getXslFilePath($sitemapType, $sitemap));
143  $this->view->assign('items', $items);
144  $this->view->assign('sitemapType', $sitemapType);
145 
146  return $this->view->render();
147  }
148  throw new InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522);
149  }
150 
151  throw new PropagateResponseException(
152  GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
153  $request,
154  'No valid configuration found for sitemap ' . $sitemap
155  ),
156  1535578569
157  );
158  }
159 
160  protected function ‪getStandaloneView(): ‪StandaloneView
161  {
162  ‪$view = GeneralUtility::makeInstance(StandaloneView::class);
163  ‪$view->‪setTemplateRootPaths($this->configuration['view']['templateRootPaths']);
164  ‪$view->‪setLayoutRootPaths($this->configuration['view']['layoutRootPaths']);
165  ‪$view->‪setPartialRootPaths($this->configuration['view']['partialRootPaths']);
166  ‪$view->‪setFormat('xml');
167 
168  return ‪$view;
169  }
170 
176  protected function ‪getXslFilePath(string $sitemapType = null, string $sitemap = null): string
177  {
178  $path = $this->configuration['config']['xslFile'] ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
179  $path = ($sitemapType !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps']['xslFile'] ?? $path) : $path;
180  $path = ($sitemapType !== null && $sitemap !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps'][$sitemap]['config']['xslFile'] ?? $path) : $path;
181  return ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
182  }
183 }
‪TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException
Definition: InvalidConfigurationException.php:22
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$typoScriptService
‪TypoScriptService $typoScriptService
Definition: XmlSitemapRenderer.php:43
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\initialize
‪initialize(array $fullConfiguration)
Definition: XmlSitemapRenderer.php:50
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderSitemap
‪string renderSitemap(ServerRequestInterface $request, string $sitemap, string $sitemapType)
Definition: XmlSitemapRenderer.php:123
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$view
‪StandaloneView $view
Definition: XmlSitemapRenderer.php:41
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:39
‪TYPO3\CMS\Seo\XmlSitemap
Definition: AbstractXmlSitemapDataProvider.php:18
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$configuration
‪array $configuration
Definition: XmlSitemapRenderer.php:37
‪TYPO3\CMS\Fluid\View\StandaloneView\setFormat
‪setFormat($format)
Definition: StandaloneView.php:62
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\render
‪string render(string $_, array $typoScriptConfiguration, ServerRequestInterface $request)
Definition: XmlSitemapRenderer.php:63
‪TYPO3\CMS\Core\Http\PropagateResponseException
Definition: PropagateResponseException.php:47
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getXslFilePath
‪string getXslFilePath(string $sitemapType=null, string $sitemap=null)
Definition: XmlSitemapRenderer.php:174
‪TYPO3\CMS\Core\TypoScript\TypoScriptService
Definition: TypoScriptService.php:25
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\__construct
‪__construct(TypoScriptService $typoScriptService)
Definition: XmlSitemapRenderer.php:45
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer
Definition: XmlSitemapRenderer.php:34
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setLayoutRootPaths
‪setLayoutRootPaths(array $layoutRootPaths)
Definition: AbstractTemplateView.php:177
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setPartialRootPaths
‪setPartialRootPaths(array $partialRootPaths)
Definition: AbstractTemplateView.php:144
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getStandaloneView
‪getStandaloneView()
Definition: XmlSitemapRenderer.php:158
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderIndex
‪string renderIndex(ServerRequestInterface $request, string $sitemapType)
Definition: XmlSitemapRenderer.php:81
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setTemplateRootPaths
‪setTemplateRootPaths(array $templateRootPaths)
Definition: AbstractTemplateView.php:124