diff options
author | Alessandro Cosentino <cosenal@gmail.com> | 2012-05-28 23:40:18 -0400 |
---|---|---|
committer | Alessandro Cosentino <cosenal@gmail.com> | 2012-05-28 23:40:18 -0400 |
commit | 07fdf6ad3c3cc659a06099cd2011aab1ce2c8c5e (patch) | |
tree | ccc41c9c30206e5406415fc9b7dfe1fa24ea7401 | |
parent | 762b4d89bc0876d8d514a21bf22babcb012cf298 (diff) |
find a feed in the database by url;Detect when databse contains already a feed
-rw-r--r-- | lib/feedmapper.php | 61 | ||||
-rw-r--r-- | lib/folder.php | 10 | ||||
-rw-r--r-- | lib/foldermapper.php | 60 | ||||
-rw-r--r-- | lib/utils.php (renamed from lib/fetcher.php) | 0 | ||||
-rw-r--r-- | templates/main.php | 23 |
5 files changed, 90 insertions, 64 deletions
diff --git a/lib/feedmapper.php b/lib/feedmapper.php index c0e0b03a5..7b148b142 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -63,11 +63,28 @@ class OC_News_FeedMapper { } /** + * @brief Find the id of a feed and all its items from the database + * @param url url of the feed + * @return id of the feed corresponding to the url passed as parameters + 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)); + $row = $result->fetchRow(); + $id = null; + if ($row != null){ + $id = $row['id']; + } + return $id; + } + + /** * @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){ + public function insert(OC_News_Feed $feed, $folderid){ $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" ); if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ $_ut = "strftime('%s','now')"; @@ -76,32 +93,36 @@ class OC_News_FeedMapper { } else { $_ut = "UNIX_TIMESTAMP()"; } - - //FIXME: Detect when user adds a known feed - //FIXME: specify folder where you want to add - $query = OCP\DB::prepare(' - INSERT INTO ' . self::tableName . - '(url, title, added, lastmodified) - VALUES (?, ?, $_ut, $_ut) - '); - + $title = $feed->getTitle(); + $url = htmlspecialchars_decode($feed->getUrl()); if(empty($title)) { $l = OC_L10N::get('news'); $title = $l->t('no title'); } - $params=array( - htmlspecialchars_decode($feed->getUrl()), - htmlspecialchars_decode($title) - - //FIXME: user_id is going to move to the folder properties - //OCP\USER::getUser() - ); - $query->execute($params); - - $feedid = OCP\DB::insertid(self::tableName); + //FIXME: Detect when feed contains already a database id + + $feedid = $this->findIdFromUrl($url); + if ($feedid == null){ + $query = OCP\DB::prepare(' + INSERT INTO ' . self::tableName . + '(url, title, folderid, added, lastmodified) + VALUES (?, ?, ?, ?, ?) + '); + + $params=array( + $url, + htmlspecialchars_decode($title), + $folderid, + $_ut, + $_ut + ); + $query->execute($params); + + $feedid = OCP\DB::insertid(self::tableName); + } $feed->setId($feedid); $itemMapper = new OC_News_ItemMapper($feed); diff --git a/lib/folder.php b/lib/folder.php index a7036e8a7..f97960c7b 100644 --- a/lib/folder.php +++ b/lib/folder.php @@ -35,6 +35,7 @@ class OC_News_Folder { $this->name = $name; $this->parent = $parent; $this->feeds = array(); + $this->id = 0; } public function getName(){ @@ -50,7 +51,14 @@ class OC_News_Folder { } public function setId($id){ - $this->$id = $id; + $this->id = $id; + } + + public function getParentId(){ + if ($this->parent == null){ + return 0; + } + return $this->parent->getId(); } public function addFeed(OC_News_Feed $feed){ diff --git a/lib/foldermapper.php b/lib/foldermapper.php index 23ced832c..92f53c35c 100644 --- a/lib/foldermapper.php +++ b/lib/foldermapper.php @@ -63,52 +63,42 @@ class OC_News_FolderMapper { } /** - * @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. + * @brief Store the folder and all its feeds into the database + * @param folder the folder to be saved + * @returns The id of the folder in the database table. */ - public function insert(OC_News_Feed $feed){ - $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" ); - if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ - $_ut = "strftime('%s','now')"; - } elseif($CONFIG_DBTYPE == 'pgsql') { - $_ut = 'date_part(\'epoch\',now())::integer'; - } else { - $_ut = "UNIX_TIMESTAMP()"; - } - - //FIXME: Detect when user adds a known feed - //FIXME: specify folder where you want to add + public function insert(OC_News_Folder $folder){ $query = OCP\DB::prepare(' INSERT INTO ' . self::tableName . - '(url, title, added, lastmodified) - VALUES (?, ?, $_ut, $_ut) + '(name, parentid, userid) + VALUES (?, ?, ?) '); - $title = $feed->getTitle(); + $name = $folder->getName(); - if(empty($title)) { + if(empty($name)) { $l = OC_L10N::get('news'); - $title = $l->t('no title'); + $name = $l->t('no name'); } + $parentid = $folder->getParentId(); + $params=array( - htmlspecialchars_decode($feed->getUrl()), - htmlspecialchars_decode($title) - - //FIXME: user_id is going to move to the folder properties - //OCP\USER::getUser() + htmlspecialchars_decode($name), + $parentid, + OCP\USER::getUser() ); $query->execute($params); - - $feedid = OCP\DB::insertid(self::tableName); - $feed->setId($feedid); + $folderid = OCP\DB::insertid(self::tableName); - $itemMapper = new OC_News_ItemMapper($feed); - - $items = $feed->getItems(); - foreach($items as $item){ - $itemMapper->insert($item); - } - return $feedid; + $folder->setId($folderid); + +// $folder->getFeeds(); +// $feedMapper = new OC_News_FeedMapper($feed); +// $items = $feed->getItems(); +// foreach($items as $item){ +// $itemMapper->insert($item); +// } +// return $folderid; } +}
\ No newline at end of file diff --git a/lib/fetcher.php b/lib/utils.php index e5ea27626..e5ea27626 100644 --- a/lib/fetcher.php +++ b/lib/utils.php diff --git a/templates/main.php b/templates/main.php index ec5a61b35..2bf5d0dd5 100644 --- a/templates/main.php +++ b/templates/main.php @@ -1,19 +1,26 @@ <?php -$mapper = new OC_News_FeedMapper(); -$feed = $mapper->fetch( 'http://algorithmsforthekitchen.com/blog/?feed=rss2' ); -echo "<br>" . $feed->getTitle() . "<br>"; -$mapper->insert($feed); -$feed = $mapper->findWithItems($feed->getId()); -echo "<br>" . $feed->getTitle() . "<br>"; +$feedmapper = new OC_News_FeedMapper(); +$foldermapper = new OC_News_FolderMapper(); + +$folder = new OC_News_Folder( 'Friends' ); +$folderid = $foldermapper->insert($folder); + +$feed = OC_News_Utils::fetch( 'http://algorithmsforthekitchen.com/blog/?feed=rss2' ); +echo '<br>' . $feed->getTitle() . '<br>'; + +$feedmapper->insert($feed, $folder->getId()); + +$feed = $feedmapper->findWithItems($feed->getId()); +echo '<br>' . $feed->getTitle() . '<br>'; $items = $feed->getItems(); foreach($items as $item) { echo $item->getTitle(); } -$feed2 = $mapper->findWithItems(45); -echo "<br>" . $feed2->getTitle() . "<br>"; +$feed2 = $feedmapper->findWithItems(45); +echo '<br>' . $feed2->getTitle() . '<br>'; /* |