• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tomasnorre / crawler / 43501585105

13 Nov 2026 08:26PM UTC coverage: 67.056% (+0.07%) from 66.983%
43501585105

push

github

web-flow
[BUGFIX] Cache exclude page trees, split ConfigurationService to reduce complexity (#1328)

120 of 177 new or added lines in 4 files covered. (67.8%)

1262 of 1882 relevant lines covered (67.06%)

5.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/Classes/Service/ExcludeStringExpander.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler\Service;
6

7
/*
8
 * (c) 2026 Tomas Norre Mikkelsen <tomasnorre@gmail.com>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21

22
use TYPO3\CMS\Backend\Tree\View\PageTreeView;
23
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
24
use TYPO3\CMS\Core\Core\Bootstrap;
25
use TYPO3\CMS\Core\Type\Bitmask\Permission;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27

28
/**
29
 * Expands the "exclude" setting of a crawler configuration, fx. "1,5+2,7+",
30
 * to the page ids it covers.
31
 *
32
 * @internal
33
 */
34
class ExcludeStringExpander
35
{
36
    private const DEFAULT_SUBPAGE_DEPTH = 99;
37

38
    /**
39
     * Page ids per exclude string, keys are kept from array_unique()
40
     *
41
     * @var array<string, array<int, int>>
42
     */
43
    private array $expandedExcludeStringCache = [];
44

45
    /**
46
     * PageTreeView::$tree per page id and depth
47
     *
48
     * @var array<int, array<int, array>>
49
     */
50
    private array $pageTreeCache = [];
51

52
    /**
53
     * Each comma separated part is a page id, optionally followed by "+" and the depth of subpages
54
     * to include. "5" is page 5 only, "5+2" adds two levels of subpages, "5+" adds all subpages.
55
     *
56
     * @return array<int, int>
57
     */
58
    public function expand(string $excludeString): array
59
    {
60
        if (isset($this->expandedExcludeStringCache[$excludeString])) {
11 ✔
61
            return $this->expandedExcludeStringCache[$excludeString];
8 ✔
62
        }
63

64
        $pidList = [];
11 ✔
65
        foreach (GeneralUtility::trimExplode(',', $excludeString, true) as $excludePart) {
11 ✔
66
            [$pid, $depth] = $this->parseExcludePart($excludePart);
11 ✔
67
            $pidList[] = $pid;
11 ✔
68
            if ($depth > 0) {
11 ✔
69
                $pidList = $this->addSubpages($pid, $depth, $pidList);
2 ✔
70
            }
71
        }
72

73
        $this->expandedExcludeStringCache[$excludeString] = array_unique($pidList);
11 ✔
74

75
        return $this->expandedExcludeStringCache[$excludeString];
11 ✔
76
    }
77

78
    /**
79
     * @return array{int, int} The page id and the depth of subpages
80
     */
81
    private function parseExcludePart(string $excludePart): array
82
    {
83
        $parts = GeneralUtility::trimExplode('+', $excludePart);
11 ✔
84
        $depth = (int) ($parts[1] ?? 0);
11 ✔
85
        if ($depth === 0 && str_contains($excludePart, '+')) {
11 ✔
NEW
86
            $depth = self::DEFAULT_SUBPAGE_DEPTH;
×
87
        }
88

89
        return [(int) $parts[0], $depth];
11 ✔
90
    }
91

92
    /**
93
     * @param list<int> $pidList
94
     * @return list<int>
95
     */
96
    private function addSubpages(int $pid, int $depth, array $pidList): array
97
    {
98
        if (!isset($this->pageTreeCache[$pid][$depth])) {
2 ✔
99
            // A new tree per page, as getTree() appends to the tree of an existing instance
100
            $tree = GeneralUtility::makeInstance(PageTreeView::class);
2 ✔
101
            $tree->init('AND ' . $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW));
2 ✔
102
            $tree->getTree($pid, $depth);
2 ✔
103
            $this->pageTreeCache[$pid][$depth] = $tree->tree;
2 ✔
104
        }
105

106
        foreach ($this->pageTreeCache[$pid][$depth] as $data) {
2 ✔
107
            $pidList[] = (int) $data['row']['uid'];
2 ✔
108
        }
109
        return $pidList;
2 ✔
110
    }
111

112
    private function getBackendUser(): BackendUserAuthentication
113
    {
114
        // Make sure the _cli_ user is loaded
115
        Bootstrap::initializeBackendAuthentication();
2 ✔
116
        return $GLOBALS['BE_USER'];
2 ✔
117
    }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc