8 public function __construct()
16 $result = $this->cliParser->parse();
17 } catch (\Exception $exc) {
18 $this->cliParser->displayError($exc->getMessage());
22 if ($result->options['debug']) {
27 $deps = new Dependencies();
29 $GLOBALS['dbdsn'], $GLOBALS['dbuser'], $GLOBALS['dbpass']
32 $deps->options = array_merge(
34 $result->command ? $result->command->options : array(),
35 ($result->command && $result->command->command)
36 ? $result->command->command->options
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);
45 $this->cliParser->displayUsage(1);
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);
56 protected function runFeed(
57 \Console_CommandLine_Result $command, Dependencies $deps
59 if ($command->command_name == 'update') {
60 return $this->runFeedUpdate($command, $deps);
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') {
71 $this->cliParser->commands['feed']->displayUsage(1);
75 protected function runFeedUpdate(
76 \Console_CommandLine_Result $result, Dependencies $deps
78 $tasks = array_flip(explode(',', $result->command->options['tasks']));
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);
89 if (isset($tasks['feeds'])) {
90 $this->runFeedUpdateFeeds($deps);
92 if (isset($tasks['entries'])) {
93 $this->runFeedUpdateEntries($deps);
95 if (isset($tasks['urls'])) {
96 $this->runFeedUpdatePingUrls($deps);
101 protected function runFeedUpdateFeeds($deps)
103 $uf = new Feed_UpdateFeeds($deps);
104 if ($deps->options['feed'] === null) {
107 $urlOrIds = explode(',', $deps->options['feed']);
108 $uf->updateSome($urlOrIds);
112 protected function runFeedUpdateEntries($deps)
114 $ue = new Feed_UpdateEntries($deps);
115 if ($deps->options['entry'] === null) {
118 $urlOrIds = explode(',', $deps->options['entry']);
119 $ue->updateSome($urlOrIds);
123 protected function runFeedUpdatePingUrls($deps)
125 $uf = new Feed_PingUrls($deps);
126 if ($deps->options['entryurl'] === null) {
129 $urls = explode(',', $deps->options['entryurl']);
130 $uf->pingSome($urls);
135 protected function runPingbackHandler(
136 \Console_CommandLine_Result $command, Dependencies $deps
138 //fetch content of pingback source pages
139 $cf = new Content_Fetcher($deps);
142 $cx = new Content_Extractor($deps);
147 public function setupCli()
149 $p = new \Console_CommandLine();
150 $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
151 $p->version = '0.0.1';
156 'short_name' => '-d',
157 'long_name' => '--debug',
158 'description' => "Output debug messages",
159 'action' => 'StoreTrue'
165 'short_name' => '-f',
166 'long_name' => '--force',
167 'description' => "Update even when resource did not change",
168 'action' => 'StoreTrue'
172 $this->setupCliFeed($p);
173 $this->setupCliPingback($p);
175 $this->cliParser = $p;
178 protected function setupCliFeed($p)
180 $feed = $p->addCommand(
183 'description' => 'Edit, list or delete feeds'
187 $add = $feed->addCommand(
190 'description' => 'Add the feed',
193 $add->addArgument('feed', array('description' => 'URL of feed'));
198 'description' => 'List all feeds',
202 $remove = $feed->addCommand(
205 'description' => 'Remove the feed',
208 $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
211 $update = $feed->addCommand(
214 'description' => 'Update feed data and send out pings'
220 'short_name' => '-i',
221 'long_name' => '--feed',
222 'description' => 'Update this feed URL or ID',
223 'help_name' => 'URL|ID',
224 'action' => 'StoreString'
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'
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',
251 'long_name' => '--list-tasks',
252 'description' => 'Show all possible tasks',
254 'action_params' => array(
255 'list' => array('feeds', 'entries', 'urls')
262 'short_name' => '-u',
263 'long_name' => '--url',
264 'description' => 'Ping this URL or ID',
265 'help_name' => 'URL|ID',
266 'action' => 'StoreString'
271 protected function setupCliPingback($p)
273 $handle = $p->addCommand(
276 'description' => 'Handle pingbacks: Fetch content, extract data'