From 41c310c5fc3790d7e5f6cb4862f5b09537002ffb Mon Sep 17 00:00:00 2001 From: Alessandro Cosentino Date: Sat, 18 Aug 2012 03:16:40 -0400 Subject: perform only a slim fetch in some cases --- lib/feedmapper.php | 32 ++++++++++++++++++++++++++------ lib/utils.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/feedmapper.php b/lib/feedmapper.php index dfc0d6054..e3d8fa67f 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -173,10 +173,12 @@ class FeedMapper { $l = \OC_L10N::get('news'); $title = $l->t('no title'); } - + + $favicon = $feed->getFavicon(); + //FIXME: Detect when feed contains already a database id $feedid = $this->findIdFromUrl($url); - if ($feedid == null){ + if ($feedid === null){ $query = \OCP\DB::prepare(" INSERT INTO " . self::tableName . "(url, url_hash, title, favicon_link, folder_id, user_id, added, lastmodified) @@ -187,7 +189,7 @@ class FeedMapper { $url, $url_hash, $title, - $feed->getFavicon(), + $favicon, $folderid, $this->userid ); @@ -195,17 +197,35 @@ class FeedMapper { $feedid = \OCP\DB::insertid(self::tableName); } + else { + //update the db. it needs to be done, since it might be the first save after a full fetch + $stmt = \OCP\DB::prepare(' + UPDATE ' . self::tableName . + ' SET favicon_link = ? , lastmodified = ? + WHERE id = ? + '); + + $params=array( + $favicon, + $_ut, + $feedid + ); + $stmt->execute($params); + } $feed->setId($feedid); $itemMapper = new ItemMapper(); $items = $feed->getItems(); - foreach($items as $item){ - $itemMapper->save($item, $feedid); + if ($items !== null) { + foreach($items as $item){ + $itemMapper->save($item, $feedid); + } } - + return $feedid; } + public function deleteById($id){ if ($id == null) { diff --git a/lib/utils.php b/lib/utils.php index 28b77e155..cc4dac232 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -98,6 +98,36 @@ class Utils { } } + /** + * Perform a "slim" fetch of a feed from remote. + * Differently from Utils::fetch(), it doesn't retrieve items nor a favicon + * + * @param url remote url of the feed + * @returns an instance of OC_News_Feed + */ + public static function slimFetch($url){ + $spfeed = new \SimplePie_Core(); + $spfeed->set_feed_url( $url ); + $spfeed->enable_cache( false ); + $spfeed->set_stupidly_fast( true ); + + if (!$spfeed->init()) { + return null; + } + + //temporary try-catch to bypass SimplePie bugs + try { + $title = $spfeed->get_title(); + + $feed = new Feed($url, $title); + + return $feed; + } + catch (Exception $e) { + return null; + } + } + public static function checkFavicon($favicon) { $file = new \SimplePie_File($favicon); // size in bytes -- cgit v1.2.3