‪TYPO3CMS  10.4
RecordsXmlSitemapDataProvider.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
29 
35 {
43  public function ‪__construct(ServerRequestInterface ‪$request, string ‪$key, array ‪$config = [], ‪ContentObjectRenderer ‪$cObj = null)
44  {
45  parent::__construct(‪$request, ‪$key, ‪$config, ‪$cObj);
46 
47  $this->‪generateItems();
48  }
49 
53  public function ‪generateItems(): void
54  {
55  $table = $this->config['table'];
56 
57  if (empty($table)) {
59  'No configuration found for sitemap ' . $this->‪getKey(),
60  1535576053
61  );
62  }
63 
64  $pids = !empty($this->config['pid']) ? ‪GeneralUtility::intExplode(',', $this->config['pid']) : [];
65  $lastModifiedField = $this->config['lastModifiedField'] ?? 'tstamp';
66  $sortField = $this->config['sortField'] ?? 'sorting';
67 
68  $changeFreqField = $this->config['changeFreqField'] ?? '';
69  $priorityField = $this->config['priorityField'] ?? '';
70 
71  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
72  ->getQueryBuilderForTable($table);
73 
74  $constraints = [];
75  if (!empty(‪$GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
76  $constraints[] = $queryBuilder->expr()->in(
77  ‪$GLOBALS['TCA'][$table]['ctrl']['languageField'],
78  [
79  -1, // All languages
80  $this->‪getLanguageId() // Current language
81  ]
82  );
83  }
84 
85  if (!empty($pids)) {
86  $recursiveLevel = isset($this->config['recursive']) ? (int)$this->config['recursive'] : 0;
87  if ($recursiveLevel) {
88  $newList = [];
89  foreach ($pids as $pid) {
90  $list = $this->cObj->getTreeList($pid, $recursiveLevel);
91  if ($list) {
92  $newList = array_merge($newList, explode(',', $list));
93  }
94  }
95  $pids = array_merge($pids, $newList);
96  }
97 
98  $constraints[] = $queryBuilder->expr()->in('pid', $pids);
99  }
100 
101  if (!empty($this->config['additionalWhere'])) {
102  $constraints[] = ‪QueryHelper::stripLogicalOperatorPrefix($this->config['additionalWhere']);
103  }
104 
105  $queryBuilder->getRestrictions()->add(
106  GeneralUtility::makeInstance(WorkspaceRestriction::class, $this->‪getCurrentWorkspaceAspect()->getId())
107  );
108 
109  $queryBuilder->select('*')
110  ->from($table);
111 
112  if (!empty($constraints)) {
113  $queryBuilder->where(
114  ...$constraints
115  );
116  }
117 
118  $rows = $queryBuilder->orderBy($sortField)
119  ->execute()
120  ->fetchAll();
121 
122  foreach ($rows as $row) {
123  $item = [
124  'data' => $row,
125  'lastMod' => (int)$row[$lastModifiedField]
126  ];
127  if (!empty($changeFreqField)) {
128  $item['changefreq'] = $row[$changeFreqField];
129  }
130  $item['priority'] = !empty($priorityField) ? $row[$priorityField] : 0.5;
131  $this->items[] = $item;
132  }
133  }
134 
139  protected function ‪defineUrl(array $data): array
140  {
141  $pageId = $this->config['url']['pageId'] ?? ‪$GLOBALS['TSFE']->id;
142  $additionalParams = [];
143 
144  $additionalParams = $this->‪getUrlFieldParameterMap($additionalParams, $data['data']);
145  $additionalParams = $this->‪getUrlAdditionalParams($additionalParams);
146 
147  $additionalParamsString = http_build_query(
148  $additionalParams,
149  '',
150  '&',
151  PHP_QUERY_RFC3986
152  );
153 
154  $typoLinkConfig = [
155  'parameter' => $pageId,
156  'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
157  'forceAbsoluteUrl' => 1,
158  ];
159 
160  $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
161 
162  return $data;
163  }
164 
170  protected function ‪getUrlFieldParameterMap(array $additionalParams, array $data): array
171  {
172  if (!empty($this->config['url']['fieldToParameterMap']) &&
173  \is_array($this->config['url']['fieldToParameterMap'])) {
174  foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
175  $additionalParams[$urlPart] = $data[$field];
176  }
177  }
178 
179  return $additionalParams;
180  }
181 
186  protected function ‪getUrlAdditionalParams(array $additionalParams): array
187  {
188  if (!empty($this->config['url']['additionalGetParameters']) &&
189  is_array($this->config['url']['additionalGetParameters'])) {
190  foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
191  foreach ($extensionConfig as ‪$key => $value) {
192  $additionalParams[$extension . '[' . ‪$key . ']'] = $value;
193  }
194  }
195  }
196 
197  return $additionalParams;
198  }
199 
204  protected function ‪getLanguageId(): int
205  {
206  $context = GeneralUtility::makeInstance(Context::class);
207  return (int)$context->getPropertyFromAspect('language', 'id');
208  }
209 
214  {
215  return GeneralUtility::makeInstance(Context::class)->getAspect('workspace');
216  }
217 }
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getUrlFieldParameterMap
‪array getUrlFieldParameterMap(array $additionalParams, array $data)
Definition: RecordsXmlSitemapDataProvider.php:170
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\getKey
‪string getKey()
Definition: AbstractXmlSitemapDataProvider.php:80
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$key
‪string $key
Definition: AbstractXmlSitemapDataProvider.php:31
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$config
‪array $config
Definition: AbstractXmlSitemapDataProvider.php:43
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Seo\XmlSitemap
Definition: AbstractXmlSitemapDataProvider.php:18
‪TYPO3\CMS\Core\Database\Query\QueryHelper
Definition: QueryHelper.php:32
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getUrlAdditionalParams
‪array getUrlAdditionalParams(array $additionalParams)
Definition: RecordsXmlSitemapDataProvider.php:186
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\defineUrl
‪array defineUrl(array $data)
Definition: RecordsXmlSitemapDataProvider.php:139
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$request
‪ServerRequestInterface $request
Definition: AbstractXmlSitemapDataProvider.php:55
‪TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException
Definition: MissingConfigurationException.php:23
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider
Definition: RecordsXmlSitemapDataProvider.php:35
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider
Definition: AbstractXmlSitemapDataProvider.php:28
‪TYPO3\CMS\Core\Database\Query\QueryHelper\stripLogicalOperatorPrefix
‪static string stripLogicalOperatorPrefix(string $constraint)
Definition: QueryHelper.php:165
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static int[] intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:988
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getLanguageId
‪int getLanguageId()
Definition: RecordsXmlSitemapDataProvider.php:204
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getCurrentWorkspaceAspect
‪WorkspaceAspect getCurrentWorkspaceAspect()
Definition: RecordsXmlSitemapDataProvider.php:213
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$cObj
‪ContentObjectRenderer $cObj
Definition: AbstractXmlSitemapDataProvider.php:47
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\__construct
‪__construct(ServerRequestInterface $request, string $key, array $config=[], ContentObjectRenderer $cObj=null)
Definition: RecordsXmlSitemapDataProvider.php:43
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\generateItems
‪generateItems()
Definition: RecordsXmlSitemapDataProvider.php:53
‪TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction
Definition: WorkspaceRestriction.php:39