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

tomasnorre / crawler / 43828331753

16 Nov 2026 08:04AM UTC coverage: 68.855%. Remained the same
43828331753

push

github

web-flow
[TASK] Mark framework entry points for Psalm (#1341)

Psalm cannot see how TYPO3 calls the extension: backend modules, console
commands, middlewares, hooks, event listeners, the context menu provider
and the TCA itemsProcFunc. It reported them, and everything only used by
them, as unused, which filled the baseline.

The entry points and the PSR-14 events for integrators are marked with
@psalm-api. This removes 56 of 85 entries from psalm-baseline.xml.

Psalm now analyses the backend modules, and found:
* setExtensionSettings() is called by the DI container (#[Required])
* BackendModuleControllerInterface::handleRequest() is called by routing
* BackendModuleStartCrawlingController::assignValues() returned a value
  that was never used, it returns void now

0 of 1 new or added line in 1 file covered. (0.0%)

1269 of 1843 relevant lines covered (68.86%)

5.19 hits per line

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

0.0
/Classes/Controller/Backend/BackendModuleStartCrawlingController.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler\Controller\Backend;
6

7
/*
8
 * (c) 2022-     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 AOE\Crawler\Controller\Backend\Helper\RequestHelper;
23
use AOE\Crawler\Controller\Backend\Helper\UrlBuilder;
24
use AOE\Crawler\Domain\Model\Reason;
25
use AOE\Crawler\Event\InvokeQueueChangeEvent;
26
use AOE\Crawler\Queue\CrawlRun;
27
use AOE\Crawler\Service\PageConfigurationResolver;
28
use AOE\Crawler\Service\PageTreeUrlCollector;
29
use AOE\Crawler\Utility\MessageUtility;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Message\UriInterface;
33
use Symfony\Contracts\Service\Attribute\Required;
34
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
35
use TYPO3\CMS\Backend\Routing\UriBuilder;
36
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38

39
/**
40
 * @internal since v12.0.0
41
 *
42
 * @psalm-api Backend module, registered in Configuration/Backend/Modules.php
43
 */
44
class BackendModuleStartCrawlingController extends AbstractBackendModuleController implements BackendModuleControllerInterface
45
{
46
    private const BACKEND_MODULE = 'web_site_crawler_start';
47
    private const LINE_FEED = 10;
48
    private const CARRIAGE_RETURN = 13;
49
    private int $reqMinute = 1000;
50
    private EventDispatcher $eventDispatcher;
51

52
    /**
53
     * @var array holds the selection of configuration from the configuration selector box
54
     */
55
    private $incomingConfigurationSelection = [];
56

57
    public function __construct(
58
        private readonly PageTreeUrlCollector $pageTreeUrlCollector,
59
        private readonly PageConfigurationResolver $pageConfigurationResolver,
60
        private readonly UriBuilder $backendUriBuilder,
61
    ) {
62
    }
×
63

64
    #[Required]
65
    public function setEventDispatcher(): void
66
    {
67
        $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
×
68
    }
69

70
    #[\Override]
71
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
72
    {
73
        $this->checkPhpBinaryForDirectRequests($this->extensionSettings);
×
74
        $this->pageUid = (int) ($request->getQueryParams()['id'] ?? -1);
×
75
        $this->moduleTemplate = $this->setupView($request, $this->pageUid);
×
76
        $this->moduleTemplate->makeDocHeaderModuleMenu([
×
77
            'id' => $request->getQueryParams()['id'] ?? -1,
×
78
        ]);
×
79

80
        $this->assignValues($request);
×
81
        return $this->moduleTemplate->renderResponse('Backend/ShowCrawlerInformation');
×
82
    }
83

84
    private function assignValues(ServerRequestInterface $request): void
85
    {
86
        $logUrl = $this->backendUriBuilder->buildUriFromRoute('web_site_crawler_log', [
×
87
            'id' => $this->pageUid,
×
88
        ]);
×
89

90
        $crawlingDepth = RequestHelper::getIntFromRequest($request, 'crawlingDepth');
×
91
        $submitCrawlUrls = RequestHelper::getBoolFromRequest($request, '_crawl');
×
92
        $downloadCrawlUrls = RequestHelper::getBoolFromRequest($request, '_download');
×
93
        $scheduledTime = $this->getScheduledTime(RequestHelper::getStringFromRequest($request, 'tstamp', 'now'));
×
94

95
        $this->incomingConfigurationSelection = RequestHelper::getArrayFromRequest($request, 'configurationSelection');
×
96

97
        $run = new CrawlRun(
×
98
            $scheduledTime,
×
99
            $this->reqMinute,
×
100
            submitUrls: $submitCrawlUrls,
×
101
            collectDownloadUrls: $downloadCrawlUrls,
×
102
            configurationSelection: $this->incomingConfigurationSelection,
×
103
        );
×
104

105
        $queueRows = '';
×
106
        $noConfigurationSelected = $this->isNoConfigurationSelected();
×
107
        if ($noConfigurationSelected) {
×
108
            MessageUtility::addWarningMessage(
×
109
                $this->getLanguageService()->sL('crawler.messages:labels.noConfigSelected')
×
110
            );
×
111
        } else {
112
            if ($submitCrawlUrls) {
×
113
                $reason = new Reason();
×
114
                $reason->setReason(Reason::REASON_GUI_SUBMIT);
×
115
                $reason->setDetailText(
×
116
                    'The user ' . $GLOBALS['BE_USER']->user['username'] . ' added pages to the crawler queue manually'
×
117
                );
×
118
                $this->eventDispatcher->dispatch(new InvokeQueueChangeEvent($reason));
×
119
            }
120

121
            $queueRows = $this->pageTreeUrlCollector->collect($this->pageUid, $crawlingDepth, $run);
×
122
        }
123

124
        // Download Urls to crawl:
125
        $downloadUrls = $run->getDownloadUrls();
×
126
        if ($downloadCrawlUrls) {
×
127
            // Creating output header:
128
            header('Content-Type: application/octet-stream');
×
129
            header('Content-Disposition: attachment; filename=CrawlerUrls.txt');
×
130

131
            // Printing the content of the CSV lines:
132
            echo implode(chr(self::CARRIAGE_RETURN) . chr(self::LINE_FEED), $downloadUrls);
×
133
            exit;
×
134
        }
135

NEW
136
        $this->moduleTemplate->assignMultiple([
×
137
            'currentPageId' => $this->pageUid,
×
138
            'noConfigurationSelected' => $noConfigurationSelected,
×
139
            'submitCrawlUrls' => $submitCrawlUrls,
×
140
            'amountOfUrls' => $run->countHandledUrls(),
×
141
            'selectors' => $this->generateConfigurationSelectors($this->pageUid, (string) $crawlingDepth, $request),
×
142
            'queueRows' => $queueRows,
×
143
            'displayActions' => 0,
×
144
            'actionUrl' => $this->getActionUrl(),
×
145
            'logUrl' => $logUrl,
×
146
        ]);
×
147
    }
148

149
    /**
150
     * Generates the configuration selectors for compiling URLs:
151
     */
152
    private function generateConfigurationSelectors(
153
        int $pageId,
154
        string $crawlingDepth,
155
        ServerRequestInterface $request
156
    ): array {
157
        $selectors = [];
×
158
        $selectors['depth'] = $this->selectorBox(
×
159
            [
×
160
                0 => $this->getLanguageService()->sL('core.core:labels.depth_0'),
×
161
                1 => $this->getLanguageService()->sL('core.core:labels.depth_1'),
×
162
                2 => $this->getLanguageService()->sL('core.core:labels.depth_2'),
×
163
                3 => $this->getLanguageService()->sL('core.core:labels.depth_3'),
×
164
                4 => $this->getLanguageService()->sL('core.core:labels.depth_4'),
×
165
                99 => $this->getLanguageService()->sL('core.core:labels.depth_infi'),
×
166
            ],
×
167
            'crawlingDepth',
×
168
            $crawlingDepth,
×
169
            false
×
170
        );
×
171

172
        // Configurations
173
        $availableConfigurations = $this->pageConfigurationResolver->getConfigurationNamesForBranch(
×
174
            $pageId,
×
175
            (int) $crawlingDepth,
×
176
        );
×
177
        $selectors['configurations'] = $this->selectorBox(
×
178
            empty($availableConfigurations) ? [] : array_combine($availableConfigurations, $availableConfigurations),
×
179
            'configurationSelection',
×
180
            $this->incomingConfigurationSelection,
×
181
            true
×
182
        );
×
183

184
        // Scheduled time:
185
        $selectors['scheduled'] = $this->selectorBox(
×
186
            [
×
187
                'now' => $this->getLanguageService()->sL('crawler.messages:labels.time.now'),
×
188
                'midnight' => $this->getLanguageService()->sL('crawler.messages:labels.time.midnight'),
×
189
                '04:00' => $this->getLanguageService()->sL('crawler.messages:labels.time.4am'),
×
190
            ],
×
191
            'tstamp',
×
192
            RequestHelper::getStringFromRequest($request, 'tstamp', 'now'),
×
193
            false
×
194
        );
×
195

196
        return $selectors;
×
197
    }
198

199
    /**
200
     * Create selector box
201
     *
202
     * @param array $optArray Options key(value) => label pairs
203
     * @param string $name Selector box name
204
     * @param string|array|null $value Selector box value (array for multiple...)
205
     * @param boolean $multiple If set, will draw multiple box.
206
     *
207
     * @return string HTML select element
208
     */
209
    private function selectorBox($optArray, $name, string|array|null $value, bool $multiple): string
210
    {
211
        if (!is_string($value) && !is_array($value)) {
×
212
            $value = '';
×
213
        }
214

215
        $options = [];
×
216
        foreach ($optArray as $key => $val) {
×
217
            $selected = (
×
218
                !$multiple && is_string($value) && !strcmp($value, (string) $key)
×
219
            ) || (
×
220
                $multiple && in_array($key, (array) $value, true)
×
221
            );
×
222
            $options[] = '
×
223
                <option value="' . $key . '" ' . ($selected ? ' selected="selected"' : '') . '>' . htmlspecialchars(
×
224
                (string) $val,
×
225
                ENT_QUOTES | ENT_HTML5
×
226
            ) . '</option>';
×
227
        }
228

229
        return '<select class="form-select" name="' . htmlspecialchars(
×
230
            $name . ($multiple ? '[]' : ''),
×
231
            ENT_QUOTES | ENT_HTML5
×
232
        ) . '"' . ($multiple ? ' multiple' : '') . '>' . implode('', $options) . '</select>';
×
233
    }
234

235
    private function getScheduledTime(string $time): int
236
    {
237
        $scheduledTime = match ($time) {
×
238
            'midnight' => mktime(0, 0, 0),
×
239
            '04:00' => mktime(0, 0, 0) + 4 * 3600,
×
240
            default => time(),
×
241
        };
×
242

243
        if (!$scheduledTime) {
×
244
            return time();
×
245
        }
246

247
        return $scheduledTime;
×
248
    }
249

250
    /**
251
     * @throws RouteNotFoundException
252
     */
253
    private function getActionUrl(): UriInterface
254
    {
255
        return GeneralUtility::makeInstance(UrlBuilder::class)->getBackendModuleUrl([], self::BACKEND_MODULE);
×
256
    }
257

258
    private function isNoConfigurationSelected(): bool
259
    {
260
        return empty($this->incomingConfigurationSelection)
×
261
            || (count($this->incomingConfigurationSelection) === 1 && empty($this->incomingConfigurationSelection[0]));
×
262
    }
263
}
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