|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MediaMonks\CrawlerBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use MediaMonks\Crawler\Page; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
class CrawlUrlCommand extends ContainerAwareCommand |
|
13
|
|
|
{ |
|
14
|
|
|
const ARGUMENT_URL = 'url'; |
|
15
|
|
|
const OPTION_LIMIT = 'limit'; |
|
16
|
|
|
const OPTION_STOP_ON_ERROR = 'stop-on-error'; |
|
17
|
|
|
const OPTION_EXCEPTION_ON_ERROR = 'exception-on-error'; |
|
18
|
|
|
|
|
19
|
1 |
|
protected function configure() |
|
20
|
|
|
{ |
|
21
|
1 |
|
$this |
|
22
|
1 |
|
->setName('mediamonks:crawler:crawl-url') |
|
23
|
1 |
|
->setDescription('Crawl an url') |
|
24
|
1 |
|
->addArgument( |
|
25
|
1 |
|
self::ARGUMENT_URL, |
|
26
|
1 |
|
InputArgument::REQUIRED, |
|
27
|
|
|
'Url of the site you want to crawl' |
|
28
|
1 |
|
) |
|
29
|
1 |
|
->addOption( |
|
30
|
1 |
|
self::OPTION_LIMIT, |
|
31
|
1 |
|
null, |
|
32
|
1 |
|
InputOption::VALUE_OPTIONAL, |
|
33
|
1 |
|
'Limit number of pages to crawl', |
|
34
|
|
|
0 |
|
35
|
1 |
|
) |
|
36
|
1 |
|
->addOption( |
|
37
|
1 |
|
self::OPTION_STOP_ON_ERROR, |
|
38
|
1 |
|
null, |
|
39
|
1 |
|
InputOption::VALUE_OPTIONAL, |
|
40
|
1 |
|
'Stop when a page could not be requested', |
|
41
|
|
|
false |
|
42
|
1 |
|
) |
|
43
|
1 |
|
->addOption( |
|
44
|
1 |
|
self::OPTION_EXCEPTION_ON_ERROR, |
|
45
|
1 |
|
null, |
|
46
|
1 |
|
InputOption::VALUE_OPTIONAL, |
|
47
|
1 |
|
'Throw exception when a page could not be requested', |
|
48
|
|
|
false |
|
49
|
1 |
|
) |
|
50
|
|
|
; |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param InputInterface $input |
|
55
|
|
|
* @param OutputInterface $output |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$crawler = $this->getContainer()->get('mediamonks_crawler.crawler'); |
|
61
|
|
|
|
|
62
|
1 |
|
$crawler->setLimit((int)$input->getOption(self::OPTION_LIMIT)); |
|
63
|
1 |
|
$crawler->setStopOnError((bool)$input->getOption(self::OPTION_STOP_ON_ERROR)); |
|
64
|
1 |
|
$crawler->setExceptionOnError((bool)$input->getOption(self::OPTION_EXCEPTION_ON_ERROR)); |
|
65
|
|
|
|
|
66
|
1 |
|
foreach ($crawler->crawl($input->getArgument('url')) as $page) { |
|
67
|
|
|
$this->handlePage($page); |
|
68
|
1 |
|
} |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Page $page |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function handlePage(Page $page) |
|
75
|
|
|
{ |
|
76
|
|
|
echo $page->getUrl() . PHP_EOL; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|