diff options
author | Alessandro Cosentino <cosenal@gmail.com> | 2012-05-23 15:14:10 -0400 |
---|---|---|
committer | Alessandro Cosentino <cosenal@gmail.com> | 2012-05-23 15:14:10 -0400 |
commit | d8a193119f442c92bd47f150d149a5d1caf44a7e (patch) | |
tree | 4929cd74ec0489d8d8475fffca8620899a03ef84 | |
parent | 97f76e8cc62ede53709539595e835c4900f44d0c (diff) |
separates fetching/importing from the modelling classes
-rw-r--r-- | lib/feed.php | 37 | ||||
-rw-r--r-- | lib/feedmapper.php | 35 | ||||
-rw-r--r-- | lib/item.php | 27 | ||||
-rw-r--r-- | lib/itemmapper.php | 2 | ||||
-rw-r--r-- | templates/main.php | 3 |
5 files changed, 54 insertions, 50 deletions
diff --git a/lib/feed.php b/lib/feed.php index e095cfa99..311068463 100644 --- a/lib/feed.php +++ b/lib/feed.php @@ -29,29 +29,15 @@ class OC_News_Feed { private $id; //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 $fetched; - public function __construct($url, $id = null){ + public function __construct($url, $title, $items, $id = null){ $this->url = $url; - $this->spfeed = new SimplePie_Core(); - $this->spfeed->set_feed_url( $url ); - $this->spfeed->enable_cache( false ); - $this->fetched = false; + $this->title = $title; + $this->items = $items; if ($id !== null){ - self::setId($id); + $this->id = $id; } } - - public function fetch(){ - $this->spfeed->init(); - $this->spfeed->handle_content_type(); - - $this->fetched = true; - } - - public function isFetched(){ - return $this->fetched; - } public function getId(){ return $this->id; @@ -66,10 +52,7 @@ class OC_News_Feed { } public function getTitle(){ - if (!$this->isFetched()) { - $this->fetch(); - } - return $this->spfeed->get_title(); + return $this->title; } public function setItems($items){ @@ -77,16 +60,6 @@ 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 12627b55d..004e443f1 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -22,13 +22,37 @@ /** * This class maps a feed to an entry in the feeds table of the database. - * It follows the Data Mapper pattern (see http://martinfowler.com/eaaCatalog/dataMapper.html). */ class OC_News_FeedMapper { const tableName = '*PREFIX*news_feeds'; /** + * @brief Fetch a feed from remote + * @param url remote url of the feed + * @returns + */ + public function fetch($url){ + $spfeed = new SimplePie_Core(); + $spfeed->set_feed_url( $url ); + $spfeed->enable_cache( false ); + $spfeed->init(); + $spfeed->handle_content_type(); + $title = $spfeed->get_title(); + + $spitems = $spfeed->get_items(); + $items = array(); + foreach($spitems as $spitem) { //FIXME: maybe we can avoid this loop + $itemUrl = $spitem->get_permalink(); + $itemTitle = $spitem->get_title(); + $items[] = new OC_News_Item($itemUrl, $itemTitle); + } + + $feed = new OC_News_Feed($url, $title, $items); + return $feed; + } + + /** * @brief Retrieve a feed from the database * @param id The id of the feed in the database table. * @returns @@ -37,10 +61,9 @@ class OC_News_FeedMapper { $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, $id); - + $title = $row['title']; + $feed = new OC_News_Feed($url, $title, null, $id); return $feed; } @@ -53,9 +76,9 @@ class OC_News_FeedMapper { $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, $id); + $title = $row['title']; + $feed = new OC_News_Feed($url, $title, null,$id); $itemMapper = new OC_News_ItemMapper($feed); $items = $itemMapper->findAll(); diff --git a/lib/item.php b/lib/item.php index 96fbf4f09..a5a49937c 100644 --- a/lib/item.php +++ b/lib/item.php @@ -35,25 +35,24 @@ class StatusFlag{ */ 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; + private $title; + private $body; + private $status; //a bit-field set with status flags + private $id; //id of the item in the database table - public function __construct(SimplePie_Item $spitem){ + public function __construct($url, $title, $id = null){ + $this->title = $title; + $this->url = $url; $this->status |= StatusFlag::Unread; - $this->spitem = $spitem; - $this->title = $spitem->get_title(); - $this->url = $spitem->get_permalink(); } public function getId(){ - return $this->itemid; + return $this->$id;; } public function setId($id){ - $this->itemid = $id; + $this->$id = $id; } public function setRead(){ @@ -72,7 +71,15 @@ class OC_News_Item { return $this->title; } + public function setTitle($title){ + $this->title = $title; + } + public function getUrl(){ return $this->url; } + + public function setUrl($url){ + $this->url = $url; + } } diff --git a/lib/itemmapper.php b/lib/itemmapper.php index ed8550376..c8368ba9e 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -46,7 +46,7 @@ class OC_News_ItemMapper { while ($row = $result->fetchRow()) { $url = $row['url']; $title = $row['title']; - $items[] = new OC_News_Item($spitem); + $items[] = new OC_News_Item($url, $title); } return $items; diff --git a/templates/main.php b/templates/main.php index 652f22f9b..fc2631412 100644 --- a/templates/main.php +++ b/templates/main.php @@ -1,7 +1,8 @@ <?php -$feed = new OC_News_Feed( 'http://algorithmsforthekitchen.com/blog/?feed=rss2' ); $mapper = new OC_News_FeedMapper(); +$feed = $mapper->fetch( 'http://algorithmsforthekitchen.com/blog/?feed=rss2' ); +echo "<br>" . $feed->getTitle() . "<br>"; $mapper->insert($feed); $feed = $mapper->find($feed->getId()); echo "<br>" . $feed->getTitle() . "<br>"; |