‪TYPO3CMS  9.5
RecordsXmlSitemapDataProvider.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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;
26 
32 {
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 
50  public function ‪generateItems(): void
51  {
52  $table = $this->config['table'];
53 
54  if (empty($table)) {
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  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
66  ->getQueryBuilderForTable($table);
67 
68  $constraints = [];
69  if (!empty(‪$GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
70  $constraints[] = $queryBuilder->expr()->in(
71  ‪$GLOBALS['TCA'][$table]['ctrl']['languageField'],
72  [
73  -1, // All languages
74  $this->‪getLanguageId() // Current language
75  ]
76  );
77  }
78 
79  if (!empty($pids)) {
80  $recursiveLevel = isset($this->config['recursive']) ? (int)$this->config['recursive'] : 0;
81  if ($recursiveLevel) {
82  $newList = [];
83  foreach ($pids as $pid) {
84  $list = $this->cObj->getTreeList($pid, $recursiveLevel);
85  if ($list) {
86  $newList = array_merge($newList, explode(',', $list));
87  }
88  }
89  $pids = array_merge($pids, $newList);
90  }
91 
92  $constraints[] = $queryBuilder->expr()->in('pid', $pids);
93  }
94 
95  if (!empty($this->config['additionalWhere'])) {
96  $constraints[] = ‪QueryHelper::stripLogicalOperatorPrefix($this->config['additionalWhere']);
97  }
98 
99  $queryBuilder->select('*')
100  ->from($table);
101 
102  if (!empty($constraints)) {
103  $queryBuilder->where(
104  ...$constraints
105  );
106  }
107 
108  $rows = $queryBuilder->orderBy($sortField)
109  ->execute()
110  ->fetchAll();
111 
112  foreach ($rows as $row) {
113  $this->items[] = [
114  'data' => $row,
115  'lastMod' => (int)$row[$lastModifiedField]
116  ];
117  }
118  }
119 
124  protected function ‪defineUrl(array $data): array
125  {
126  $pageId = $this->config['url']['pageId'] ?? ‪$GLOBALS['TSFE']->id;
127  $additionalParams = [];
128 
129  $additionalParams = $this->‪getUrlFieldParameterMap($additionalParams, $data['data']);
130  $additionalParams = $this->‪getUrlAdditionalParams($additionalParams);
131 
132  $additionalParamsString = http_build_query(
133  $additionalParams,
134  '',
135  '&',
136  PHP_QUERY_RFC3986
137  );
138 
139  $typoLinkConfig = [
140  'parameter' => $pageId,
141  'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
142  'forceAbsoluteUrl' => 1,
143  'useCacheHash' => $this->config['url']['useCacheHash'] ?? 0
144  ];
145 
146  $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
147 
148  return $data;
149  }
150 
156  protected function ‪getUrlFieldParameterMap(array $additionalParams, array $data): array
157  {
158  if (!empty($this->config['url']['fieldToParameterMap']) &&
159  \is_array($this->config['url']['fieldToParameterMap'])) {
160  foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
161  $additionalParams[$urlPart] = $data[$field];
162  }
163  }
164 
165  return $additionalParams;
166  }
167 
172  protected function ‪getUrlAdditionalParams(array $additionalParams): array
173  {
174  if (!empty($this->config['url']['additionalGetParameters']) &&
175  is_array($this->config['url']['additionalGetParameters'])) {
176  foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
177  foreach ($extensionConfig as ‪$key => $value) {
178  $additionalParams[$extension . '[' . ‪$key . ']'] = $value;
179  }
180  }
181  }
182 
183  return $additionalParams;
184  }
185 
190  protected function ‪getLanguageId(): int
191  {
192  $context = GeneralUtility::makeInstance(Context::class);
193  return (int)$context->getPropertyFromAspect('language', 'id');
194  }
195 }
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getUrlFieldParameterMap
‪array getUrlFieldParameterMap(array $additionalParams, array $data)
Definition: RecordsXmlSitemapDataProvider.php:156
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\getKey
‪string getKey()
Definition: AbstractXmlSitemapDataProvider.php:79
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$key
‪string $key
Definition: AbstractXmlSitemapDataProvider.php:30
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$config
‪array $config
Definition: AbstractXmlSitemapDataProvider.php:42
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Seo\XmlSitemap
Definition: AbstractXmlSitemapDataProvider.php:4
‪TYPO3\CMS\Core\Database\Query\QueryHelper
Definition: QueryHelper.php:30
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getUrlAdditionalParams
‪array getUrlAdditionalParams(array $additionalParams)
Definition: RecordsXmlSitemapDataProvider.php:172
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\defineUrl
‪array defineUrl(array $data)
Definition: RecordsXmlSitemapDataProvider.php:124
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$request
‪ServerRequestInterface $request
Definition: AbstractXmlSitemapDataProvider.php:54
‪TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException
Definition: MissingConfigurationException.php:19
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider
Definition: RecordsXmlSitemapDataProvider.php:32
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider
Definition: AbstractXmlSitemapDataProvider.php:27
‪TYPO3\CMS\Core\Database\Query\QueryHelper\stripLogicalOperatorPrefix
‪static string stripLogicalOperatorPrefix(string $constraint)
Definition: QueryHelper.php:163
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\getLanguageId
‪int getLanguageId()
Definition: RecordsXmlSitemapDataProvider.php:190
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Seo\XmlSitemap\AbstractXmlSitemapDataProvider\$cObj
‪ContentObjectRenderer $cObj
Definition: AbstractXmlSitemapDataProvider.php:46
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\__construct
‪__construct(ServerRequestInterface $request, string $key, array $config=[], ContentObjectRenderer $cObj=null)
Definition: RecordsXmlSitemapDataProvider.php:40
‪TYPO3\CMS\Seo\XmlSitemap\RecordsXmlSitemapDataProvider\generateItems
‪generateItems()
Definition: RecordsXmlSitemapDataProvider.php:50