From a6e57a9a1f4427de872c4bf0050cc0ee38d2adfe Mon Sep 17 00:00:00 2001 From: Alessandro Cosentino Date: Tue, 29 May 2012 16:07:12 -0400 Subject: adds indices to database schema --- appinfo/database.xml | 35 +++++++++++++++++++++++++++++++---- lib/feedmapper.php | 9 ++++----- lib/item.php | 16 +++++++++++++--- lib/itemmapper.php | 30 ++++++++++++------------------ lib/utils.php | 3 ++- 5 files changed, 62 insertions(+), 31 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index 30f337e5b..7ef656da3 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -93,13 +93,23 @@ - id + feed_id true + true id descending + + + feed_url + true + false + + url + + @@ -115,15 +125,19 @@ 4 - url + guid text true 100 + + url + text + 100 + title text - true 100 @@ -132,15 +146,28 @@ true 4 + + body + text + 4000 + - id + item_id true id descending + + item_guid + true + + guid + descending + + diff --git a/lib/feedmapper.php b/lib/feedmapper.php index 7b148b142..a08d730b1 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -55,8 +55,8 @@ class OC_News_FeedMapper { $title = $row['title']; $feed = new OC_News_Feed($url, $title, null,$id); - $itemMapper = new OC_News_ItemMapper($feed); - $items = $itemMapper->findAll(); + $itemMapper = new OC_News_ItemMapper(); + $items = $itemMapper->findAll($id); $feed->setItems($items); return $feed; @@ -103,7 +103,6 @@ class OC_News_FeedMapper { } //FIXME: Detect when feed contains already a database id - $feedid = $this->findIdFromUrl($url); if ($feedid == null){ $query = OCP\DB::prepare(' @@ -125,11 +124,11 @@ class OC_News_FeedMapper { } $feed->setId($feedid); - $itemMapper = new OC_News_ItemMapper($feed); + $itemMapper = new OC_News_ItemMapper(); $items = $feed->getItems(); foreach($items as $item){ - $itemMapper->insert($item); + $itemMapper->insert($item, $feedid); } return $feedid; } diff --git a/lib/item.php b/lib/item.php index a5a49937c..e048e9d11 100644 --- a/lib/item.php +++ b/lib/item.php @@ -37,22 +37,32 @@ class OC_News_Item { private $url; private $title; + private $guid; private $body; private $status; //a bit-field set with status flags private $id; //id of the item in the database table - public function __construct($url, $title, $id = null){ + public function __construct($url, $title, $guid, $id = null){ $this->title = $title; $this->url = $url; + $this->guid = $guid; $this->status |= StatusFlag::Unread; } + public function getGuid(){ + return $this->guid; + } + + public function setGuid($guid){ + $this->guid = $guid; + } + public function getId(){ - return $this->$id;; + return $this->id; } public function setId($id){ - $this->$id = $id; + $this->id = $id; } public function setRead(){ diff --git a/lib/itemmapper.php b/lib/itemmapper.php index c8368ba9e..65f1eb2e2 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -20,25 +20,19 @@ * */ -/** +/**e 49 * This class maps an item to a row of the items table in the database. * It follows the Data Mapper pattern (see http://martinfowler.com/eaaCatalog/dataMapper.html). */ class OC_News_ItemMapper { const tableName = '*PREFIX*news_items'; - private $feed; - - public function __construct(OC_News_Feed $feed){ - $this->feed = $feed; - } /** - * @brief Retrieve an item from the database - * @param id The id of the feed in the database table. + * @brief Retrieve all the item corresponding to a feed from the database + * @param feedid The id of the feed in the database table. */ - public function findAll(){ - $feedid = $this->feed->getId(); + public function findAll($feedid){ $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feedid = ?'); $result = $stmt->execute(array($feedid)); @@ -46,7 +40,8 @@ class OC_News_ItemMapper { while ($row = $result->fetchRow()) { $url = $row['url']; $title = $row['title']; - $items[] = new OC_News_Item($url, $title); + $guid = $row['guid']; + $items[] = new OC_News_Item($url, $title, $guid); } return $items; @@ -56,18 +51,16 @@ class OC_News_ItemMapper { * @brief Save the feed and all its items into the database * @returns The id of the feed in the database table. */ - public function insert(OC_News_Item $item){ - - $feedid = $this->feed->getId(); + public function insert(OC_News_Item $item, $feedid){ + $guid = $item->getGuid(); + $title = $item->getTitle(); $query = OCP\DB::prepare(' INSERT INTO ' . self::tableName . - '(url, title, feedid) - VALUES (?, ?, ?) + '(url, title, guid, feedid) + VALUES (?, ?, ?, ?) '); - $title = $item->getTitle(); - if(empty($title)) { $l = OC_L10N::get('news'); $title = $l->t('no title'); @@ -76,6 +69,7 @@ class OC_News_ItemMapper { $params=array( htmlspecialchars_decode($item->getUrl()), htmlspecialchars_decode($title), + $guid, $feedid ); diff --git a/lib/utils.php b/lib/utils.php index e5ea27626..dd752aea4 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -41,7 +41,8 @@ class OC_News_Utils { 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); + $itemGUID = $spitem->get_id(); + $items[] = new OC_News_Item($itemUrl, $itemTitle, $itemGUID); } $feed = new OC_News_Feed($url, $title, $items); -- cgit v1.2.3