Completed
Push — master ( 5e5725...eff4fd )
by
unknown
40:58 queued 27:03
created

PagesRepository::getAllSubpagesForPage()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 45
rs 8.0555
c 1
b 0
f 0
cc 9
nc 6
nop 4
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\Linkvalidator\Repository;
19
20
use TYPO3\CMS\Core\Database\Connection;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
use TYPO3\CMS\Core\Database\Query\QueryHelper;
23
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
24
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * Class for pages database queries.
29
 *
30
 * @internal not part of the TYPO3 Core API.
31
 */
32
class PagesRepository
33
{
34
    protected const TABLE = 'pages';
35
36
    /**
37
     * Check if rootline contains a hidden page
38
     *
39
     * @param array $pageInfo Array with uid, title, hidden, extendToSubpages from pages table
40
     * @return bool TRUE if rootline contains a hidden page, FALSE if not
41
     */
42
    public function doesRootLineContainHiddenPages(array $pageInfo): bool
43
    {
44
        $pid = (int)($pageInfo['pid']);
45
        if ($pid === 0) {
46
            return false;
47
        }
48
        $isHidden = (bool)($pageInfo['hidden']);
49
        $extendToSubpages = (bool)($pageInfo['extendToSubpages']);
50
51
        if ($extendToSubpages === true && $isHidden === true) {
52
            return true;
53
        }
54
55
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
56
            ->getQueryBuilderForTable(self::TABLE);
57
        $queryBuilder->getRestrictions()->removeAll();
58
59
        $row = $queryBuilder
60
            ->select('uid', 'title', 'hidden', 'extendToSubpages')
61
            ->from(self::TABLE)
62
            ->where(
63
                $queryBuilder->expr()->eq(
64
                    'uid',
65
                    $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
66
                )
67
            )
68
            ->execute()
69
            ->fetch();
70
71
        if ($row !== false) {
72
            return $this->doesRootLineContainHiddenPages($row);
73
        }
74
        return false;
75
    }
76
77
    /**
78
     * Generates a list of page uids from $id. List does not include $id itself.
79
     * The only pages excluded from the list are deleted pages.
80
     *
81
     * @param int $id Start page id
82
     * @param int $depth Depth to traverse down the page tree.
83
     * @param string $permsClause Perms clause
84
     * @param bool $considerHidden Whether to consider hidden pages or not
85
     * @return int[] Returns the list of subpages (if any pages selected!)
86
     */
87
    public function getAllSubpagesForPage(
88
        int $id,
89
        int $depth,
90
        string $permsClause,
91
        bool $considerHidden = false
92
    ): array {
93
        $subPageIds = [];
94
        if ($depth === 0) {
95
            return $subPageIds;
96
        }
97
98
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
99
            ->getQueryBuilderForTable(self::TABLE);
100
        $queryBuilder->getRestrictions()
101
            ->removeAll()
102
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
103
104
        $result = $queryBuilder
105
            ->select('uid', 'title', 'hidden', 'extendToSubpages')
106
            ->from(self::TABLE)
107
            ->where(
108
                $queryBuilder->expr()->eq(
109
                    'pid',
110
                    $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)
111
                ),
112
                QueryHelper::stripLogicalOperatorPrefix($permsClause)
113
            )
114
            ->execute();
115
116
        while ($row = $result->fetch()) {
117
            $subpageId = (int)$row['uid'];
118
            $isHidden = (bool)$row['hidden'];
119
            if (!$isHidden || $considerHidden) {
120
                $subPageIds[] = $subpageId;
121
            }
122
            if ($depth > 1 && (!($isHidden && $row['extendToSubpages'] == 1) || $considerHidden)) {
123
                $subPageIds = array_merge($subPageIds, $this->getAllSubpagesForPage(
124
                    $subpageId,
125
                    $depth - 1,
126
                    $permsClause,
127
                    $considerHidden
128
                ));
129
            }
130
        }
131
        return $subPageIds;
132
    }
133
134
    /**
135
     * Add page translations to list of pages
136
     *
137
     * @param int $currentPage
138
     * @param string $permsClause
139
     * @param bool $considerHiddenPages
140
     * @param int[] $limitToLanguageIds
141
     * @return int[]
142
     */
143
    public function getTranslationForPage(
144
        int $currentPage,
145
        string $permsClause,
146
        bool $considerHiddenPages,
147
        array $limitToLanguageIds = []
148
    ): array {
149
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE);
150
        $queryBuilder->getRestrictions()
151
            ->removeAll()
152
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
153
        if (!$considerHiddenPages) {
154
            $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
155
        }
156
        $constraints = [
157
            $queryBuilder->expr()->eq(
158
                'l10n_parent',
159
                $queryBuilder->createNamedParameter($currentPage, \PDO::PARAM_INT)
160
            )
161
        ];
162
        if (!empty($limitToLanguageIds)) {
163
            $constraints[] = $queryBuilder->expr()->in(
164
                'sys_language_uid',
165
                $queryBuilder->createNamedParameter($limitToLanguageIds, Connection::PARAM_INT_ARRAY)
166
            );
167
        }
168
        if ($permsClause) {
169
            $constraints[] = QueryHelper::stripLogicalOperatorPrefix($permsClause);
170
        }
171
172
        $result = $queryBuilder
173
            ->select('uid', 'title', 'hidden')
174
            ->from(self::TABLE)
175
            ->where(...$constraints)
176
            ->execute();
177
178
        $translatedPages = [];
179
        while ($row = $result->fetch()) {
180
            $translatedPages[] = (int)$row['uid'];
181
        }
182
183
        return $translatedPages;
184
    }
185
}
186