1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace AOE\Crawler; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 Crawler Extension. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use AOE\Crawler\CrawlStrategy\CallbackExecutionStrategy; |
20
|
|
|
use AOE\Crawler\CrawlStrategy\GuzzleExecutionStrategy; |
21
|
|
|
use AOE\Crawler\CrawlStrategy\SubProcessExecutionStrategy; |
22
|
|
|
use AOE\Crawler\Configuration\ExtensionConfigurationProvider; |
23
|
|
|
use AOE\Crawler\Controller\CrawlerController; |
24
|
|
|
use AOE\Crawler\Event\EventDispatcher; |
25
|
|
|
use TYPO3\CMS\Core\Http\Uri; |
26
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
27
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fetches a URL based on the selected strategy or via a callback. |
31
|
|
|
*/ |
32
|
|
|
class QueueExecutor implements SingletonInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var GuzzleExecutionStrategy|SubProcessExecutionStrategy |
36
|
|
|
*/ |
37
|
|
|
protected $selectedStrategy; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $extensionSettings; |
43
|
|
|
|
44
|
29 |
|
public function __construct(ExtensionConfigurationProvider $configurationProvider = null) |
45
|
|
|
{ |
46
|
29 |
|
$configurationProvider = $configurationProvider ?? GeneralUtility::makeInstance(ExtensionConfigurationProvider::class); |
47
|
29 |
|
$settings = $configurationProvider->getExtensionConfiguration(); |
48
|
29 |
|
$this->extensionSettings = is_array($settings) ? $settings : []; |
49
|
29 |
|
if ($this->extensionSettings['makeDirectRequests']) { |
50
|
1 |
|
$this->selectedStrategy = GeneralUtility::makeInstance(SubProcessExecutionStrategy::class); |
51
|
|
|
} else { |
52
|
28 |
|
$this->selectedStrategy = GeneralUtility::makeInstance(GuzzleExecutionStrategy::class); |
53
|
|
|
} |
54
|
29 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Takes a queue record and fetches the contents of the URL. |
58
|
|
|
* In the future, updating the queue item & additional signal/slot/events should also happen in here. |
59
|
|
|
* |
60
|
|
|
* @param array $queueItem |
61
|
|
|
* @param CrawlerController $crawlerController |
62
|
|
|
* @return array|bool|mixed|string |
63
|
|
|
*/ |
64
|
1 |
|
public function executeQueueItem(array $queueItem, CrawlerController $crawlerController) |
65
|
|
|
{ |
66
|
|
|
// Decode parameters: |
67
|
1 |
|
$parameters = unserialize($queueItem['parameters'] ?? ''); |
68
|
1 |
|
$result = 'ERROR'; |
|
|
|
|
69
|
1 |
|
if (!is_array($parameters)) { |
70
|
1 |
|
return 'ERROR'; |
71
|
|
|
} |
72
|
|
|
if ($parameters['_CALLBACKOBJ']) { |
73
|
|
|
$className = $parameters['_CALLBACKOBJ']; |
74
|
|
|
unset($parameters['_CALLBACKOBJ']); |
75
|
|
|
$result = GeneralUtility::makeInstance(CallbackExecutionStrategy::class) |
76
|
|
|
->fetchByCallback($className, $parameters, $crawlerController); |
77
|
|
|
$result = ['content' => serialize($result)]; |
78
|
|
|
} else { |
79
|
|
|
// Regular FE request |
80
|
|
|
$crawlerId = $this->generateCrawlerIdFromQueueItem($queueItem); |
81
|
|
|
|
82
|
|
|
// Get result: |
83
|
|
|
$url = new Uri($parameters['url']); |
84
|
|
|
$result = $this->selectedStrategy->fetchUrlContents($url, $crawlerId); |
85
|
|
|
if ($result !== false) { |
86
|
|
|
$result = ['content' => serialize($result)]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
EventDispatcher::getInstance()->post( |
90
|
|
|
'urlCrawled', |
91
|
|
|
$queueItem['set_id'], |
92
|
|
|
['url' => $parameters['url'], 'result' => $result] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function generateCrawlerIdFromQueueItem(array $queueItem): string |
99
|
|
|
{ |
100
|
|
|
return $queueItem['qid'] . ':' . md5($queueItem['qid'] . '|' . $queueItem['set_id'] . '|' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.