diff options
Diffstat (limited to 'app/Services/ImportService.php')
| -rw-r--r-- | app/Services/ImportService.php | 176 |
1 files changed, 105 insertions, 71 deletions
diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 7e7cccfdb..4cd866377 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -10,25 +10,36 @@ class FreshRSS_Import_Service { /** @var FreshRSS_FeedDAO */ private $feedDAO; + /** @var bool true if success, false otherwise */ + private $lastStatus; + /** * Initialize the service for the given user. * * @param string $username */ - public function __construct($username) { + public function __construct($username = null) { require_once(LIB_PATH . '/lib_opml.php'); $this->catDAO = FreshRSS_Factory::createCategoryDao($username); $this->feedDAO = FreshRSS_Factory::createFeedDao($username); } + /** @return bool true if success, false otherwise */ + public function lastStatus(): bool { + return $this->lastStatus; + } + /** * This method parses and imports an OPML file. * * @param string $opml_file the OPML file content. - * @return boolean false if an error occurred, true otherwise. + * @param FreshRSS_Category|null $parent_cat the name of the parent category. + * @param boolean $flatten true to disable categories, false otherwise. + * @return array<FreshRSS_Category>|false an array of categories containing some feeds, or false if an error occurred. */ - public function importOpml($opml_file) { + public function importOpml(string $opml_file, $parent_cat = null, $flatten = false, $dryRun = false) { + $this->lastStatus = true; $opml_array = array(); try { $opml_array = libopml_parse_string($opml_file, false); @@ -38,24 +49,22 @@ class FreshRSS_Import_Service { } else { Minz_Log::warning($e->getMessage()); } + $this->lastStatus = false; return false; } - $this->catDAO->checkDefault(); - - return $this->addOpmlElements($opml_array['body']); + return $this->addOpmlElements($opml_array['body'], $parent_cat, $flatten, $dryRun); } /** * This method imports an OPML file based on its body. * * @param array $opml_elements an OPML element (body or outline). - * @param string $parent_cat the name of the parent category. - * @return boolean false if an error occurred, true otherwise. + * @param FreshRSS_Category|null $parent_cat the name of the parent category. + * @param boolean $flatten true to disable categories, false otherwise. + * @return array<FreshRSS_Category> an array of categories containing some feeds */ - private function addOpmlElements($opml_elements, $parent_cat = null) { - $isOkStatus = true; - + private function addOpmlElements($opml_elements, $parent_cat = null, $flatten = false, $dryRun = false) { $nb_feeds = count($this->feedDAO->listFeeds()); $nb_cats = count($this->catDAO->listCategories(false)); $limits = FreshRSS_Context::$system_conf->limits; @@ -67,64 +76,61 @@ class FreshRSS_Import_Service { (isset($b['xmlUrl']) ? 'Z' : 'A') . (isset($b['text']) ? $b['text'] : '')); }); + $categories = []; + foreach ($opml_elements as $elt) { if (isset($elt['xmlUrl'])) { // If xmlUrl exists, it means it is a feed if (FreshRSS_Context::$isCli && $nb_feeds >= $limits['max_feeds']) { Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds'])); - $isOkStatus = false; + $this->lastStatus = false; continue; } - if ($this->addFeedOpml($elt, $parent_cat)) { + if ($this->addFeedOpml($elt, $parent_cat, $dryRun)) { $nb_feeds++; } else { - $isOkStatus = false; + $this->lastStatus = false; } } elseif (!empty($elt['text'])) { // No xmlUrl? It should be a category! - $limit_reached = ($nb_cats >= $limits['max_categories']); + $limit_reached = !$flatten && ($nb_cats >= $limits['max_categories']); if (!FreshRSS_Context::$isCli && $limit_reached) { Minz_Log::warning(_t('feedback.sub.category.over_max', $limits['max_categories'])); - $isOkStatus = false; - continue; + $this->lastStatus = false; + $flatten = true; } - if ($this->addCategoryOpml($elt, $parent_cat, $limit_reached)) { + $category = $this->addCategoryOpml($elt, $parent_cat, $flatten, $dryRun); + + if ($category) { $nb_cats++; - } else { - $isOkStatus = false; + $categories[] = $category; } } } - return $isOkStatus; + return $categories; } /** * This method imports an OPML feed element. * * @param array $feed_elt an OPML element (must be a feed element). - * @param string $parent_cat the name of the parent category. - * @return boolean false if an error occurred, true otherwise. + * @param FreshRSS_Category|null $parent_cat the name of the parent category. + * @return FreshRSS_Feed|null a feed. */ - private function addFeedOpml($feed_elt, $parent_cat) { + private function addFeedOpml($feed_elt, $parent_cat, $dryRun = false) { if ($parent_cat == null) { // This feed has no parent category so we get the default one $this->catDAO->checkDefault(); - $default_cat = $this->catDAO->getDefault(); - $parent_cat = $default_cat->name(); - } - - $cat = $this->catDAO->searchByName($parent_cat); - if ($cat == null) { - // If there is not $cat, it means parent category does not exist in - // database. - // If it happens, take the default category. - $this->catDAO->checkDefault(); - $cat = $this->catDAO->getDefault(); + $parent_cat = $this->catDAO->getDefault(); + if ($parent_cat == null) { + $this->lastStatus = false; + return null; + } } // We get different useful information @@ -139,11 +145,11 @@ class FreshRSS_Import_Service { $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']); } - $error = false; try { // Create a Feed object and add it in DB $feed = new FreshRSS_Feed($url); - $feed->_category($cat->id()); + $feed->_category($parent_cat->id()); + $parent_cat->addFeed($feed); $feed->_name($name); $feed->_website($website); $feed->_description($description); @@ -180,14 +186,20 @@ class FreshRSS_Import_Service { } // Call the extension hook + /** @var FreshRSS_Feed|null */ $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed); + if ($dryRun) { + return $feed; + } if ($feed != null) { - // addFeedObject checks if feed is already in DB so nothing else to - // check here + // addFeedObject checks if feed is already in DB $id = $this->feedDAO->addFeedObject($feed); - $error = ($id == false); - } else { - $error = true; + if ($id == false) { + $this->lastStatus = false; + } else { + $feed->_id($id); + return $feed; + } } } catch (FreshRSS_Feed_Exception $e) { if (FreshRSS_Context::$isCli) { @@ -195,54 +207,76 @@ class FreshRSS_Import_Service { } else { Minz_Log::warning($e->getMessage()); } - $error = true; + $this->lastStatus = false; } - if ($error) { - if (FreshRSS_Context::$isCli) { - fwrite(STDERR, 'FreshRSS error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id() . "\n"); - } else { - Minz_Log::warning('Error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id()); - } + if (FreshRSS_Context::$isCli) { + fwrite(STDERR, 'FreshRSS error during OPML feed import from URL: ' . + SimplePie_Misc::url_remove_credentials($url) . ' in category ' . $parent_cat->id() . "\n"); + } else { + Minz_Log::warning('Error during OPML feed import from URL: ' . + SimplePie_Misc::url_remove_credentials($url) . ' in category ' . $parent_cat->id()); } - return !$error; + return null; } /** * This method imports an OPML category element. * * @param array $cat_elt an OPML element (must be a category element). - * @param string $parent_cat the name of the parent category. - * @param boolean $cat_limit_reached indicates if category limit has been reached. - * if yes, category is not added (but we try for feeds!) - * @return boolean false if an error occurred, true otherwise. + * @param FreshRSS_Category|null $parent_cat the name of the parent category. + * @param boolean $flatten true to disable categories, false otherwise. + * @return FreshRSS_Category|null a new category containing some feeds, or null if no category was created, or false if an error occurred. */ - private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) { - // Create a new Category object - $catName = Minz_Helper::htmlspecialchars_utf8($cat_elt['text']); - $cat = new FreshRSS_Category($catName); - - $error = true; - if (FreshRSS_Context::$isCli || !$cat_limit_reached) { - $id = $this->catDAO->addCategoryObject($cat); - $error = ($id === false); - } - if ($error) { - if (FreshRSS_Context::$isCli) { - fwrite(STDERR, 'FreshRSS error during OPML category import from URL: ' . $catName . "\n"); + private function addCategoryOpml($cat_elt, $parent_cat, $flatten = false, $dryRun = false) { + $error = false; + $cat = null; + if (!$flatten) { + $catName = Minz_Helper::htmlspecialchars_utf8($cat_elt['text']); + $cat = new FreshRSS_Category($catName); + + foreach ($cat_elt as $key => $value) { + if (is_array($value) && !empty($value['value']) && ($value['namespace'] ?? '') === FreshRSS_Export_Service::FRSS_NAMESPACE) { + switch ($key) { + case 'opmlUrl': + $opml_url = checkUrl($value['value']); + if ($opml_url != '') { + $cat->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML); + $cat->_attributes('opml_url', $opml_url); + } + break; + } + } + } + + if (!$dryRun) { + $id = $this->catDAO->addCategoryObject($cat); + if ($id == false) { + $this->lastStatus = false; + $error = true; + } else { + $cat->_id($id); + } + } + if ($error) { + if (FreshRSS_Context::$isCli) { + fwrite(STDERR, 'FreshRSS error during OPML category import from URL: ' . $catName . "\n"); + } else { + Minz_Log::warning('Error during OPML category import from URL: ' . $catName); + } } else { - Minz_Log::warning('Error during OPML category import from URL: ' . $catName); + $parent_cat = $cat; } } if (isset($cat_elt['@outlines'])) { // Our cat_elt contains more categories or more feeds, so we // add them recursively. - // Note: FreshRSS does not support yet category arborescence - $error &= !$this->addOpmlElements($cat_elt['@outlines'], $catName); + // Note: FreshRSS does not support yet category arborescence, so always flatten from here + $this->addOpmlElements($cat_elt['@outlines'], $parent_cat, true, $dryRun); } - return !$error; + return $cat; } } |
