Passed
Push — github-actions-codeception ( 9f1860...481159 )
by Tomas Norre
06:54
created

CrawlerController::readUrl()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 89
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 11.6653

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 53
c 3
b 0
f 0
nc 18
nop 2
dl 0
loc 89
ccs 42
cts 51
cp 0.8235
crap 11.6653
rs 6.8787

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Controller;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2020 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
32
use AOE\Crawler\Converter\JsonCompatibilityConverter;
33
use AOE\Crawler\Crawler;
34
use AOE\Crawler\CrawlStrategy\CrawlStrategyFactory;
35
use AOE\Crawler\Domain\Model\Process;
36
use AOE\Crawler\Domain\Repository\ConfigurationRepository;
37
use AOE\Crawler\Domain\Repository\ProcessRepository;
38
use AOE\Crawler\Domain\Repository\QueueRepository;
39
use AOE\Crawler\QueueExecutor;
40
use AOE\Crawler\Service\ConfigurationService;
41
use AOE\Crawler\Service\UrlService;
42
use AOE\Crawler\Service\UserService;
43
use AOE\Crawler\Utility\SignalSlotUtility;
44
use AOE\Crawler\Value\QueueFilter;
45
use PDO;
46
use Psr\Http\Message\UriInterface;
47
use Psr\Log\LoggerAwareInterface;
48
use Psr\Log\LoggerAwareTrait;
49
use TYPO3\CMS\Backend\Tree\View\PageTreeView;
50
use TYPO3\CMS\Backend\Utility\BackendUtility;
51
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
52
use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
53
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
54
use TYPO3\CMS\Core\Core\Bootstrap;
55
use TYPO3\CMS\Core\Core\Environment;
56
use TYPO3\CMS\Core\Database\Connection;
57
use TYPO3\CMS\Core\Database\ConnectionPool;
58
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
59
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
60
use TYPO3\CMS\Core\Database\QueryGenerator;
61
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
62
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
63
use TYPO3\CMS\Core\Imaging\Icon;
64
use TYPO3\CMS\Core\Imaging\IconFactory;
65
use TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException;
66
use TYPO3\CMS\Core\Site\Entity\Site;
67
use TYPO3\CMS\Core\Type\Bitmask\Permission;
68
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
69
use TYPO3\CMS\Core\Utility\DebugUtility;
70
use TYPO3\CMS\Core\Utility\GeneralUtility;
71
use TYPO3\CMS\Core\Utility\MathUtility;
72
use TYPO3\CMS\Extbase\Object\ObjectManager;
73
74
/**
75
 * Class CrawlerController
76
 *
77
 * @package AOE\Crawler\Controller
78
 */
79
class CrawlerController implements LoggerAwareInterface
80
{
81
    use LoggerAwareTrait;
82
    use PublicMethodDeprecationTrait;
83
    use PublicPropertyDeprecationTrait;
84
85
    public const CLI_STATUS_NOTHING_PROCCESSED = 0;
86
87
    //queue not empty
88
    public const CLI_STATUS_REMAIN = 1;
89
90
    //(some) queue items where processed
91
    public const CLI_STATUS_PROCESSED = 2;
92
93
    //instance didn't finish
94
    public const CLI_STATUS_ABORTED = 4;
95
96
    public const CLI_STATUS_POLLABLE_PROCESSED = 8;
97
98
    /**
99
     * @var integer
100
     */
101
    public $setID = 0;
102
103
    /**
104
     * @var string
105
     */
106
    public $processID = '';
107
108
    /**
109
     * @var array
110
     */
111
    public $duplicateTrack = [];
112
113
    /**
114
     * @var array
115
     */
116
    public $downloadUrls = [];
117
118
    /**
119
     * @var array
120
     */
121
    public $incomingProcInstructions = [];
122
123
    /**
124
     * @var array
125
     */
126
    public $incomingConfigurationSelection = [];
127
128
    /**
129
     * @var bool
130
     */
131
    public $registerQueueEntriesInternallyOnly = false;
132
133
    /**
134
     * @var array
135
     */
136
    public $queueEntries = [];
137
138
    /**
139
     * @var array
140
     */
141
    public $urlList = [];
142
143
    /**
144
     * @var array
145
     */
146
    public $extensionSettings = [];
147
148
    /**
149
     * Mount Point
150
     *
151
     * @var bool
152
     * Todo: Check what this is used for and adjust the type hint or code, as bool doesn't match the current code.
153
     */
154
    public $MP = false;
155
156
    /**
157
     * @var string
158
     * @deprecated
159
     */
160
    protected $processFilename;
161
162
    /**
163
     * Holds the internal access mode can be 'gui','cli' or 'cli_im'
164
     *
165
     * @var string
166
     * @deprecated
167
     */
168
    protected $accessMode;
169
170
    /**
171
     * @var QueueRepository
172
     */
173
    protected $queueRepository;
174
175
    /**
176
     * @var ProcessRepository
177
     */
178
    protected $processRepository;
179
180
    /**
181
     * @var ConfigurationRepository
182
     */
183
    protected $configurationRepository;
184
185
    /**
186
     * @var string
187
     */
188
    protected $tableName = 'tx_crawler_queue';
189
190
    /**
191
     * @var QueueExecutor
192
     */
193
    protected $queueExecutor;
194
195
    /**
196
     * @var int
197
     */
198
    protected $maximumUrlsToCompile = 10000;
199
200
    /**
201
     * @var IconFactory
202
     */
203
    protected $iconFactory;
204
205
    /**
206
     * @var string[]
207
     */
208
    private $deprecatedPublicMethods = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicMethods is not used, and could be removed.
Loading history...
209
        'cleanUpOldQueueEntries' => 'Using CrawlerController::cleanUpOldQueueEntries() is deprecated since 9.0.1 and will be removed in v11.x, please use QueueRepository->cleanUpOldQueueEntries() instead.',
210
        'CLI_debug' => 'Using CrawlerController->CLI_debug() is deprecated since 9.1.3 and will be removed in v11.x',
211
        'CLI_releaseProcesses' => 'Using CrawlerController->CLI_releaseProcesses() is deprecated since 9.2.2 and will be removed in v11.x',
212
        'CLI_runHooks' => 'Using CrawlerController->CLI_runHooks() is deprecated since 9.1.5 and will be removed in v11.x',
213
        'getAccessMode' => 'Using CrawlerController->getAccessMode() is deprecated since 9.1.3 and will be removed in v11.x',
214
        'getLogEntriesForPageId' => 'Using CrawlerController->getLogEntriesForPageId() is deprecated since 9.1.5 and will be remove in v11.x',
215
        'getLogEntriesForSetId' => 'Using crawlerController::getLogEntriesForSetId() is deprecated since 9.0.1 and will be removed in v11.x',
216
        'hasGroupAccess' => 'Using CrawlerController->getLogEntriesForPageId() is deprecated since 9.2.2 and will be remove in v11.x, please use UserService::hasGroupAccess() instead.',
217
        'flushQueue' => 'Using CrawlerController::flushQueue() is deprecated since 9.0.1 and will be removed in v11.x, please use QueueRepository->flushQueue() instead.',
218
        'setAccessMode' => 'Using CrawlerController->setAccessMode() is deprecated since 9.1.3 and will be removed in v11.x',
219
        'getDisabled' => 'Using CrawlerController->getDisabled() is deprecated since 9.1.3 and will be removed in v11.x, please use Crawler->isDisabled() instead',
220
        'setDisabled' => 'Using CrawlerController->setDisabled() is deprecated since 9.1.3 and will be removed in v11.x, please use Crawler->setDisabled() instead',
221
        'getProcessFilename' => 'Using CrawlerController->getProcessFilename() is deprecated since 9.1.3 and will be removed in v11.x',
222
        'setProcessFilename' => 'Using CrawlerController->setProcessFilename() is deprecated since 9.1.3 and will be removed in v11.x',
223
        'getDuplicateRowsIfExist' => 'Using CrawlerController->getDuplicateRowsIfExist() is deprecated since 9.1.4 and will be remove in v11.x, please use QueueRepository->getDuplicateQueueItemsIfExists() instead',
224
    ];
225
226
    /**
227
     * @var string[]
228
     */
229
    private $deprecatedPublicProperties = [
230
        'accessMode' => 'Using CrawlerController->accessMode is deprecated since 9.1.3 and will be removed in v11.x',
231
        'processFilename' => 'Using CrawlerController->accessMode is deprecated since 9.1.3 and will be removed in v11.x',
232
    ];
233
234
    /**
235
     * @var BackendUserAuthentication|null
236
     */
237
    private $backendUser;
238
239
    /**
240
     * @var integer
241
     */
242
    private $scheduledTime = 0;
243
244
    /**
245
     * @var integer
246
     */
247
    private $reqMinute = 0;
248
249
    /**
250
     * @var bool
251
     */
252
    private $submitCrawlUrls = false;
253
254
    /**
255
     * @var bool
256
     */
257
    private $downloadCrawlUrls = false;
258
259
    /**
260
     * @var PageRepository
261
     */
262
    private $pageRepository;
263
264
    /**
265
     * @var Crawler
266
     */
267
    private $crawler;
268
269
    /************************************
270
     *
271
     * Getting URLs based on Page TSconfig
272
     *
273
     ************************************/
274
275 41
    public function __construct()
276
    {
277 41
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
278 41
        $crawlStrategyFactory = GeneralUtility::makeInstance(CrawlStrategyFactory::class);
279 41
        $this->queueRepository = $objectManager->get(QueueRepository::class);
280 41
        $this->processRepository = $objectManager->get(ProcessRepository::class);
281 41
        $this->configurationRepository = $objectManager->get(ConfigurationRepository::class);
282 41
        $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
283 41
        $this->queueExecutor = GeneralUtility::makeInstance(QueueExecutor::class, $crawlStrategyFactory);
284 41
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
285 41
        $this->crawler = GeneralUtility::makeInstance(Crawler::class);
286
287 41
        $this->processFilename = Environment::getVarPath() . '/lock/tx_crawler.proc';
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

287
        /** @scrutinizer ignore-deprecated */ $this->processFilename = Environment::getVarPath() . '/lock/tx_crawler.proc';
Loading history...
288
289
        /** @var ExtensionConfigurationProvider $configurationProvider */
290 41
        $configurationProvider = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class);
291 41
        $settings = $configurationProvider->getExtensionConfiguration();
292 41
        $this->extensionSettings = is_array($settings) ? $settings : [];
0 ignored issues
show
introduced by
The condition is_array($settings) is always true.
Loading history...
293
294
        // set defaults:
295 41
        if (MathUtility::convertToPositiveInteger($this->extensionSettings['countInARun']) === 0) {
296
            $this->extensionSettings['countInARun'] = 100;
297
        }
298
299 41
        $this->extensionSettings['processLimit'] = MathUtility::forceIntegerInRange($this->extensionSettings['processLimit'], 1, 99, 1);
300 41
        $this->setMaximumUrlsToCompile(MathUtility::forceIntegerInRange($this->extensionSettings['maxCompileUrls'], 1, 1000000000, 10000));
301 41
    }
302
303 45
    public function setMaximumUrlsToCompile(int $maximumUrlsToCompile): void
304
    {
305 45
        $this->maximumUrlsToCompile = $maximumUrlsToCompile;
306 45
    }
307
308
    /**
309
     * Method to set the accessMode can be gui, cli or cli_im
310
     *
311
     * @return string
312
     * @deprecated
313
     */
314 1
    public function getAccessMode()
315
    {
316 1
        return $this->accessMode;
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...Controller::$accessMode has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

316
        return /** @scrutinizer ignore-deprecated */ $this->accessMode;
Loading history...
317
    }
318
319
    /**
320
     * @param string $accessMode
321
     * @deprecated
322
     */
323 1
    public function setAccessMode($accessMode): void
324
    {
325 1
        $this->accessMode = $accessMode;
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...Controller::$accessMode has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

325
        /** @scrutinizer ignore-deprecated */ $this->accessMode = $accessMode;
Loading history...
326 1
    }
327
328
    /**
329
     * Set disabled status to prevent processes from being processed
330
     * @deprecated
331
     */
332 3
    public function setDisabled(?bool $disabled = true): void
333
    {
334 3
        if ($disabled) {
335 2
            GeneralUtility::writeFile($this->processFilename, 'disabled');
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

335
            GeneralUtility::writeFile(/** @scrutinizer ignore-deprecated */ $this->processFilename, 'disabled');
Loading history...
336 1
        } elseif (is_file($this->processFilename)) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

336
        } elseif (is_file(/** @scrutinizer ignore-deprecated */ $this->processFilename)) {
Loading history...
337 1
            unlink($this->processFilename);
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

337
            unlink(/** @scrutinizer ignore-deprecated */ $this->processFilename);
Loading history...
338
        }
339 3
    }
340
341
    /**
342
     * Get disable status
343
     * @deprecated
344
     */
345 3
    public function getDisabled(): bool
346
    {
347 3
        return is_file($this->processFilename);
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

347
        return is_file(/** @scrutinizer ignore-deprecated */ $this->processFilename);
Loading history...
348
    }
349
350
    /**
351
     * @param string $filenameWithPath
352
     * @deprecated
353
     */
354 4
    public function setProcessFilename($filenameWithPath): void
355
    {
356 4
        $this->processFilename = $filenameWithPath;
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

356
        /** @scrutinizer ignore-deprecated */ $this->processFilename = $filenameWithPath;
Loading history...
357 4
    }
358
359
    /**
360
     * @return string
361
     * @deprecated
362
     */
363 1
    public function getProcessFilename()
364
    {
365 1
        return $this->processFilename;
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Controller\C...oller::$processFilename has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

365
        return /** @scrutinizer ignore-deprecated */ $this->processFilename;
Loading history...
366
    }
367
368
    /**
369
     * Sets the extensions settings (unserialized pendant of $TYPO3_CONF_VARS['EXT']['extConf']['crawler']).
370
     */
371 14
    public function setExtensionSettings(array $extensionSettings): void
372
    {
373 14
        $this->extensionSettings = $extensionSettings;
374 14
    }
375
376
    /**
377
     * Check if the given page should be crawled
378
     *
379
     * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped
380
     */
381 15
    public function checkIfPageShouldBeSkipped(array $pageRow)
382
    {
383
        // if page is hidden
384 15
        if (! $this->extensionSettings['crawlHiddenPages'] && $pageRow['hidden']) {
385 1
            return 'Because page is hidden';
386
        }
387
388 14
        if (GeneralUtility::inList('3,4,199,254,255', $pageRow['doktype'])) {
389 3
            return 'Because doktype is not allowed';
390
        }
391
392 11
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['excludeDoktype'] ?? [] as $key => $doktypeList) {
393 1
            if (GeneralUtility::inList($doktypeList, $pageRow['doktype'])) {
394 1
                return 'Doktype was excluded by "' . $key . '"';
395
            }
396
        }
397
398
        // veto hook
399 10
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pageVeto'] ?? [] as $key => $func) {
400
            $params = [
401 2
                'pageRow' => $pageRow,
402
            ];
403
            // expects "false" if page is ok and "true" or a skipMessage if this page should _not_ be crawled
404 2
            $veto = GeneralUtility::callUserFunction($func, $params, $this);
405 2
            if ($veto !== false) {
406 2
                if (is_string($veto)) {
407 1
                    return $veto;
408
                }
409 1
                return 'Veto from hook "' . htmlspecialchars($key) . '"';
410
            }
411
        }
412
413 8
        return false;
414
    }
415
416
    /**
417
     * Wrapper method for getUrlsForPageId()
418
     * It returns an array of configurations and no urls!
419
     *
420
     * @param array $pageRow Page record with at least dok-type and uid columns.
421
     * @param string $skipMessage
422
     * @return array
423
     * @see getUrlsForPageId()
424
     */
425 9
    public function getUrlsForPageRow(array $pageRow, &$skipMessage = '')
426
    {
427 9
        if (! is_int($pageRow['uid'])) {
428
            $skipMessage = 'PageUid ' . $pageRow['uid'] . ' was not an integer';
429
            return [];
430
        }
431
432 9
        $message = $this->checkIfPageShouldBeSkipped($pageRow);
433 9
        if ($message === false) {
434 8
            $res = $this->getUrlsForPageId($pageRow['uid']);
435 8
            $skipMessage = '';
436
        } else {
437 1
            $skipMessage = $message;
438 1
            $res = [];
439
        }
440
441 9
        return $res;
442
    }
443
444
    /**
445
     * Creates a list of URLs from input array (and submits them to queue if asked for)
446
     * See Web > Info module script + "indexed_search"'s crawler hook-client using this!
447
     *
448
     * @param array $vv Information about URLs from pageRow to crawl.
449
     * @param array $pageRow Page row
450
     * @param int $scheduledTime Unix time to schedule indexing to, typically time()
451
     * @param int $reqMinute Number of requests per minute (creates the interleave between requests)
452
     * @param bool $submitCrawlUrls If set, submits the URLs to queue
453
     * @param bool $downloadCrawlUrls If set (and submitcrawlUrls is false) will fill $downloadUrls with entries)
454
     * @param array $duplicateTrack Array which is passed by reference and contains the an id per url to secure we will not crawl duplicates
455
     * @param array $downloadUrls Array which will be filled with URLS for download if flag is set.
456
     * @param array $incomingProcInstructions Array of processing instructions
457
     * @return string List of URLs (meant for display in backend module)
458
     */
459 7
    public function urlListFromUrlArray(
460
        array $vv,
461
        array $pageRow,
462
        $scheduledTime,
463
        $reqMinute,
464
        $submitCrawlUrls,
465
        $downloadCrawlUrls,
466
        array &$duplicateTrack,
467
        array &$downloadUrls,
468
        array $incomingProcInstructions
469
    ) {
470 7
        if (! is_array($vv['URLs'])) {
471
            return 'ERROR - no URL generated';
472
        }
473 7
        $urlLog = [];
474 7
        $pageId = (int) $pageRow['uid'];
475 7
        $configurationHash = $this->getConfigurationHash($vv);
476 7
        $skipInnerCheck = $this->queueRepository->noUnprocessedQueueEntriesForPageWithConfigurationHashExist($pageId, $configurationHash);
477
478 7
        $urlService = new UrlService();
479
480 7
        foreach ($vv['URLs'] as $urlQuery) {
481 7
            if (! $this->drawURLs_PIfilter($vv['subCfg']['procInstrFilter'], $incomingProcInstructions)) {
482
                continue;
483
            }
484 7
            $url = (string) $urlService->getUrlFromPageAndQueryParameters(
485 7
                $pageId,
486 7
                $urlQuery,
487 7
                $vv['subCfg']['baseUrl'] ?? null,
488 7
                $vv['subCfg']['force_ssl'] ?? 0
489
            );
490
491
            // Create key by which to determine unique-ness:
492 7
            $uKey = $url . '|' . $vv['subCfg']['userGroups'] . '|' . $vv['subCfg']['procInstrFilter'];
493
494 7
            if (isset($duplicateTrack[$uKey])) {
495
                //if the url key is registered just display it and do not resubmit is
496
                $urlLog[] = '<em><span class="text-muted">' . htmlspecialchars($url) . '</span></em>';
497
            } else {
498
                // Scheduled time:
499 7
                $schTime = $scheduledTime + round(count($duplicateTrack) * (60 / $reqMinute));
500 7
                $schTime = intval($schTime / 60) * 60;
501 7
                $formattedDate = BackendUtility::datetime($schTime);
502 7
                $this->urlList[] = '[' . $formattedDate . '] ' . $url;
503 7
                $urlList = '[' . $formattedDate . '] ' . htmlspecialchars($url);
504
505
                // Submit for crawling!
506 7
                if ($submitCrawlUrls) {
507 7
                    $added = $this->addUrl(
508 7
                        $pageId,
509 7
                        $url,
510 7
                        $vv['subCfg'],
511 7
                        $scheduledTime,
512 7
                        $configurationHash,
513 7
                        $skipInnerCheck
514
                    );
515 7
                    if ($added === false) {
516 7
                        $urlList .= ' (URL already existed)';
517
                    }
518
                } elseif ($downloadCrawlUrls) {
519
                    $downloadUrls[$url] = $url;
520
                }
521 7
                $urlLog[] = $urlList;
522
            }
523 7
            $duplicateTrack[$uKey] = true;
524
        }
525
526 7
        return implode('<br>', $urlLog);
527
    }
528
529
    /**
530
     * Returns true if input processing instruction is among registered ones.
531
     *
532
     * @param string $piString PI to test
533
     * @param array $incomingProcInstructions Processing instructions
534
     * @return boolean
535
     */
536 8
    public function drawURLs_PIfilter($piString, array $incomingProcInstructions)
537
    {
538 8
        if (empty($incomingProcInstructions)) {
539 4
            return true;
540
        }
541
542 4
        foreach ($incomingProcInstructions as $pi) {
543 4
            if (GeneralUtility::inList($piString, $pi)) {
544 2
                return true;
545
            }
546
        }
547 2
        return false;
548
    }
549
550 9
    public function getPageTSconfigForId($id): array
551
    {
552 9
        if (! $this->MP) {
553 9
            $pageTSconfig = BackendUtility::getPagesTSconfig($id);
554
        } else {
555
            // TODO: Please check, this makes no sense to split a boolean value.
556
            [, $mountPointId] = explode('-', $this->MP);
0 ignored issues
show
Bug introduced by
$this->MP of type true is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

556
            [, $mountPointId] = explode('-', /** @scrutinizer ignore-type */ $this->MP);
Loading history...
557
            $pageTSconfig = BackendUtility::getPagesTSconfig($mountPointId);
0 ignored issues
show
Bug introduced by
$mountPointId of type string is incompatible with the type integer expected by parameter $id of TYPO3\CMS\Backend\Utilit...ity::getPagesTSconfig(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

557
            $pageTSconfig = BackendUtility::getPagesTSconfig(/** @scrutinizer ignore-type */ $mountPointId);
Loading history...
558
        }
559
560
        // Call a hook to alter configuration
561 9
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'])) {
562
            $params = [
563
                'pageId' => $id,
564
                'pageTSConfig' => &$pageTSconfig,
565
            ];
566
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'] as $userFunc) {
567
                GeneralUtility::callUserFunction($userFunc, $params, $this);
568
            }
569
        }
570 9
        return $pageTSconfig;
571
    }
572
573
    /**
574
     * This methods returns an array of configurations.
575
     * Adds no urls!
576
     */
577 7
    public function getUrlsForPageId(int $pageId): array
578
    {
579
        // Get page TSconfig for page ID
580 7
        $pageTSconfig = $this->getPageTSconfigForId($pageId);
581
582 7
        $res = [];
583
584
        // Fetch Crawler Configuration from pageTSconfig
585 7
        $crawlerCfg = $pageTSconfig['tx_crawler.']['crawlerCfg.']['paramSets.'] ?? [];
586 7
        foreach ($crawlerCfg as $key => $values) {
587 6
            if (! is_array($values)) {
588 6
                continue;
589
            }
590 6
            $key = str_replace('.', '', $key);
591
            // Sub configuration for a single configuration string:
592 6
            $subCfg = (array) $crawlerCfg[$key . '.'];
593 6
            $subCfg['key'] = $key;
594
595 6
            if (strcmp($subCfg['procInstrFilter'] ?? '', '')) {
596 6
                $subCfg['procInstrFilter'] = implode(',', GeneralUtility::trimExplode(',', $subCfg['procInstrFilter']));
597
            }
598 6
            $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $subCfg['pidsOnly'], true));
599
600
            // process configuration if it is not page-specific or if the specific page is the current page:
601
            // TODO: Check if $pidOnlyList can be kept as Array instead of imploded
602 6
            if (! strcmp((string) $subCfg['pidsOnly'], '') || GeneralUtility::inList($pidOnlyList, strval($pageId))) {
603
604
                // Explode, process etc.:
605 6
                $res[$key] = [];
606 6
                $res[$key]['subCfg'] = $subCfg;
607 6
                $res[$key]['paramParsed'] = GeneralUtility::explodeUrl2Array($crawlerCfg[$key]);
608 6
                $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $pageId);
609 6
                $res[$key]['origin'] = 'pagets';
610
611
                // recognize MP value
612 6
                if (! $this->MP) {
613 6
                    $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId]);
614
                } else {
615
                    $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId . '&MP=' . $this->MP]);
0 ignored issues
show
Bug introduced by
Are you sure $this->MP of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

615
                    $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId . '&MP=' . /** @scrutinizer ignore-type */ $this->MP]);
Loading history...
616
                }
617
            }
618
        }
619
620
        // Get configuration from tx_crawler_configuration records up the rootline
621 7
        $crawlerConfigurations = $this->configurationRepository->getCrawlerConfigurationRecordsFromRootLine($pageId);
622 7
        foreach ($crawlerConfigurations as $configurationRecord) {
623
624
            // check access to the configuration record
625 1
            if (empty($configurationRecord['begroups']) || $this->getBackendUser()->isAdmin() || UserService::hasGroupAccess($this->getBackendUser()->user['usergroup_cached_list'], $configurationRecord['begroups'])) {
626 1
                $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $configurationRecord['pidsonly'], true));
627
628
                // process configuration if it is not page-specific or if the specific page is the current page:
629
                // TODO: Check if $pidOnlyList can be kept as Array instead of imploded
630 1
                if (! strcmp($configurationRecord['pidsonly'], '') || GeneralUtility::inList($pidOnlyList, strval($pageId))) {
631 1
                    $key = $configurationRecord['name'];
632
633
                    // don't overwrite previously defined paramSets
634 1
                    if (! isset($res[$key])) {
635
636
                        /* @var $TSparserObject TypoScriptParser */
637 1
                        $TSparserObject = GeneralUtility::makeInstance(TypoScriptParser::class);
638 1
                        $TSparserObject->parse($configurationRecord['processing_instruction_parameters_ts']);
639
640
                        $subCfg = [
641 1
                            'procInstrFilter' => $configurationRecord['processing_instruction_filter'],
642 1
                            'procInstrParams.' => $TSparserObject->setup,
643 1
                            'baseUrl' => $configurationRecord['base_url'],
644 1
                            'force_ssl' => (int) $configurationRecord['force_ssl'],
645 1
                            'userGroups' => $configurationRecord['fegroups'],
646 1
                            'exclude' => $configurationRecord['exclude'],
647 1
                            'key' => $key,
648
                        ];
649
650 1
                        if (! in_array($pageId, $this->expandExcludeString($subCfg['exclude']), true)) {
651 1
                            $res[$key] = [];
652 1
                            $res[$key]['subCfg'] = $subCfg;
653 1
                            $res[$key]['paramParsed'] = GeneralUtility::explodeUrl2Array($configurationRecord['configuration']);
654 1
                            $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $pageId);
655 1
                            $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId]);
656 1
                            $res[$key]['origin'] = 'tx_crawler_configuration_' . $configurationRecord['uid'];
657
                        }
658
                    }
659
                }
660
            }
661
        }
662
663 7
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'] ?? [] as $func) {
664
            $params = [
665
                'res' => &$res,
666
            ];
667
            GeneralUtility::callUserFunction($func, $params, $this);
668
        }
669 7
        return $res;
670
    }
671
672
    /**
673
     * Find all configurations of subpages of a page
674
     * TODO: Write Functional Tests
675
     */
676 2
    public function getConfigurationsForBranch(int $rootid, int $depth): array
677
    {
678 2
        $configurationsForBranch = [];
679 2
        $pageTSconfig = $this->getPageTSconfigForId($rootid);
680 2
        $sets = $pageTSconfig['tx_crawler.']['crawlerCfg.']['paramSets.'] ?? [];
681 2
        foreach ($sets as $key => $value) {
682
            if (! is_array($value)) {
683
                continue;
684
            }
685
            $configurationsForBranch[] = substr($key, -1) === '.' ? substr($key, 0, -1) : $key;
686
        }
687 2
        $pids = [];
688 2
        $rootLine = BackendUtility::BEgetRootLine($rootid);
689 2
        foreach ($rootLine as $node) {
690 1
            $pids[] = $node['uid'];
691
        }
692
        /* @var PageTreeView $tree */
693 2
        $tree = GeneralUtility::makeInstance(PageTreeView::class);
694 2
        $perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
695 2
        $tree->init(empty($perms_clause) ? '' : ('AND ' . $perms_clause));
696 2
        $tree->getTree($rootid, $depth, '');
697 2
        foreach ($tree->tree as $node) {
698
            $pids[] = $node['row']['uid'];
699
        }
700
701 2
        $queryBuilder = $this->getQueryBuilder('tx_crawler_configuration');
702
        $statement = $queryBuilder
703 2
            ->select('name')
704 2
            ->from('tx_crawler_configuration')
705 2
            ->where(
706 2
                $queryBuilder->expr()->in('pid', $queryBuilder->createNamedParameter($pids, Connection::PARAM_INT_ARRAY))
707
            )
708 2
            ->execute();
709
710 2
        while ($row = $statement->fetch()) {
711 1
            $configurationsForBranch[] = $row['name'];
712
        }
713 2
        return $configurationsForBranch;
714
    }
715
716
    /**
717
     * Check if a user has access to an item
718
     * (e.g. get the group list of the current logged in user from $GLOBALS['TSFE']->gr_list)
719
     *
720
     * @param string $groupList Comma-separated list of (fe_)group UIDs from a user
721
     * @param string $accessList Comma-separated list of (fe_)group UIDs of the item to access
722
     * @return bool TRUE if at least one of the users group UIDs is in the access list or the access list is empty
723
     * @see \TYPO3\CMS\Frontend\Page\PageRepository::getMultipleGroupsWhereClause()
724
     * @deprecated
725
     * @codeCoverageIgnore
726
     */
727
    public function hasGroupAccess($groupList, $accessList)
728
    {
729
        if (empty($accessList)) {
730
            return true;
731
        }
732
        foreach (GeneralUtility::intExplode(',', $groupList) as $groupUid) {
733
            if (GeneralUtility::inList($accessList, $groupUid)) {
734
                return true;
735
            }
736
        }
737
        return false;
738
    }
739
740
    /**
741
     * Will expand the parameters configuration to individual values. This follows a certain syntax of the value of each parameter.
742
     * Syntax of values:
743
     * - Basically: If the value is wrapped in [...] it will be expanded according to the following syntax, otherwise the value is taken literally
744
     * - Configuration is splitted by "|" and the parts are processed individually and finally added together
745
     * - For each configuration part:
746
     *         - "[int]-[int]" = Integer range, will be expanded to all values in between, values included, starting from low to high (max. 1000). Example "1-34" or "-40--30"
747
     *         - "_TABLE:[TCA table name];[_PID:[optional page id, default is current page]];[_ENABLELANG:1]" = Look up of table records from PID, filtering out deleted records. Example "_TABLE:tt_content; _PID:123"
748
     *        _ENABLELANG:1 picks only original records without their language overlays
749
     *         - Default: Literal value
750
     *
751
     * @param array $paramArray Array with key (GET var name) and values (value of GET var which is configuration for expansion)
752
     * @param integer $pid Current page ID
753
     * @return array
754
     *
755
     * TODO: Write Functional Tests
756
     */
757 14
    public function expandParameters($paramArray, $pid)
758
    {
759
        // Traverse parameter names:
760 14
        foreach ($paramArray as $p => $v) {
761 14
            $v = trim($v);
762
763
            // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal
764 14
            if (strpos($v, '[') === 0 && substr($v, -1) === ']') {
765
                // So, find the value inside brackets and reset the paramArray value as an array.
766 14
                $v = substr($v, 1, -1);
767 14
                $paramArray[$p] = [];
768
769
                // Explode parts and traverse them:
770 14
                $parts = explode('|', $v);
771 14
                foreach ($parts as $pV) {
772
773
                    // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30)
774 14
                    if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($pV), $reg)) {
775 1
                        $reg = $this->swapIfFirstIsLargerThanSecond($reg);
776
777
                        // Traverse range, add values:
778
                        // Limit to size of range!
779 1
                        $runAwayBrake = 1000;
780 1
                        for ($a = $reg[1]; $a <= $reg[2]; $a++) {
781 1
                            $paramArray[$p][] = $a;
782 1
                            $runAwayBrake--;
783 1
                            if ($runAwayBrake <= 0) {
784
                                break;
785
                            }
786
                        }
787 13
                    } elseif (strpos(trim($pV), '_TABLE:') === 0) {
788
789
                        // Parse parameters:
790 6
                        $subparts = GeneralUtility::trimExplode(';', $pV);
791 6
                        $subpartParams = [];
792 6
                        foreach ($subparts as $spV) {
793 6
                            [$pKey, $pVal] = GeneralUtility::trimExplode(':', $spV);
794 6
                            $subpartParams[$pKey] = $pVal;
795
                        }
796
797
                        // Table exists:
798 6
                        if (isset($GLOBALS['TCA'][$subpartParams['_TABLE']])) {
799 6
                            $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : intval($pid);
800 6
                            $recursiveDepth = isset($subpartParams['_RECURSIVE']) ? intval($subpartParams['_RECURSIVE']) : 0;
801 6
                            $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid';
802 6
                            $where = $subpartParams['_WHERE'] ?? '';
803 6
                            $addTable = $subpartParams['_ADDTABLE'] ?? '';
804
805 6
                            $fieldName = $subpartParams['_FIELD'] ? $subpartParams['_FIELD'] : 'uid';
806 6
                            if ($fieldName === 'uid' || $GLOBALS['TCA'][$subpartParams['_TABLE']]['columns'][$fieldName]) {
807 6
                                $queryBuilder = $this->getQueryBuilder($subpartParams['_TABLE']);
808
809 6
                                if ($recursiveDepth > 0) {
810
                                    /** @var QueryGenerator $queryGenerator */
811 2
                                    $queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class);
812 2
                                    $pidList = $queryGenerator->getTreeList($lookUpPid, $recursiveDepth, 0, 1);
813 2
                                    $pidArray = GeneralUtility::intExplode(',', $pidList);
814
                                } else {
815 4
                                    $pidArray = [(string) $lookUpPid];
816
                                }
817
818 6
                                $queryBuilder->getRestrictions()
819 6
                                    ->removeAll()
820 6
                                    ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
821
822
                                $queryBuilder
823 6
                                    ->select($fieldName)
824 6
                                    ->from($subpartParams['_TABLE'])
825 6
                                    ->where(
826 6
                                        $queryBuilder->expr()->in($pidField, $queryBuilder->createNamedParameter($pidArray, Connection::PARAM_INT_ARRAY)),
827 6
                                        $where
828
                                    );
829
830 6
                                if (! empty($addTable)) {
831
                                    // TODO: Check if this works as intended!
832
                                    $queryBuilder->add('from', $addTable);
833
                                }
834 6
                                $transOrigPointerField = $GLOBALS['TCA'][$subpartParams['_TABLE']]['ctrl']['transOrigPointerField'];
835
836 6
                                if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) {
837
                                    $queryBuilder->andWhere(
838
                                        $queryBuilder->expr()->lte(
839
                                            $transOrigPointerField,
840
                                            0
841
                                        )
842
                                    );
843
                                }
844
845 6
                                $statement = $queryBuilder->execute();
846
847 6
                                $rows = [];
848 6
                                while ($row = $statement->fetch()) {
849 6
                                    $rows[$row[$fieldName]] = $row;
850
                                }
851
852 6
                                if (is_array($rows)) {
853 6
                                    $paramArray[$p] = array_merge($paramArray[$p], array_keys($rows));
854
                                }
855
                            }
856
                        }
857
                    } else {
858
                        // Just add value:
859 7
                        $paramArray[$p][] = $pV;
860
                    }
861
                    // Hook for processing own expandParameters place holder
862 14
                    if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'])) {
863
                        $_params = [
864
                            'pObj' => &$this,
865
                            'paramArray' => &$paramArray,
866
                            'currentKey' => $p,
867
                            'currentValue' => $pV,
868
                            'pid' => $pid,
869
                        ];
870
                        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'] as $_funcRef) {
871
                            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
872
                        }
873
                    }
874
                }
875
876
                // Make unique set of values and sort array by key:
877 14
                $paramArray[$p] = array_unique($paramArray[$p]);
878 14
                ksort($paramArray);
879
            } else {
880
                // Set the literal value as only value in array:
881 7
                $paramArray[$p] = [$v];
882
            }
883
        }
884
885 14
        return $paramArray;
886
    }
887
888
    /**
889
     * Compiling URLs from parameter array (output of expandParameters())
890
     * The number of URLs will be the multiplication of the number of parameter values for each key
891
     *
892
     * @param array $paramArray Output of expandParameters(): Array with keys (GET var names) and for each an array of values
893
     * @param array $urls URLs accumulated in this array (for recursion)
894
     * @return array
895
     */
896 11
    public function compileUrls($paramArray, array $urls)
897
    {
898 11
        if (empty($paramArray)) {
899 11
            return $urls;
900
        }
901 10
        $varName = key($paramArray);
902 10
        $valueSet = array_shift($paramArray);
903
904
        // Traverse value set:
905 10
        $newUrls = [];
906 10
        foreach ($urls as $url) {
907 9
            foreach ($valueSet as $val) {
908 9
                if (count($newUrls) < $this->getMaximumUrlsToCompile()) {
909 9
                    $newUrls[] = $url . (strcmp((string) $val, '') ? '&' . rawurlencode($varName) . '=' . rawurlencode((string) $val) : '');
910
                }
911
            }
912
        }
913 10
        return $this->compileUrls($paramArray, $newUrls);
914
    }
915
916
    /************************************
917
     *
918
     * Crawler log
919
     *
920
     ************************************/
921
922
    /**
923
     * Return array of records from crawler queue for input page ID
924
     *
925
     * @param integer $id Page ID for which to look up log entries.
926
     * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected!
927
     * @param boolean $doFullFlush
928
     * @param integer $itemsPerPage Limit the amount of entries per page default is 10
929
     * @return array
930
     *
931
     * @deprecated
932
     */
933 4
    public function getLogEntriesForPageId($id, QueueFilter $queueFilter, $doFlush = false, $doFullFlush = false, $itemsPerPage = 10)
0 ignored issues
show
Unused Code introduced by
The parameter $doFullFlush is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

933
    public function getLogEntriesForPageId($id, QueueFilter $queueFilter, $doFlush = false, /** @scrutinizer ignore-unused */ $doFullFlush = false, $itemsPerPage = 10)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
934
    {
935 4
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
936
        $queryBuilder
937 4
            ->select('*')
938 4
            ->from($this->tableName)
939 4
            ->where(
940 4
                $queryBuilder->expr()->eq('page_id', $queryBuilder->createNamedParameter($id, PDO::PARAM_INT))
941
            )
942 4
            ->orderBy('scheduled', 'DESC');
943
944 4
        $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
945 4
            ->getConnectionForTable($this->tableName)
946 4
            ->getExpressionBuilder();
947 4
        $query = $expressionBuilder->andX();
0 ignored issues
show
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
948
        // PHPStorm adds the highlight that the $addWhere is immediately overwritten,
949
        // but the $query = $expressionBuilder->andX() ensures that the $addWhere is written correctly with AND
950
        // between the statements, it's not a mistake in the code.
951 4
        switch ($queueFilter) {
952 4
            case 'pending':
953
                $queryBuilder->andWhere($queryBuilder->expr()->eq('exec_time', 0));
954
                break;
955 4
            case 'finished':
956
                $queryBuilder->andWhere($queryBuilder->expr()->gt('exec_time', 0));
957
                break;
958
        }
959
960 4
        if ($doFlush) {
961 2
            $this->queueRepository->flushQueue($queueFilter);
962
        }
963 4
        if ($itemsPerPage > 0) {
964
            $queryBuilder
965 4
                ->setMaxResults((int) $itemsPerPage);
966
        }
967
968 4
        return $queryBuilder->execute()->fetchAll();
969
    }
970
971
    /**
972
     * Return array of records from crawler queue for input set ID
973
     *
974
     * @param int $set_id Set ID for which to look up log entries.
975
     * @param string $filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones
976
     * @param bool $doFlush If TRUE, then entries selected at DELETED(!) instead of selected!
977
     * @param int $itemsPerPage Limit the amount of entries per page default is 10
978
     * @return array
979
     *
980
     * @deprecated
981
     */
982 6
    public function getLogEntriesForSetId(int $set_id, string $filter = '', bool $doFlush = false, bool $doFullFlush = false, int $itemsPerPage = 10)
983
    {
984 6
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
985
        $queryBuilder
986 6
            ->select('*')
987 6
            ->from($this->tableName)
988 6
            ->where(
989 6
                $queryBuilder->expr()->eq('set_id', $queryBuilder->createNamedParameter($set_id, PDO::PARAM_INT))
990
            )
991 6
            ->orderBy('scheduled', 'DESC');
992
993 6
        $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
994 6
            ->getConnectionForTable($this->tableName)
995 6
            ->getExpressionBuilder();
996 6
        $query = $expressionBuilder->andX();
997
        // PHPStorm adds the highlight that the $addWhere is immediately overwritten,
998
        // but the $query = $expressionBuilder->andX() ensures that the $addWhere is written correctly with AND
999
        // between the statements, it's not a mistake in the code.
1000 6
        $addWhere = '';
1001 6
        switch ($filter) {
1002 6
            case 'pending':
1003 1
                $queryBuilder->andWhere($queryBuilder->expr()->eq('exec_time', 0));
1004 1
                $addWhere = $query->add($expressionBuilder->eq('exec_time', 0));
0 ignored issues
show
Unused Code introduced by
The assignment to $addWhere is dead and can be removed.
Loading history...
1005 1
                break;
1006 5
            case 'finished':
1007 1
                $queryBuilder->andWhere($queryBuilder->expr()->gt('exec_time', 0));
1008 1
                $addWhere = $query->add($expressionBuilder->gt('exec_time', 0));
1009 1
                break;
1010
        }
1011 6
        if ($doFlush) {
1012 4
            $addWhere = $query->add($expressionBuilder->eq('set_id', (int) $set_id));
1013 4
            $this->flushQueue($doFullFlush ? '' : $addWhere);
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Controller\C...ontroller::flushQueue() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1013
            /** @scrutinizer ignore-deprecated */ $this->flushQueue($doFullFlush ? '' : $addWhere);
Loading history...
1014 4
            return [];
1015
        }
1016 2
        if ($itemsPerPage > 0) {
1017
            $queryBuilder
1018 2
                ->setMaxResults((int) $itemsPerPage);
1019
        }
1020
1021 2
        return $queryBuilder->execute()->fetchAll();
1022
    }
1023
1024
    /**
1025
     * Adding call back entries to log (called from hooks typically, see indexed search class "class.crawler.php"
1026
     *
1027
     * @param integer $setId Set ID
1028
     * @param array $params Parameters to pass to call back function
1029
     * @param string $callBack Call back object reference, eg. 'EXT:indexed_search/class.crawler.php:&tx_indexedsearch_crawler'
1030
     * @param integer $page_id Page ID to attach it to
1031
     * @param integer $schedule Time at which to activate
1032
     */
1033
    public function addQueueEntry_callBack($setId, $params, $callBack, $page_id = 0, $schedule = 0): void
1034
    {
1035
        if (! is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
1036
            $params = [];
1037
        }
1038
        $params['_CALLBACKOBJ'] = $callBack;
1039
1040
        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue')
1041
            ->insert(
1042
                'tx_crawler_queue',
1043
                [
1044
                    'page_id' => (int) $page_id,
1045
                    'parameters' => json_encode($params),
1046
                    'scheduled' => (int) $schedule ?: $this->getCurrentTime(),
1047
                    'exec_time' => 0,
1048
                    'set_id' => (int) $setId,
1049
                    'result_data' => '',
1050
                ]
1051
            );
1052
    }
1053
1054
    /************************************
1055
     *
1056
     * URL setting
1057
     *
1058
     ************************************/
1059
1060
    /**
1061
     * Setting a URL for crawling:
1062
     *
1063
     * @param integer $id Page ID
1064
     * @param string $url Complete URL
1065
     * @param array $subCfg Sub configuration array (from TS config)
1066
     * @param integer $tstamp Scheduled-time
1067
     * @param string $configurationHash (optional) configuration hash
1068
     * @param bool $skipInnerDuplicationCheck (optional) skip inner duplication check
1069
     * @return bool
1070
     */
1071 11
    public function addUrl(
1072
        $id,
1073
        $url,
1074
        array $subCfg,
1075
        $tstamp,
1076
        $configurationHash = '',
1077
        $skipInnerDuplicationCheck = false
1078
    ) {
1079 11
        $urlAdded = false;
1080 11
        $rows = [];
1081
1082
        // Creating parameters:
1083
        $parameters = [
1084 11
            'url' => $url,
1085
        ];
1086
1087
        // fe user group simulation:
1088 11
        $uGs = implode(',', array_unique(GeneralUtility::intExplode(',', $subCfg['userGroups'], true)));
1089 11
        if ($uGs) {
1090 1
            $parameters['feUserGroupList'] = $uGs;
1091
        }
1092
1093
        // Setting processing instructions
1094 11
        $parameters['procInstructions'] = GeneralUtility::trimExplode(',', $subCfg['procInstrFilter']);
1095 11
        if (is_array($subCfg['procInstrParams.'])) {
1096 8
            $parameters['procInstrParams'] = $subCfg['procInstrParams.'];
1097
        }
1098
1099
        // Compile value array:
1100 11
        $parameters_serialized = json_encode($parameters);
1101
        $fieldArray = [
1102 11
            'page_id' => (int) $id,
1103 11
            'parameters' => $parameters_serialized,
1104 11
            'parameters_hash' => GeneralUtility::shortMD5($parameters_serialized),
1105 11
            'configuration_hash' => $configurationHash,
1106 11
            'scheduled' => $tstamp,
1107 11
            'exec_time' => 0,
1108 11
            'set_id' => (int) $this->setID,
1109 11
            'result_data' => '',
1110 11
            'configuration' => $subCfg['key'],
1111
        ];
1112
1113 11
        if ($this->registerQueueEntriesInternallyOnly) {
1114
            //the entries will only be registered and not stored to the database
1115 1
            $this->queueEntries[] = $fieldArray;
1116
        } else {
1117 10
            if (! $skipInnerDuplicationCheck) {
1118
                // check if there is already an equal entry
1119 9
                $rows = $this->queueRepository->getDuplicateQueueItemsIfExists(
1120 9
                    (bool) $this->extensionSettings['enableTimeslot'],
1121 9
                    $tstamp,
1122 9
                    $this->getCurrentTime(),
1123 9
                    $fieldArray['page_id'],
1124 9
                    $fieldArray['parameters_hash']
1125
                );
1126
            }
1127
1128 10
            if (empty($rows)) {
1129 9
                $connectionForCrawlerQueue = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue');
1130 9
                $connectionForCrawlerQueue->insert(
1131 9
                    'tx_crawler_queue',
1132 9
                    $fieldArray
1133
                );
1134 9
                $uid = $connectionForCrawlerQueue->lastInsertId('tx_crawler_queue', 'qid');
1135 9
                $rows[] = $uid;
1136 9
                $urlAdded = true;
1137
1138 9
                $signalPayload = ['uid' => $uid, 'fieldArray' => $fieldArray];
1139 9
                SignalSlotUtility::emitSignal(
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Utility\Sign...otUtility::emitSignal() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1139
                /** @scrutinizer ignore-deprecated */ SignalSlotUtility::emitSignal(
Loading history...
1140 9
                    self::class,
1141 9
                    SignalSlotUtility::SIGNAL_URL_ADDED_TO_QUEUE,
1142 9
                    $signalPayload
1143
                );
1144
            } else {
1145 5
                $signalPayload = ['rows' => $rows, 'fieldArray' => $fieldArray];
1146 5
                SignalSlotUtility::emitSignal(
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Utility\Sign...otUtility::emitSignal() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1146
                /** @scrutinizer ignore-deprecated */ SignalSlotUtility::emitSignal(
Loading history...
1147 5
                    self::class,
1148 5
                    SignalSlotUtility::SIGNAL_DUPLICATE_URL_IN_QUEUE,
1149 5
                    $signalPayload
1150
                );
1151
            }
1152
        }
1153
1154 11
        return $urlAdded;
1155
    }
1156
1157
    /**
1158
     * Returns the current system time
1159
     *
1160
     * @return int
1161
     */
1162 4
    public function getCurrentTime()
1163
    {
1164 4
        return time();
1165
    }
1166
1167
    /************************************
1168
     *
1169
     * URL reading
1170
     *
1171
     ************************************/
1172
1173
    /**
1174
     * Read URL for single queue entry
1175
     *
1176
     * @param integer $queueId
1177
     * @param boolean $force If set, will process even if exec_time has been set!
1178
     * @return integer
1179
     */
1180 2
    public function readUrl($queueId, $force = false)
1181
    {
1182 2
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
1183 2
        $ret = 0;
1184 2
        $this->logger->debug('crawler-readurl start ' . microtime(true));
1185
        // Get entry:
1186
        $queryBuilder
1187 2
            ->select('*')
1188 2
            ->from('tx_crawler_queue')
1189 2
            ->where(
1190 2
                $queryBuilder->expr()->eq('qid', $queryBuilder->createNamedParameter($queueId, PDO::PARAM_INT))
1191
            );
1192 2
        if (! $force) {
1193
            $queryBuilder
1194 2
                ->andWhere('exec_time = 0')
1195 2
                ->andWhere('process_scheduled > 0');
1196
        }
1197 2
        $queueRec = $queryBuilder->execute()->fetch();
1198
1199 2
        if (! is_array($queueRec)) {
1200
            return;
1201
        }
1202
1203 2
        SignalSlotUtility::emitSignal(
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Utility\Sign...otUtility::emitSignal() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1203
        /** @scrutinizer ignore-deprecated */ SignalSlotUtility::emitSignal(
Loading history...
1204 2
            self::class,
1205 2
            SignalSlotUtility::SIGNAL_QUEUEITEM_PREPROCESS,
1206 2
            [$queueId, &$queueRec]
1207
        );
1208
1209
        // Set exec_time to lock record:
1210 2
        $field_array = ['exec_time' => $this->getCurrentTime()];
1211
1212 2
        if (isset($this->processID)) {
1213
            //if mulitprocessing is used we need to store the id of the process which has handled this entry
1214 2
            $field_array['process_id_completed'] = $this->processID;
1215
        }
1216
1217 2
        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue')
1218 2
            ->update(
1219 2
                'tx_crawler_queue',
1220 2
                $field_array,
1221 2
                ['qid' => (int) $queueId]
1222
            );
1223
1224 2
        $result = $this->queueExecutor->executeQueueItem($queueRec, $this);
1225 2
        if ($result['content'] === null) {
1226
            $resultData = 'An errors happened';
1227
        } else {
1228
            /** @var JsonCompatibilityConverter $jsonCompatibilityConverter */
1229 2
            $jsonCompatibilityConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
1230 2
            $resultData = $jsonCompatibilityConverter->convert($result['content']);
1231
        }
1232
1233
        //atm there's no need to point to specific pollable extensions
1234 2
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'])) {
1235
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] as $pollable) {
1236
                // only check the success value if the instruction is runnig
1237
                // it is important to name the pollSuccess key same as the procInstructions key
1238
                if (is_array($resultData['parameters']['procInstructions'])
1239
                    && in_array(
1240
                        $pollable,
1241
                        $resultData['parameters']['procInstructions'], true
1242
                    )
1243
                ) {
1244
                    if (! empty($resultData['success'][$pollable]) && $resultData['success'][$pollable]) {
1245
                        $ret |= self::CLI_STATUS_POLLABLE_PROCESSED;
1246
                    }
1247
                }
1248
            }
1249
        }
1250
1251
        // Set result in log which also denotes the end of the processing of this entry.
1252 2
        $field_array = ['result_data' => json_encode($result)];
1253
1254 2
        SignalSlotUtility::emitSignal(
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Utility\Sign...otUtility::emitSignal() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1254
        /** @scrutinizer ignore-deprecated */ SignalSlotUtility::emitSignal(
Loading history...
1255 2
            self::class,
1256 2
            SignalSlotUtility::SIGNAL_QUEUEITEM_POSTPROCESS,
1257 2
            [$queueId, &$field_array]
1258
        );
1259
1260 2
        GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue')
1261 2
            ->update(
1262 2
                'tx_crawler_queue',
1263 2
                $field_array,
1264 2
                ['qid' => (int) $queueId]
1265
            );
1266
1267 2
        $this->logger->debug('crawler-readurl stop ' . microtime(true));
1268 2
        return $ret;
1269
    }
1270
1271
    /**
1272
     * Read URL for not-yet-inserted log-entry
1273
     *
1274
     * @param array $field_array Queue field array,
1275
     *
1276
     * @return array|bool|mixed|string
1277
     */
1278
    public function readUrlFromArray($field_array)
1279
    {
1280
        // Set exec_time to lock record:
1281
        $field_array['exec_time'] = $this->getCurrentTime();
1282
        $connectionForCrawlerQueue = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName);
1283
        $connectionForCrawlerQueue->insert(
1284
            $this->tableName,
1285
            $field_array
1286
        );
1287
        $queueId = $connectionForCrawlerQueue->lastInsertId($this->tableName, 'qid');
1288
        $result = $this->queueExecutor->executeQueueItem($field_array, $this);
1289
1290
        // Set result in log which also denotes the end of the processing of this entry.
1291
        $field_array = ['result_data' => json_encode($result)];
1292
1293
        SignalSlotUtility::emitSignal(
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Utility\Sign...otUtility::emitSignal() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1293
        /** @scrutinizer ignore-deprecated */ SignalSlotUtility::emitSignal(
Loading history...
1294
            self::class,
1295
            SignalSlotUtility::SIGNAL_QUEUEITEM_POSTPROCESS,
1296
            [$queueId, &$field_array]
1297
        );
1298
1299
        $connectionForCrawlerQueue->update(
1300
            $this->tableName,
1301
            $field_array,
1302
            ['qid' => $queueId]
1303
        );
1304
1305
        return $result;
1306
    }
1307
1308
    /*****************************
1309
     *
1310
     * Compiling URLs to crawl - tools
1311
     *
1312
     *****************************/
1313
1314
    /**
1315
     * @param integer $id Root page id to start from.
1316
     * @param integer $depth Depth of tree, 0=only id-page, 1= on sublevel, 99 = infinite
1317
     * @param integer $scheduledTime Unix Time when the URL is timed to be visited when put in queue
1318
     * @param integer $reqMinute Number of requests per minute (creates the interleave between requests)
1319
     * @param boolean $submitCrawlUrls If set, submits the URLs to queue in database (real crawling)
1320
     * @param boolean $downloadCrawlUrls If set (and submitcrawlUrls is false) will fill $downloadUrls with entries)
1321
     * @param array $incomingProcInstructions Array of processing instructions
1322
     * @param array $configurationSelection Array of configuration keys
1323
     * @return string
1324
     */
1325
    public function getPageTreeAndUrls(
1326
        $id,
1327
        $depth,
1328
        $scheduledTime,
1329
        $reqMinute,
1330
        $submitCrawlUrls,
1331
        $downloadCrawlUrls,
1332
        array $incomingProcInstructions,
1333
        array $configurationSelection
1334
    ) {
1335
        $this->scheduledTime = $scheduledTime;
1336
        $this->reqMinute = $reqMinute;
1337
        $this->submitCrawlUrls = $submitCrawlUrls;
1338
        $this->downloadCrawlUrls = $downloadCrawlUrls;
1339
        $this->incomingProcInstructions = $incomingProcInstructions;
1340
        $this->incomingConfigurationSelection = $configurationSelection;
1341
1342
        $this->duplicateTrack = [];
1343
        $this->downloadUrls = [];
1344
1345
        // Drawing tree:
1346
        /* @var PageTreeView $tree */
1347
        $tree = GeneralUtility::makeInstance(PageTreeView::class);
1348
        $perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
1349
        $tree->init('AND ' . $perms_clause);
1350
1351
        $pageInfo = BackendUtility::readPageAccess($id, $perms_clause);
1352
        if (is_array($pageInfo)) {
1353
            // Set root row:
1354
            $tree->tree[] = [
1355
                'row' => $pageInfo,
1356
                'HTML' => $this->iconFactory->getIconForRecord('pages', $pageInfo, Icon::SIZE_SMALL),
1357
            ];
1358
        }
1359
1360
        // Get branch beneath:
1361
        if ($depth) {
1362
            $tree->getTree($id, $depth, '');
1363
        }
1364
1365
        // Traverse page tree:
1366
        $code = '';
1367
1368
        foreach ($tree->tree as $data) {
1369
            $this->MP = false;
1370
1371
            // recognize mount points
1372
            if ($data['row']['doktype'] === PageRepository::DOKTYPE_MOUNTPOINT) {
1373
                $mountpage = $this->pageRepository->getPage($data['row']['uid']);
1374
1375
                // fetch mounted pages
1376
                $this->MP = $mountpage[0]['mount_pid'] . '-' . $data['row']['uid'];
0 ignored issues
show
Documentation Bug introduced by
The property $MP was declared of type boolean, but $mountpage[0]['mount_pid...' . $data['row']['uid'] is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1377
1378
                $mountTree = GeneralUtility::makeInstance(PageTreeView::class);
1379
                $mountTree->init('AND ' . $perms_clause);
1380
                $mountTree->getTree($mountpage[0]['mount_pid'], $depth);
1381
1382
                foreach ($mountTree->tree as $mountData) {
1383
                    $code .= $this->drawURLs_addRowsForPage(
1384
                        $mountData['row'],
1385
                        $mountData['HTML'] . BackendUtility::getRecordTitle('pages', $mountData['row'], true)
1386
                    );
1387
                }
1388
1389
                // replace page when mount_pid_ol is enabled
1390
                if ($mountpage[0]['mount_pid_ol']) {
1391
                    $data['row']['uid'] = $mountpage[0]['mount_pid'];
1392
                } else {
1393
                    // if the mount_pid_ol is not set the MP must not be used for the mountpoint page
1394
                    $this->MP = false;
1395
                }
1396
            }
1397
1398
            $code .= $this->drawURLs_addRowsForPage(
1399
                $data['row'],
1400
                $data['HTML'] . BackendUtility::getRecordTitle('pages', $data['row'], true)
1401
            );
1402
        }
1403
1404
        return $code;
1405
    }
1406
1407
    /**
1408
     * Expands exclude string
1409
     *
1410
     * @param string $excludeString Exclude string
1411
     * @return array
1412
     */
1413 2
    public function expandExcludeString($excludeString)
1414
    {
1415
        // internal static caches;
1416 2
        static $expandedExcludeStringCache;
1417 2
        static $treeCache;
1418
1419 2
        if (empty($expandedExcludeStringCache[$excludeString])) {
1420 2
            $pidList = [];
1421
1422 2
            if (! empty($excludeString)) {
1423
                /** @var PageTreeView $tree */
1424 1
                $tree = GeneralUtility::makeInstance(PageTreeView::class);
1425 1
                $tree->init('AND ' . $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW));
1426
1427 1
                $excludeParts = GeneralUtility::trimExplode(',', $excludeString);
1428
1429 1
                foreach ($excludeParts as $excludePart) {
1430 1
                    [$pid, $depth] = GeneralUtility::trimExplode('+', $excludePart);
1431
1432
                    // default is "page only" = "depth=0"
1433 1
                    if (empty($depth)) {
1434 1
                        $depth = (stristr($excludePart, '+')) ? 99 : 0;
1435
                    }
1436
1437 1
                    $pidList[] = (int) $pid;
1438
1439 1
                    if ($depth > 0) {
1440
                        if (empty($treeCache[$pid][$depth])) {
1441
                            $tree->reset();
1442
                            $tree->getTree($pid, $depth);
0 ignored issues
show
Bug introduced by
$pid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Backend\Tree\V...ractTreeView::getTree(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1442
                            $tree->getTree(/** @scrutinizer ignore-type */ $pid, $depth);
Loading history...
1443
                            $treeCache[$pid][$depth] = $tree->tree;
1444
                        }
1445
1446
                        foreach ($treeCache[$pid][$depth] as $data) {
1447
                            $pidList[] = (int) $data['row']['uid'];
1448
                        }
1449
                    }
1450
                }
1451
            }
1452
1453 2
            $expandedExcludeStringCache[$excludeString] = array_unique($pidList);
1454
        }
1455
1456 2
        return $expandedExcludeStringCache[$excludeString];
1457
    }
1458
1459
    /**
1460
     * Create the rows for display of the page tree
1461
     * For each page a number of rows are shown displaying GET variable configuration
1462
     */
1463
    public function drawURLs_addRowsForPage(array $pageRow, string $pageTitle): string
1464
    {
1465
        $skipMessage = '';
1466
1467
        // Get list of configurations
1468
        $configurations = $this->getUrlsForPageRow($pageRow, $skipMessage);
1469
        $configurations = ConfigurationService::removeDisallowedConfigurations($this->incomingConfigurationSelection, $configurations);
1470
1471
        // Traverse parameter combinations:
1472
        $c = 0;
1473
        $content = '';
1474
        if (! empty($configurations)) {
1475
            foreach ($configurations as $confKey => $confArray) {
1476
1477
                // Title column:
1478
                if (! $c) {
1479
                    $titleClm = '<td rowspan="' . count($configurations) . '">' . $pageTitle . '</td>';
1480
                } else {
1481
                    $titleClm = '';
1482
                }
1483
1484
                if (! in_array($pageRow['uid'], $this->expandExcludeString($confArray['subCfg']['exclude']), true)) {
1485
1486
                    // URL list:
1487
                    $urlList = $this->urlListFromUrlArray(
1488
                        $confArray,
1489
                        $pageRow,
1490
                        $this->scheduledTime,
1491
                        $this->reqMinute,
1492
                        $this->submitCrawlUrls,
1493
                        $this->downloadCrawlUrls,
1494
                        $this->duplicateTrack,
1495
                        $this->downloadUrls,
1496
                        // if empty the urls won't be filtered by processing instructions
1497
                        $this->incomingProcInstructions
1498
                    );
1499
1500
                    // Expanded parameters:
1501
                    $paramExpanded = '';
1502
                    $calcAccu = [];
1503
                    $calcRes = 1;
1504
                    foreach ($confArray['paramExpanded'] as $gVar => $gVal) {
1505
                        $paramExpanded .= '
1506
                            <tr>
1507
                                <td>' . htmlspecialchars('&' . $gVar . '=') . '<br/>' .
1508
                            '(' . count($gVal) . ')' .
1509
                            '</td>
1510
                                <td nowrap="nowrap">' . nl2br(htmlspecialchars(implode(chr(10), $gVal))) . '</td>
1511
                            </tr>
1512
                        ';
1513
                        $calcRes *= count($gVal);
1514
                        $calcAccu[] = count($gVal);
1515
                    }
1516
                    $paramExpanded = '<table>' . $paramExpanded . '</table>';
1517
                    $paramExpanded .= 'Comb: ' . implode('*', $calcAccu) . '=' . $calcRes;
1518
1519
                    // Options
1520
                    $optionValues = '';
1521
                    if ($confArray['subCfg']['userGroups']) {
1522
                        $optionValues .= 'User Groups: ' . $confArray['subCfg']['userGroups'] . '<br/>';
1523
                    }
1524
                    if ($confArray['subCfg']['procInstrFilter']) {
1525
                        $optionValues .= 'ProcInstr: ' . $confArray['subCfg']['procInstrFilter'] . '<br/>';
1526
                    }
1527
1528
                    // Compile row:
1529
                    $content .= '
1530
                        <tr>
1531
                            ' . $titleClm . '
1532
                            <td>' . htmlspecialchars($confKey) . '</td>
1533
                            <td>' . nl2br(htmlspecialchars(rawurldecode(trim(str_replace('&', chr(10) . '&', GeneralUtility::implodeArrayForUrl('', $confArray['paramParsed'])))))) . '</td>
1534
                            <td>' . $paramExpanded . '</td>
1535
                            <td nowrap="nowrap">' . $urlList . '</td>
1536
                            <td nowrap="nowrap">' . $optionValues . '</td>
1537
                            <td nowrap="nowrap">' . DebugUtility::viewArray($confArray['subCfg']['procInstrParams.']) . '</td>
1538
                        </tr>';
1539
                } else {
1540
                    $content .= '<tr>
1541
                            ' . $titleClm . '
1542
                            <td>' . htmlspecialchars($confKey) . '</td>
1543
                            <td colspan="5"><em>No entries</em> (Page is excluded in this configuration)</td>
1544
                        </tr>';
1545
                }
1546
1547
                $c++;
1548
            }
1549
        } else {
1550
            $message = ! empty($skipMessage) ? ' (' . $skipMessage . ')' : '';
1551
1552
            // Compile row:
1553
            $content .= '
1554
                <tr>
1555
                    <td>' . $pageTitle . '</td>
1556
                    <td colspan="6"><em>No entries</em>' . $message . '</td>
1557
                </tr>';
1558
        }
1559
1560
        return $content;
1561
    }
1562
1563
    /*****************************
1564
     *
1565
     * CLI functions
1566
     *
1567
     *****************************/
1568
1569
    /**
1570
     * Running the functionality of the CLI (crawling URLs from queue)
1571
     */
1572 2
    public function CLI_run(int $countInARun, int $sleepTime, int $sleepAfterFinish): int
1573
    {
1574 2
        $result = 0;
1575 2
        $counter = 0;
1576
1577
        // First, run hooks:
1578 2
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['cli_hooks'] ?? [] as $objRef) {
1579 2
            trigger_error(
1580 2
                'This hook (crawler/cli_hooks) is deprecated since 9.1.5 and will be removed when dropping support for TYPO3 9LTS and 10LTS',
1581 2
                E_USER_DEPRECATED
1582
            );
1583 2
            $hookObj = GeneralUtility::makeInstance($objRef);
1584 2
            if (is_object($hookObj)) {
1585 2
                $hookObj->crawler_init($this);
1586
            }
1587
        }
1588
1589
        // Clean up the queue
1590 2
        $this->queueRepository->cleanupQueue();
1591
1592
        // Select entries:
1593 2
        $rows = $this->queueRepository->fetchRecordsToBeCrawled($countInARun);
1594
1595 2
        if (! empty($rows)) {
1596 2
            $quidList = [];
1597
1598 2
            foreach ($rows as $r) {
1599 2
                $quidList[] = $r['qid'];
1600
            }
1601
1602 2
            $processId = $this->CLI_buildProcessId();
1603
1604
            //save the number of assigned queue entries to determine how many have been processed later
1605 2
            $numberOfAffectedRows = $this->queueRepository->updateProcessIdAndSchedulerForQueueIds($quidList, $processId);
1606 2
            $this->processRepository->updateProcessAssignItemsCount($numberOfAffectedRows, $processId);
1607
1608 2
            if ($numberOfAffectedRows !== count($quidList)) {
1609
                return ($result | self::CLI_STATUS_ABORTED);
1610
            }
1611
1612 2
            foreach ($rows as $r) {
1613 2
                $result |= $this->readUrl($r['qid']);
1614
1615 2
                $counter++;
1616
                // Just to relax the system
1617 2
                usleep((int) $sleepTime);
1618
1619
                // if during the start and the current read url the cli has been disable we need to return from the function
1620
                // mark the process NOT as ended.
1621 2
                if ($this->crawler->isDisabled()) {
1622
                    return ($result | self::CLI_STATUS_ABORTED);
1623
                }
1624
1625 2
                if (! $this->processRepository->isProcessActive($this->CLI_buildProcessId())) {
1626
                    $this->CLI_debug('conflict / timeout (' . $this->CLI_buildProcessId() . ')');
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Controller\C...Controller::CLI_debug() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1626
                    /** @scrutinizer ignore-deprecated */ $this->CLI_debug('conflict / timeout (' . $this->CLI_buildProcessId() . ')');
Loading history...
1627
                    $result |= self::CLI_STATUS_ABORTED;
1628
                    //possible timeout
1629
                    break;
1630
                }
1631
            }
1632
1633 2
            sleep((int) $sleepAfterFinish);
1634
        }
1635
1636 2
        if ($counter > 0) {
1637 2
            $result |= self::CLI_STATUS_PROCESSED;
1638
        }
1639
1640 2
        return $result;
1641
    }
1642
1643
    /**
1644
     * Activate hooks
1645
     * @deprecated
1646
     */
1647
    public function CLI_runHooks(): void
1648
    {
1649
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['cli_hooks'] ?? [] as $objRef) {
1650
            $hookObj = GeneralUtility::makeInstance($objRef);
1651
            if (is_object($hookObj)) {
1652
                $hookObj->crawler_init($this);
1653
            }
1654
        }
1655
    }
1656
1657
    /**
1658
     * Try to acquire a new process with the given id
1659
     * also performs some auto-cleanup for orphan processes
1660
     * @param string $id identification string for the process
1661
     * @return boolean
1662
     * @todo preemption might not be the most elegant way to clean up
1663
     */
1664 2
    public function CLI_checkAndAcquireNewProcess($id)
1665
    {
1666 2
        $ret = true;
1667
1668 2
        $systemProcessId = getmypid();
1669 2
        if (! $systemProcessId) {
1670
            return false;
1671
        }
1672
1673 2
        $processCount = 0;
1674 2
        $orphanProcesses = [];
1675
1676 2
        $activeProcesses = $this->processRepository->findAllActive();
1677 2
        $currentTime = $this->getCurrentTime();
1678
1679
        /** @var Process $process */
1680 2
        foreach ($activeProcesses as $process) {
1681
            if ($process->getTtl() < $currentTime) {
1682
                $orphanProcesses[] = $process->getProcessId();
1683
            } else {
1684
                $processCount++;
1685
            }
1686
        }
1687
1688
        // if there are less than allowed active processes then add a new one
1689 2
        if ($processCount < (int) $this->extensionSettings['processLimit']) {
1690 2
            GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_process')->insert(
1691 2
                'tx_crawler_process',
1692
                [
1693 2
                    'process_id' => $id,
1694 2
                    'active' => 1,
1695 2
                    'ttl' => $currentTime + (int) $this->extensionSettings['processMaxRunTime'],
1696 2
                    'system_process_id' => $systemProcessId,
1697
                ]
1698
            );
1699
        } else {
1700
            $ret = false;
1701
        }
1702
1703 2
        $this->processRepository->deleteProcessesMarkedAsDeleted();
1704 2
        $this->processRepository->markRequestedProcessesAsNotActive($orphanProcesses);
1705 2
        $this->queueRepository->unsetProcessScheduledAndProcessIdForQueueEntries($orphanProcesses);
1706
1707 2
        return $ret;
1708
    }
1709
1710
    /**
1711
     * Release a process and the required resources
1712
     *
1713
     * @param mixed $releaseIds string with a single process-id or array with multiple process-ids
1714
     * @return boolean
1715
     * @deprecated
1716
     */
1717
    public function CLI_releaseProcesses($releaseIds)
1718
    {
1719
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
1720
1721
        if (! is_array($releaseIds)) {
1722
            $releaseIds = [$releaseIds];
1723
        }
1724
1725
        if (empty($releaseIds)) {
1726
            //nothing to release
1727
            return false;
1728
        }
1729
1730
        // some kind of 2nd chance algo - this way you need at least 2 processes to have a real cleanup
1731
        // this ensures that a single process can't mess up the entire process table
1732
1733
        // mark all processes as deleted which have no "waiting" queue-entires and which are not active
1734
1735
        $queryBuilder
1736
            ->update($this->tableName, 'q')
1737
            ->where(
1738
                'q.process_id IN(SELECT p.process_id FROM tx_crawler_process as p WHERE p.active = 0)'
1739
            )
1740
            ->set('q.process_scheduled', 0)
1741
            ->set('q.process_id', '')
1742
            ->execute();
1743
1744
        // FIXME: Not entirely sure that this is equivalent to the previous version
1745
        $queryBuilder->resetQueryPart('set');
1746
1747
        $queryBuilder
1748
            ->update('tx_crawler_process')
1749
            ->where(
1750
                $queryBuilder->expr()->eq('active', 0),
1751
                'process_id IN(SELECT q.process_id FROM tx_crawler_queue as q WHERE q.exec_time = 0)'
1752
            )
1753
            ->set('system_process_id', 0)
1754
            ->execute();
1755
1756
        $this->processRepository->markRequestedProcessesAsNotActive($releaseIds);
1757
        $this->queueRepository->unsetProcessScheduledAndProcessIdForQueueEntries($releaseIds);
1758
1759
        return true;
1760
    }
1761
1762
    /**
1763
     * Create a unique Id for the current process
1764
     *
1765
     * @return string the ID
1766
     */
1767 3
    public function CLI_buildProcessId()
1768
    {
1769 3
        if (! $this->processID) {
1770 2
            $this->processID = GeneralUtility::shortMD5(microtime(true));
1771
        }
1772 3
        return $this->processID;
1773
    }
1774
1775
    /**
1776
     * Prints a message to the stdout (only if debug-mode is enabled)
1777
     *
1778
     * @param string $msg the message
1779
     * @deprecated
1780
     * @codeCoverageIgnore
1781
     */
1782
    public function CLI_debug($msg): void
1783
    {
1784
        if ((int) $this->extensionSettings['processDebug']) {
1785
            echo $msg . "\n";
1786
            flush();
1787
        }
1788
    }
1789
1790
    /**
1791
     * Cleans up entries that stayed for too long in the queue. These are:
1792
     * - processed entries that are over 1.5 days in age
1793
     * - scheduled entries that are over 7 days old
1794
     *
1795
     * @deprecated
1796
     */
1797 1
    public function cleanUpOldQueueEntries(): void
1798
    {
1799
        // 24*60*60 Seconds in 24 hours
1800 1
        $processedAgeInSeconds = $this->extensionSettings['cleanUpProcessedAge'] * 86400;
1801 1
        $scheduledAgeInSeconds = $this->extensionSettings['cleanUpScheduledAge'] * 86400;
1802
1803 1
        $now = time();
1804 1
        $condition = '(exec_time<>0 AND exec_time<' . ($now - $processedAgeInSeconds) . ') OR scheduled<=' . ($now - $scheduledAgeInSeconds);
1805 1
        $this->flushQueue($condition);
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Controller\C...ontroller::flushQueue() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1805
        /** @scrutinizer ignore-deprecated */ $this->flushQueue($condition);
Loading history...
1806 1
    }
1807
1808
    /**
1809
     * Removes queue entries
1810
     *
1811
     * @param string $where SQL related filter for the entries which should be removed
1812
     *
1813
     * @deprecated
1814
     */
1815 5
    protected function flushQueue($where = ''): void
1816
    {
1817 5
        $realWhere = strlen((string) $where) > 0 ? $where : '1=1';
1818
1819 5
        $queryBuilder = $this->getQueryBuilder($this->tableName);
1820
1821
        $groups = $queryBuilder
1822 5
            ->selectLiteral('DISTINCT set_id')
1823 5
            ->from($this->tableName)
1824 5
            ->where($realWhere)
1825 5
            ->execute()
1826 5
            ->fetchAll();
1827 5
        if (is_array($groups)) {
0 ignored issues
show
introduced by
The condition is_array($groups) is always true.
Loading history...
1828 5
            foreach ($groups as $group) {
1829
                $subSet = $queryBuilder
1830 4
                    ->select('qid', 'set_id')
1831 4
                    ->from($this->tableName)
1832 4
                    ->where(
1833 4
                        $realWhere,
1834 4
                        $queryBuilder->expr()->eq('set_id', $group['set_id'])
1835
                    )
1836 4
                    ->execute()
1837 4
                    ->fetchAll();
1838
1839 4
                $payLoad = ['subSet' => $subSet];
1840 4
                SignalSlotUtility::emitSignal(
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Utility\Sign...otUtility::emitSignal() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1840
                /** @scrutinizer ignore-deprecated */ SignalSlotUtility::emitSignal(
Loading history...
1841 4
                    self::class,
1842 4
                    SignalSlotUtility::SIGNAL_QUEUE_ENTRY_FLUSH,
1843 4
                    $payLoad
1844
                );
1845
            }
1846
        }
1847
1848
        $queryBuilder
1849 5
            ->delete($this->tableName)
1850 5
            ->where($realWhere)
1851 5
            ->execute();
1852 5
    }
1853
1854
    /**
1855
     * This method determines duplicates for a queue entry with the same parameters and this timestamp.
1856
     * If the timestamp is in the past, it will check if there is any unprocessed queue entry in the past.
1857
     * If the timestamp is in the future it will check, if the queued entry has exactly the same timestamp
1858
     *
1859
     * @param int $tstamp
1860
     * @param array $fieldArray
1861
     *
1862
     * @return array
1863
     * @deprecated
1864
     */
1865 5
    protected function getDuplicateRowsIfExist($tstamp, $fieldArray)
1866
    {
1867 5
        $rows = [];
1868
1869 5
        $currentTime = $this->getCurrentTime();
1870
1871 5
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
1872
        $queryBuilder
1873 5
            ->select('qid')
1874 5
            ->from('tx_crawler_queue');
1875
        //if this entry is scheduled with "now"
1876 5
        if ($tstamp <= $currentTime) {
1877 2
            if ($this->extensionSettings['enableTimeslot']) {
1878 1
                $timeBegin = $currentTime - 100;
1879 1
                $timeEnd = $currentTime + 100;
1880
                $queryBuilder
1881 1
                    ->where(
1882 1
                        'scheduled BETWEEN ' . $timeBegin . ' AND ' . $timeEnd . ''
1883
                    )
1884 1
                    ->orWhere(
1885 1
                        $queryBuilder->expr()->lte('scheduled', $currentTime)
1886
                    );
1887
            } else {
1888
                $queryBuilder
1889 1
                    ->where(
1890 2
                        $queryBuilder->expr()->lte('scheduled', $currentTime)
1891
                    );
1892
            }
1893 3
        } elseif ($tstamp > $currentTime) {
1894
            //entry with a timestamp in the future need to have the same schedule time
1895
            $queryBuilder
1896 3
                ->where(
1897 3
                    $queryBuilder->expr()->eq('scheduled', $tstamp)
1898
                );
1899
        }
1900
1901
        $queryBuilder
1902 5
            ->andWhere('NOT exec_time')
1903 5
            ->andWhere('NOT process_id')
1904 5
            ->andWhere($queryBuilder->expr()->eq('page_id', $queryBuilder->createNamedParameter($fieldArray['page_id'], PDO::PARAM_INT)))
1905 5
            ->andWhere($queryBuilder->expr()->eq('parameters_hash', $queryBuilder->createNamedParameter($fieldArray['parameters_hash'], PDO::PARAM_STR)));
1906
1907 5
        $statement = $queryBuilder->execute();
1908
1909 5
        while ($row = $statement->fetch()) {
1910 5
            $rows[] = $row['qid'];
1911
        }
1912
1913 5
        return $rows;
1914
    }
1915
1916
    /**
1917
     * Returns a md5 hash generated from a serialized configuration array.
1918
     *
1919
     * @return string
1920
     */
1921 13
    protected function getConfigurationHash(array $configuration)
1922
    {
1923 13
        unset($configuration['paramExpanded']);
1924 13
        unset($configuration['URLs']);
1925 13
        return md5(serialize($configuration));
1926
    }
1927
1928
    /**
1929
     * Build a URL from a Page and the Query String. If the page has a Site configuration, it can be built by using
1930
     * the Site instance.
1931
     *
1932
     * @param int $httpsOrHttp see tx_crawler_configuration.force_ssl
1933
     * @throws SiteNotFoundException
1934
     * @throws InvalidRouteArgumentsException
1935
     *
1936
     * @deprecated Using CrawlerController::getUrlFromPageAndQueryParameters() is deprecated since 9.1.1 and will be removed in v11.x, please use UrlService->getUrlFromPageAndQueryParameters() instead.
1937
     * @codeCoverageIgnore
1938
     */
1939
    protected function getUrlFromPageAndQueryParameters(int $pageId, string $queryString, ?string $alternativeBaseUrl, int $httpsOrHttp): UriInterface
1940
    {
1941
        $urlService = new UrlService();
1942
        return $urlService->getUrlFromPageAndQueryParameters($pageId, $queryString, $alternativeBaseUrl, $httpsOrHttp);
1943
    }
1944
1945 1
    protected function swapIfFirstIsLargerThanSecond(array $reg): array
1946
    {
1947
        // Swap if first is larger than last:
1948 1
        if ($reg[1] > $reg[2]) {
1949
            $temp = $reg[2];
1950
            $reg[2] = $reg[1];
1951
            $reg[1] = $temp;
1952
        }
1953
1954 1
        return $reg;
1955
    }
1956
1957 9
    private function getMaximumUrlsToCompile(): int
1958
    {
1959 9
        return $this->maximumUrlsToCompile;
1960
    }
1961
1962
    /**
1963
     * @return BackendUserAuthentication
1964
     */
1965 3
    private function getBackendUser()
1966
    {
1967
        // Make sure the _cli_ user is loaded
1968 3
        Bootstrap::initializeBackendAuthentication();
1969 3
        if ($this->backendUser === null) {
1970 3
            $this->backendUser = $GLOBALS['BE_USER'];
1971
        }
1972 3
        return $this->backendUser;
1973
    }
1974
1975
    /**
1976
     * Get querybuilder for given table
1977
     *
1978
     * @return QueryBuilder
1979
     */
1980 13
    private function getQueryBuilder(string $table)
1981
    {
1982 13
        return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1983
    }
1984
}
1985