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

tomasnorre / crawler / 3696698329

pending completion
3696698329

Pull #988

github

GitHub
Merge d1d32bd2d into 3f94d6a4e
Pull Request #988: [WIP][FEATURE] Setup new Backend Module

417 of 417 new or added lines in 9 files covered. (100.0%)

1601 of 2523 relevant lines covered (63.46%)

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

36
/**
37
 * @internal since v12.0.0
38
 */
39
final class BackendModuleStartCrawlingController extends AbstractBackendModuleController implements BackendModuleControllerInterface
40
{
41
    private const BACKEND_MODULE = 'web_site_crawler_start';
42
    private int $reqMinute = 1000;
43
    private EventDispatcher $eventDispatcher;
44

45
    /**
46
     * @var array holds the selection of configuration from the configuration selector box
47
     */
48
    private $incomingConfigurationSelection = [];
49

50
    public function __construct(
51
        private CrawlerController $crawlerController
52
    ) {
53
    }
×
54

55
    #[Required]
56
    public function setEventDispatcher(): void
57
    {
58
        $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
×
59
    }
60

61
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
62
    {
63
        $this->makeCrawlerProcessableChecks($this->extensionSettings);
×
64
        $this->pageUid = (int) ($request->getQueryParams()['id'] ?? -1);
×
65
        $this->moduleTemplate = $this->setupView($request, $this->pageUid);
×
66
        $this->moduleTemplate = $this->moduleTemplate->makeDocHeaderModuleMenu(
×
67
            ['id' => $request->getQueryParams()['id'] ?? -1]
×
68
        );
×
69
        $this->moduleTemplate = $this->assignValues();
×
70

71
        return $this->moduleTemplate->renderResponse('Backend/ShowCrawlerInformation');
×
72
    }
73

74
    private function assignValues(): ModuleTemplate
75
    {
76
        $backendUriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
×
77
        $logUrl = $backendUriBuilder->buildUriFromRoute('web_site_crawler_log', ['id' => $this->pageUid]);
×
78

79
        $crawlingDepth = GeneralUtility::_GP('crawlingDepth') ?? '0';
×
80
        $crawlParameter = GeneralUtility::_GP('_crawl');
×
81
        $downloadParameter = GeneralUtility::_GP('_download');
×
82
        $scheduledTime = $this->getScheduledTime((string) GeneralUtility::_GP('tstamp'));
×
83
        $submitCrawlUrls = isset($crawlParameter);
×
84
        $downloadCrawlUrls = isset($downloadParameter);
×
85

86
        $this->incomingConfigurationSelection = GeneralUtility::_GP('configurationSelection');
×
87
        $this->incomingConfigurationSelection = is_array(
×
88
            $this->incomingConfigurationSelection
×
89
        ) ? $this->incomingConfigurationSelection : [];
×
90

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

94
        $queueRows = '';
×
95
        $noConfigurationSelected = empty($this->incomingConfigurationSelection)
×
96
            || (count($this->incomingConfigurationSelection) === 1 && empty($this->incomingConfigurationSelection[0]));
×
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(13) . chr(10), $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 ?? []),
×
143
            'selectors' => $this->generateConfigurationSelectors($this->pageUid, $crawlingDepth),
×
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(int $pageId, string $crawlingDepth): array
155
    {
156
        $selectors = [];
×
157
        $selectors['depth'] = $this->selectorBox(
×
158
            [
×
159
                0 => $this->getLanguageService()->sL(
×
160
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'
×
161
                ),
×
162
                1 => $this->getLanguageService()->sL(
×
163
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'
×
164
                ),
×
165
                2 => $this->getLanguageService()->sL(
×
166
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'
×
167
                ),
×
168
                3 => $this->getLanguageService()->sL(
×
169
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'
×
170
                ),
×
171
                4 => $this->getLanguageService()->sL(
×
172
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'
×
173
                ),
×
174
                99 => $this->getLanguageService()->sL(
×
175
                    'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'
×
176
                ),
×
177
            ],
×
178
            'crawlingDepth',
×
179
            $crawlingDepth,
×
180
            false
×
181
        );
×
182

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

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

213
        return $selectors;
×
214
    }
215

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

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

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

252
    /**
253
     * @return int
254
     */
255
    private function getScheduledTime(string $time)
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(): Uri
274
    {
275
        return GeneralUtility::makeInstance(UrlBuilder::class)->getBackendModuleUrl([], self::BACKEND_MODULE);
×
276
    }
277
}
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

© 2025 Coveralls, Inc