-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunOctopusCommand.php
264 lines (225 loc) · 11.3 KB
/
RunOctopusCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
declare(strict_types=1);
namespace Octopus\Command;
use DateTime;
use Octopus\Config;
use Octopus\Presenter\TablePresenter;
use Octopus\Processor;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RunOctopusCommand extends Command
{
/**
* @var string
*/
private const COMMAND_ARGUMENT_SITEMAP_FILE = 'sitemap';
/**
* @var string
*/
private const COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT = 'additionalResponseHeadersToCount';
/**
* @var string
*/
private const COMMAND_OPTION_CONCURRENCY = 'concurrency';
/**
* @var string
*/
private const COMMAND_OPTION_PRESENTER = 'presenter';
/**
* @var string
*/
private const COMMAND_OPTION_TIMEOUT = 'timeout';
/**
* @var string
*/
private const COMMAND_OPTION_TIMER_UI = 'timerUI';
/**
* @var string
*/
private const COMMAND_OPTION_FOLLOW_HTTP_REDIRECTS = 'followRedirects';
/**
* @var string
*/
private const COMMAND_OPTION_USER_AGENT = 'userAgent';
/**
* @var string
*/
private const COMMAND_OPTION_REQUEST_TYPE = 'requestType';
/**
* @var string
*/
private const COMMAND_OPTION_TARGET_TYPE = 'targetFormat';
/**
* @var string
*/
private const DATE_FORMAT = DateTime::W3C;
private DateTime $crawlingStartedDateTime;
private DateTime $crawlingEndedDateTime;
private InputInterface $input;
private LoggerInterface $logger;
private ConsoleOutputInterface $output;
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
protected function configure(): void
{
$this->setName('octopus:run');
$this->setDescription('Run the Octopus Sitemap Crawler.');
$this->addArgument(self::COMMAND_ARGUMENT_SITEMAP_FILE, InputArgument::REQUIRED, 'What is the location of the sitemap you want to crawl?');
$this->addOption(self::COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT, null, InputOption::VALUE_OPTIONAL, 'A comma separated list of the additional response headers to keep track of / count during crawling');
$this->addOption(self::COMMAND_OPTION_CONCURRENCY, null, InputOption::VALUE_OPTIONAL, 'The amount of connections used concurrently. Defaults to '.Config::CONCURRENCY_DEFAULT);
$this->addOption(self::COMMAND_OPTION_FOLLOW_HTTP_REDIRECTS, null, InputOption::VALUE_OPTIONAL, 'Should the crawler follow HTTP redirects? Defaults to '.(Config::FOLLOW_HTTP_REDIRECTS_DEFAULT ? 'true' : 'false')); // @phpstan-ignore-line
$this->addOption(self::COMMAND_OPTION_USER_AGENT, null, InputOption::VALUE_OPTIONAL, 'The UserAgent to use when issuing requests, defaults to '.Config::REQUEST_HEADER_USER_AGENT_DEFAULT);
$this->addOption(self::COMMAND_OPTION_PRESENTER, null, InputOption::VALUE_OPTIONAL, 'The type of Presenter to display the Results, defaults to '.Config::PRESENTER_DEFAULT);
$this->addOption(self::COMMAND_OPTION_REQUEST_TYPE, null, InputOption::VALUE_OPTIONAL, 'The type of HTTP request, HEAD put lesser load to server while GET loads whole page. Defaults to '.Config::REQUEST_TYPE_DEFAULT);
$this->addOption(self::COMMAND_OPTION_TARGET_TYPE, null, InputOption::VALUE_OPTIONAL, 'List of URLs to parse could be provided as sitemap or plain text. Defaults to '.Config::TARGET_TYPE_DEFAULT);
$this->addOption(self::COMMAND_OPTION_TIMEOUT, null, InputOption::VALUE_OPTIONAL, 'Timeout for a request, in seconds. Defaults to '.Config::TIMEOUT_DEFAULT);
$this->addOption(self::COMMAND_OPTION_TIMER_UI, null, InputOption::VALUE_OPTIONAL, 'How often to update current statistics in the UserInterface. Defaults to '.Config::TIMER_UI_DEFAULT);
$this->setHelp(
\sprintf(
'Usage:
<info> - php application.php examples/sitemap-wiki.xml</info>
using a specific concurrency:
<info> - php application.php http://www.domain.ext/sitemap.xml --%s 15</info>',
self::COMMAND_OPTION_CONCURRENCY
)
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
\assert($output instanceof ConsoleOutputInterface);
$this->input = $input;
$this->output = $output;
$this->crawlingStartedDateTime = new DateTime();
$this->getLogger()->debug('Starting Octopus Sitemap Crawler');
$config = $this->determineConfiguration();
$processor = new Processor($config, $this->getLogger());
$processor->run();
$this->crawlingEndedDateTime = new DateTime();
$this->renderResultsTable($processor);
if ($config->outputBroken && \count($processor->result->getBrokenUrls()) > 0) {
$this->outputBrokenUrls($processor, $config->outputDestination);
}
return 0;
}
private function getLogger(): LoggerInterface
{
return $this->logger ??= new ConsoleLogger($this->output);
}
private function determineConfiguration(): Config
{
$config = new Config();
$config->targetFile = $this->input->getArgument(self::COMMAND_ARGUMENT_SITEMAP_FILE);
$this->getLogger()->notice('Loading URLs from Sitemap: '.$config->targetFile);
if (\is_string($this->input->getOption(self::COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT))) {
$additionalResponseHeadersToCount = $this->input->getOption(self::COMMAND_OPTION_ADDITIONAL_RESPONSE_HEADERS_TO_COUNT);
$config->additionalResponseHeadersToCount = \explode(',', $additionalResponseHeadersToCount);
$this->getLogger()->notice('Keep track of additional response headers: '.$additionalResponseHeadersToCount);
}
if (\is_numeric($this->input->getOption(self::COMMAND_OPTION_CONCURRENCY))) {
$config->concurrency = (int) $this->input->getOption(self::COMMAND_OPTION_CONCURRENCY);
$this->getLogger()->notice('Using concurrency: '.$config->concurrency);
}
if (\is_string($this->input->getOption(self::COMMAND_OPTION_FOLLOW_HTTP_REDIRECTS))) {
$followRedirectsValue = $this->input->getOption(self::COMMAND_OPTION_FOLLOW_HTTP_REDIRECTS);
$config->followRedirects = $followRedirectsValue === 'true';
$this->getLogger()->notice('Follow HTTP redirects: '.$followRedirectsValue);
}
if (\is_string($this->input->getOption(self::COMMAND_OPTION_USER_AGENT))) {
$userAgentValue = $this->input->getOption(self::COMMAND_OPTION_USER_AGENT);
$config->requestHeaders[$config::REQUEST_HEADER_USER_AGENT] = $userAgentValue;
$this->getLogger()->notice('Use UserAgent for issued requests: '.$userAgentValue);
}
if (\is_string($this->input->getOption(self::COMMAND_OPTION_PRESENTER))) {
$config->presenter = $this->input->getOption(self::COMMAND_OPTION_PRESENTER);
$this->getLogger()->notice('Using Presenter: '.$config->presenter);
if ($config->presenter === TablePresenter::class) {
$this->getLogger()->notice('Instantiate Presenter to be able to inject output interface');
$config->presenter = new TablePresenter($this->output);
}
}
if (\is_string($this->input->getOption(self::COMMAND_OPTION_REQUEST_TYPE))) {
$config->requestType = $this->input->getOption(self::COMMAND_OPTION_REQUEST_TYPE);
$this->getLogger()->notice('Using request type: '.$config->requestType);
}
if (\is_string($this->input->getOption(self::COMMAND_OPTION_TARGET_TYPE))) {
$config->targetType = $this->input->getOption(self::COMMAND_OPTION_TARGET_TYPE);
$this->getLogger()->notice('Source file format: '.$config->targetType);
}
if (\is_numeric($this->input->getOption(self::COMMAND_OPTION_TIMEOUT))) {
$config->timeout = (float) $this->input->getOption(self::COMMAND_OPTION_TIMEOUT);
$this->getLogger()->notice('Using per-request timeout: '.$config->timeout);
}
if (\is_numeric($this->input->getOption(self::COMMAND_OPTION_TIMER_UI))) {
$config->timerUI = (float) $this->input->getOption(self::COMMAND_OPTION_TIMER_UI);
$this->getLogger()->notice('Using timerUI refresh rate: '.$config->timerUI);
}
return $config;
}
private function renderResultsTable(Processor $processor): void
{
$statusCodes = $processor->result->getStatusCodes();
if (\count($statusCodes) === 0) {
return;
}
\ksort($statusCodes);
$rowColumnSpan = ['colspan' => \count($statusCodes)];
$table = new Table($this->output);
$table->setHeaders(
[
[new TableCell('Crawling summary for: '.$processor->config->targetFile, $rowColumnSpan)],
\array_keys($statusCodes),
]
);
$table->addRow(\array_values($statusCodes));
$table->addRow(new TableSeparator());
$table->addRows(
[
[new TableCell('Crawling started: '.$this->getCrawlingStartedLabel(), $rowColumnSpan)],
[new TableCell('Crawling ended: '.$this->getCrawlingEndedLabel(), $rowColumnSpan)],
[new TableCell('Crawling duration: '.$this->getCrawlingDurationLabel(), $rowColumnSpan)],
[new TableCell('Applied concurrency: '.$processor->config->concurrency, $rowColumnSpan)],
[new TableCell('Total amount of processed data: '.$processor->result->getTotalData(), $rowColumnSpan)],
[new TableCell('Failed to load #URLs: '.\count($processor->result->getBrokenUrls()), $rowColumnSpan)],
[new TableCell('Redirected #URLs: '.\count($processor->result->getRedirectedUrls()), $rowColumnSpan)],
]
);
$table->render();
}
private function getCrawlingStartedLabel(): string
{
return $this->crawlingStartedDateTime->format(self::DATE_FORMAT);
}
private function getCrawlingEndedLabel(): string
{
return $this->crawlingEndedDateTime->format(self::DATE_FORMAT);
}
private function getCrawlingDurationLabel(): string
{
$numberOfSeconds = $this->crawlingEndedDateTime->getTimestamp() - $this->crawlingStartedDateTime->getTimestamp();
return \sprintf('%d seconds', $numberOfSeconds);
}
private function outputBrokenUrls(Processor $processor, string $outputDestination): void
{
$content = [];
foreach ($processor->result->getBrokenUrls() as $url => $reason) {
$label = \sprintf('%s: %s', $reason, $url);
$this->getLogger()->debug(\sprintf('broken URL: %s', $label));
$content[] = $label;
}
\file_put_contents(
$outputDestination.\DIRECTORY_SEPARATOR.'broken.txt',
\implode(\PHP_EOL, $content)
);
}
}