[FEATURE] Support recursive in record sitemap
[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\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23 use TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException;
24
25 /**
26 * XmlSiteDataProvider will provide information for the XML sitemap for a specific database table
27 * @internal this class is not part of TYPO3's Core API.
28 */
29 class RecordsXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
30 {
31 /**
32 * @param ServerRequestInterface $request
33 * @param string $key
34 * @param array $config
35 * @param ContentObjectRenderer|null $cObj
36 * @throws MissingConfigurationException
37 */
38 public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
39 {
40 parent::__construct($request, $key, $config, $cObj);
41
42 $this->generateItems();
43 }
44
45 /**
46 * @throws MissingConfigurationException
47 */
48 public function generateItems(): void
49 {
50 if (empty($this->config['table'])) {
51 throw new MissingConfigurationException(
52 'No configuration found for sitemap ' . $this->getKey(),
53 1535576053
54 );
55 }
56
57 $pids = !empty($this->config['pid']) ? GeneralUtility::intExplode(',', $this->config['pid']) : [];
58 $lastModifiedField = $this->config['lastModifiedField'] ?? 'tstamp';
59 $sortField = $this->config['sortField'] ?? 'sorting';
60
61 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
62 ->getQueryBuilderForTable($this->config['table']);
63
64 $constraints = [];
65
66 if (!empty($pids)) {
67 $recursiveLevel = isset($this->config['recursive']) ? (int)$this->config['recursive'] : 0;
68 if ($recursiveLevel) {
69 $newList = [];
70 foreach ($pids as $pid) {
71 $list = $this->cObj->getTreeList($pid, $recursiveLevel);
72 if ($list) {
73 $newList = array_merge($newList, explode(',', $list));
74 }
75 }
76 $pids = array_merge($pids, $newList);
77 }
78
79 $constraints[] = $queryBuilder->expr()->in('pid', $pids);
80 }
81
82 if (!empty($this->config['additionalWhere'])) {
83 $constraints[] = $this->config['additionalWhere'];
84 }
85
86 $queryBuilder->select('*')
87 ->from($this->config['table']);
88
89 if (!empty($constraints)) {
90 $queryBuilder->where(
91 ...$constraints
92 );
93 }
94
95 $rows = $queryBuilder->orderBy($sortField)
96 ->execute()
97 ->fetchAll();
98
99 foreach ($rows as $row) {
100 $this->items[] = [
101 'data' => $row,
102 'lastMod' => $row[$lastModifiedField]
103 ];
104 }
105 }
106
107 /**
108 * @param array $data
109 * @return array
110 */
111 protected function defineUrl(array $data): array
112 {
113 $pageId = $this->config['url']['pageId'] ?? $GLOBALS['TSFE']->id;
114 $additionalParams = [];
115
116 $additionalParams = $this->getUrlFieldParameterMap($additionalParams, $data['data']);
117 $additionalParams = $this->getUrlAdditionalParams($additionalParams);
118
119 $additionalParamsString = http_build_query(
120 $additionalParams,
121 '',
122 '&',
123 PHP_QUERY_RFC3986
124 );
125
126 $typoLinkConfig = [
127 'parameter' => $pageId,
128 'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
129 'forceAbsoluteUrl' => 1,
130 'useCacheHash' => $this->config['url']['useCacheHash'] ?? 0
131 ];
132
133 $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
134
135 return $data;
136 }
137
138 /**
139 * @param array $additionalParams
140 * @param array $data
141 * @return array
142 */
143 protected function getUrlFieldParameterMap(array $additionalParams, array $data): array
144 {
145 if (!empty($this->config['url']['fieldToParameterMap']) &&
146 \is_array($this->config['url']['fieldToParameterMap'])) {
147 foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
148 $additionalParams[$urlPart] = $data[$field];
149 }
150 }
151
152 return $additionalParams;
153 }
154
155 /**
156 * @param array $additionalParams
157 * @return array
158 */
159 protected function getUrlAdditionalParams(array $additionalParams): array
160 {
161 if (!empty($this->config['url']['additionalGetParameters']) &&
162 is_array($this->config['url']['additionalGetParameters'])) {
163 foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
164 foreach ($extensionConfig as $key => $value) {
165 $additionalParams[$extension . '[' . $key . ']'] = $value;
166 }
167 }
168 }
169
170 return $additionalParams;
171 }
172 }