4
0

ExportService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Provide useful methods to generate files to export.
  5. */
  6. class FreshRSS_Export_Service {
  7. private readonly FreshRSS_CategoryDAO $category_dao;
  8. private readonly FreshRSS_FeedDAO $feed_dao;
  9. private readonly FreshRSS_EntryDAO $entry_dao;
  10. private readonly FreshRSS_TagDAO $tag_dao;
  11. final public const FRSS_NAMESPACE = 'https://freshrss.org/opml';
  12. final public const TYPE_HTML_XPATH = 'HTML+XPath';
  13. final public const TYPE_XML_XPATH = 'XML+XPath';
  14. final public const TYPE_RSS_ATOM = 'rss';
  15. final public const TYPE_JSON_DOTPATH = 'JSON+DotPath'; // Legacy 1.24.0-dev
  16. final public const TYPE_JSON_DOTNOTATION = 'JSON+DotNotation';
  17. final public const TYPE_JSONFEED = 'JSONFeed';
  18. final public const TYPE_HTML_XPATH_JSON_DOTNOTATION = 'HTML+XPath+JSON+DotNotation';
  19. final public const PRIORITY_IMPORTANT = 'important';
  20. final public const PRIORITY_MAIN_STREAM = 'main';
  21. final public const PRIORITY_CATEGORY = 'category';
  22. final public const PRIORITY_FEED = 'feed';
  23. final public const PRIORITY_HIDDEN = 'hidden';
  24. /**
  25. * Initialize the service for the given user.
  26. */
  27. public function __construct(private readonly string $username) {
  28. $this->category_dao = FreshRSS_Factory::createCategoryDao($this->username);
  29. $this->feed_dao = FreshRSS_Factory::createFeedDao($this->username);
  30. $this->entry_dao = FreshRSS_Factory::createEntryDao($this->username);
  31. $this->tag_dao = FreshRSS_Factory::createTagDao();
  32. }
  33. /**
  34. * Generate OPML file content.
  35. * @return array{0:string,1:string} First item is the filename, second item is the content
  36. */
  37. public function generateOpml(): array {
  38. $view = new FreshRSS_View();
  39. $day = date('Y-m-d');
  40. $view->categories = $this->category_dao->listCategories(prePopulateFeeds: true, details: true);
  41. $view->excludeMutedFeeds = false;
  42. return [
  43. "feeds_{$day}.opml.xml",
  44. $view->helperToString('export/opml')
  45. ];
  46. }
  47. /**
  48. * Generate the starred and labelled entries file content.
  49. *
  50. * Both starred and labelled entries are put into a "starred" file, that’s
  51. * why there is only one method for both.
  52. *
  53. * @phpstan-param 'S'|'T'|'ST' $type
  54. * @param string $type must be one of:
  55. * 'S' (starred/favourite),
  56. * 'T' (taggued/labelled),
  57. * 'ST' (starred or labelled)
  58. * @return array{0:string,1:string} First item is the filename, second item is the content
  59. */
  60. public function generateStarredEntries(string $type): array {
  61. $view = new FreshRSS_View();
  62. $view->categories = $this->category_dao->listCategories(prePopulateFeeds: true);
  63. $day = date('Y-m-d');
  64. $view->list_title = _t('sub.import_export.starred_list');
  65. $view->type = 'starred';
  66. $entriesId = $this->entry_dao->listIdsWhere($type, 0, FreshRSS_Entry::STATE_ALL, order: 'ASC', limit: -1) ?? [];
  67. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  68. // The following is a streamable query, i.e. must be last
  69. $view->entries = $this->entry_dao->listWhere(
  70. $type, 0, FreshRSS_Entry::STATE_ALL, order: 'ASC', limit: -1
  71. );
  72. return [
  73. "starred_{$day}.json",
  74. $view->helperToString('export/articles')
  75. ];
  76. }
  77. /**
  78. * Generate the entries file content for the given feed.
  79. * @return array{0:string,1:string}|null First item is the filename, second item is the content.
  80. * It also can return null if the feed doesn’t exist.
  81. */
  82. public function generateFeedEntries(int $feed_id, int $max_number_entries): ?array {
  83. $view = new FreshRSS_View();
  84. $view->categories = $this->category_dao->listCategories(prePopulateFeeds: true);
  85. $feed = FreshRSS_Category::findFeed($view->categories, $feed_id);
  86. if ($feed === null) {
  87. return null;
  88. }
  89. $view->feed = $feed;
  90. $day = date('Y-m-d');
  91. $filename = "feed_{$day}_" . $feed->categoryId() . '_' . $feed->id() . '.json';
  92. $view->list_title = _t('sub.import_export.feed_list', $feed->name());
  93. $view->type = 'feed/' . $feed->id();
  94. $entriesId = $this->entry_dao->listIdsWhere(
  95. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, order: 'ASC', limit: $max_number_entries
  96. ) ?? [];
  97. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  98. // The following is a streamable query, i.e. must be last
  99. $view->entries = $this->entry_dao->listWhere(
  100. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, order: 'ASC', limit: $max_number_entries
  101. );
  102. return [
  103. $filename,
  104. $view->helperToString('export/articles')
  105. ];
  106. }
  107. /**
  108. * Generate the entries file content for all the feeds.
  109. * @return array<string,string> Keys are filenames and values are contents.
  110. */
  111. public function generateAllFeedEntries(int $max_number_entries): array {
  112. $feed_ids = $this->feed_dao->listFeedsIds();
  113. $exported_files = [];
  114. foreach ($feed_ids as $feed_id) {
  115. $result = $this->generateFeedEntries($feed_id, $max_number_entries);
  116. if ($result === null) {
  117. continue;
  118. }
  119. [$filename, $content] = $result;
  120. $exported_files[$filename] = $content;
  121. }
  122. return $exported_files;
  123. }
  124. /**
  125. * Compress several files in a Zip file.
  126. * @param array<string,string> $files where the key is the filename, the value is the content
  127. * @return array{0:string,1:string|false} First item is the zip filename, second item is the zip content
  128. */
  129. public function zip(array $files): array {
  130. $day = date('Y-m-d');
  131. $zip_filename = 'freshrss_' . $this->username . '_' . $day . '_export.zip';
  132. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  133. $zip_file = tempnam(TMP_PATH, 'zip');
  134. if ($zip_file === false) {
  135. return [$zip_filename, false];
  136. }
  137. $zip_archive = new ZipArchive();
  138. $zip_archive->open($zip_file, ZipArchive::OVERWRITE);
  139. foreach ($files as $filename => $content) {
  140. $zip_archive->addFromString($filename, $content);
  141. }
  142. $zip_archive->close();
  143. $content = file_get_contents($zip_file);
  144. unlink($zip_file);
  145. return [
  146. $zip_filename,
  147. $content,
  148. ];
  149. }
  150. }