Pages::getPriority()   B
last analyzed

Complexity

Conditions 9
Paths 26

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
cc 9
nc 26
nop 2
1
<?php
2
3
/**
4
 * Description of Pages
5
 */
6
7
namespace FRUIT\GoogleServices\Service\SitemapProvider;
8
9
use FRUIT\GoogleServices\Controller\SitemapController;
10
use FRUIT\GoogleServices\Domain\Model\Node;
11
use FRUIT\GoogleServices\Service\SitemapDataService;
12
use FRUIT\GoogleServices\Service\SitemapProviderInterface;
13
use TYPO3\CMS\Backend\Utility\BackendUtility;
14
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
15
use TYPO3\CMS\Frontend\Page\PageRepository;
16
17
/**
18
 * Description of Pages
19
 *
20
 * @author timlochmueller
21
 */
22
class Pages implements SitemapProviderInterface
23
{
24
25
    /**
26
     * Get the Records
27
     *
28
     * @param integer $startPage
29
     * @param array $basePages
30
     * @param SitemapController $obj
31
     *
32
     * @return array
33
     */
34
    public function getRecords($startPage, $basePages, SitemapController $obj): array
35
    {
36
        $nodes = [];
37
        foreach ($basePages as $uid) {
38
            // If currently in another language than default, check if the page is translated - else continue
39
            if ($GLOBALS['TSFE']->sys_language_uid != 0) {
40
                $localizedPagesTable = 'pages';
41
42
                // in TYPO3 6.x the localization isn't correctly handled when using BackendUtility::getRecordLocalization('pages')
43
                // so we have to use the 'pages_language_overlay' table as param
44
                if (VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version) < 7000000) {
45
                    $localizedPagesTable = 'pages_language_overlay';
46
                }
47
48
                if (BackendUtility::getRecordLocalization($localizedPagesTable, $uid, $GLOBALS['TSFE']->sys_language_uid) == false) {
49
                    continue;
50
                }
51
            }
52
53
            // Build URL
54
            $url = $obj->getUriBuilder()
55
                ->setTargetPageUid($uid)
56
                ->build();
57
58
            // can't generate a valid url
59
            if (!strlen($url)) {
60
                continue;
61
            }
62
63
            // Get Record
64
            $record = BackendUtility::getRecord('pages', $uid);
65
66
            // exclude Doctypes
67
            if (in_array($record['doktype'], [
68
                PageRepository::DOKTYPE_LINK,
69
                PageRepository::DOKTYPE_SHORTCUT,
70
                PageRepository::DOKTYPE_SPACER
71
            ])) {
72
                continue;
73
            }
74
75
            // Exclude pages with exclude flag
76
            if ($record['exclude_sitemap']) {
77
                continue;
78
            }
79
80
                // Check FE Access
81
            if ($record['fe_group'] != 0 || $record['no_search'] != 0) {
82
                continue;
83
            }
84
85
            $rootLineList = $GLOBALS['TSFE']->sys_page->getRootLine($record['uid']);
86
            $addToNode = true;
87
            foreach ($rootLineList as $rootPage) {
88
                if ($rootPage['extendToSubpages'] == 1 && ($rootPage['fe_group'] != 0 || $rootPage['no_search'] != 0)) {
89
                    $addToNode = false;
90
                    break;
91
                }
92
            }
93
            if ($addToNode === false) {
94
                continue;
95
            }
96
97
            // Build Node
98
            $node = new Node();
99
            $node->setPid($uid);
100
            $node->setLoc($url);
101
            $node->setPriority($this->getPriority($startPage, $record));
102
            $node->setChangefreq(SitemapDataService::mapTimeout2Period($record['cache_timeout']));
103
            $node->setLastmod($this->getModifiedDate($record));
104
105
            #$geo = new Geo();
106
            #$geo->setFormat('kml');
107
            #$node->setGeo($geo);
108
109
            $nodes[] = $node;
110
        }
111
112
        return $nodes;
113
    }
114
115
    /**
116
     * Get the priority
117
     *
118
     * @param integer $startPage
119
     * @param array $record
120
     *
121
     * @return float
122
     */
123
    protected function getPriority($startPage, $record)
124
    {
125
        $nodePrio = (float)$record['node_priority'];
126
        if ($nodePrio > 0) {
127
            return $nodePrio;
128
        }
129
130
        // Prio
131
        $rootline = $GLOBALS['TSFE']->sys_page->getRootLine($record['uid']);
132
        $find = false;
133
        foreach ($rootline as $key => $value) {
134
            if ($find) {
135
                unset($rootline[$key]);
136
            }
137
            if ($value['uid'] == $startPage) {
138
                $find = true;
139
            }
140
        }
141
142
        switch (sizeof($rootline)) {
143
            case 1:
144
                return 1.0;
145
            case 2:
146
                return 0.9;
147
            case 3:
148
                return 0.9;
149
            case 4:
150
                return 0.7;
151
            default:
152
                return 0.5;
153
        }
154
    }
155
156
    /**
157
     * get the modifiedDate
158
     *
159
     * @param array $record
160
     *
161
     * @return integer
162
     */
163
    protected function getModifiedDate($record)
164
    {
165
        // Last mod
166
        $lastMod = $record['crdate'];
167
        if ($record['tstamp'] > $lastMod) {
168
            $lastMod = $record['tstamp'];
169
        }
170
        if ($record['SYS_LASTCHANGED'] > $lastMod) {
171
            $lastMod = $record['SYS_LASTCHANGED'];
172
        }
173
        return $lastMod;
174
    }
175
}
176