A free, self-hostable news aggregator…
Import of articles is implemented!
Marien Fressinaud779afe9c4ee0
diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.phpindex 43435d99d..95c9c1e9c 100755--- a/app/Controllers/feedController.php+++ b/app/Controllers/feedController.php@@ -327,82 +327,6 @@ class FreshRSS_feed_Controller extends Minz_ActionController {}}- public function massiveImportAction () {- # TODO: this function has moved to importExportController.php- # I keep it for the moment but should be deleted in a near future.- @set_time_limit(300);-- $this->catDAO = new FreshRSS_CategoryDAO ();- $this->catDAO->checkDefault ();-- $entryDAO = new FreshRSS_EntryDAO ();- $feedDAO = new FreshRSS_FeedDAO ();-- $categories = Minz_Request::param ('categories', array (), true);- $feeds = Minz_Request::param ('feeds', array (), true);-- // on ajoute les catégories en masse dans une fonction à part- $this->addCategories ($categories);-- // on calcule la date des articles les plus anciens qu'on accepte- $nb_month_old = $this->view->conf->old_entries;- $date_min = time () - (3600 * 24 * 30 * $nb_month_old);-- // la variable $error permet de savoir si une erreur est survenue- // Le but est de ne pas arrêter l'import même en cas d'erreur- // L'utilisateur sera mis au courant s'il y a eu des erreurs, mais- // ne connaîtra pas les détails. Ceux-ci seront toutefois logguées- $error = false;- $i = 0;- foreach ($feeds as $feed) {- try {- $values = array (- 'id' => $feed->id (),- 'url' => $feed->url (),- 'category' => $feed->category (),- 'name' => $feed->name (),- 'website' => $feed->website (),- 'description' => $feed->description (),- 'lastUpdate' => 0,- 'httpAuth' => $feed->httpAuth ()- );-- // ajout du flux que s'il n'est pas déjà en BDD- if (!$feedDAO->searchByUrl ($values['url'])) {- $id = $feedDAO->addFeed ($values);- if ($id) {- $feed->_id ($id);- $feed->faviconPrepare();- } else {- $error = true;- }- }- } catch (FreshRSS_Feed_Exception $e) {- $error = true;- Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);- }- }-- if ($error) {- $res = Minz_Translate::t ('feeds_imported_with_errors');- } else {- $res = Minz_Translate::t ('feeds_imported');- }-- $notif = array (- 'type' => 'good',- 'content' => $res- );- Minz_Session::_param ('notification', $notif);- Minz_Session::_param ('actualize_feeds', true);-- // et on redirige vers la page d'accueil- Minz_Request::forward (array (- 'c' => 'index',- 'a' => 'index'- ), true);- }-public function deleteAction () {if (Minz_Request::isPost ()) {$type = Minz_Request::param ('type', 'feed');@@ -446,16 +370,4 @@ class FreshRSS_feed_Controller extends Minz_ActionController {}}}-- private function addCategories ($categories) {- foreach ($categories as $cat) {- if (!$this->catDAO->searchByName ($cat->name ())) {- $values = array (- 'id' => $cat->id (),- 'name' => $cat->name (),- );- $catDAO->addCategory ($values);- }- }- }}diff --git a/app/Controllers/importExportController.php b/app/Controllers/importExportController.phpindex b6b4d0fed..3b6bf2c5c 100644--- a/app/Controllers/importExportController.php+++ b/app/Controllers/importExportController.php@@ -31,7 +31,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {@set_time_limit(300);$file = $_FILES['file'];- $type_file = $this->guess_file_type($file['name']);+ $type_file = $this->guessFileType($file['name']);$list_files = array('opml' => array(),@@ -46,7 +46,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {$zip = zip_open($file['tmp_name']);while (($zipfile = zip_read($zip)) !== false) {- $type_zipfile = $this->guess_file_type(zip_entry_name($zipfile));+ $type_zipfile = $this->guessFileType(zip_entry_name($zipfile));if ($type_file !== 'unknown') {$list_files[$type_zipfile][] = zip_entry_read(@@ -67,13 +67,13 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {// And finally all other files.$error = false;foreach ($list_files['opml'] as $opml_file) {- $error = $this->import_opml($opml_file);+ $error = $this->importOpml($opml_file);}foreach ($list_files['json_starred'] as $article_file) {- $error = $this->import_articles($article_file, true);+ $error = $this->importArticles($article_file, true);}foreach ($list_files['json_feed'] as $article_file) {- $error = $this->import_articles($article_file);+ $error = $this->importArticles($article_file);}// And finally, we get import status and redirect to the home page@@ -107,7 +107,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {));}- private function guess_file_type($filename) {+ private function guessFileType($filename) {// A *very* basic guess file type function. Only based on filename// That's could be improved but should be enough, at least for a first// implementation.@@ -128,7 +128,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {}}- private function import_opml($opml_file) {+ private function importOpml($opml_file) {$opml_array = array();try {$opml_array = libopml_parse_string($opml_file);@@ -164,7 +164,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {private function addFeedOpml($feed_elt, $parent_cat) {if (is_null($parent_cat)) {// This feed has no parent category so we get the default one- $parent_cat = $catDAO->getDefault()->name();+ $parent_cat = $this->catDAO->getDefault()->name();}$cat = $this->catDAO->searchByName($parent_cat);@@ -199,7 +199,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {$id = $this->feedDAO->addFeedObject($feed);$error = ($id === false);} catch (FreshRSS_Feed_Exception $e) {- Minz_Log::record($e->getMessage(), Minz_Log::WARNING);+ Minz_Log::warning($e->getMessage());$error = true;}@@ -226,28 +226,23 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {return $error;}- private function import_articles($article_file, $starred = false) {+ private function importArticles($article_file, $starred = false) {$article_object = json_decode($article_file, true);if (is_null($article_object)) {Minz_Log::warning('Try to import a non-JSON file');return true;}+ $is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;+$google_compliant = (strpos($article_object['id'], 'com.google') !== false);+ $error = false;foreach ($article_object['items'] as $item) {- $key = $google_compliant ? 'htmlUrl' : 'feedUrl';- $feed = $this->feedDAO->searchByUrl($item['origin'][$key]);+ $feed = $this->addFeedArticles($item['origin'], $google_compliant);if (is_null($feed)) {- $feed = new FreshRSS_Feed($item['origin'][$key]);- $feed->_name ($item['origin']['title']);- $feed->_website ($item['origin']['htmlUrl']);-- $error = $this->addFeed($feed); // TODO-- if ($error) {- continue;- }+ $error = true;+ continue;}$author = isset($item['author']) ? $item['author'] : '';@@ -258,14 +253,55 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {return strpos($var, '/state/com.google') === false;});}+$entry = new FreshRSS_Entry($feed->id(), $item['id'], $item['title'], $author,$item[$key_content]['content'], $item['alternate'][0]['href'],- $item['published'], false, $starred, $tags+ $item['published'], $is_read, $starred);+ $entry->_tags($tags);- Minz_Log::debug(print_r($entry, true)); // TODO+ $id = $this->entryDAO->addEntryObject(+ $entry, $this->view->conf, $feed->keepHistory()+ );++ if (!$error && ($id === false)) {+ $error = true;+ }}++ return $error;+ }++ private function addFeedArticles($origin, $google_compliant) {+ $default_cat = $this->catDAO->getDefault();++ $return = null;+ $key = $google_compliant ? 'htmlUrl' : 'feedUrl';+ $url = $origin[$key];+ $name = $origin['title'];+ $website = $origin['htmlUrl'];+ $error = false;+ try {+ // Create a Feed object and add it in DB+ $feed = new FreshRSS_Feed($url);+ $feed->_category($default_cat->id());+ $feed->_name($name);+ $feed->_website($website);++ // addFeedObject checks if feed is already in DB so nothing else to+ // check here+ $id = $this->feedDAO->addFeedObject($feed);++ if ($id !== false) {+ $feed->_id($id);+ $return = $feed;+ }+ } catch (FreshRSS_Feed_Exception $e) {+ Minz_Log::warning($e->getMessage());+ }++ return $return;}public function exportAction() {@@ -283,16 +319,16 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {// Stuff with contentif ($export_opml) {- $zip->addFromString('feeds.opml', $this->generate_opml());+ $zip->addFromString('feeds.opml', $this->generateOpml());}if ($export_starred) {- $zip->addFromString('starred.json', $this->generate_articles('starred'));+ $zip->addFromString('starred.json', $this->generateArticles('starred'));}foreach ($export_feeds as $feed_id) {$feed = $this->feedDAO->searchById($feed_id);$zip->addFromString('feed_' . $feed->category() . '_' . $feed->id() . '.json',- $this->generate_articles('feed', $feed)+ $this->generateArticles('feed', $feed));}@@ -306,7 +342,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {}}- private function generate_opml() {+ private function generateOpml() {$list = array();foreach ($this->catDAO->listCategories() as $key => $cat) {$list[$key]['name'] = $cat->name();@@ -317,7 +353,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {return $this->view->helperToString('export/opml');}- private function generate_articles($type, $feed = NULL) {+ private function generateArticles($type, $feed = NULL) {$this->view->categories = $this->catDAO->listCategories();if ($type == 'starred') {diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.phpindex 8be732b98..6a9b839b9 100644--- a/app/Models/CategoryDAO.php+++ b/app/Models/CategoryDAO.php@@ -19,7 +19,8 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo {}public function addCategoryObject($category) {- if (!$this->searchByName($category->name())) {+ $cat = $this->searchByName($category->name());+ if (!$cat) {// Category does not exist yet in DB so we add it before continue$values = array('name' => $category->name(),@@ -27,7 +28,7 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo {return $this->addCategory($values);}- return false;+ return $cat->id();}public function updateCategory ($id, $valuesTmp) {diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.phpindex e4cf128ea..6d00967fc 100644--- a/app/Models/EntryDAO.php+++ b/app/Models/EntryDAO.php@@ -35,6 +35,36 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo {}}+ public function addEntryObject($entry, $conf, $feedHistory) {+ $existingGuids = array_fill_keys(+ $this->listLastGuidsByFeed($entry->feed(), 20), 1+ );++ $nb_month_old = max($conf->old_entries, 1);+ $date_min = time() - (3600 * 24 * 30 * $nb_month_old);++ $eDate = $entry->date(true);++ if ($feedHistory == -2) {+ $feedHistory = $conf->keep_history_default;+ }++ if (!isset($existingGuids[$entry->guid()]) &&+ ($feedHistory != 0 || $eDate >= $date_min)) {+ $values = $entry->toArray();++ $useDeclaredDate = empty($existingGuids);+ $values['id'] = ($useDeclaredDate || $eDate < $date_min) ?+ min(time(), $eDate) . uSecString() :+ uTimeString();++ return $this->addEntry($values);+ }++ // We don't return Entry object to avoid a research in DB+ return -1;+ }+public function markFavorite($ids, $is_favorite = true) {if (!is_array($ids)) {$ids = array($ids);diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.phpindex eac21df7e..b65ff4af0 100644--- a/app/Models/FeedDAO.php+++ b/app/Models/FeedDAO.php@@ -29,7 +29,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {// should not be aware about feed class// Add feed only if we don't find it in DB- if (!$this->searchByUrl($feed->url())) {+ $feed_search = $this->searchByUrl($feed->url());+ if (!$feed_search) {$values = array('id' => $feed->id(),'url' => $feed->url(),@@ -50,7 +51,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {return $id;}- return false;+ return $feed_search->id();}public function updateFeed ($id, $valuesTmp) {