introduce CLI update command
[stapibas.git] / src / stapibas / Cli.php
1 <?php
2 namespace stapibas;
3
4 class Cli
5 {
6     protected $cliParser;
7
8     public function __construct()
9     {
10         $this->setupCli();
11     }
12
13     public function run()
14     {
15         try {
16             $result = $this->cliParser->parse();
17         } catch (\Exception $exc) {
18             $this->cliParser->displayError($exc->getMessage());
19         }
20
21         $log = new Logger();
22         if ($result->options['debug']) {
23             $log->debug = true;
24         }
25
26         try {
27             $deps = new Dependencies();
28             $deps->db = new PDO(
29                 $GLOBALS['dbdsn'], $GLOBALS['dbuser'], $GLOBALS['dbpass']
30             );
31             $deps->log = $log;
32             $deps->options = array_merge(
33                 $result->options, $result->command->options
34             );
35
36             if ($result->command_name == 'update') {
37                 $this->runUpdate($result, $deps);
38             }
39         } catch (\Exception $e) {
40             $msg = 'stapibas exception!' . "\n"
41                 . 'Code: ' . $e->getCode() . "\n"
42                 . 'Message: ' . $e->getMessage() . "\n";
43             file_put_contents('php://stderr', $msg);
44             exit(1);
45         }
46     }
47
48     protected function runUpdate($result, $deps)
49     {
50         $tasks = array_flip(explode(',', $result->command->options['tasks']));
51
52         if (isset($tasks['feeds'])) {
53             $this->runUpdateFeeds($deps);
54         }
55         if (isset($tasks['entries'])) {
56             $this->runUpdateEntries($deps);
57         }
58         if (isset($tasks['urls'])) {
59             $this->runUpdatePingUrls($deps);
60         }
61     }
62
63     protected function runUpdateFeeds($deps)
64     {
65         $uf = new Feed_UpdateFeeds($deps);
66         if ($deps->options['feed'] === null) {
67             $uf->updateAll();
68         } else {
69             $urlOrIds = explode(',', $deps->options['feed']);
70             $uf->updateSome($urlOrIds);
71         }
72     }
73
74     protected function runUpdateEntries($deps)
75     {
76         $ue = new Feed_UpdateEntries($deps);
77         if ($deps->options['entry'] === null) {
78             $ue->updateAll();
79         } else {
80             $urlOrIds = explode(',', $deps->options['entry']);
81             $ue->updateSome($urlOrIds);
82         }
83     }
84
85     protected function runUpdatePingUrls($deps)
86     {
87         $uf = new Feed_PingUrls($deps);
88         if ($deps->options['entryurl'] === null) {
89             $uf->pingAll();
90         } else {
91             $urls = explode(',', $deps->options['entryurl']);
92             $uf->pingSome($urls);
93         }
94     }
95
96
97     public function setupCli()
98     {
99         $p = new \Console_CommandLine();
100         $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
101         $p->version = '0.0.1';
102
103         $p->addOption(
104             'debug',
105             array(
106                 'short_name'  => '-d',
107                 'long_name'   => '--debug',
108                 'description' => "Output debug messages",
109                 'action'      => 'StoreTrue'
110             )
111         );
112         $p->addOption(
113             'force',
114             array(
115                 'short_name'  => '-f',
116                 'long_name'   => '--force',
117                 'description' => "Update even when resource did not change",
118                 'action'      => 'StoreTrue'
119             )
120         );
121
122
123         $update = $p->addCommand(
124             'update',
125             array(
126                 'description' => 'Update feed data and send out pings'
127             )
128         );
129
130         $update->addOption(
131             'feed',
132             array(
133                 'short_name'  => '-i',
134                 'long_name'   => '--feed',
135                 'description' => 'Update this feed URL or ID',
136                 'help_name'   => 'URL|ID',
137                 'action'      => 'StoreString'
138             )
139         );
140
141         $update->addOption(
142             'entry',
143             array(
144                 'short_name'  => '-e',
145                 'long_name'   => '--entry',
146                 'description' => 'Update this feed entry URL or ID',
147                 'help_name'   => 'URL|ID',
148                 'action'      => 'StoreString'
149             )
150         );
151
152         $update->addOption(
153             'tasks',
154             array(
155                 'short_name'  => '-t',
156                 'long_name'   => '--tasks',
157                 'description' => 'Execute the given tasks (comma-separated: feeds,entries,urls)',
158                 'help_name'   => 'tasks',
159                 'action'      => 'StoreString',
160                 'default'     => 'feeds,entries,urls',
161             )
162         );
163         $update->addOption(
164             'list_tasks',
165             array(
166                 'long_name'   => '--list-tasks',
167                 'description' => 'Show all possible tasks',
168                 'action'      => 'List',
169                 'list'        => array('feeds', 'entries', 'urls')
170             )
171         );
172
173         $update->addOption(
174             'entryurl',
175             array(
176                 'short_name'  => '-u',
177                 'long_name'   => '--url',
178                 'description' => 'Ping this URL or ID',
179                 'help_name'   => 'URL|ID',
180                 'action'      => 'StoreString'
181             )
182         );
183
184         $this->cliParser = $p;
185     }
186
187 }
188 ?>