opml.phtml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @param array<FreshRSS_Feed> $feeds
  5. * @return array<array<string,string|null>>
  6. */
  7. function feedsToOutlines(array $feeds, bool $excludeMutedFeeds = false): array {
  8. $outlines = [];
  9. foreach ($feeds as $feed) {
  10. if ($feed->mute() && $excludeMutedFeeds) {
  11. continue;
  12. }
  13. $outline = [
  14. 'text' => htmlspecialchars_decode($feed->name(), ENT_QUOTES),
  15. 'type' => FreshRSS_Export_Service::TYPE_RSS_ATOM,
  16. 'xmlUrl' => htmlspecialchars_decode($feed->url(), ENT_QUOTES),
  17. 'htmlUrl' => htmlspecialchars_decode($feed->website(), ENT_QUOTES),
  18. 'description' => htmlspecialchars_decode($feed->description(), ENT_QUOTES),
  19. ];
  20. if ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH || $feed->kind() === FreshRSS_Feed::KIND_XML_XPATH) {
  21. switch ($feed->kind()) {
  22. case FreshRSS_Feed::KIND_HTML_XPATH:
  23. $outline['type'] = FreshRSS_Export_Service::TYPE_HTML_XPATH;
  24. break;
  25. case FreshRSS_Feed::KIND_XML_XPATH:
  26. $outline['type'] = FreshRSS_Export_Service::TYPE_XML_XPATH;
  27. break;
  28. }
  29. /** @var array<string,string> */
  30. $xPathSettings = $feed->attributes('xpath');
  31. $outline['frss:xPathItem'] = $xPathSettings['item'] ?? null;
  32. $outline['frss:xPathItemTitle'] = $xPathSettings['itemTitle'] ?? null;
  33. $outline['frss:xPathItemContent'] = $xPathSettings['itemContent'] ?? null;
  34. $outline['frss:xPathItemUri'] = $xPathSettings['itemUri'] ?? null;
  35. $outline['frss:xPathItemAuthor'] = $xPathSettings['itemAuthor'] ?? null;
  36. $outline['frss:xPathItemTimestamp'] = $xPathSettings['itemTimestamp'] ?? null;
  37. $outline['frss:xPathItemTimeformat'] = $xPathSettings['itemTimeformat'] ?? null;
  38. $outline['frss:xPathItemThumbnail'] = $xPathSettings['itemThumbnail'] ?? null;
  39. $outline['frss:xPathItemCategories'] = $xPathSettings['itemCategories'] ?? null;
  40. $outline['frss:xPathItemUid'] = $xPathSettings['itemUid'] ?? null;
  41. }
  42. if (!empty($feed->filtersAction('read'))) {
  43. $filters = '';
  44. foreach ($feed->filtersAction('read') as $filterRead) {
  45. $filters .= $filterRead->getRawInput() . "\n";
  46. }
  47. $filters = trim($filters);
  48. $outline['frss:filtersActionRead'] = $filters;
  49. }
  50. if ($feed->pathEntries() != '') {
  51. $outline['frss:cssFullContent'] = htmlspecialchars_decode($feed->pathEntries(), ENT_QUOTES);
  52. }
  53. if ($feed->attributes('path_entries_filter') != '') {
  54. $outline['frss:cssFullContentFilter'] = $feed->attributes('path_entries_filter');
  55. }
  56. // Remove null attributes
  57. $outline = array_filter($outline, static function (?string $value) { return $value !== null; });
  58. $outlines[] = $outline;
  59. }
  60. return $outlines;
  61. }
  62. /** @var FreshRSS_View $this */
  63. $opml_array = [
  64. 'namespaces' => [
  65. 'frss' => FreshRSS_Export_Service::FRSS_NAMESPACE,
  66. ],
  67. 'head' => [
  68. 'title' => FreshRSS_Context::$system_conf->title,
  69. 'dateCreated' => new DateTime(),
  70. ],
  71. 'body' => [],
  72. ];
  73. if (!empty($this->categories)) {
  74. foreach ($this->categories as $cat) {
  75. $outline = [
  76. 'text' => htmlspecialchars_decode($cat->name(), ENT_QUOTES),
  77. '@outlines' => feedsToOutlines($cat->feeds(), $this->excludeMutedFeeds),
  78. ];
  79. if ($cat->kind() === FreshRSS_Category::KIND_DYNAMIC_OPML) {
  80. $outline['frss:opmlUrl'] = $cat->attributes('opml_url');
  81. }
  82. $opml_array['body'][] = $outline;
  83. }
  84. }
  85. if (!empty($this->feeds)) {
  86. $opml_array['body'] = array_merge($opml_array['body'], feedsToOutlines($this->feeds, $this->excludeMutedFeeds));
  87. }
  88. $libopml = new \marienfressinaud\LibOpml\LibOpml(true);
  89. $opml = $libopml->render($opml_array);
  90. /** @var string $opml */
  91. echo $opml;