ImportService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Provide methods to import files.
  4. */
  5. class FreshRSS_Import_Service {
  6. /** @var FreshRSS_CategoryDAO */
  7. private $catDAO;
  8. /** @var FreshRSS_FeedDAO */
  9. private $feedDAO;
  10. /** @var bool true if success, false otherwise */
  11. private $lastStatus;
  12. /**
  13. * Initialize the service for the given user.
  14. *
  15. * @param string $username
  16. */
  17. public function __construct($username = null) {
  18. require_once(LIB_PATH . '/lib_opml.php');
  19. $this->catDAO = FreshRSS_Factory::createCategoryDao($username);
  20. $this->feedDAO = FreshRSS_Factory::createFeedDao($username);
  21. }
  22. /** @return bool true if success, false otherwise */
  23. public function lastStatus(): bool {
  24. return $this->lastStatus;
  25. }
  26. /**
  27. * This method parses and imports an OPML file.
  28. *
  29. * @param string $opml_file the OPML file content.
  30. * @param FreshRSS_Category|null $parent_cat the name of the parent category.
  31. * @param boolean $flatten true to disable categories, false otherwise.
  32. * @return array<FreshRSS_Category>|false an array of categories containing some feeds, or false if an error occurred.
  33. */
  34. public function importOpml(string $opml_file, $parent_cat = null, $flatten = false, $dryRun = false) {
  35. $this->lastStatus = true;
  36. $opml_array = array();
  37. try {
  38. $opml_array = libopml_parse_string($opml_file, false);
  39. } catch (LibOPML_Exception $e) {
  40. if (FreshRSS_Context::$isCli) {
  41. fwrite(STDERR, 'FreshRSS error during OPML parsing: ' . $e->getMessage() . "\n");
  42. } else {
  43. Minz_Log::warning($e->getMessage());
  44. }
  45. $this->lastStatus = false;
  46. return false;
  47. }
  48. return $this->addOpmlElements($opml_array['body'], $parent_cat, $flatten, $dryRun);
  49. }
  50. /**
  51. * This method imports an OPML file based on its body.
  52. *
  53. * @param array $opml_elements an OPML element (body or outline).
  54. * @param FreshRSS_Category|null $parent_cat the name of the parent category.
  55. * @param boolean $flatten true to disable categories, false otherwise.
  56. * @return array<FreshRSS_Category> an array of categories containing some feeds
  57. */
  58. private function addOpmlElements($opml_elements, $parent_cat = null, $flatten = false, $dryRun = false) {
  59. $nb_feeds = count($this->feedDAO->listFeeds());
  60. $nb_cats = count($this->catDAO->listCategories(false));
  61. $limits = FreshRSS_Context::$system_conf->limits;
  62. //Sort with categories first
  63. usort($opml_elements, static function ($a, $b) {
  64. return strcmp(
  65. (isset($a['xmlUrl']) ? 'Z' : 'A') . (isset($a['text']) ? $a['text'] : ''),
  66. (isset($b['xmlUrl']) ? 'Z' : 'A') . (isset($b['text']) ? $b['text'] : ''));
  67. });
  68. $categories = [];
  69. foreach ($opml_elements as $elt) {
  70. if (isset($elt['xmlUrl'])) {
  71. // If xmlUrl exists, it means it is a feed
  72. if (FreshRSS_Context::$isCli && $nb_feeds >= $limits['max_feeds']) {
  73. Minz_Log::warning(_t('feedback.sub.feed.over_max',
  74. $limits['max_feeds']));
  75. $this->lastStatus = false;
  76. continue;
  77. }
  78. if ($this->addFeedOpml($elt, $parent_cat, $dryRun)) {
  79. $nb_feeds++;
  80. } else {
  81. $this->lastStatus = false;
  82. }
  83. } elseif (!empty($elt['text'])) {
  84. // No xmlUrl? It should be a category!
  85. $limit_reached = !$flatten && ($nb_cats >= $limits['max_categories']);
  86. if (!FreshRSS_Context::$isCli && $limit_reached) {
  87. Minz_Log::warning(_t('feedback.sub.category.over_max',
  88. $limits['max_categories']));
  89. $this->lastStatus = false;
  90. $flatten = true;
  91. }
  92. $category = $this->addCategoryOpml($elt, $parent_cat, $flatten, $dryRun);
  93. if ($category) {
  94. $nb_cats++;
  95. $categories[] = $category;
  96. }
  97. }
  98. }
  99. return $categories;
  100. }
  101. /**
  102. * This method imports an OPML feed element.
  103. *
  104. * @param array $feed_elt an OPML element (must be a feed element).
  105. * @param FreshRSS_Category|null $parent_cat the name of the parent category.
  106. * @return FreshRSS_Feed|null a feed.
  107. */
  108. private function addFeedOpml($feed_elt, $parent_cat, $dryRun = false) {
  109. if (empty($feed_elt['xmlUrl'])) {
  110. return null;
  111. }
  112. if ($parent_cat == null) {
  113. // This feed has no parent category so we get the default one
  114. $this->catDAO->checkDefault();
  115. $parent_cat = $this->catDAO->getDefault();
  116. if ($parent_cat == null) {
  117. $this->lastStatus = false;
  118. return null;
  119. }
  120. }
  121. // We get different useful information
  122. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  123. $name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text'] ?? '');
  124. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl'] ?? '');
  125. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description'] ?? '');
  126. try {
  127. // Create a Feed object and add it in DB
  128. $feed = new FreshRSS_Feed($url);
  129. $feed->_categoryId($parent_cat->id());
  130. $parent_cat->addFeed($feed);
  131. $feed->_name($name);
  132. $feed->_website($website);
  133. $feed->_description($description);
  134. switch ($feed_elt['type'] ?? '') {
  135. case FreshRSS_Export_Service::TYPE_HTML_XPATH:
  136. $feed->_kind(FreshRSS_Feed::KIND_HTML_XPATH);
  137. break;
  138. case FreshRSS_Export_Service::TYPE_RSS_ATOM:
  139. default:
  140. $feed->_kind(FreshRSS_Feed::KIND_RSS);
  141. break;
  142. }
  143. $xPathSettings = [];
  144. foreach ($feed_elt as $key => $value) {
  145. if (is_array($value) && !empty($value['value']) && ($value['namespace'] ?? '') === FreshRSS_Export_Service::FRSS_NAMESPACE) {
  146. switch ($key) {
  147. case 'cssFullContent': $feed->_pathEntries($value['value']); break;
  148. case 'cssFullContentFilter': $feed->_attributes('path_entries_filter', $value['value']); break;
  149. case 'filtersActionRead': $feed->_filtersAction('read', preg_split('/[\n\r]+/', $value['value'])); break;
  150. case 'xPathItem': $xPathSettings['item'] = $value['value']; break;
  151. case 'xPathItemTitle': $xPathSettings['itemTitle'] = $value['value']; break;
  152. case 'xPathItemContent': $xPathSettings['itemContent'] = $value['value']; break;
  153. case 'xPathItemUri': $xPathSettings['itemUri'] = $value['value']; break;
  154. case 'xPathItemAuthor': $xPathSettings['itemAuthor'] = $value['value']; break;
  155. case 'xPathItemTimestamp': $xPathSettings['itemTimestamp'] = $value['value']; break;
  156. case 'xPathItemThumbnail': $xPathSettings['itemThumbnail'] = $value['value']; break;
  157. case 'xPathItemCategories': $xPathSettings['itemCategories'] = $value['value']; break;
  158. case 'xPathItemUid': $xPathSettings['itemUid'] = $value['value']; break;
  159. }
  160. }
  161. }
  162. if (!empty($xPathSettings)) {
  163. $feed->_attributes('xpath', $xPathSettings);
  164. }
  165. // Call the extension hook
  166. /** @var FreshRSS_Feed|null */
  167. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  168. if ($dryRun) {
  169. return $feed;
  170. }
  171. if ($feed != null) {
  172. // addFeedObject checks if feed is already in DB
  173. $id = $this->feedDAO->addFeedObject($feed);
  174. if ($id == false) {
  175. $this->lastStatus = false;
  176. } else {
  177. $feed->_id($id);
  178. return $feed;
  179. }
  180. }
  181. } catch (FreshRSS_Feed_Exception $e) {
  182. if (FreshRSS_Context::$isCli) {
  183. fwrite(STDERR, 'FreshRSS error during OPML feed import: ' . $e->getMessage() . "\n");
  184. } else {
  185. Minz_Log::warning($e->getMessage());
  186. }
  187. $this->lastStatus = false;
  188. }
  189. if (FreshRSS_Context::$isCli) {
  190. fwrite(STDERR, 'FreshRSS error during OPML feed import from URL: ' .
  191. SimplePie_Misc::url_remove_credentials($url) . ' in category ' . $parent_cat->id() . "\n");
  192. } else {
  193. Minz_Log::warning('Error during OPML feed import from URL: ' .
  194. SimplePie_Misc::url_remove_credentials($url) . ' in category ' . $parent_cat->id());
  195. }
  196. return null;
  197. }
  198. /**
  199. * This method imports an OPML category element.
  200. *
  201. * @param array $cat_elt an OPML element (must be a category element).
  202. * @param FreshRSS_Category|null $parent_cat the name of the parent category.
  203. * @param boolean $flatten true to disable categories, false otherwise.
  204. * @return FreshRSS_Category|null a new category containing some feeds, or null if no category was created, or false if an error occurred.
  205. */
  206. private function addCategoryOpml($cat_elt, $parent_cat, $flatten = false, $dryRun = false) {
  207. $error = false;
  208. $cat = null;
  209. if (!$flatten) {
  210. $catName = Minz_Helper::htmlspecialchars_utf8($cat_elt['text']);
  211. $cat = new FreshRSS_Category($catName);
  212. foreach ($cat_elt as $key => $value) {
  213. if (is_array($value) && !empty($value['value']) && ($value['namespace'] ?? '') === FreshRSS_Export_Service::FRSS_NAMESPACE) {
  214. switch ($key) {
  215. case 'opmlUrl':
  216. $opml_url = checkUrl($value['value']);
  217. if ($opml_url != '') {
  218. $cat->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  219. $cat->_attributes('opml_url', $opml_url);
  220. }
  221. break;
  222. }
  223. }
  224. }
  225. if (!$dryRun) {
  226. $id = $this->catDAO->addCategoryObject($cat);
  227. if ($id == false) {
  228. $this->lastStatus = false;
  229. $error = true;
  230. } else {
  231. $cat->_id($id);
  232. }
  233. }
  234. if ($error) {
  235. if (FreshRSS_Context::$isCli) {
  236. fwrite(STDERR, 'FreshRSS error during OPML category import from URL: ' . $catName . "\n");
  237. } else {
  238. Minz_Log::warning('Error during OPML category import from URL: ' . $catName);
  239. }
  240. } else {
  241. $parent_cat = $cat;
  242. }
  243. }
  244. if (isset($cat_elt['@outlines'])) {
  245. // Our cat_elt contains more categories or more feeds, so we
  246. // add them recursively.
  247. // Note: FreshRSS does not support yet category arborescence, so always flatten from here
  248. $this->addOpmlElements($cat_elt['@outlines'], $parent_cat, true, $dryRun);
  249. }
  250. return $cat;
  251. }
  252. }