0db50ab56b97a488b11dbdb39df444ac0281daca
[stapibas.git] / src / stapibas / Feed / Manage.php
1 <?php
2 namespace stapibas;
3
4 class Feed_Manage
5 {
6     public $db;
7     public $log;
8
9     public function __construct(Dependencies $deps)
10     {
11         $this->deps = $deps;
12         $this->db   = $deps->db;
13         $this->log  = $deps->log;
14     }
15
16     public function listAll()
17     {
18         $this->log->info('Listing all feeds..');
19         $res = $this->db->query('SELECT * FROM feeds ORDER BY f_id');
20         $items = 0;
21         while ($feedRow = $res->fetch(\PDO::FETCH_OBJ)) {
22             echo '#' . $feedRow->f_id . ' ' . $feedRow->f_url . "\n";
23             ++$items;
24         }
25         $this->log->info('Finished pinging %d URLs.', $items);
26     }
27
28     public function addFeed($url)
29     {
30         if ($url == '') {
31             echo "URL empty\n";
32             exit(1);
33         }
34
35         $this->db->exec(
36             'INSERT INTO feeds SET'
37             . '  f_url = ' . $this->db->quote($url)
38             . ', f_needs_update = 1'
39         );
40         echo "Feed has been added\n";
41     }
42
43     public function removeFeed($urlOrId)
44     {
45         if ($urlOrId == '') {
46             echo "URL/ID empty\n";
47             exit(1);
48         }
49
50         if (is_numeric($urlOrId)) {
51             $sqlWhere = ' f_id = ' . $this->db->quote($urlOrId);
52         } else {
53             $sqlWhere = ' f_url = ' . $this->db->quote($urlOrId);
54         }
55
56         $nRows = $this->db->exec(
57             'DELETE FROM feeds WHERE' . $sqlWhere
58         );
59         echo sprintf("%d feed has been removed\n", $nRows);;
60     }
61 }
62 ?>