1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\SeoPocketCrawler\Command; |
4
|
|
|
|
5
|
|
|
use PiedWeb\SeoPocketCrawler\ExtractExternalLinks; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class ShowExternalLinksCommand extends Command |
14
|
|
|
{ |
15
|
|
|
protected static $defaultName = 'crawler:external-links'; |
16
|
|
|
|
17
|
|
|
protected $id; |
18
|
|
|
|
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this->setDescription('List external domain linked.'); |
22
|
|
|
|
23
|
|
|
$this |
24
|
|
|
->addArgument( |
25
|
|
|
'id', |
26
|
|
|
InputArgument::REQUIRED, |
27
|
|
|
'id from a previous crawl' |
28
|
|
|
.PHP_EOL.'You can use `last` to get show external links from the last crawl.' |
29
|
|
|
) |
30
|
|
|
->addOption('host', 'ho', InputOption::VALUE_NONE, 'get only host') |
31
|
|
|
; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
$table = new Table($output); |
37
|
|
|
|
38
|
|
|
$table->setHeaders($input->getOption('host') ? ['Host'] : ['url', 'from']); |
39
|
|
|
|
40
|
|
|
$links = ExtractExternalLinks::scan((string) $input->getArgument('id')); |
41
|
|
|
arsort($links); |
42
|
|
|
$ever = []; |
43
|
|
|
foreach ($links as $link => $from) { |
44
|
|
|
if ($input->getOption('host')) { |
45
|
|
|
$host = parse_url($link, PHP_URL_HOST); |
46
|
|
|
if ($host && ! isset($ever[$host])) { |
47
|
|
|
$ever[$host] = 1; |
48
|
|
|
$table->addRow([$host]); |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
$first = true; |
52
|
|
|
foreach ($from as $url) { |
53
|
|
|
$table->addRow([true === $first ? $link : '', $url]); |
54
|
|
|
$first = false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$table->render(); |
60
|
|
|
|
61
|
|
|
return 0; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|