ImportService.php 6.5 KB

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