From 74129d1b9e97a5d59f72bd38b70a40a5d21a019f Mon Sep 17 00:00:00 2001 From: Alessandro Cosentino Date: Mon, 13 Aug 2012 17:06:12 -0400 Subject: database tables changed --- lib/feed.php | 2 +- lib/feedmapper.php | 47 ++++++++++++++++++++++++----------------------- lib/item.php | 9 +++++++++ lib/itemmapper.php | 27 +++++++++++++++++---------- 4 files changed, 51 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/feed.php b/lib/feed.php index ecff8f67a..6dfdcbcfb 100644 --- a/lib/feed.php +++ b/lib/feed.php @@ -17,8 +17,8 @@ namespace OCA\News; */ class Feed extends Collection { + private $title; private $url; - private $spfeed; //encapsulate a SimplePie_Core object private $items; //array that contains all the items of the feed private $favicon; diff --git a/lib/feedmapper.php b/lib/feedmapper.php index 596858538..dfc0d6054 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -30,13 +30,21 @@ class FeedMapper { /** * @brief * @param row a row from the feeds table of the database - * @returns an object of the class OC_News_Feed + * @returns an object of the class OCA\News\Feed */ public function fromRow($row){ + $url = $row['url']; + $title = htmlspecialchars_decode($row['title']); + $id = $row['id']; + $feed = new Feed($url, $title, null, $id); + $favicon = $row['favicon_link']; + $feed->setFavicon($favicon); + + return $feed; } /** - * @brief + * @brief as a list that can be easily parsed using JSON * @param userid * @returns */ @@ -72,10 +80,7 @@ class FeedMapper { $result = $stmt->execute(array($id)); if(!$row = $result->fetchRow()) return null; - - $url = $row['url']; - $title = htmlspecialchars_decode($row['title']); - $feed = new Feed($url, $title, null, $id); + $feed = self::fromRow($row); return $feed; } @@ -89,12 +94,7 @@ class FeedMapper { $result = $stmt->execute(array($this->userid, $folderid)); $feeds = array(); while ($row = $result->fetchRow()) { - $url = $row['url']; - $title = htmlspecialchars_decode($row['title']); - $id = $row['id']; - $feed = new Feed($url, $title, null, $id); - $favicon = $row['favicon_link']; - $feed->setFavicon($favicon); + $feed = self::fromRow($row); $feeds[] = $feed; } return $feeds; @@ -104,17 +104,14 @@ class FeedMapper { /** * @brief Retrieve a feed and all its items from the database * @param id The id of the feed in the database table. - * @returns an instance of OC_News_Feed + * @returns an instance of OCA\News\Feed */ public function findWithItems($id){ $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); $result = $stmt->execute(array($id)); $row = $result->fetchRow(); - $url = $row['url']; - $title = htmlspecialchars_decode($row['title']); - $feed = new Feed($url, $title, null,$id); - $favicon = $row['favicon_link']; - $feed->setFavicon($favicon); + + $feed = self::fromRow($row); $itemMapper = new ItemMapper(); $items = $itemMapper->findAll($id); $feed->setItems($items); @@ -129,8 +126,9 @@ class FeedMapper { * null - if there is no such feed */ public function findIdFromUrl($url){ - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE url = ?'); - $result = $stmt->execute(array($url)); + $url_hash = md5($url); + $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE url_hash = ?'); + $result = $stmt->execute(array($url_hash)); $row = $result->fetchRow(); $id = null; if ($row != null){ @@ -168,7 +166,8 @@ class FeedMapper { } $title = $feed->getTitle(); - $url = htmlspecialchars_decode($feed->getUrl()); + $url = $feed->getUrl(); + $url_hash = md5($url); if(empty($title)) { $l = \OC_L10N::get('news'); @@ -180,12 +179,13 @@ class FeedMapper { if ($feedid == null){ $query = \OCP\DB::prepare(" INSERT INTO " . self::tableName . - "(url, title, favicon_link, folder_id, user_id, added, lastmodified) - VALUES (?, ?, ?, ?, ?, $_ut, $_ut) + "(url, url_hash, title, favicon_link, folder_id, user_id, added, lastmodified) + VALUES (?, ?, ?, ?, ?, ?, $_ut, $_ut) "); $params=array( $url, + $url_hash, $title, $feed->getFavicon(), $folderid, @@ -221,6 +221,7 @@ class FeedMapper { return true; } + public function delete(Feed $feed){ $id = $feed->getId(); return deleteById($id); diff --git a/lib/item.php b/lib/item.php index ffbf71760..3e9b3f5b7 100644 --- a/lib/item.php +++ b/lib/item.php @@ -32,6 +32,7 @@ class Item { private $body; private $status; //a bit-field set with status flags private $id; //id of the item in the database table + private $author; public function __construct($url, $title, $guid, $body, $id = null){ $this->title = $title; @@ -121,4 +122,12 @@ class Item { public function setBody($body){ $this->body = $body; } + + public function getAuthor(){ + return $this->author; + } + + public function setAuthor($author){ + $this->author = $author; + } } diff --git a/lib/itemmapper.php b/lib/itemmapper.php index 40693071f..35060f0a4 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -34,6 +34,9 @@ class ItemMapper { $id = $row['id']; $item = new Item($url, $title, $guid, $body, $id); $item->setStatus($status); + $date = $row['date']; + $author = $row['author']; + $item->setAuthor($author); return $item; } @@ -75,13 +78,14 @@ class ItemMapper { return $items; } - public function findIdFromGuid($guid, $feedid){ + public function findIdFromGuid($guid_hash, $guid, $feedid){ $stmt = \OCP\DB::prepare(' SELECT * FROM ' . self::tableName . ' - WHERE guid = ? + WHERE guid_hash = ? AND feed_id = ? '); - $result = $stmt->execute(array($guid, $feedid)); + $result = $stmt->execute(array($guid_hash, $feedid)); + //TODO: if there is more than one row, falling back to comparing $guid $row = $result->fetchRow(); $id = null; if ($row != null){ @@ -120,9 +124,11 @@ class ItemMapper { */ public function save(Item $item, $feedid){ $guid = $item->getGuid(); + $guid_hash = md5($guid); + $status = $item->getStatus(); - $itemid = $this->findIdFromGuid($guid, $feedid); + $itemid = $this->findIdFromGuid($guid_hash, $guid, $feedid); if ($itemid == null){ $title = $item->getTitle(); @@ -130,8 +136,8 @@ class ItemMapper { $stmt = \OCP\DB::prepare(' INSERT INTO ' . self::tableName . - '(url, title, body, guid, feed_id, status) - VALUES (?, ?, ?, ?, ?, ?) + '(url, title, body, guid, guid_hash, feed_id, status) + VALUES (?, ?, ?, ?, ?, ?, ?) '); if(empty($title)) { @@ -143,12 +149,13 @@ class ItemMapper { $l = \OC_L10N::get('news'); $body = $l->t('no body'); } - + $params=array( - htmlspecialchars_decode($item->getUrl()), - htmlspecialchars_decode($title), + $item->getUrl(), + $title, $body, - htmlspecialchars_decode($guid), + $guid, + $guid_hash, $feedid, $status ); -- cgit v1.2.3