ImportService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /**
  11. * Initialize the service for the given user.
  12. *
  13. * @param string $username
  14. */
  15. public function __construct($username) {
  16. require_once(LIB_PATH . '/lib_opml.php');
  17. $this->catDAO = FreshRSS_Factory::createCategoryDao($username);
  18. $this->feedDAO = FreshRSS_Factory::createFeedDao($username);
  19. }
  20. /**
  21. * This method parses and imports an OPML file.
  22. *
  23. * @param string $opml_file the OPML file content.
  24. * @return boolean false if an error occurred, true otherwise.
  25. */
  26. public function importOpml($opml_file) {
  27. $opml_array = array();
  28. try {
  29. $opml_array = libopml_parse_string($opml_file, false);
  30. } catch (LibOPML_Exception $e) {
  31. if (FreshRSS_Context::$isCli) {
  32. fwrite(STDERR, 'FreshRSS error during OPML parsing: ' . $e->getMessage() . "\n");
  33. } else {
  34. Minz_Log::warning($e->getMessage());
  35. }
  36. return false;
  37. }
  38. $this->catDAO->checkDefault();
  39. return $this->addOpmlElements($opml_array['body']);
  40. }
  41. /**
  42. * This method imports an OPML file based on its body.
  43. *
  44. * @param array $opml_elements an OPML element (body or outline).
  45. * @param string $parent_cat the name of the parent category.
  46. * @return boolean false if an error occurred, true otherwise.
  47. */
  48. private function addOpmlElements($opml_elements, $parent_cat = null) {
  49. $isOkStatus = true;
  50. $nb_feeds = count($this->feedDAO->listFeeds());
  51. $nb_cats = count($this->catDAO->listCategories(false));
  52. $limits = FreshRSS_Context::$system_conf->limits;
  53. //Sort with categories first
  54. usort($opml_elements, function ($a, $b) {
  55. return strcmp(
  56. (isset($a['xmlUrl']) ? 'Z' : 'A') . (isset($a['text']) ? $a['text'] : ''),
  57. (isset($b['xmlUrl']) ? 'Z' : 'A') . (isset($b['text']) ? $b['text'] : ''));
  58. });
  59. foreach ($opml_elements as $elt) {
  60. if (isset($elt['xmlUrl'])) {
  61. // If xmlUrl exists, it means it is a feed
  62. if (FreshRSS_Context::$isCli && $nb_feeds >= $limits['max_feeds']) {
  63. Minz_Log::warning(_t('feedback.sub.feed.over_max',
  64. $limits['max_feeds']));
  65. $isOkStatus = false;
  66. continue;
  67. }
  68. if ($this->addFeedOpml($elt, $parent_cat)) {
  69. $nb_feeds++;
  70. } else {
  71. $isOkStatus = false;
  72. }
  73. } elseif (!empty($elt['text'])) {
  74. // No xmlUrl? It should be a category!
  75. $limit_reached = ($nb_cats >= $limits['max_categories']);
  76. if (!FreshRSS_Context::$isCli && $limit_reached) {
  77. Minz_Log::warning(_t('feedback.sub.category.over_max',
  78. $limits['max_categories']));
  79. $isOkStatus = false;
  80. continue;
  81. }
  82. if ($this->addCategoryOpml($elt, $parent_cat, $limit_reached)) {
  83. $nb_cats++;
  84. } else {
  85. $isOkStatus = false;
  86. }
  87. }
  88. }
  89. return $isOkStatus;
  90. }
  91. /**
  92. * This method imports an OPML feed element.
  93. *
  94. * @param array $feed_elt an OPML element (must be a feed element).
  95. * @param string $parent_cat the name of the parent category.
  96. * @return boolean false if an error occurred, true otherwise.
  97. */
  98. private function addFeedOpml($feed_elt, $parent_cat) {
  99. if ($parent_cat == null) {
  100. // This feed has no parent category so we get the default one
  101. $this->catDAO->checkDefault();
  102. $default_cat = $this->catDAO->getDefault();
  103. $parent_cat = $default_cat->name();
  104. }
  105. $cat = $this->catDAO->searchByName($parent_cat);
  106. if ($cat == null) {
  107. // If there is not $cat, it means parent category does not exist in
  108. // database.
  109. // If it happens, take the default category.
  110. $this->catDAO->checkDefault();
  111. $cat = $this->catDAO->getDefault();
  112. }
  113. // We get different useful information
  114. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  115. $name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text']);
  116. $website = '';
  117. if (isset($feed_elt['htmlUrl'])) {
  118. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl']);
  119. }
  120. $description = '';
  121. if (isset($feed_elt['description'])) {
  122. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']);
  123. }
  124. $error = false;
  125. try {
  126. // Create a Feed object and add it in DB
  127. $feed = new FreshRSS_Feed($url);
  128. $feed->_category($cat->id());
  129. $feed->_name($name);
  130. $feed->_website($website);
  131. $feed->_description($description);
  132. // Call the extension hook
  133. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  134. if ($feed != null) {
  135. // addFeedObject checks if feed is already in DB so nothing else to
  136. // check here
  137. $id = $this->feedDAO->addFeedObject($feed);
  138. $error = ($id === false);
  139. } else {
  140. $error = true;
  141. }
  142. } catch (FreshRSS_Feed_Exception $e) {
  143. if (FreshRSS_Context::$isCli) {
  144. fwrite(STDERR, 'FreshRSS error during OPML feed import: ' . $e->getMessage() . "\n");
  145. } else {
  146. Minz_Log::warning($e->getMessage());
  147. }
  148. $error = true;
  149. }
  150. if ($error) {
  151. if (FreshRSS_Context::$isCli) {
  152. fwrite(STDERR, 'FreshRSS error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id() . "\n");
  153. } else {
  154. Minz_Log::warning('Error during OPML feed import from URL: ' . $url . ' in category ' . $cat->id());
  155. }
  156. }
  157. return !$error;
  158. }
  159. /**
  160. * This method imports an OPML category element.
  161. *
  162. * @param array $cat_elt an OPML element (must be a category element).
  163. * @param string $parent_cat the name of the parent category.
  164. * @param boolean $cat_limit_reached indicates if category limit has been reached.
  165. * if yes, category is not added (but we try for feeds!)
  166. * @return boolean false if an error occurred, true otherwise.
  167. */
  168. private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
  169. // Create a new Category object
  170. $catName = Minz_Helper::htmlspecialchars_utf8($cat_elt['text']);
  171. $cat = new FreshRSS_Category($catName);
  172. $error = true;
  173. if (FreshRSS_Context::$isCli || !$cat_limit_reached) {
  174. $id = $this->catDAO->addCategoryObject($cat);
  175. $error = ($id === false);
  176. }
  177. if ($error) {
  178. if (FreshRSS_Context::$isCli) {
  179. fwrite(STDERR, 'FreshRSS error during OPML category import from URL: ' . $catName . "\n");
  180. } else {
  181. Minz_Log::warning('Error during OPML category import from URL: ' . $catName);
  182. }
  183. }
  184. if (isset($cat_elt['@outlines'])) {
  185. // Our cat_elt contains more categories or more feeds, so we
  186. // add them recursively.
  187. // Note: FreshRSS does not support yet category arborescence
  188. $error &= !$this->addOpmlElements($cat_elt['@outlines'], $catName);
  189. }
  190. return !$error;
  191. }
  192. }