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