|
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
|
|
|
) |
|
29
|
|
|
->addOption('host', 'ho', InputOption::VALUE_NONE, 'get only host') |
|
30
|
|
|
; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
34
|
|
|
{ |
|
35
|
|
|
$table = new Table($output); |
|
36
|
|
|
|
|
37
|
|
|
$table->setHeaders($input->getOption('host') ? ['Host'] : ['url', 'from']); |
|
38
|
|
|
|
|
39
|
|
|
$links = ExtractExternalLinks::scan($input->getArgument('id')); |
|
|
|
|
|
|
40
|
|
|
arsort($links); |
|
41
|
|
|
$ever = []; |
|
42
|
|
|
foreach ($links as $link => $from) { |
|
43
|
|
|
if ($input->getOption('host')) { |
|
44
|
|
|
$host = parse_url($link, PHP_URL_HOST); |
|
45
|
|
|
if ($host && !isset($ever[$host])) { |
|
46
|
|
|
$ever[$host] = 1; |
|
47
|
|
|
$table->addRow([$host]); |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
$first = true; |
|
51
|
|
|
foreach ($from as $url) { |
|
52
|
|
|
$table->addRow([true === $first ? $link : '', $url]); |
|
53
|
|
|
$first = false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$table->render(); |
|
59
|
|
|
|
|
60
|
|
|
return 0; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|