Completed
Branch master (d17104)
by Christian
21:20
created

getTypoScriptFrontendController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
namespace TYPO3\CMS\Seo\XmlSitemap;
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
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Core\Context\Context;
20
use TYPO3\CMS\Core\Context\LanguageAspect;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24
25
/**
26
 * Class to generate a XML sitemap for pages
27
 */
28
class PagesXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
29
{
30
    public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
31
    {
32
        parent::__construct($request, $key, $config, $cObj);
33
34
        $this->generateItems($request);
35
    }
36
37
    /**
38
     * @param \Psr\Http\Message\ServerRequestInterface $request
39
     */
40
    public function generateItems(ServerRequestInterface $request): void
41
    {
42
        $site = $request->getAttribute('site');
43
        $rootPageId = $site->getRootPageId();
44
45
        $additionalWhere = $this->config['additionalWhere'] ?? '';
46
        if (!empty($this->config['excludedDoktypes'])) {
47
            $excludedDoktypes = GeneralUtility::trimExplode(',', $this->config['excludedDoktypes']);
48
            if (!empty($excludedDoktypes)) {
49
                $additionalWhere .= ' AND doktype NOT IN (' . implode(',', $excludedDoktypes) . ')';
50
            }
51
        }
52
53
        $rootPage = $this->getTypoScriptFrontendController()->page;
54
        $pages = [
55
            [
56
                'uid' => $rootPage['uid'],
57
                'tstamp' => $rootPage['tstamp'],
58
                'l18n_cfg' => $rootPage['l18n_cfg'],
59
                'SYS_LASTCHANGED' => $rootPage['SYS_LASTCHANGED']
60
            ]
61
        ];
62
63
        $pages = $this->getSubPages($rootPageId, $pages, ltrim($additionalWhere));
64
65
        $languageId = $this->getCurrentLanguageAspect()->getId();
66
        foreach ($pages as $page) {
67
            /**
68
             * @todo Checking if the page has to be shown/hidden should normally be handled by the
69
             * PageRepository but to prevent major breaking changes this is checked here for now
70
             */
71
            if (
72
                !(
73
                    GeneralUtility::hideIfDefaultLanguage($page['l18n_cfg'])
74
                    && (!$languageId || ($languageId && !$page['_PAGES_OVERLAY']))
75
                )
76
                &&
77
                !(
78
                    $languageId
79
                    && GeneralUtility::hideIfNotTranslated($page['l18n_cfg'])
80
                    && !$page['_PAGES_OVERLAY']
81
                )
82
            ) {
83
                $typoLinkConfig = [
84
                    'parameter' => $page['uid'],
85
                    'forceAbsoluteUrl' => 1,
86
                ];
87
88
                $loc = $this->cObj->typoLink_URL($typoLinkConfig);
89
                $lastMod = $page['SYS_LASTCHANGED'] ?: $page['tstamp'];
90
91
                $this->items[] = [
92
                    'loc' => $loc,
93
                    'lastMod' => (int)$lastMod
94
                ];
95
            }
96
        }
97
    }
98
99
    /**
100
     * Get subpages
101
     *
102
     * @param int $parentPageId
103
     * @param array $pages
104
     * @param string $additionalWhere
105
     * @return array
106
     */
107
    protected function getSubPages(int $parentPageId, array $pages = [], $additionalWhere = ''): array
108
    {
109
        $subPages = $this->getTypoScriptFrontendController()->sys_page->getMenu(
110
            $parentPageId,
111
            'uid, tstamp, SYS_LASTCHANGED, l18n_cfg',
112
            'sorting',
113
            $additionalWhere,
114
            false
115
        );
116
        $pages = array_merge($pages, $subPages);
117
118
        foreach ($subPages as $subPage) {
119
            $pages = $this->getSubPages((int)$subPage['uid'], $pages, $additionalWhere);
120
        }
121
122
        return $pages;
123
    }
124
125
    /**
126
     * @return TypoScriptFrontendController
127
     */
128
    protected function getTypoScriptFrontendController(): TypoScriptFrontendController
129
    {
130
        return $GLOBALS['TSFE'];
131
    }
132
133
    /**
134
     * @return LanguageAspect
135
     * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
136
     */
137
    protected function getCurrentLanguageAspect(): LanguageAspect
138
    {
139
        return GeneralUtility::makeInstance(Context::class)->getAspect('language');
140
    }
141
}
142