[TASK] Streamline PHPDoc comment matches function/method signature
[Packages/TYPO3.CMS.git] / typo3 / sysext / seo / Classes / XmlSitemap / RecordsXmlSitemapDataProvider.php
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\Database\ConnectionPool;
7 use TYPO3\CMS\Core\Utility\GeneralUtility;
8 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
9 use TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException;
10
11 /**
12 * XmlSiteDataProvider will provide information for the XML sitemap for a specific database table
13 */
14 class RecordsXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
15 {
16 /**
17 * @param ServerRequestInterface $request
18 * @param string $key
19 * @param array $config
20 * @param ContentObjectRenderer|null $cObj
21 * @throws MissingConfigurationException
22 */
23 public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
24 {
25 parent::__construct($request, $key, $config, $cObj);
26
27 $this->generateItems();
28 }
29
30 /**
31 * @throws MissingConfigurationException
32 */
33 public function generateItems(): void
34 {
35 if (empty($this->config['table'])) {
36 throw new MissingConfigurationException(
37 'No configuration found for sitemap ' . $this->getKey(),
38 1535576053
39 );
40 }
41
42 $pids = GeneralUtility::intExplode(',', $this->config['pid']) ?? $GLOBALS['TSFE']->id;
43 $lastModifiedField = $this->config['lastModifiedField'] ?? 'tstamp';
44 $sortField = $this->config['sortField'] ?? 'sorting';
45
46 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
47 ->getQueryBuilderForTable($this->config['table']);
48
49 $constraints = [
50 $queryBuilder->expr()->in('pid', $pids)
51 ];
52
53 if (!empty($this->config['additionalWhere'])) {
54 $constraints[] = $this->config['additionalWhere'];
55 }
56
57 $rows = $queryBuilder->select('*')
58 ->from($this->config['table'])
59 ->where(
60 ...$constraints
61 )
62 ->orderBy($sortField)
63 ->execute()
64 ->fetchAll();
65
66 foreach ($rows as $row) {
67 $this->items[] = [
68 'loc' => $this->defineUrl($row),
69 'lastMod' => $row[$lastModifiedField]
70 ];
71 }
72 }
73
74 /**
75 * @param array $data
76 * @return string
77 */
78 protected function defineUrl(array $data): string
79 {
80 $pageId = $this->config['url']['pageId'] ?? $GLOBALS['TSFE']->id;
81 $additionalParams = [];
82
83 $additionalParams = $this->getUrlFieldParameterMap($additionalParams, $data);
84 $additionalParams = $this->getUrlAdditionalParams($additionalParams);
85
86 $additionalParamsString = http_build_query(
87 $additionalParams,
88 '',
89 '&',
90 PHP_QUERY_RFC3986
91 );
92
93 $typoLinkConfig = [
94 'parameter' => $pageId,
95 'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
96 'forceAbsoluteUrl' => 1,
97 'useCacheHash' => $this->config['url']['useCacheHash'] ?? 0
98 ];
99
100 return $this->cObj->typoLink_URL($typoLinkConfig);
101 }
102
103 /**
104 * @param array $additionalParams
105 * @param array $data
106 * @return array
107 */
108 protected function getUrlFieldParameterMap(array $additionalParams, array $data): array
109 {
110 if (!empty($this->config['url']['fieldToParameterMap']) &&
111 \is_array($this->config['url']['fieldToParameterMap'])) {
112 foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
113 $additionalParams[$urlPart] = $data[$field];
114 }
115 }
116
117 return $additionalParams;
118 }
119
120 /**
121 * @param array $additionalParams
122 * @return array
123 */
124 protected function getUrlAdditionalParams(array $additionalParams): array
125 {
126 if (!empty($this->config['url']['additionalGetParameters']) &&
127 is_array($this->config['url']['additionalGetParameters'])) {
128 foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
129 foreach ($extensionConfig as $key => $value) {
130 $additionalParams[$extension . '[' . $key . ']'] = $value;
131 }
132 }
133 }
134
135 return $additionalParams;
136 }
137 }