02942e4593147f4d1b7e71e2a138f3efa590e508
[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,
34                 $result->command ? $result->command->options : array(),
35                 ($result->command && $result->command->command)
36                     ? $result->command->command->options
37                     : array()
38             );
39
40             if ($result->command_name == 'feed') {
41                 $this->runFeed($result->command, $deps);
42             } else if ($result->command_name == 'handle') {
43                 $this->runPingbackHandler($result->command, $deps);
44             } else {
45                 $this->cliParser->displayUsage(1);
46             }
47         } catch (\Exception $e) {
48             $msg = 'stapibas exception!' . "\n"
49                 . 'Code: ' . $e->getCode() . "\n"
50                 . 'Message: ' . $e->getMessage() . "\n";
51             file_put_contents('php://stderr', $msg);
52             exit(1);
53         }
54     }
55
56     protected function runFeed(
57         \Console_CommandLine_Result $command, Dependencies $deps
58     ) {
59         if ($command->command_name == 'update') {
60             return $this->runFeedUpdate($command, $deps);
61         }
62
63         $mg = new Feed_Manage($deps);
64         if ($command->command_name == 'add') {
65             $mg->addFeed($command->command->args['feed']);
66         } else if ($command->command_name ==  'remove') {
67             $mg->removeFeed($command->command->args['feed']);
68         } else {
69             $mg->listAll();
70         }
71     }
72
73     protected function runFeedUpdate(
74         \Console_CommandLine_Result $result, Dependencies $deps
75     ) {
76         $tasks = array_flip(explode(',', $result->command->options['tasks']));
77
78         if (isset($tasks['feeds'])) {
79             $this->runFeedUpdateFeeds($deps);
80         }
81         if (isset($tasks['entries'])) {
82             $this->runFeedUpdateEntries($deps);
83         }
84         if (isset($tasks['urls'])) {
85             $this->runFeedUpdatePingUrls($deps);
86         }
87     }
88
89
90     protected function runFeedUpdateFeeds($deps)
91     {
92         $uf = new Feed_UpdateFeeds($deps);
93         if ($deps->options['feed'] === null) {
94             $uf->updateAll();
95         } else {
96             $urlOrIds = explode(',', $deps->options['feed']);
97             $uf->updateSome($urlOrIds);
98         }
99     }
100
101     protected function runFeedUpdateEntries($deps)
102     {
103         $ue = new Feed_UpdateEntries($deps);
104         if ($deps->options['entry'] === null) {
105             $ue->updateAll();
106         } else {
107             $urlOrIds = explode(',', $deps->options['entry']);
108             $ue->updateSome($urlOrIds);
109         }
110     }
111
112     protected function runFeedUpdatePingUrls($deps)
113     {
114         $uf = new Feed_PingUrls($deps);
115         if ($deps->options['entryurl'] === null) {
116             $uf->pingAll();
117         } else {
118             $urls = explode(',', $deps->options['entryurl']);
119             $uf->pingSome($urls);
120         }
121     }
122
123
124     protected function runPingbackHandler(
125         \Console_CommandLine_Result $command, Dependencies $deps
126     ) {
127         //fetch content of pingback source pages
128         $cf = new Pingback_ContentFetcher($deps);
129         $cf->updateAll();
130
131         //FIXME
132     }
133
134
135     public function setupCli()
136     {
137         $p = new \Console_CommandLine();
138         $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
139         $p->version = '0.0.1';
140
141         $p->addOption(
142             'debug',
143             array(
144                 'short_name'  => '-d',
145                 'long_name'   => '--debug',
146                 'description' => "Output debug messages",
147                 'action'      => 'StoreTrue'
148             )
149         );
150         $p->addOption(
151             'force',
152             array(
153                 'short_name'  => '-f',
154                 'long_name'   => '--force',
155                 'description' => "Update even when resource did not change",
156                 'action'      => 'StoreTrue'
157             )
158         );
159
160         $this->setupCliFeed($p);
161         $this->setupCliPingback($p);
162
163         $this->cliParser = $p;
164     }
165
166     protected function setupCliFeed($p)
167     {
168         $feed = $p->addCommand(
169             'feed',
170             array(
171                 'description' => 'Edit, list or delete feeds'
172             )
173         );
174
175         $add = $feed->addCommand(
176             'add',
177             array(
178                 'description' => 'Add the feed',
179             )
180         );
181         $add->addArgument('feed', array('description' => 'URL of feed'));
182
183         $remove = $feed->addCommand(
184             'remove',
185             array(
186                 'description' => 'Remove the feed',
187             )
188         );
189         $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
190         
191
192         $update = $feed->addCommand(
193             'update',
194             array(
195                 'description' => 'Update feed data and send out pings'
196             )
197         );
198         $update->addOption(
199             'feed',
200             array(
201                 'short_name'  => '-i',
202                 'long_name'   => '--feed',
203                 'description' => 'Update this feed URL or ID',
204                 'help_name'   => 'URL|ID',
205                 'action'      => 'StoreString'
206             )
207         );
208         $update->addOption(
209             'entry',
210             array(
211                 'short_name'  => '-e',
212                 'long_name'   => '--entry',
213                 'description' => 'Update this feed entry URL or ID',
214                 'help_name'   => 'URL|ID',
215                 'action'      => 'StoreString'
216             )
217         );
218         $update->addOption(
219             'tasks',
220             array(
221                 'short_name'  => '-t',
222                 'long_name'   => '--tasks',
223                 'description' => 'Execute the given tasks (comma-separated: feeds,entries,urls)',
224                 'help_name'   => 'tasks',
225                 'action'      => 'StoreString',
226                 'default'     => 'feeds,entries,urls',
227             )
228         );
229         $update->addOption(
230             'list_tasks',
231             array(
232                 'long_name'     => '--list-tasks',
233                 'description'   => 'Show all possible tasks',
234                 'action'        => 'List',
235                 'action_params' => array(
236                     'list' => array('feeds', 'entries', 'urls')
237                 )
238             )
239         );
240         $update->addOption(
241             'entryurl',
242             array(
243                 'short_name'  => '-u',
244                 'long_name'   => '--url',
245                 'description' => 'Ping this URL or ID',
246                 'help_name'   => 'URL|ID',
247                 'action'      => 'StoreString'
248             )
249         );
250     }
251
252     protected function setupCliPingback($p)
253     {
254         $handle = $p->addCommand(
255             'handle',
256             array(
257                 'description' => 'Handle pingbacks: Fetch content, extract data'
258             )
259         );
260     }
261 }
262 ?>