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']);
73 protected function runFeedUpdate(
74 \Console_CommandLine_Result $result, Dependencies $deps
76 $tasks = array_flip(explode(',', $result->command->options['tasks']));
78 if (isset($tasks['feeds'])) {
79 $this->runFeedUpdateFeeds($deps);
81 if (isset($tasks['entries'])) {
82 $this->runFeedUpdateEntries($deps);
84 if (isset($tasks['urls'])) {
85 $this->runFeedUpdatePingUrls($deps);
90 protected function runFeedUpdateFeeds($deps)
92 $uf = new Feed_UpdateFeeds($deps);
93 if ($deps->options['feed'] === null) {
96 $urlOrIds = explode(',', $deps->options['feed']);
97 $uf->updateSome($urlOrIds);
101 protected function runFeedUpdateEntries($deps)
103 $ue = new Feed_UpdateEntries($deps);
104 if ($deps->options['entry'] === null) {
107 $urlOrIds = explode(',', $deps->options['entry']);
108 $ue->updateSome($urlOrIds);
112 protected function runFeedUpdatePingUrls($deps)
114 $uf = new Feed_PingUrls($deps);
115 if ($deps->options['entryurl'] === null) {
118 $urls = explode(',', $deps->options['entryurl']);
119 $uf->pingSome($urls);
124 protected function runPingbackHandler(
125 \Console_CommandLine_Result $command, Dependencies $deps
127 //fetch content of pingback source pages
128 $cf = new Content_Fetcher($deps);
131 $cx = new Content_Extractor($deps);
136 public function setupCli()
138 $p = new \Console_CommandLine();
139 $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
140 $p->version = '0.0.1';
145 'short_name' => '-d',
146 'long_name' => '--debug',
147 'description' => "Output debug messages",
148 'action' => 'StoreTrue'
154 'short_name' => '-f',
155 'long_name' => '--force',
156 'description' => "Update even when resource did not change",
157 'action' => 'StoreTrue'
161 $this->setupCliFeed($p);
162 $this->setupCliPingback($p);
164 $this->cliParser = $p;
167 protected function setupCliFeed($p)
169 $feed = $p->addCommand(
172 'description' => 'Edit, list or delete feeds'
176 $add = $feed->addCommand(
179 'description' => 'Add the feed',
182 $add->addArgument('feed', array('description' => 'URL of feed'));
184 $remove = $feed->addCommand(
187 'description' => 'Remove the feed',
190 $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
193 $update = $feed->addCommand(
196 'description' => 'Update feed data and send out pings'
202 'short_name' => '-i',
203 'long_name' => '--feed',
204 'description' => 'Update this feed URL or ID',
205 'help_name' => 'URL|ID',
206 'action' => 'StoreString'
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'
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',
233 'long_name' => '--list-tasks',
234 'description' => 'Show all possible tasks',
236 'action_params' => array(
237 'list' => array('feeds', 'entries', 'urls')
244 'short_name' => '-u',
245 'long_name' => '--url',
246 'description' => 'Ping this URL or ID',
247 'help_name' => 'URL|ID',
248 'action' => 'StoreString'
253 protected function setupCliPingback($p)
255 $handle = $p->addCommand(
258 'description' => 'Handle pingbacks: Fetch content, extract data'