feed management interface for CLI
authorChristian Weiske <cweiske@cweiske.de>
Sat, 15 Jun 2013 13:35:54 +0000 (15:35 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sat, 15 Jun 2013 13:35:54 +0000 (15:35 +0200)
src/stapibas/Cli.php
src/stapibas/Feed/Manage.php [new file with mode: 0644]

index 07be39f2adce35f74052034baf3d4f8b989fa5db..86a427276e57cadda3c44afe1307cc5ed7b1aaec 100644 (file)
@@ -30,11 +30,14 @@ class Cli
             );
             $deps->log = $log;
             $deps->options = array_merge(
-                $result->options, $result->command->options
+                $result->options,
+                $result->command ? $result->command->options : array()
             );
 
             if ($result->command_name == 'update') {
                 $this->runUpdate($result, $deps);
+            } else if ($result->command_name == 'feed') {
+                $this->manageFeed($result, $deps);
             }
         } catch (\Exception $e) {
             $msg = 'stapibas exception!' . "\n"
@@ -45,8 +48,9 @@ class Cli
         }
     }
 
-    protected function runUpdate($result, $deps)
-    {
+    protected function runUpdate(
+        \Console_CommandLine_Result $result, Dependencies $deps
+    ) {
         $tasks = array_flip(explode(',', $result->command->options['tasks']));
 
         if (isset($tasks['feeds'])) {
@@ -94,6 +98,20 @@ class Cli
     }
 
 
+    protected function manageFeed(
+        \Console_CommandLine_Result $result, Dependencies $deps
+    ) {
+        $mg = new Feed_Manage($deps);
+        if ($deps->options['add']) {
+            $mg->addFeed($result->command->args['feed']);
+        } else if ($deps->options['remove']) {
+            $mg->removeFeed($result->command->args['feed']);
+        } else {
+            $mg->listAll();
+        }
+    }
+
+
     public function setupCli()
     {
         $p = new \Console_CommandLine();
@@ -119,6 +137,38 @@ class Cli
             )
         );
 
+        $feed = $p->addCommand(
+            'feed',
+            array(
+                'description' => 'Edit, list or delete feeds'
+            )
+        );
+        $feed->addOption(
+            'add',
+            array(
+                'short_name'  => '-a',
+                'long_name'   => '--add',
+                'description' => 'Add the feed',
+                'action'      => 'StoreTrue'
+            )
+        );
+        $feed->addOption(
+            'remove',
+            array(
+                'short_name'  => '-r',
+                'long_name'   => '--remove',
+                'description' => 'Remove the feed',
+                'action'      => 'StoreTrue'
+            )
+        );
+        $feed->addArgument(
+            'feed',
+            array(
+                'description' => 'URL or ID of feed',
+                'optional' => true
+            )
+        );
+        
 
         $update = $p->addCommand(
             'update',
diff --git a/src/stapibas/Feed/Manage.php b/src/stapibas/Feed/Manage.php
new file mode 100644 (file)
index 0000000..0db50ab
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+namespace stapibas;
+
+class Feed_Manage
+{
+    public $db;
+    public $log;
+
+    public function __construct(Dependencies $deps)
+    {
+        $this->deps = $deps;
+        $this->db   = $deps->db;
+        $this->log  = $deps->log;
+    }
+
+    public function listAll()
+    {
+        $this->log->info('Listing all feeds..');
+        $res = $this->db->query('SELECT * FROM feeds ORDER BY f_id');
+        $items = 0;
+        while ($feedRow = $res->fetch(\PDO::FETCH_OBJ)) {
+            echo '#' . $feedRow->f_id . ' ' . $feedRow->f_url . "\n";
+            ++$items;
+        }
+        $this->log->info('Finished pinging %d URLs.', $items);
+    }
+
+    public function addFeed($url)
+    {
+        if ($url == '') {
+            echo "URL empty\n";
+            exit(1);
+        }
+
+        $this->db->exec(
+            'INSERT INTO feeds SET'
+            . '  f_url = ' . $this->db->quote($url)
+            . ', f_needs_update = 1'
+        );
+        echo "Feed has been added\n";
+    }
+
+    public function removeFeed($urlOrId)
+    {
+        if ($urlOrId == '') {
+            echo "URL/ID empty\n";
+            exit(1);
+        }
+
+        if (is_numeric($urlOrId)) {
+            $sqlWhere = ' f_id = ' . $this->db->quote($urlOrId);
+        } else {
+            $sqlWhere = ' f_url = ' . $this->db->quote($urlOrId);
+        }
+
+        $nRows = $this->db->exec(
+            'DELETE FROM feeds WHERE' . $sqlWhere
+        );
+        echo sprintf("%d feed has been removed\n", $nRows);;
+    }
+}
+?>