ExportService.php 5.5 KB

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