diff options
author | Alessandro Cosentino <cosenal@gmail.com> | 2012-05-20 19:14:34 -0400 |
---|---|---|
committer | Alessandro Cosentino <cosenal@gmail.com> | 2012-05-20 19:14:34 -0400 |
commit | 2a15c85cd9c08af627798c61eced0511b05a1ac3 (patch) | |
tree | 96824baa8d9748669851fd95714448d2bcd880fd | |
parent | 4cb80788b059506fa30c8aeae3da87dec931332b (diff) |
fetching items from remote feed working now
-rw-r--r-- | lib/feed.php | 12 | ||||
-rw-r--r-- | lib/feedmapper.php | 29 | ||||
-rw-r--r-- | lib/item.php | 15 | ||||
-rw-r--r-- | lib/itemmapper.php | 14 | ||||
-rw-r--r-- | templates/main.php | 4 |
5 files changed, 57 insertions, 17 deletions
diff --git a/lib/feed.php b/lib/feed.php index 215ac7c1f..ce1cc956a 100644 --- a/lib/feed.php +++ b/lib/feed.php @@ -28,7 +28,7 @@ class OC_News_Feed { private $url; private $feedid; //id of the feed in the database private $spfeed; //encapsulate a SimplePie_Core object - private $items; //array that contains all the items of the feed + private $items; //array that contains all the items of the feed private $fetched; public function __construct($url){ @@ -69,6 +69,16 @@ class OC_News_Feed { } public function getItems(){ + if (!isset($this->items)){ + if (!$this->isFetched()) { + $this->fetch(); + } + $spitems = $this->spfeed->get_items(); + $this->items = array(); + foreach($spitems as $spitem) { //FIXME: maybe we can avoid this loop + $this->items[] = new OC_News_Item($spitem); + } + } return $this->items; } } diff --git a/lib/feedmapper.php b/lib/feedmapper.php index f3dde51ba..d9b488595 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -26,24 +26,43 @@ */ class OC_News_FeedMapper { - private $tableName = '*PREFIX*news_feeds'; + const tableName = '*PREFIX*news_feeds'; /** * @brief Retrieve a feed from the database * @param id The id of the feed in the database table. + * @returns */ public function find($id){ - $stmt = OCP\DB::prepare('SELECT * FROM ' . $this->tableName . ' WHERE id = ?'); + $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); $result = $stmt->execute(array($id)); $row = $result->fetchRow(); $url = $row['url']; - $title = $row['title']; + $feed = new OC_News_Feed($url); + return $feed; + } + + /** + * @brief Retrieve a feed and all its items from the database + * @param id The id of the feed in the database table. + * @returns + */ + public function findAllItems($id){ + $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); + $result = $stmt->execute(array($id)); + $row = $result->fetchRow(); + + $url = $row['url']; + $feed = new OC_News_Feed($url); + + return $feed; } /** * @brief Save the feed and all its items into the database + * @param feed the feed to be saved * @returns The id of the feed in the database table. */ public function insert(OC_News_Feed $feed){ @@ -59,7 +78,7 @@ class OC_News_FeedMapper { //FIXME: Detect when user adds a known feed //FIXME: specify folder where you want to add $query = OCP\DB::prepare(' - INSERT INTO ' . $this->tableName . + INSERT INTO ' . self::tableName . '(url, title, added, lastmodified) VALUES (?, ?, $_ut, $_ut) '); @@ -80,7 +99,7 @@ class OC_News_FeedMapper { ); $query->execute($params); - $feedid = OCP\DB::insertid($this->tableName); + $feedid = OCP\DB::insertid(self::tableName); $feed->setId($feedid); $itemMapper = new OC_News_ItemMapper($feed); diff --git a/lib/item.php b/lib/item.php index 0c397c18d..96fbf4f09 100644 --- a/lib/item.php +++ b/lib/item.php @@ -33,15 +33,19 @@ class StatusFlag{ * * It encapsulate a SimplePie_Item object and adds a status flag to it */ -class OC_News_Item extends SimplePie_Item { +class OC_News_Item { private $status; //a bit-field set with status flags private $spitem; //encapsulate a SimplePie_Item object private $itemid; + private $title; + private $url; - public function __construct($spitem){ + public function __construct(SimplePie_Item $spitem){ $this->status |= StatusFlag::Unread; $this->spitem = $spitem; + $this->title = $spitem->get_title(); + $this->url = $spitem->get_permalink(); } public function getId(){ @@ -64,6 +68,11 @@ class OC_News_Item extends SimplePie_Item { return ($this->status & ~StatusFlag::Unread); } + public function getTitle(){ + return $this->title; + } - + public function getUrl(){ + return $this->url; + } } diff --git a/lib/itemmapper.php b/lib/itemmapper.php index 177a95fd6..fa6705f69 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -26,7 +26,7 @@ */ class OC_News_ItemMapper { - private $tableName = '*PREFIX*news_items'; + const tableName = '*PREFIX*news_items'; private $feed; public function __construct(OC_News_Feed $feed){ @@ -34,11 +34,11 @@ class OC_News_ItemMapper { } /** - * @brief Retrieve a feed from the database + * @brief Retrieve an item from the database * @param id The id of the feed in the database table. */ public function find($id){ - $stmt = OCP\DB::prepare('SELECT * FROM ' . $this->feedTableName . ' WHERE id = ?'); + $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); $result = $stmt->execute(array($id)); $row = $result->fetchRow(); @@ -56,25 +56,25 @@ class OC_News_ItemMapper { $feedid = $this->feed->getId(); $query = OCP\DB::prepare(' - INSERT INTO ' . $this->tableName . + INSERT INTO ' . self::tableName . '(url, title, feedid) VALUES (?, ?, $feedid) '); $title = $item->getTitle(); -echo $title; +echo $title . '<br>'; if(empty($title)) { $l = OC_L10N::get('news'); $title = $l->t('no title'); } $params=array( - htmlspecialchars_decode($feed->getUrl()), + htmlspecialchars_decode($item->getUrl()), htmlspecialchars_decode($title) ); $query->execute($params); - $itemid = OCP\DB::insertid($this->tableName); + $itemid = OCP\DB::insertid(self::tableName); $item->setId($itemid); return $itemid; } diff --git a/templates/main.php b/templates/main.php index d4cc21082..88d1b9fb8 100644 --- a/templates/main.php +++ b/templates/main.php @@ -3,8 +3,10 @@ $feed = new OC_News_Feed( 'http://algorithmsforthekitchen.com/blog/?feed=rss2' ); $mapper = new OC_News_FeedMapper(); $mapper->insert($feed); -$mapper->find($feed->getId()); +$feed = $mapper->find($feed->getId()); echo "<br>" . $feed->getTitle() . "<br>"; +$items = $feed->getItems(); + /* $item = $feed->get_item(1); |