4a360502bb2e5ee86307627520d210567175f0d5
[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         //FIXME
128     }
129
130
131     public function setupCli()
132     {
133         $p = new \Console_CommandLine();
134         $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
135         $p->version = '0.0.1';
136
137         $p->addOption(
138             'debug',
139             array(
140                 'short_name'  => '-d',
141                 'long_name'   => '--debug',
142                 'description' => "Output debug messages",
143                 'action'      => 'StoreTrue'
144             )
145         );
146         $p->addOption(
147             'force',
148             array(
149                 'short_name'  => '-f',
150                 'long_name'   => '--force',
151                 'description' => "Update even when resource did not change",
152                 'action'      => 'StoreTrue'
153             )
154         );
155
156         $this->setupCliFeed($p);
157         $this->setupCliPingback($p);
158
159         $this->cliParser = $p;
160     }
161
162     protected function setupCliFeed($p)
163     {
164         $feed = $p->addCommand(
165             'feed',
166             array(
167                 'description' => 'Edit, list or delete feeds'
168             )
169         );
170
171         $add = $feed->addCommand(
172             'add',
173             array(
174                 'description' => 'Add the feed',
175             )
176         );
177         $add->addArgument('feed', array('description' => 'URL of feed'));
178
179         $remove = $feed->addCommand(
180             'remove',
181             array(
182                 'description' => 'Remove the feed',
183             )
184         );
185         $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
186         
187
188         $update = $feed->addCommand(
189             'update',
190             array(
191                 'description' => 'Update feed data and send out pings'
192             )
193         );
194         $update->addOption(
195             'feed',
196             array(
197                 'short_name'  => '-i',
198                 'long_name'   => '--feed',
199                 'description' => 'Update this feed URL or ID',
200                 'help_name'   => 'URL|ID',
201                 'action'      => 'StoreString'
202             )
203         );
204         $update->addOption(
205             'entry',
206             array(
207                 'short_name'  => '-e',
208                 'long_name'   => '--entry',
209                 'description' => 'Update this feed entry URL or ID',
210                 'help_name'   => 'URL|ID',
211                 'action'      => 'StoreString'
212             )
213         );
214         $update->addOption(
215             'tasks',
216             array(
217                 'short_name'  => '-t',
218                 'long_name'   => '--tasks',
219                 'description' => 'Execute the given tasks (comma-separated: feeds,entries,urls)',
220                 'help_name'   => 'tasks',
221                 'action'      => 'StoreString',
222                 'default'     => 'feeds,entries,urls',
223             )
224         );
225         $update->addOption(
226             'list_tasks',
227             array(
228                 'long_name'     => '--list-tasks',
229                 'description'   => 'Show all possible tasks',
230                 'action'        => 'List',
231                 'action_params' => array(
232                     'list' => array('feeds', 'entries', 'urls')
233                 )
234             )
235         );
236         $update->addOption(
237             'entryurl',
238             array(
239                 'short_name'  => '-u',
240                 'long_name'   => '--url',
241                 'description' => 'Ping this URL or ID',
242                 'help_name'   => 'URL|ID',
243                 'action'      => 'StoreString'
244             )
245         );
246     }
247
248     protected function setupCliPingback($p)
249     {
250         $handle = $p->addCommand(
251             'handle',
252             array(
253                 'description' => 'Handle pingbacks: Fetch content, extract data'
254             )
255         );
256     }
257 }
258 ?>