4
0

ImportService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Provide methods to import files.
  5. */
  6. class FreshRSS_Import_Service {
  7. private readonly FreshRSS_CategoryDAO $catDAO;
  8. private readonly FreshRSS_FeedDAO $feedDAO;
  9. /** true if success, false otherwise */
  10. private bool $lastStatus;
  11. /**
  12. * Initialize the service for the given user.
  13. */
  14. public function __construct(?string $username = null) {
  15. $this->catDAO = FreshRSS_Factory::createCategoryDao($username);
  16. $this->feedDAO = FreshRSS_Factory::createFeedDao($username);
  17. }
  18. /** @return bool true if success, false otherwise */
  19. public function lastStatus(): bool {
  20. return $this->lastStatus;
  21. }
  22. /**
  23. * This method parses and imports an OPML file.
  24. *
  25. * @param string $opml_file the OPML file content.
  26. * @param FreshRSS_Category|null $forced_category force the feeds to be associated to this category.
  27. * @param bool $dry_run true to not create categories and feeds in database.
  28. */
  29. public function importOpml(string $opml_file, ?FreshRSS_Category $forced_category = null, bool $dry_run = false): void {
  30. if (function_exists('set_time_limit')) {
  31. @set_time_limit(300);
  32. }
  33. $this->lastStatus = true;
  34. $opml_array = [];
  35. try {
  36. $libopml = new \marienfressinaud\LibOpml\LibOpml(strict: false);
  37. /** @var array{body:array<array<mixed>>} $opml_array */
  38. $opml_array = $libopml->parseString($opml_file);
  39. } catch (\marienfressinaud\LibOpml\Exception $e) {
  40. self::log($e->getMessage());
  41. $this->lastStatus = false;
  42. return;
  43. }
  44. $this->catDAO->checkDefault();
  45. $default_category = $this->catDAO->getDefault();
  46. if ($default_category === null) {
  47. self::log('Cannot get the default category');
  48. $this->lastStatus = false;
  49. return;
  50. }
  51. // Get the categories by names so we can use this array to retrieve
  52. // existing categories later.
  53. $categories = $this->catDAO->listCategories(prePopulateFeeds: false);
  54. $categories_by_names = [];
  55. $highest_position = PHP_INT_MIN; // To order the numbers as best as possible in case of negative positions, we choose the lowest possible number first
  56. foreach ($categories as $category) {
  57. $categories_by_names[$category->name()] = $category;
  58. $position = $category->attributeInt('position') ?? PHP_INT_MIN;
  59. if ($position > $highest_position) {
  60. $highest_position = $position;
  61. }
  62. }
  63. if ($highest_position === PHP_INT_MIN) {
  64. // If it's still the same number, default to -1 instead
  65. $highest_position = -1; // will be incremented to 0
  66. }
  67. // Get current numbers of categories and feeds, and the limits to
  68. // verify the user can import its categories/feeds.
  69. $nb_categories = count($categories);
  70. $nb_feeds = count($this->feedDAO->listFeeds());
  71. $limits = FreshRSS_Context::systemConf()->limits;
  72. // Process the OPML outlines to get a list of categories and a list of
  73. // feeds elements indexed by their categories names.
  74. [$categories_elements, $categories_to_feeds] = $this->loadFromOutlines($opml_array['body'], '');
  75. foreach ($categories_to_feeds as $category_name => $feeds_elements) {
  76. $category_element = $categories_elements[$category_name] ?? null;
  77. $category = null;
  78. if ($forced_category !== null) {
  79. // If the category is forced, ignore the actual category name
  80. $category = $forced_category;
  81. } elseif (isset($categories_by_names[$category_name])) {
  82. // If the category already exists, get it from $categories_by_names
  83. $category = $categories_by_names[$category_name];
  84. } elseif (is_array($category_element)) {
  85. // Otherwise, create the category (if possible)
  86. $limit_reached = $nb_categories >= $limits['max_categories'];
  87. $can_create_category = FreshRSS_Context::$isCli || !$limit_reached;
  88. if ($can_create_category) {
  89. // Import category in the exact order as the outline's placement in the OPML, at the end of positioned categories
  90. $category = $this->createCategory($category_element, $dry_run, ++$highest_position);
  91. if ($category !== null) {
  92. $categories_by_names[$category->name()] = $category;
  93. $nb_categories++;
  94. }
  95. } else {
  96. Minz_Log::warning(
  97. _t('feedback.sub.category.over_max', $limits['max_categories'])
  98. );
  99. }
  100. }
  101. if ($category === null) {
  102. // Category can be null if the feeds weren't in a category
  103. // outline, or if we weren't able to create the category.
  104. $category = $default_category;
  105. }
  106. // Then, create the feeds one by one and attach them to the
  107. // category we just got.
  108. foreach ($feeds_elements as $feed_element) {
  109. $limit_reached = $nb_feeds >= $limits['max_feeds'];
  110. $can_create_feed = FreshRSS_Context::$isCli || !$limit_reached;
  111. if (!$can_create_feed) {
  112. Minz_Log::warning(
  113. _t('feedback.sub.feed.over_max', $limits['max_feeds'])
  114. );
  115. $this->lastStatus = false;
  116. break;
  117. }
  118. if ($this->createFeed($feed_element, $category, $dry_run) !== null) {
  119. // TODO what if the feed already exists in the database?
  120. $nb_feeds++;
  121. } else {
  122. $this->lastStatus = false;
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * Create a feed from a feed element (i.e. OPML outline).
  129. *
  130. * @param array<string,string> $feed_elt An OPML element (must be a feed element).
  131. * @param FreshRSS_Category $category The category to associate to the feed.
  132. * @param bool $dry_run true to not create the feed in database.
  133. * @return FreshRSS_Feed|null The created feed, or null if it failed.
  134. */
  135. private function createFeed(array $feed_elt, FreshRSS_Category $category, bool $dry_run): ?FreshRSS_Feed {
  136. $url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
  137. $name = $feed_elt['text'] ?? $feed_elt['title'] ?? '';
  138. $name = Minz_Helper::htmlspecialchars_utf8($name);
  139. $website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl'] ?? '');
  140. $description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description'] ?? '');
  141. try {
  142. // Create a Feed object and add it in DB
  143. $feed = new FreshRSS_Feed($url);
  144. $feed->_category($category);
  145. $feed->_name($name);
  146. $feed->_website($website);
  147. $feed->_description($description);
  148. switch (strtolower($feed_elt['type'] ?? '')) {
  149. case strtolower(FreshRSS_Export_Service::TYPE_HTML_XPATH):
  150. $feed->_kind(FreshRSS_Feed::KIND_HTML_XPATH);
  151. break;
  152. case strtolower(FreshRSS_Export_Service::TYPE_XML_XPATH):
  153. $feed->_kind(FreshRSS_Feed::KIND_XML_XPATH);
  154. break;
  155. case strtolower(FreshRSS_Export_Service::TYPE_JSON_DOTNOTATION):
  156. case strtolower(FreshRSS_Export_Service::TYPE_JSON_DOTPATH):
  157. $feed->_kind(FreshRSS_Feed::KIND_JSON_DOTNOTATION);
  158. break;
  159. case strtolower(FreshRSS_Export_Service::TYPE_JSONFEED):
  160. $feed->_kind(FreshRSS_Feed::KIND_JSONFEED);
  161. break;
  162. case strtolower(FreshRSS_Export_Service::TYPE_HTML_XPATH_JSON_DOTNOTATION):
  163. $feed->_kind(FreshRSS_Feed::KIND_HTML_XPATH_JSON_DOTNOTATION);
  164. break;
  165. default:
  166. $feed->_kind(FreshRSS_Feed::KIND_RSS);
  167. break;
  168. }
  169. $feed->_priority(match (strtolower($feed_elt['frss:priority'] ?? '')) {
  170. FreshRSS_Export_Service::PRIORITY_IMPORTANT => FreshRSS_Feed::PRIORITY_IMPORTANT,
  171. FreshRSS_Export_Service::PRIORITY_MAIN_STREAM => FreshRSS_Feed::PRIORITY_MAIN_STREAM,
  172. FreshRSS_Export_Service::PRIORITY_CATEGORY => FreshRSS_Feed::PRIORITY_CATEGORY,
  173. FreshRSS_Export_Service::PRIORITY_FEED => FreshRSS_Feed::PRIORITY_FEED,
  174. FreshRSS_Export_Service::PRIORITY_HIDDEN => FreshRSS_Feed::PRIORITY_HIDDEN,
  175. default => FreshRSS_Feed::PRIORITY_MAIN_STREAM,
  176. });
  177. if (isset($feed_elt['frss:unicityCriteria']) && $feed_elt['frss:unicityCriteria'] !== 'id'
  178. && preg_match('/^[a-z:_-]{2,64}$/', $feed_elt['frss:unicityCriteria'])) {
  179. $feed->_attribute('unicityCriteria', $feed_elt['frss:unicityCriteria']);
  180. }
  181. if (filter_var($feed_elt['frss:unicityCriteriaForced'] ?? '', FILTER_VALIDATE_BOOLEAN)) {
  182. $feed->_attribute('unicityCriteriaForced', true);
  183. }
  184. if (isset($feed_elt['frss:ttl'])) {
  185. // Signed refresh interval (TTL); a negative value indicates a muted feed
  186. $feed->_ttl((int)$feed_elt['frss:ttl']);
  187. }
  188. if (isset($feed_elt['frss:cssFullContent'])) {
  189. $feed->_pathEntries(Minz_Helper::htmlspecialchars_utf8($feed_elt['frss:cssFullContent']));
  190. }
  191. if (isset($feed_elt['frss:cssFullContentConditions'])) {
  192. $feed->_attribute(
  193. 'path_entries_conditions',
  194. preg_split('/\R/u', $feed_elt['frss:cssFullContentConditions']) ?: []
  195. );
  196. }
  197. if (isset($feed_elt['frss:cssContentFilter']) || isset($feed_elt['frss:cssFullContentFilter'])) {
  198. $feed->_attribute('path_entries_filter', $feed_elt['frss:cssContentFilter'] ?? $feed_elt['frss:cssFullContentFilter']);
  199. }
  200. if (isset($feed_elt['frss:filtersActionRead'])) {
  201. $feed->_filtersAction(
  202. 'read',
  203. preg_split('/\R/u', $feed_elt['frss:filtersActionRead']) ?: []
  204. );
  205. }
  206. $xPathSettings = [];
  207. if (isset($feed_elt['frss:xPathItem'])) {
  208. $xPathSettings['item'] = $feed_elt['frss:xPathItem'];
  209. }
  210. if (isset($feed_elt['frss:xPathItemTitle'])) {
  211. $xPathSettings['itemTitle'] = $feed_elt['frss:xPathItemTitle'];
  212. }
  213. if (isset($feed_elt['frss:xPathItemContent'])) {
  214. $xPathSettings['itemContent'] = $feed_elt['frss:xPathItemContent'];
  215. }
  216. if (isset($feed_elt['frss:xPathItemUri'])) {
  217. $xPathSettings['itemUri'] = $feed_elt['frss:xPathItemUri'];
  218. }
  219. if (isset($feed_elt['frss:xPathItemAuthor'])) {
  220. $xPathSettings['itemAuthor'] = $feed_elt['frss:xPathItemAuthor'];
  221. }
  222. if (isset($feed_elt['frss:xPathItemTimestamp'])) {
  223. $xPathSettings['itemTimestamp'] = $feed_elt['frss:xPathItemTimestamp'];
  224. }
  225. if (isset($feed_elt['frss:xPathItemTimeFormat'])) {
  226. $xPathSettings['itemTimeFormat'] = $feed_elt['frss:xPathItemTimeFormat'];
  227. }
  228. if (isset($feed_elt['frss:xPathItemThumbnail'])) {
  229. $xPathSettings['itemThumbnail'] = $feed_elt['frss:xPathItemThumbnail'];
  230. }
  231. if (isset($feed_elt['frss:xPathItemCategories'])) {
  232. $xPathSettings['itemCategories'] = $feed_elt['frss:xPathItemCategories'];
  233. }
  234. if (isset($feed_elt['frss:xPathItemUid'])) {
  235. $xPathSettings['itemUid'] = $feed_elt['frss:xPathItemUid'];
  236. }
  237. if (!empty($xPathSettings)) {
  238. $feed->_attribute('xpath', $xPathSettings);
  239. }
  240. $jsonSettings = [];
  241. if (isset($feed_elt['frss:jsonItem'])) {
  242. $jsonSettings['item'] = $feed_elt['frss:jsonItem'];
  243. }
  244. if (isset($feed_elt['frss:jsonItemTitle'])) {
  245. $jsonSettings['itemTitle'] = $feed_elt['frss:jsonItemTitle'];
  246. }
  247. if (isset($feed_elt['frss:jsonItemContent'])) {
  248. $jsonSettings['itemContent'] = $feed_elt['frss:jsonItemContent'];
  249. }
  250. if (isset($feed_elt['frss:jsonItemUri'])) {
  251. $jsonSettings['itemUri'] = $feed_elt['frss:jsonItemUri'];
  252. }
  253. if (isset($feed_elt['frss:jsonItemAuthor'])) {
  254. $jsonSettings['itemAuthor'] = $feed_elt['frss:jsonItemAuthor'];
  255. }
  256. if (isset($feed_elt['frss:jsonItemTimestamp'])) {
  257. $jsonSettings['itemTimestamp'] = $feed_elt['frss:jsonItemTimestamp'];
  258. }
  259. if (isset($feed_elt['frss:jsonItemTimeFormat'])) {
  260. $jsonSettings['itemTimeFormat'] = $feed_elt['frss:jsonItemTimeFormat'];
  261. }
  262. if (isset($feed_elt['frss:jsonItemThumbnail'])) {
  263. $jsonSettings['itemThumbnail'] = $feed_elt['frss:jsonItemThumbnail'];
  264. }
  265. if (isset($feed_elt['frss:jsonItemCategories'])) {
  266. $jsonSettings['itemCategories'] = $feed_elt['frss:jsonItemCategories'];
  267. }
  268. if (isset($feed_elt['frss:jsonItemUid'])) {
  269. $jsonSettings['itemUid'] = $feed_elt['frss:jsonItemUid'];
  270. }
  271. if (!empty($jsonSettings)) {
  272. $feed->_attribute('json_dotnotation', $jsonSettings);
  273. }
  274. $feed->_attribute('xPathToJson', $feed_elt['frss:xPathToJson'] ?? null);
  275. $curl_params = [];
  276. if (isset($feed_elt['frss:CURLOPT_COOKIE'])) {
  277. $curl_params[CURLOPT_COOKIE] = $feed_elt['frss:CURLOPT_COOKIE'];
  278. }
  279. if (isset($feed_elt['frss:CURLOPT_COOKIEFILE'])) {
  280. // Allow only an empty value just to enable the libcurl cookie engine
  281. $curl_params[CURLOPT_COOKIEFILE] = '';
  282. }
  283. if (isset($feed_elt['frss:CURLOPT_FOLLOWLOCATION'])) {
  284. $curl_params[CURLOPT_FOLLOWLOCATION] = (bool)$feed_elt['frss:CURLOPT_FOLLOWLOCATION'];
  285. }
  286. if (isset($feed_elt['frss:CURLOPT_HTTPHEADER'])) {
  287. $curl_params[CURLOPT_HTTPHEADER] = preg_split('/\R/u', $feed_elt['frss:CURLOPT_HTTPHEADER']) ?: [];
  288. }
  289. if (isset($feed_elt['frss:CURLOPT_MAXREDIRS'])) {
  290. $curl_params[CURLOPT_MAXREDIRS] = (int)$feed_elt['frss:CURLOPT_MAXREDIRS'];
  291. }
  292. if (isset($feed_elt['frss:CURLOPT_POST'])) {
  293. $curl_params[CURLOPT_POST] = (bool)$feed_elt['frss:CURLOPT_POST'];
  294. }
  295. if (isset($feed_elt['frss:CURLOPT_POSTFIELDS'])) {
  296. $curl_params[CURLOPT_POSTFIELDS] = $feed_elt['frss:CURLOPT_POSTFIELDS'];
  297. }
  298. if (isset($feed_elt['frss:CURLOPT_PROXY'])) {
  299. $curl_params[CURLOPT_PROXY] = $feed_elt['frss:CURLOPT_PROXY'];
  300. }
  301. if (isset($feed_elt['frss:CURLOPT_PROXYTYPE'])) {
  302. $curl_params[CURLOPT_PROXYTYPE] = (int)$feed_elt['frss:CURLOPT_PROXYTYPE'];
  303. if ($curl_params[CURLOPT_PROXYTYPE] === 3) { // Legacy for NONE
  304. $curl_params[CURLOPT_PROXYTYPE] = -1;
  305. }
  306. }
  307. if (isset($feed_elt['frss:CURLOPT_USERAGENT'])) {
  308. $curl_params[CURLOPT_USERAGENT] = $feed_elt['frss:CURLOPT_USERAGENT'];
  309. }
  310. if (!empty($curl_params)) {
  311. $feed->_attribute('curl_params', FreshRSS_http_Util::sanitizeCurlParams($curl_params));
  312. }
  313. // Call the extension hook
  314. /** @var FreshRSS_Feed|null */
  315. $feed = Minz_ExtensionManager::callHook(Minz_HookType::FeedBeforeInsert, $feed);
  316. if ($dry_run) {
  317. if ($feed !== null) {
  318. $category->addFeed($feed);
  319. }
  320. return $feed;
  321. }
  322. if ($feed !== null) {
  323. // addFeedObject checks if feed is already in DB
  324. $id = $this->feedDAO->addFeedObject($feed);
  325. if ($id == false) {
  326. $this->lastStatus = false;
  327. } else {
  328. $feed->_id($id);
  329. $category->addFeed($feed);
  330. return $feed;
  331. }
  332. }
  333. } catch (FreshRSS_Feed_Exception $e) {
  334. self::log($e->getMessage());
  335. $this->lastStatus = false;
  336. }
  337. $clean_url = \SimplePie\Misc::url_remove_credentials($url);
  338. self::log("Cannot create {$clean_url} feed in category {$category->name()}");
  339. return null;
  340. }
  341. /**
  342. * Create and return a category.
  343. *
  344. * @param array<string,string> $category_element An OPML element (must be a category element).
  345. * @param bool $dry_run true to not create the category in database.
  346. * @return FreshRSS_Category|null The created category, or null if it failed.
  347. */
  348. private function createCategory(array $category_element, bool $dry_run, int $position): ?FreshRSS_Category {
  349. $name = $category_element['text'] ?? $category_element['title'] ?? '';
  350. $name = Minz_Helper::htmlspecialchars_utf8($name);
  351. $category = new FreshRSS_Category($name);
  352. if (isset($category_element['frss:opmlUrl'])) {
  353. $opml_url = FreshRSS_http_Util::checkUrl($category_element['frss:opmlUrl']);
  354. if ($opml_url != '') {
  355. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  356. $category->_attribute('opml_url', $opml_url);
  357. }
  358. }
  359. $category->_attribute('position', $position);
  360. if ($dry_run) {
  361. return $category;
  362. }
  363. $id = $this->catDAO->addCategoryObject($category);
  364. if ($id !== false) {
  365. $category->_id($id);
  366. return $category;
  367. } else {
  368. self::log("Cannot create category {$category->name()}");
  369. $this->lastStatus = false;
  370. return null;
  371. }
  372. }
  373. /**
  374. * Return the list of category and feed outlines by categories names.
  375. *
  376. * This method is applied to a list of outlines. It merges the different
  377. * list of feeds from several outlines into one array.
  378. *
  379. * @param array<array<mixed>> $outlines The outlines from which to extract the outlines.
  380. * @param string $parent_category_name The name of the parent category of the current outlines.
  381. * @return array{0:array<string,array<string,string>>,1:array<string,list<array<string,string>>>}
  382. */
  383. private function loadFromOutlines(array $outlines, string $parent_category_name): array {
  384. $categories_elements = [];
  385. $categories_to_feeds = [];
  386. foreach ($outlines as $outline) {
  387. if (!is_array($outline)) {
  388. continue;
  389. }
  390. // Get the categories and feeds from the child outline (it may
  391. // return several categories and feeds if the outline is a category).
  392. [$outline_categories, $outline_categories_to_feeds] = $this->loadFromOutline($outline, $parent_category_name);
  393. // Then, we merge the initial arrays with the arrays returned by
  394. // the outline.
  395. $categories_elements = array_merge($categories_elements, $outline_categories);
  396. foreach ($outline_categories_to_feeds as $category_name => $feeds) {
  397. if (!is_string($category_name) || // @phpstan-ignore function.alreadyNarrowedType (defensive)
  398. !is_array($feeds)) { // @phpstan-ignore function.alreadyNarrowedType (defensive)
  399. continue;
  400. }
  401. if (!isset($categories_to_feeds[$category_name])) {
  402. $categories_to_feeds[$category_name] = [];
  403. }
  404. $categories_to_feeds[$category_name] = array_merge(
  405. $categories_to_feeds[$category_name],
  406. $feeds
  407. );
  408. }
  409. }
  410. return [$categories_elements, $categories_to_feeds];
  411. }
  412. /**
  413. * Return the list of category and feed outlines by categories names.
  414. *
  415. * This method is applied to a specific outline. If the outline represents
  416. * a category (i.e. @outlines key exists), it will reapply loadFromOutlines()
  417. * to its children. If the outline represents a feed (i.e. xmlUrl key
  418. * exists), it will add the outline to an array accessible by its category
  419. * name.
  420. *
  421. * @param array<mixed> $outline The outline from which to extract the categories and feeds outlines.
  422. * @param string $parent_category_name The name of the parent category of the current outline.
  423. *
  424. * @return array{0:array<string,array<string,string>>,1:array<string,list<array<string,string>>>}
  425. */
  426. private function loadFromOutline(array $outline, string $parent_category_name): array {
  427. $categories_elements = [];
  428. $categories_to_feeds = [];
  429. if ($parent_category_name === '' && is_array($outline['category'] ?? null)) {
  430. // The outline has no parent category, but its OPML category
  431. // attribute is set, so we use it as the category name.
  432. // lib_opml parses this attribute as an array of strings, so we
  433. // rebuild a string here.
  434. $category_names = array_filter($outline['category'], 'is_string');
  435. $parent_category_name = implode(', ', $category_names);
  436. $categories_elements[$parent_category_name] = [
  437. 'text' => $parent_category_name,
  438. ];
  439. }
  440. if (is_array($outline['@outlines'] ?? null)) {
  441. // The outline has children, it’s probably a category
  442. if (!empty($outline['text']) && is_string($outline['text'])) {
  443. $category_name = $outline['text'];
  444. } elseif (!empty($outline['title']) && is_string($outline['title'])) {
  445. $category_name = $outline['title'];
  446. } else {
  447. $category_name = $parent_category_name;
  448. }
  449. $children = array_filter($outline['@outlines'], 'is_array');
  450. [$categories_elements, $categories_to_feeds] = $this->loadFromOutlines($children, $category_name);
  451. unset($outline['@outlines']);
  452. $categories_elements[$category_name] = array_filter($outline, static fn($value, $key) => is_string($key) && is_string($value), ARRAY_FILTER_USE_BOTH);
  453. }
  454. // The xmlUrl means it’s a feed URL: add the outline to the array if it exists.
  455. if (isset($outline['xmlUrl'])) {
  456. if (!isset($categories_to_feeds[$parent_category_name])) {
  457. $categories_to_feeds[$parent_category_name] = [];
  458. }
  459. $feed = array_filter($outline, static fn($value, $key) => is_string($key) && is_string($value), ARRAY_FILTER_USE_BOTH);
  460. $categories_to_feeds[$parent_category_name][] = $feed;
  461. }
  462. return [$categories_elements, $categories_to_feeds];
  463. }
  464. private static function log(string $message): void {
  465. if (FreshRSS_Context::$isCli) {
  466. fwrite(STDERR, "FreshRSS error during OPML import: {$message}\n");
  467. } else {
  468. Minz_Log::warning("Error during OPML import: {$message}");
  469. }
  470. }
  471. }