1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace TYPO3\CMS\Seo\XmlSitemap; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; |
7
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
8
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
9
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
10
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
11
|
|
|
use TYPO3\CMS\Fluid\View\StandaloneView; |
12
|
|
|
use TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class to render the XML Sitemap to be used as a UserFunction |
16
|
|
|
*/ |
17
|
|
|
class XmlSitemapRenderer |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \TYPO3\CMS\Fluid\View\StandaloneView |
26
|
|
|
*/ |
27
|
|
|
protected $view; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->configuration = $this->getConfiguration(); |
32
|
|
|
$this->view = $this->getStandaloneView(); |
33
|
|
|
$this->view->assign( |
34
|
|
|
'xslFile', |
35
|
|
|
PathUtility::stripPathSitePrefix( |
36
|
|
|
ExtensionManagementUtility::extPath('seo', 'Resources/Public/CSS/Sitemap.xsl') |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
* @throws InvalidConfigurationException |
44
|
|
|
*/ |
45
|
|
|
public function render(): string |
46
|
|
|
{ |
47
|
|
|
// Inject request from globals until request will be available to cObj |
48
|
|
|
$request = $GLOBALS['TYPO3_REQUEST']; |
49
|
|
|
$this->view->assign('type', $request->getQueryParams()['type']); |
50
|
|
|
if (!empty($sitemap = $request->getQueryParams()['sitemap'])) { |
51
|
|
|
return $this->renderSitemap($request, $sitemap); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->renderIndex($request); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function renderIndex(ServerRequestInterface $request): string |
62
|
|
|
{ |
63
|
|
|
$sitemaps = []; |
64
|
|
|
foreach ($this->configuration['config']['xmlSitemap']['sitemaps'] ?? [] as $sitemap => $config) { |
65
|
|
|
if (class_exists($config['provider']) && |
66
|
|
|
is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)) { |
67
|
|
|
/** @var XmlSitemapDataProviderInterface $provider */ |
68
|
|
|
$provider = GeneralUtility::makeInstance( |
69
|
|
|
$config['provider'], |
70
|
|
|
$request, |
71
|
|
|
$sitemap, |
72
|
|
|
(array)$config['config'] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$sitemaps[] = [ |
76
|
|
|
'key' => $sitemap, |
77
|
|
|
'lastMod' => $provider->getLastModified() |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->view->assign('sitemaps', $sitemaps); |
83
|
|
|
$this->view->setTemplate('Index'); |
84
|
|
|
|
85
|
|
|
return $this->view->render(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
90
|
|
|
* @param string $sitemap |
91
|
|
|
* @return string |
92
|
|
|
* @throws \TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException |
93
|
|
|
*/ |
94
|
|
|
protected function renderSitemap(ServerRequestInterface $request, string $sitemap): string |
95
|
|
|
{ |
96
|
|
|
if (!empty($sitemapConfig = $this->configuration['config']['xmlSitemap']['sitemaps'][$sitemap])) { |
97
|
|
|
if (class_exists($sitemapConfig['provider']) && |
98
|
|
|
is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) { |
99
|
|
|
/** @var XmlSitemapDataProviderInterface $provider */ |
100
|
|
|
$provider = GeneralUtility::makeInstance( |
101
|
|
|
$sitemapConfig['provider'], |
102
|
|
|
$request, |
103
|
|
|
$sitemap, |
104
|
|
|
(array)$sitemapConfig['config'] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$items = $provider->getItems(); |
108
|
|
|
|
109
|
|
|
$template = $sitemapConfig['config']['template'] ?: 'Sitemap'; |
110
|
|
|
$this->view->setTemplate($template); |
111
|
|
|
$this->view->assign('items', $items); |
112
|
|
|
|
113
|
|
|
return $this->view->render(); |
114
|
|
|
} |
115
|
|
|
throw new InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
throw new InvalidConfigurationException('No valid configuration found for sitemap ' . $sitemap, 1535578569); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return \TYPO3\CMS\Fluid\View\StandaloneView |
123
|
|
|
*/ |
124
|
|
|
protected function getStandaloneView(): StandaloneView |
125
|
|
|
{ |
126
|
|
|
$view = GeneralUtility::makeInstance(StandaloneView::class); |
127
|
|
|
$view->setTemplateRootPaths($this->configuration['view']['templateRootPaths']); |
128
|
|
|
$view->setLayoutRootPaths($this->configuration['view']['layoutRootPaths']); |
129
|
|
|
$view->setPartialRootPaths($this->configuration['view']['partialRootPaths']); |
130
|
|
|
$view->setFormat('xml'); |
131
|
|
|
|
132
|
|
|
return $view; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the whole typoscript array |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
private function getConfiguration(): array |
140
|
|
|
{ |
141
|
|
|
$configurationManager = GeneralUtility::makeInstance(ObjectManager::class) |
142
|
|
|
->get(ConfigurationManagerInterface::class); |
143
|
|
|
|
144
|
|
|
return $configurationManager->getConfiguration( |
145
|
|
|
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, |
146
|
|
|
'seo' |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|