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 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);
87 if (isset($tasks['feeds'])) {
88 $this->runFeedUpdateFeeds($deps);
90 if (isset($tasks['entries'])) {
91 $this->runFeedUpdateEntries($deps);
93 if (isset($tasks['urls'])) {
94 $this->runFeedUpdatePingUrls($deps);
99 protected function runFeedUpdateFeeds($deps)
101 $uf = new Feed_UpdateFeeds($deps);
102 if ($deps->options['feed'] === null) {
105 $urlOrIds = explode(',', $deps->options['feed']);
106 $uf->updateSome($urlOrIds);
110 protected function runFeedUpdateEntries($deps)
112 $ue = new Feed_UpdateEntries($deps);
113 if ($deps->options['entry'] === null) {
116 $urlOrIds = explode(',', $deps->options['entry']);
117 $ue->updateSome($urlOrIds);
121 protected function runFeedUpdatePingUrls($deps)
123 $uf = new Feed_PingUrls($deps);
124 if ($deps->options['entryurl'] === null) {
127 $urls = explode(',', $deps->options['entryurl']);
128 $uf->pingSome($urls);
133 protected function runPingbackHandler(
134 \Console_CommandLine_Result $command, Dependencies $deps
136 //fetch content of pingback source pages
137 $cf = new Content_Fetcher($deps);
140 $cx = new Content_Extractor($deps);
145 public function setupCli()
147 $p = new \Console_CommandLine();
148 $p->description = 'Sends pingbacks to URLs linked in Atom feed entries';
149 $p->version = '0.0.1';
154 'short_name' => '-d',
155 'long_name' => '--debug',
156 'description' => "Output debug messages",
157 'action' => 'StoreTrue'
163 'short_name' => '-f',
164 'long_name' => '--force',
165 'description' => "Update even when resource did not change",
166 'action' => 'StoreTrue'
170 $this->setupCliFeed($p);
171 $this->setupCliPingback($p);
173 $this->cliParser = $p;
176 protected function setupCliFeed($p)
178 $feed = $p->addCommand(
181 'description' => 'Edit, list or delete feeds'
185 $add = $feed->addCommand(
188 'description' => 'Add the feed',
191 $add->addArgument('feed', array('description' => 'URL of feed'));
193 $remove = $feed->addCommand(
196 'description' => 'Remove the feed',
199 $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
202 $update = $feed->addCommand(
205 'description' => 'Update feed data and send out pings'
211 'short_name' => '-i',
212 'long_name' => '--feed',
213 'description' => 'Update this feed URL or ID',
214 'help_name' => 'URL|ID',
215 'action' => 'StoreString'
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'
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',
242 'long_name' => '--list-tasks',
243 'description' => 'Show all possible tasks',
245 'action_params' => array(
246 'list' => array('feeds', 'entries', 'urls')
253 'short_name' => '-u',
254 'long_name' => '--url',
255 'description' => 'Ping this URL or ID',
256 'help_name' => 'URL|ID',
257 'action' => 'StoreString'
262 protected function setupCliPingback($p)
264 $handle = $p->addCommand(
267 'description' => 'Handle pingbacks: Fetch content, extract data'