blacklist config option is not used
[phinde.git] / bin / process.php
1 #!/usr/bin/env php
2 <?php
3 namespace phinde;
4 require_once __DIR__ . '/../src/init.php';
5
6 $cc = new \Console_CommandLine();
7 $cc->description = 'phinde URL processor';
8 $cc->version = '0.0.1';
9 $cc->addOption(
10     'force',
11     array(
12         'short_name'  => '-f',
13         'long_name'   => '--force',
14         'description' => 'Always process URL, even when it did not change',
15         'action'      => 'StoreTrue',
16         'default'     => false
17     )
18 );
19 $cc->addOption(
20     'showLinksOnly',
21     array(
22         'short_name'  => '-s',
23         'long_name'   => '--show-links',
24         'description' => 'Only show which URLs were found',
25         'action'      => 'StoreTrue',
26         'default'     => false
27     )
28 );
29 $cc->addArgument(
30     'url',
31     array(
32         'description' => 'URL to process',
33         'multiple'    => false
34     )
35 );
36 $cc->addArgument(
37     'actions',
38     array(
39         'description' => 'Actions to take',
40         'multiple'    => true,
41         'optional'    => true,
42         'choices'     => array('index', 'crawl'),
43         'default'     => array('index', 'crawl'),
44     )
45 );
46 try {
47     $res = $cc->parse();
48 } catch (\Exception $e) {
49     $cc->displayError($e->getMessage());
50 }
51
52 $url = $res->args['url'];
53 $url = Helper::addSchema($url);
54 $urlObj = new \Net_URL2($url);
55 $url = $urlObj->getNormalizedURL();
56 if (!Helper::isUrlAllowed($url)) {
57     Log::error("Domain is not allowed; not crawling");
58     exit(2);
59 }
60
61 try {
62     $actions = array();
63     foreach ($res->args['actions'] as $action) {
64         if ($action == 'crawl') {
65             $crawler = new Crawler();
66             $crawler->setShowLinksOnly($res->options['showLinksOnly']);
67             $actions[$action] = $crawler;
68         } else if ($action == 'index') {
69             $actions[$action] = new Indexer();
70         }
71     }
72
73     $fetcher   = new Fetcher();
74     $retrieved = $fetcher->fetch($url, $actions, $res->options['force']);
75     if ($retrieved === false) {
76         exit(0);
77     }
78
79     $update = false;
80     foreach ($actions as $key => $action) {
81         Log::info("step: $key");
82         $update |= $action->run($retrieved);
83     }
84
85     if ($update) {
86         //FIXME: update index if it exists already
87         $fetcher->storeDoc($retrieved->url, $retrieved->esDoc);
88     } else {
89         Log::info("Not updating");
90     }
91 } catch (\Exception $e) {
92     Log::error($e->getMessage());
93     exit(10);
94 }
95 ?>