Passed
Push — master ( b68c5d...58042b )
by
unknown
17:53
created

PagesXmlSitemapDataProvider::getPages()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 44
rs 8.8337
c 0
b 0
f 0
cc 6
nc 24
nop 0
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
18
namespace TYPO3\CMS\Seo\XmlSitemap;
19
20
use Psr\Http\Message\ServerRequestInterface;
21
use TYPO3\CMS\Core\Context\Context;
22
use TYPO3\CMS\Core\Context\LanguageAspect;
23
use TYPO3\CMS\Core\Database\ConnectionPool;
24
use TYPO3\CMS\Core\Database\Query\QueryHelper;
25
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
28
29
/**
30
 * Class to generate a XML sitemap for pages
31
 * @internal this class is not part of TYPO3's Core API.
32
 */
33
class PagesXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
34
{
35
    public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
36
    {
37
        parent::__construct($request, $key, $config, $cObj);
38
39
        $this->generateItems();
40
    }
41
42
    protected function generateItems(): void
43
    {
44
        $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
45
        $pages = $pageRepository->getPagesOverlay($this->getPages());
46
        $languageAspect = $this->getCurrentLanguageAspect();
47
        foreach ($pages as $page) {
48
            if (!$pageRepository->isPageSuitableForLanguage($page, $languageAspect)) {
49
                continue;
50
            }
51
52
            $this->items[] = [
53
                'uid' => $page['uid'],
54
                'lastMod' => (int)($page['SYS_LASTCHANGED'] ?: $page['tstamp']),
55
                'changefreq' => $page['sitemap_changefreq'],
56
                'priority' => (float)$page['sitemap_priority'],
57
            ];
58
        }
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    protected function getPages(): array
65
    {
66
        if (!empty($this->config['rootPage'])) {
67
            $rootPageId = (int)$this->config['rootPage'];
68
        } else {
69
            $site = $this->request->getAttribute('site');
70
            $rootPageId = $site->getRootPageId();
71
        }
72
73
        $excludePagesRecursive = GeneralUtility::intExplode(',', $this->config['excludePagesRecursive'] ?? '', true);
74
        $excludePagesRecursiveWhereClause = '';
75
        if ($excludePagesRecursive !== []) {
76
            $excludePagesRecursiveWhereClause = sprintf('uid NOT IN (%s)', implode(',', $excludePagesRecursive));
77
        }
78
79
        $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
80
        $treeList = $cObj->getTreeList(-$rootPageId, 99, 0, false, '', $excludePagesRecursiveWhereClause);
81
        $treeListArray = GeneralUtility::intExplode(',', $treeList);
82
83
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
84
            ->getQueryBuilderForTable('pages');
85
86
        $constraints = [
87
            $queryBuilder->expr()->in('uid', $treeListArray)
88
        ];
89
90
        if (!empty($this->config['additionalWhere'])) {
91
            $constraints[] = QueryHelper::stripLogicalOperatorPrefix($this->config['additionalWhere']);
92
        }
93
94
        if (!empty($this->config['excludedDoktypes'])) {
95
            $excludedDoktypes = GeneralUtility::intExplode(',', $this->config['excludedDoktypes']);
96
            if (!empty($excludedDoktypes)) {
97
                $constraints[] = $queryBuilder->expr()->notIn('doktype', implode(',', $excludedDoktypes));
98
            }
99
        }
100
        $pages = $queryBuilder->select('*')
101
            ->from('pages')
102
            ->where(...$constraints)
103
            ->orderBy('uid', 'ASC')
104
            ->execute()
105
            ->fetchAll();
106
107
        return $pages;
108
    }
109
110
    /**
111
     * @return LanguageAspect
112
     */
113
    protected function getCurrentLanguageAspect(): LanguageAspect
114
    {
115
        return GeneralUtility::makeInstance(Context::class)->getAspect('language');
116
    }
117
118
    /**
119
     * @param array $data
120
     * @return array
121
     */
122
    protected function defineUrl(array $data): array
123
    {
124
        $typoLinkConfig = [
125
            'parameter' => $data['uid'],
126
            'forceAbsoluteUrl' => 1,
127
        ];
128
129
        $data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
130
131
        return $data;
132
    }
133
}
134