);
$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"
}
}
- 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'])) {
}
+ 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();
)
);
+ $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',
--- /dev/null
+<?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);;
+ }
+}
+?>