[FEATURE] Add properties changefreq and priority to XML sitemap entries
[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 $changeFreqField = $this->config['changeFreqField'] ?? '';
62 $priorityField = $this->config['priorityField'] ?? '';
63
64 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
65 ->getQueryBuilderForTable($this->config['table']);
66
67 $constraints = [];
68
69 if (!empty($pids)) {
70 $recursiveLevel = isset($this->config['recursive']) ? (int)$this->config['recursive'] : 0;
71 if ($recursiveLevel) {
72 $newList = [];
73 foreach ($pids as $pid) {
74 $list = $this->cObj->getTreeList($pid, $recursiveLevel);
75 if ($list) {
76 $newList = array_merge($newList, explode(',', $list));
77 }
78 }
79 $pids = array_merge($pids, $newList);
80 }
81
82 $constraints[] = $queryBuilder->expr()->in('pid', $pids);
83 }
84
85 if (!empty($this->config['additionalWhere'])) {
86 $constraints[] = $this->config['additionalWhere'];
87 }
88
89 $queryBuilder->select('*')
90 ->from($this->config['table']);
91
92 if (!empty($constraints)) {
93 $queryBuilder->where(
94 ...$constraints
95 );
96 }
97
98 $rows = $queryBuilder->orderBy($sortField)
99 ->execute()
100 ->fetchAll();
101
102 foreach ($rows as $row) {
103 $item = [
104 'data' => $row,
105 'lastMod' => (int)$row[$lastModifiedField]
106 ];
107 if (!empty($changeFreqField)) {
108 $item['changefreq'] = $row[$changeFreqField];
109 }
110 $item['priority'] = !empty($priorityField) ? $row[$priorityField] : 0.5;
111 $this->items[] = $item;
112 }
113 }
114
115 /**
116 * @param array $data
117 * @return array
118 */
119 protected function defineUrl(array $data): array
120 {
121 $pageId = $this->config['url']['pageId'] ?? $GLOBALS['TSFE']->id;
122 $additionalParams = [];
123
124 $additionalParams = $this->getUrlFieldParameterMap($additionalParams, $data['data']);
125 $additionalParams = $this->getUrlAdditionalParams($additionalParams);
126
127 $additionalParamsString = http_build_query(
128 $additionalParams,
129 '',
130 '&',
131 PHP_QUERY_RFC3986
132 );
133
134 $typoLinkConfig = [
135 'parameter' => $pageId,
136 'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
137 'forceAbsoluteUrl' => 1,
138 'useCacheHash' => $this->config['url']['useCacheHash'] ?? 0
139 ];
140
141 $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
142
143 return $data;
144 }
145
146 /**
147 * @param array $additionalParams
148 * @param array $data
149 * @return array
150 */
151 protected function getUrlFieldParameterMap(array $additionalParams, array $data): array
152 {
153 if (!empty($this->config['url']['fieldToParameterMap']) &&
154 \is_array($this->config['url']['fieldToParameterMap'])) {
155 foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
156 $additionalParams[$urlPart] = $data[$field];
157 }
158 }
159
160 return $additionalParams;
161 }
162
163 /**
164 * @param array $additionalParams
165 * @return array
166 */
167 protected function getUrlAdditionalParams(array $additionalParams): array
168 {
169 if (!empty($this->config['url']['additionalGetParameters']) &&
170 is_array($this->config['url']['additionalGetParameters'])) {
171 foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
172 foreach ($extensionConfig as $key => $value) {
173 $additionalParams[$extension . '[' . $key . ']'] = $value;
174 }
175 }
176 }
177
178 return $additionalParams;
179 }
180 }