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

tomasnorre / crawler / 18366940435

09 Oct 2025 05:55AM UTC coverage: 69.666% (+0.4%) from 69.284%
18366940435

push

github

web-flow
[TASK] Add RequestHelper to cleanup the BackendControllers (#1198)

23 of 46 new or added lines in 4 files covered. (50.0%)

1920 of 2756 relevant lines covered (69.67%)

3.24 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\Controller\CrawlerController;
25
use AOE\Crawler\Domain\Model\Reason;
26
use AOE\Crawler\Event\InvokeQueueChangeEvent;
27
use AOE\Crawler\Utility\MessageUtility;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Message\UriInterface;
31
use Symfony\Contracts\Service\Attribute\Required;
32
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
33
use TYPO3\CMS\Backend\Routing\UriBuilder;
34
use TYPO3\CMS\Backend\Template\ModuleTemplate;
35
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
36
use TYPO3\CMS\Core\Utility\GeneralUtility;
37

38
/**
39
 * @internal since v12.0.0
40
 */
41
class BackendModuleStartCrawlingController extends AbstractBackendModuleController implements BackendModuleControllerInterface
42
{
43
    private const BACKEND_MODULE = 'web_site_crawler_start';
44
    private const LINE_FEED = 10;
45
    private const CARRIAGE_RETURN = 13;
46
    private int $reqMinute = 1000;
47
    private EventDispatcher $eventDispatcher;
48

49
    /**
50
     * @var array holds the selection of configuration from the configuration selector box
51
     */
52
    private $incomingConfigurationSelection = [];
53

54
    public function __construct(
55
        private readonly CrawlerController $crawlerController,
56
        private readonly UriBuilder $backendUriBuilder,
57
    ) {
58
    }
×
59

60
    #[Required]
61
    public function setEventDispatcher(): void
62
    {
63
        $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
×
64
    }
65

66
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
67
    {
68
        $this->makeCrawlerProcessableChecks($this->extensionSettings);
×
69
        $this->pageUid = (int) ($request->getQueryParams()['id'] ?? -1);
×
70
        $this->moduleTemplate = $this->setupView($request, $this->pageUid);
×
71
        $this->moduleTemplate->makeDocHeaderModuleMenu([
×
72
            'id' => $request->getQueryParams()['id'] ?? -1,
×
73
        ]);
×
74

75
        $this->assignValues($request);
×
76
        return $this->moduleTemplate->renderResponse('Backend/ShowCrawlerInformation');
×
77
    }
78

79
    private function assignValues(ServerRequestInterface $request): ModuleTemplate
80
    {
81
        $logUrl = $this->backendUriBuilder->buildUriFromRoute('web_site_crawler_log', [
×
82
            'id' => $this->pageUid,
×
83
        ]);
×
84

NEW
85
        $crawlingDepth = RequestHelper::getIntFromRequest($request, 'crawlingDepth');
×
NEW
86
        $submitCrawlUrls = RequestHelper::getBoolFromRequest($request, '_crawl');
×
NEW
87
        $downloadCrawlUrls = RequestHelper::getBoolFromRequest($request, '_download');
×
NEW
88
        $scheduledTime = $this->getScheduledTime(RequestHelper::getStringFromRequest($request, 'tstamp', 'now'));
×
89

NEW
90
        $this->incomingConfigurationSelection = RequestHelper::getArrayFromRequest($request, 'configurationSelection');
×
91

92
        //$this->crawlerController = $this->getCrawlerController();
93
        $this->crawlerController->setID = GeneralUtility::md5int(microtime());
×
94

95
        $queueRows = '';
×
NEW
96
        $noConfigurationSelected = $this->isNoConfigurationSelected();
×
97
        if ($noConfigurationSelected) {
×
98
            MessageUtility::addWarningMessage(
×
99
                $this->getLanguageService()->sL(
×
100
                    'LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.noConfigSelected'
×
101
                )
×
102
            );
×
103
        } else {
104
            if ($submitCrawlUrls) {
×
105
                $reason = new Reason();
×
106
                $reason->setReason(Reason::REASON_GUI_SUBMIT);
×
107
                $reason->setDetailText(
×
108
                    'The user ' . $GLOBALS['BE_USER']->user['username'] . ' added pages to the crawler queue manually'
×
109
                );
×
110
                $this->eventDispatcher->dispatch(new InvokeQueueChangeEvent($reason));
×
111
            }
112

113
            $queueRows = $this->crawlerController->getPageTreeAndUrls(
×
114
                $this->pageUid,
×
115
                $crawlingDepth,
×
116
                $scheduledTime,
×
117
                $this->reqMinute,
×
118
                $submitCrawlUrls,
×
119
                $downloadCrawlUrls,
×
120
                // Do not filter any processing instructions
121
                [],
×
122
                $this->incomingConfigurationSelection
×
123
            );
×
124
        }
125

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

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

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

151
    /**
152
     * Generates the configuration selectors for compiling URLs:
153
     */
154
    private function generateConfigurationSelectors(
155
        int $pageId,
156
        string $crawlingDepth,
157
        ServerRequestInterface $request
158
    ): array {
159
        $selectors = [];
×
160
        $selectors['depth'] = $this->selectorBox(
×
161
            [
×
162
                0 => $this->getLanguageService()->sL(
×
163
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'
×
164
                ),
×
165
                1 => $this->getLanguageService()->sL(
×
166
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'
×
167
                ),
×
168
                2 => $this->getLanguageService()->sL(
×
169
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'
×
170
                ),
×
171
                3 => $this->getLanguageService()->sL(
×
172
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'
×
173
                ),
×
174
                4 => $this->getLanguageService()->sL(
×
175
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'
×
176
                ),
×
177
                99 => $this->getLanguageService()->sL(
×
178
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'
×
179
                ),
×
180
            ],
×
181
            'crawlingDepth',
×
182
            $crawlingDepth,
×
183
            false
×
184
        );
×
185

186
        // Configurations
187
        $availableConfigurations = $this->crawlerController->getConfigurationsForBranch(
×
188
            $pageId,
×
189
            (int) $crawlingDepth,
×
190
        );
×
191
        $selectors['configurations'] = $this->selectorBox(
×
192
            empty($availableConfigurations) ? [] : array_combine($availableConfigurations, $availableConfigurations),
×
193
            'configurationSelection',
×
194
            $this->incomingConfigurationSelection,
×
195
            true
×
196
        );
×
197

198
        // Scheduled time:
199
        $selectors['scheduled'] = $this->selectorBox(
×
200
            [
×
201
                'now' => $this->getLanguageService()->sL(
×
202
                    'LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.time.now'
×
203
                ),
×
204
                'midnight' => $this->getLanguageService()->sL(
×
205
                    'LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.time.midnight'
×
206
                ),
×
207
                '04:00' => $this->getLanguageService()->sL(
×
208
                    'LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.time.4am'
×
209
                ),
×
210
            ],
×
211
            'tstamp',
×
NEW
212
            RequestHelper::getStringFromRequest($request, 'tstamp', 'now'),
×
213
            false
×
214
        );
×
215

216
        return $selectors;
×
217
    }
218

219
    /**
220
     * Create selector box
221
     *
222
     * @param array $optArray Options key(value) => label pairs
223
     * @param string $name Selector box name
224
     * @param string|array|null $value Selector box value (array for multiple...)
225
     * @param boolean $multiple If set, will draw multiple box.
226
     *
227
     * @return string HTML select element
228
     */
229
    private function selectorBox($optArray, $name, string|array|null $value, bool $multiple): string
230
    {
231
        if (!is_string($value) && !is_array($value)) {
×
232
            $value = '';
×
233
        }
234

235
        $options = [];
×
236
        foreach ($optArray as $key => $val) {
×
237
            $selected = (
×
238
                !$multiple && is_string($value) && !strcmp($value, (string) $key)
×
239
            ) || (
×
240
                $multiple && in_array($key, (array) $value, true)
×
241
            );
×
242
            $options[] = '
×
243
                <option value="' . $key . '" ' . ($selected ? ' selected="selected"' : '') . '>' . htmlspecialchars(
×
244
                (string) $val,
×
245
                ENT_QUOTES | ENT_HTML5
×
246
            ) . '</option>';
×
247
        }
248

249
        return '<select class="form-select" name="' . htmlspecialchars(
×
250
            $name . ($multiple ? '[]' : ''),
×
251
            ENT_QUOTES | ENT_HTML5
×
252
        ) . '"' . ($multiple ? ' multiple' : '') . '>' . implode('', $options) . '</select>';
×
253
    }
254

255
    private function getScheduledTime(string $time): int
256
    {
257
        $scheduledTime = match ($time) {
×
258
            'midnight' => mktime(0, 0, 0),
×
259
            '04:00' => mktime(0, 0, 0) + 4 * 3600,
×
260
            default => time(),
×
261
        };
×
262

263
        if (!$scheduledTime) {
×
264
            return time();
×
265
        }
266

267
        return $scheduledTime;
×
268
    }
269

270
    /**
271
     * @throws RouteNotFoundException
272
     */
273
    private function getActionUrl(): UriInterface
274
    {
275
        return GeneralUtility::makeInstance(UrlBuilder::class)->getBackendModuleUrl([], self::BACKEND_MODULE);
×
276
    }
277

278
    private function isNoConfigurationSelected(): bool
279
    {
NEW
280
        return empty($this->incomingConfigurationSelection)
×
NEW
281
            || (count($this->incomingConfigurationSelection) === 1 && empty($this->incomingConfigurationSelection[0]));
×
282
    }
283
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc