first version of comment extraction
[stapibas.git] / src / stapibas / Cli.php
index 683a726e9a1fdcb8cf7ccceb7174a60de57ee4e8..52c7143761bf1031081513b6d4c9fdb8af7d9ad8 100644 (file)
@@ -29,18 +29,20 @@ class Cli
                 $GLOBALS['dbdsn'], $GLOBALS['dbuser'], $GLOBALS['dbpass']
             );
             $deps->log = $log;
-            $deps->options = $result->options;
-
-            $tasks = array_flip(explode(',', $result->options['tasks']));
+            $deps->options = array_merge(
+                $result->options,
+                $result->command ? $result->command->options : array(),
+                ($result->command && $result->command->command)
+                    ? $result->command->command->options
+                    : array()
+            );
 
-            if (isset($tasks['feeds'])) {
-                $this->runUpdateFeeds($deps);
-            }
-            if (isset($tasks['entries'])) {
-                $this->runUpdateEntries($deps);
-            }
-            if (isset($tasks['urls'])) {
-                $this->runPingUrls($deps);
+            if ($result->command_name == 'feed') {
+                $this->runFeed($result->command, $deps);
+            } else if ($result->command_name == 'handle') {
+                $this->runPingbackHandler($result->command, $deps);
+            } else {
+                $this->cliParser->displayUsage(1);
             }
         } catch (\Exception $e) {
             $msg = 'stapibas exception!' . "\n"
@@ -51,7 +53,41 @@ class Cli
         }
     }
 
-    protected function runUpdateFeeds($deps)
+    protected function runFeed(
+        \Console_CommandLine_Result $command, Dependencies $deps
+    ) {
+        if ($command->command_name == 'update') {
+            return $this->runFeedUpdate($command, $deps);
+        }
+
+        $mg = new Feed_Manage($deps);
+        if ($command->command_name == 'add') {
+            $mg->addFeed($command->command->args['feed']);
+        } else if ($command->command_name ==  'remove') {
+            $mg->removeFeed($command->command->args['feed']);
+        } else {
+            $mg->listAll();
+        }
+    }
+
+    protected function runFeedUpdate(
+        \Console_CommandLine_Result $result, Dependencies $deps
+    ) {
+        $tasks = array_flip(explode(',', $result->command->options['tasks']));
+
+        if (isset($tasks['feeds'])) {
+            $this->runFeedUpdateFeeds($deps);
+        }
+        if (isset($tasks['entries'])) {
+            $this->runFeedUpdateEntries($deps);
+        }
+        if (isset($tasks['urls'])) {
+            $this->runFeedUpdatePingUrls($deps);
+        }
+    }
+
+
+    protected function runFeedUpdateFeeds($deps)
     {
         $uf = new Feed_UpdateFeeds($deps);
         if ($deps->options['feed'] === null) {
@@ -62,7 +98,7 @@ class Cli
         }
     }
 
-    protected function runUpdateEntries($deps)
+    protected function runFeedUpdateEntries($deps)
     {
         $ue = new Feed_UpdateEntries($deps);
         if ($deps->options['entry'] === null) {
@@ -73,7 +109,7 @@ class Cli
         }
     }
 
-    protected function runPingUrls($deps)
+    protected function runFeedUpdatePingUrls($deps)
     {
         $uf = new Feed_PingUrls($deps);
         if ($deps->options['entryurl'] === null) {
@@ -85,6 +121,18 @@ class Cli
     }
 
 
+    protected function runPingbackHandler(
+        \Console_CommandLine_Result $command, Dependencies $deps
+    ) {
+        //fetch content of pingback source pages
+        $cf = new Content_Fetcher($deps);
+        $cf->updateAll();
+
+        $cx = new Content_Extractor($deps);
+        $cx->updateAll();
+    }
+
+
     public function setupCli()
     {
         $p = new \Console_CommandLine();
@@ -92,6 +140,63 @@ class Cli
         $p->version = '0.0.1';
 
         $p->addOption(
+            'debug',
+            array(
+                'short_name'  => '-d',
+                'long_name'   => '--debug',
+                'description' => "Output debug messages",
+                'action'      => 'StoreTrue'
+            )
+        );
+        $p->addOption(
+            'force',
+            array(
+                'short_name'  => '-f',
+                'long_name'   => '--force',
+                'description' => "Update even when resource did not change",
+                'action'      => 'StoreTrue'
+            )
+        );
+
+        $this->setupCliFeed($p);
+        $this->setupCliPingback($p);
+
+        $this->cliParser = $p;
+    }
+
+    protected function setupCliFeed($p)
+    {
+        $feed = $p->addCommand(
+            'feed',
+            array(
+                'description' => 'Edit, list or delete feeds'
+            )
+        );
+
+        $add = $feed->addCommand(
+            'add',
+            array(
+                'description' => 'Add the feed',
+            )
+        );
+        $add->addArgument('feed', array('description' => 'URL of feed'));
+
+        $remove = $feed->addCommand(
+            'remove',
+            array(
+                'description' => 'Remove the feed',
+            )
+        );
+        $remove->addArgument('feed', array('description' => 'URL or ID of feed'));
+        
+
+        $update = $feed->addCommand(
+            'update',
+            array(
+                'description' => 'Update feed data and send out pings'
+            )
+        );
+        $update->addOption(
             'feed',
             array(
                 'short_name'  => '-i',
@@ -101,8 +206,7 @@ class Cli
                 'action'      => 'StoreString'
             )
         );
-
-        $p->addOption(
+        $update->addOption(
             'entry',
             array(
                 'short_name'  => '-e',
@@ -112,29 +216,29 @@ class Cli
                 'action'      => 'StoreString'
             )
         );
-
-        $p->addOption(
+        $update->addOption(
             'tasks',
             array(
                 'short_name'  => '-t',
                 'long_name'   => '--tasks',
-                'description' => 'Execute the given tasks (comma-separated)',
+                'description' => 'Execute the given tasks (comma-separated: feeds,entries,urls)',
                 'help_name'   => 'tasks',
                 'action'      => 'StoreString',
                 'default'     => 'feeds,entries,urls',
             )
         );
-        $p->addOption(
+        $update->addOption(
             'list_tasks',
             array(
-                'long_name'   => '--list-tasks',
-                'description' => 'Show all possible tasks',
-                'action'      => 'List',
-                'list'        => array('feeds', 'entries', 'urls')
+                'long_name'     => '--list-tasks',
+                'description'   => 'Show all possible tasks',
+                'action'        => 'List',
+                'action_params' => array(
+                    'list' => array('feeds', 'entries', 'urls')
+                )
             )
         );
-
-        $p->addOption(
+        $update->addOption(
             'entryurl',
             array(
                 'short_name'  => '-u',
@@ -144,29 +248,16 @@ class Cli
                 'action'      => 'StoreString'
             )
         );
+    }
 
-
-        $p->addOption(
-            'debug',
-            array(
-                'short_name'  => '-d',
-                'long_name'   => '--debug',
-                'description' => "Output debug messages",
-                'action'      => 'StoreTrue'
-            )
-        );
-        $p->addOption(
-            'force',
+    protected function setupCliPingback($p)
+    {
+        $handle = $p->addCommand(
+            'handle',
             array(
-                'short_name'  => '-f',
-                'long_name'   => '--force',
-                'description' => "Update even when resource did not change",
-                'action'      => 'StoreTrue'
+                'description' => 'Handle pingbacks: Fetch content, extract data'
             )
         );
-
-        $this->cliParser = $p;
     }
-
 }
 ?>