opml.phtml 3.4 KB

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