‪TYPO3CMS  10.4
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 
49  public function ‪__construct()
50  {
51  $this->configuration = $this->‪getConfiguration();
52  $this->view = $this->‪getStandaloneView();
53  }
54 
61  public function ‪render(string $_, array $typoScriptConfiguration): string
62  {
63  // Inject request from globals until request will be available to cObj
64  $request = ‪$GLOBALS['TYPO3_REQUEST'];
65  $this->view->assign('type', ‪$GLOBALS['TSFE']->type);
66  $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
67  $this->view->assign('xslFile', $this->‪getXslFilePath($sitemapType));
68  if (!empty($sitemap = $request->getQueryParams()['sitemap'])) {
69  return $this->‪renderSitemap($request, $sitemap, $sitemapType);
70  }
71 
72  return $this->‪renderIndex($request, $sitemapType);
73  }
74 
80  protected function ‪renderIndex(ServerRequestInterface $request, string $sitemapType): string
81  {
82  $sitemaps = [];
83  foreach ($this->configuration['config'][$sitemapType]['sitemaps'] ?? [] as $sitemap => $config) {
84  if (class_exists($config['provider']) &&
85  is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)) {
87  $provider = GeneralUtility::makeInstance(
88  $config['provider'],
89  $request,
90  $sitemap,
91  (array)$config['config']
92  );
93 
94  $pages = $provider->getNumberOfPages();
95 
96  for ($page = 0; $page < $pages; $page++) {
97  $sitemaps[] = [
98  'key' => $sitemap,
99  'page' => $page,
100  'lastMod' => $provider->getLastModified()
101  ];
102  }
103  }
104  }
105 
106  $this->view->assign('sitemapType', $sitemapType);
107  $this->view->assign('sitemaps', $sitemaps);
108  $this->view->setTemplate('Index');
109 
110  return $this->view->render();
111  }
112 
120  protected function ‪renderSitemap(ServerRequestInterface $request, string $sitemap, string $sitemapType): string
121  {
122  if (!empty($sitemapConfig = $this->configuration['config'][$sitemapType]['sitemaps'][$sitemap])) {
123  if (class_exists($sitemapConfig['provider']) &&
124  is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) {
126  $provider = GeneralUtility::makeInstance(
127  $sitemapConfig['provider'],
128  $request,
129  $sitemap,
130  (array)$sitemapConfig['config']
131  );
132 
133  $items = $provider->getItems();
134 
135  $template = $sitemapConfig['config']['template'] ?: 'Sitemap';
136  $this->view->setTemplate($template);
137  $this->view->assign('xslFile', $this->‪getXslFilePath($sitemapType, $sitemap));
138  $this->view->assign('items', $items);
139  $this->view->assign('sitemapType', $sitemapType);
140 
141  return $this->view->render();
142  }
143  throw new ‪InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522);
144  }
145 
146  throw new ‪InvalidConfigurationException('No valid configuration found for sitemap ' . $sitemap, 1535578569);
147  }
148 
152  protected function ‪getStandaloneView(): ‪StandaloneView
153  {
154  ‪$view = GeneralUtility::makeInstance(StandaloneView::class);
155  ‪$view->‪setTemplateRootPaths($this->configuration['view']['templateRootPaths']);
156  ‪$view->‪setLayoutRootPaths($this->configuration['view']['layoutRootPaths']);
157  ‪$view->‪setPartialRootPaths($this->configuration['view']['partialRootPaths']);
158  ‪$view->‪setFormat('xml');
159 
160  return ‪$view;
161  }
162 
168  protected function ‪getXslFilePath(string $sitemapType = null, string $sitemap = null): string
169  {
170  $path = $this->configuration['config']['xslFile'] ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
171  $path = ($sitemapType !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps']['xslFile'] ?? $path) : $path;
172  $path = ($sitemapType !== null && $sitemap !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps'][$sitemap]['config']['xslFile'] ?? $path) : $path;
173  return ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
174  }
175 
181  private function ‪getConfiguration(): array
182  {
183  $configurationManager = GeneralUtility::makeInstance(ObjectManager::class)
184  ->get(ConfigurationManagerInterface::class);
185 
186  return $configurationManager->getConfiguration(
188  'seo'
189  );
190  }
191 }
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\render
‪string render(string $_, array $typoScriptConfiguration)
Definition: XmlSitemapRenderer.php:59
‪TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException
Definition: InvalidConfigurationException.php:23
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getConfiguration
‪array getConfiguration()
Definition: XmlSitemapRenderer.php:179
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderSitemap
‪string renderSitemap(ServerRequestInterface $request, string $sitemap, string $sitemapType)
Definition: XmlSitemapRenderer.php:118
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$view
‪StandaloneView $view
Definition: XmlSitemapRenderer.php:41
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Seo\XmlSitemap
Definition: AbstractXmlSitemapDataProvider.php:18
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\$configuration
‪array $configuration
Definition: XmlSitemapRenderer.php:37
‪TYPO3\CMS\Extbase\Object\Exception
Definition: CannotBuildObjectException.php:18
‪TYPO3\CMS\Fluid\View\StandaloneView\setFormat
‪setFormat($format)
Definition: StandaloneView.php:85
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getStandaloneView
‪StandaloneView getStandaloneView()
Definition: XmlSitemapRenderer.php:150
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\__construct
‪__construct()
Definition: XmlSitemapRenderer.php:47
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\getXslFilePath
‪string getXslFilePath(string $sitemapType=null, string $sitemap=null)
Definition: XmlSitemapRenderer.php:166
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer
Definition: XmlSitemapRenderer.php:34
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setLayoutRootPaths
‪setLayoutRootPaths(array $layoutRootPaths)
Definition: AbstractTemplateView.php:167
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setPartialRootPaths
‪setPartialRootPaths(array $partialRootPaths)
Definition: AbstractTemplateView.php:134
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Seo\XmlSitemap\XmlSitemapRenderer\renderIndex
‪string renderIndex(ServerRequestInterface $request, string $sitemapType)
Definition: XmlSitemapRenderer.php:78
‪TYPO3\CMS\Fluid\View\AbstractTemplateView\setTemplateRootPaths
‪setTemplateRootPaths(array $templateRootPaths)
Definition: AbstractTemplateView.php:114