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