add log file support
[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
57 try {
58     $actions = array();
59     foreach ($res->args['actions'] as $action) {
60         if ($action == 'crawl') {
61             $crawler = new Crawler();
62             $crawler->setShowLinksOnly($res->options['showLinksOnly']);
63             $actions[$action] = $crawler;
64         } else if ($action == 'index') {
65             $actions[$action] = new Indexer();
66         }
67     }
68
69     $fetcher   = new Fetcher();
70     $retrieved = $fetcher->fetch($url, $actions, $res->options['force']);
71     if ($retrieved === false) {
72         exit(0);
73     }
74
75     $update = false;
76     foreach ($actions as $key => $action) {
77         Log::info("step: $key");
78         $update |= $action->run($retrieved);
79     }
80
81     if ($update) {
82         //FIXME: update index if it exists already
83         $fetcher->storeDoc($retrieved->url, $retrieved->esDoc);
84     } else {
85         Log::info("Not updating");
86     }
87 } catch (\Exception $e) {
88     Log::error($e->getMessage());
89     exit(10);
90 }
91 ?>