add feed/list command, show help when no feed command given
[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 if ($command->command_name == 'list') {
69             $mg->listAll();
70         } else {
71             $this->cliParser->commands['feed']->displayUsage(1);
72         }
73     }
74
75     protected function runFeedUpdate(
76         \Console_CommandLine_Result $result, Dependencies $deps
77     ) {
78         $tasks = array_flip(explode(',', $result->command->options['tasks']));
79
80         //if an ID/url is given, only execute the matching task
81         if (isset($result->command->options['feed'])) {
82             $tasks = array('feeds' => 1);
83         } else if (isset($result->command->options['entry'])) {
84             $tasks = array('entries' => 1);
85         } else if (isset($result->command->options['entryurl'])) {
86             $tasks = array('urls' => 1);
87         }
88
89         if (isset($tasks['feeds'])) {
90             $this->runFeedUpdateFeeds($deps);
91         }
92         if (isset($tasks['entries'])) {
93             $this->runFeedUpdateEntries($deps);
94         }
95         if (isset($tasks['urls'])) {
96             $this->runFeedUpdatePingUrls($deps);
97         }
98     }
99
100
101     protected function runFeedUpdateFeeds($deps)
102     {
103         $uf = new Feed_UpdateFeeds($deps);
104         if ($deps->options['feed'] === null) {
105             $uf->updateAll();
106         } else {
107             $urlOrIds = explode(',', $deps->options['feed']);
108             $uf->updateSome($urlOrIds);
109         }
110     }
111
112     protected function runFeedUpdateEntries($deps)
113     {
114         $ue = new Feed_UpdateEntries($deps);
115         if ($deps->options['entry'] === null) {
116             $ue->updateAll();
117         } else {
118             $urlOrIds = explode(',', $deps->options['entry']);
119             $ue->updateSome($urlOrIds);
120         }
121     }
122
123     protected function runFeedUpdatePingUrls($deps)
124     {
125         $uf = new Feed_PingUrls($deps);
126         if ($deps->options['entryurl'] === null) {
127             $uf->pingAll();
128         } else {
129             $urls = explode(',', $deps->options['entryurl']);
130             $uf->pingSome($urls);
131         }
132     }
133
134
135     protected function runPingbackHandler(
136         \Console_CommandLine_Result $command, Dependencies $deps
137     ) {
138         //fetch content of pingback source pages
139         $cf = new Content_Fetcher($deps);
140         $cf->updateAll();
141
142         $cx = new Content_Extractor($deps);
143         $cx->updateAll();
144     }
145
146
147     public function setupCli()
148     {
149         $p = new \Console_CommandLine();
150         $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
151         $p->version = '0.0.1';
152
153         $p->addOption(
154             'debug',
155             array(
156                 'short_name'  => '-d',
157                 'long_name'   => '--debug',
158                 'description' => "Output debug messages",
159                 'action'      => 'StoreTrue'
160             )
161         );
162         $p->addOption(
163             'force',
164             array(
165                 'short_name'  => '-f',
166                 'long_name'   => '--force',
167                 'description' => "Update even when resource did not change",
168                 'action'      => 'StoreTrue'
169             )
170         );
171
172         $this->setupCliFeed($p);
173         $this->setupCliPingback($p);
174
175         $this->cliParser = $p;
176     }
177
178     protected function setupCliFeed($p)
179     {
180         $feed = $p->addCommand(
181             'feed',
182             array(
183                 'description' => 'Edit, list or delete feeds'
184             )
185         );
186
187         $add = $feed->addCommand(
188             'add',
189             array(
190                 'description' => 'Add the feed',
191             )
192         );
193         $add->addArgument('feed', array('description' => 'URL of feed'));
194
195         $feed->addCommand(
196             'list',
197             array(
198                 'description' => 'List all feeds',
199             )
200         );
201
202         $remove = $feed->addCommand(
203             'remove',
204             array(
205                 'description' => 'Remove the feed',
206             )
207         );
208         $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
209         
210
211         $update = $feed->addCommand(
212             'update',
213             array(
214                 'description' => 'Update feed data and send out pings'
215             )
216         );
217         $update->addOption(
218             'feed',
219             array(
220                 'short_name'  => '-i',
221                 'long_name'   => '--feed',
222                 'description' => 'Update this feed URL or ID',
223                 'help_name'   => 'URL|ID',
224                 'action'      => 'StoreString'
225             )
226         );
227         $update->addOption(
228             'entry',
229             array(
230                 'short_name'  => '-e',
231                 'long_name'   => '--entry',
232                 'description' => 'Update this feed entry URL or ID',
233                 'help_name'   => 'URL|ID',
234                 'action'      => 'StoreString'
235             )
236         );
237         $update->addOption(
238             'tasks',
239             array(
240                 'short_name'  => '-t',
241                 'long_name'   => '--tasks',
242                 'description' => 'Execute the given tasks (comma-separated: feeds,entries,urls)',
243                 'help_name'   => 'tasks',
244                 'action'      => 'StoreString',
245                 'default'     => 'feeds,entries,urls',
246             )
247         );
248         $update->addOption(
249             'list_tasks',
250             array(
251                 'long_name'     => '--list-tasks',
252                 'description'   => 'Show all possible tasks',
253                 'action'        => 'List',
254                 'action_params' => array(
255                     'list' => array('feeds', 'entries', 'urls')
256                 )
257             )
258         );
259         $update->addOption(
260             'entryurl',
261             array(
262                 'short_name'  => '-u',
263                 'long_name'   => '--url',
264                 'description' => 'Ping this URL or ID',
265                 'help_name'   => 'URL|ID',
266                 'action'      => 'StoreString'
267             )
268         );
269     }
270
271     protected function setupCliPingback($p)
272     {
273         $handle = $p->addCommand(
274             'handle',
275             array(
276                 'description' => 'Handle pingbacks: Fetch content, extract data'
277             )
278         );
279     }
280 }
281 ?>