390c77c1f777822ec62b6b62d8459c1d6e544da2
[Packages/TYPO3.CMS.git] / typo3 / sysext / seo / Classes / XmlSitemap / RecordsXmlSitemapDataProvider.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Seo\XmlSitemap;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Core\Context\Context;
21 use TYPO3\CMS\Core\Database\ConnectionPool;
22 use TYPO3\CMS\Core\Database\Query\QueryHelper;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
25 use TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException;
26
27 /**
28 * XmlSiteDataProvider will provide information for the XML sitemap for a specific database table
29 * @internal this class is not part of TYPO3's Core API.
30 */
31 class RecordsXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
32 {
33 /**
34 * @param ServerRequestInterface $request
35 * @param string $key
36 * @param array $config
37 * @param ContentObjectRenderer|null $cObj
38 * @throws MissingConfigurationException
39 */
40 public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
41 {
42 parent::__construct($request, $key, $config, $cObj);
43
44 $this->generateItems();
45 }
46
47 /**
48 * @throws MissingConfigurationException
49 */
50 public function generateItems(): void
51 {
52 $table = $this->config['table'];
53
54 if (empty($table)) {
55 throw new MissingConfigurationException(
56 'No configuration found for sitemap ' . $this->getKey(),
57 1535576053
58 );
59 }
60
61 $pids = !empty($this->config['pid']) ? GeneralUtility::intExplode(',', $this->config['pid']) : [];
62 $lastModifiedField = $this->config['lastModifiedField'] ?? 'tstamp';
63 $sortField = $this->config['sortField'] ?? 'sorting';
64
65 $changeFreqField = $this->config['changeFreqField'] ?? '';
66 $priorityField = $this->config['priorityField'] ?? '';
67
68 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
69 ->getQueryBuilderForTable($table);
70
71 $constraints = [];
72 if (!empty($GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
73 $constraints[] = $queryBuilder->expr()->in(
74 $GLOBALS['TCA'][$table]['ctrl']['languageField'],
75 [
76 -1, // All languages
77 $this->getLanguageId() // Current language
78 ]
79 );
80 }
81
82 if (!empty($pids)) {
83 $recursiveLevel = isset($this->config['recursive']) ? (int)$this->config['recursive'] : 0;
84 if ($recursiveLevel) {
85 $newList = [[]];
86 foreach ($pids as $pid) {
87 $list = $this->cObj->getTreeList($pid, $recursiveLevel);
88 if ($list) {
89 $newList[] = explode(',', $list);
90 }
91 }
92 $pids = array_merge($pids, ...$newList);
93 }
94
95 $constraints[] = $queryBuilder->expr()->in('pid', $pids);
96 }
97
98 if (!empty($this->config['additionalWhere'])) {
99 $constraints[] = QueryHelper::stripLogicalOperatorPrefix($this->config['additionalWhere']);
100 }
101
102 $queryBuilder->select('*')
103 ->from($table);
104
105 if (!empty($constraints)) {
106 $queryBuilder->where(
107 ...$constraints
108 );
109 }
110
111 $rows = $queryBuilder->orderBy($sortField)
112 ->execute()
113 ->fetchAll();
114
115 foreach ($rows as $row) {
116 $item = [
117 'data' => $row,
118 'lastMod' => (int)$row[$lastModifiedField]
119 ];
120 if (!empty($changeFreqField)) {
121 $item['changefreq'] = $row[$changeFreqField];
122 }
123 $item['priority'] = !empty($priorityField) ? $row[$priorityField] : 0.5;
124 $this->items[] = $item;
125 }
126 }
127
128 /**
129 * @param array $data
130 * @return array
131 */
132 protected function defineUrl(array $data): array
133 {
134 $pageId = $this->config['url']['pageId'] ?? $GLOBALS['TSFE']->id;
135 $additionalParams = [];
136
137 $additionalParams = $this->getUrlFieldParameterMap($additionalParams, $data['data']);
138 $additionalParams = $this->getUrlAdditionalParams($additionalParams);
139
140 $additionalParamsString = http_build_query(
141 $additionalParams,
142 '',
143 '&',
144 PHP_QUERY_RFC3986
145 );
146
147 $typoLinkConfig = [
148 'parameter' => $pageId,
149 'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
150 'forceAbsoluteUrl' => 1,
151 ];
152
153 $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
154
155 return $data;
156 }
157
158 /**
159 * @param array $additionalParams
160 * @param array $data
161 * @return array
162 */
163 protected function getUrlFieldParameterMap(array $additionalParams, array $data): array
164 {
165 if (!empty($this->config['url']['fieldToParameterMap']) &&
166 \is_array($this->config['url']['fieldToParameterMap'])) {
167 foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
168 $additionalParams[$urlPart] = $data[$field];
169 }
170 }
171
172 return $additionalParams;
173 }
174
175 /**
176 * @param array $additionalParams
177 * @return array
178 */
179 protected function getUrlAdditionalParams(array $additionalParams): array
180 {
181 if (!empty($this->config['url']['additionalGetParameters']) &&
182 is_array($this->config['url']['additionalGetParameters'])) {
183 foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
184 foreach ($extensionConfig as $key => $value) {
185 $additionalParams[$extension . '[' . $key . ']'] = $value;
186 }
187 }
188 }
189
190 return $additionalParams;
191 }
192
193 /**
194 * @return int
195 * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
196 */
197 protected function getLanguageId(): int
198 {
199 $context = GeneralUtility::makeInstance(Context::class);
200 return (int)$context->getPropertyFromAspect('language', 'id');
201 }
202 }