ExportService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Provide useful methods to generate files to export.
  4. */
  5. class FreshRSS_Export_Service {
  6. private string $username;
  7. private FreshRSS_CategoryDAO $category_dao;
  8. private FreshRSS_FeedDAO $feed_dao;
  9. private FreshRSS_EntryDAO $entry_dao;
  10. private FreshRSS_TagDAO $tag_dao;
  11. public const FRSS_NAMESPACE = 'https://freshrss.org/opml';
  12. public const TYPE_HTML_XPATH = 'HTML+XPath';
  13. public const TYPE_XML_XPATH = 'XML+XPath';
  14. public const TYPE_RSS_ATOM = 'rss';
  15. /**
  16. * Initialize the service for the given user.
  17. */
  18. public function __construct(string $username) {
  19. $this->username = $username;
  20. $this->category_dao = FreshRSS_Factory::createCategoryDao($username);
  21. $this->feed_dao = FreshRSS_Factory::createFeedDao($username);
  22. $this->entry_dao = FreshRSS_Factory::createEntryDao($username);
  23. $this->tag_dao = FreshRSS_Factory::createTagDao();
  24. }
  25. /**
  26. * Generate OPML file content.
  27. * @return array{0:string,1:string} First item is the filename, second item is the content
  28. */
  29. public function generateOpml(): array {
  30. $view = new FreshRSS_View();
  31. $day = date('Y-m-d');
  32. $view->categories = $this->category_dao->listCategories(true, true) ?: [];
  33. $view->excludeMutedFeeds = false;
  34. return [
  35. "feeds_{$day}.opml.xml",
  36. $view->helperToString('export/opml')
  37. ];
  38. }
  39. /**
  40. * Generate the starred and labelled entries file content.
  41. *
  42. * Both starred and labelled entries are put into a "starred" file, that’s
  43. * why there is only one method for both.
  44. *
  45. * @phpstan-param 'S'|'T'|'ST' $type
  46. * @param string $type must be one of:
  47. * 'S' (starred/favourite),
  48. * 'T' (taggued/labelled),
  49. * 'ST' (starred or labelled)
  50. * @return array{0:string,1:string} First item is the filename, second item is the content
  51. */
  52. public function generateStarredEntries(string $type): array {
  53. $view = new FreshRSS_View();
  54. $view->categories = $this->category_dao->listCategories(true) ?: [];
  55. $day = date('Y-m-d');
  56. $view->list_title = _t('sub.import_export.starred_list');
  57. $view->type = 'starred';
  58. $entriesId = $this->entry_dao->listIdsWhere($type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1) ?? [];
  59. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  60. // The following is a streamable query, i.e. must be last
  61. $view->entries = $this->entry_dao->listWhere(
  62. $type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1
  63. );
  64. return [
  65. "starred_{$day}.json",
  66. $view->helperToString('export/articles')
  67. ];
  68. }
  69. /**
  70. * Generate the entries file content for the given feed.
  71. * @param int $feed_id
  72. * @param int $max_number_entries
  73. * @return array{0:string,1:string}|null First item is the filename, second item is the content.
  74. * It also can return null if the feed doesn’t exist.
  75. */
  76. public function generateFeedEntries(int $feed_id, int $max_number_entries): ?array {
  77. $feed = $this->feed_dao->searchById($feed_id);
  78. if ($feed === null) {
  79. return null;
  80. }
  81. $view = new FreshRSS_View();
  82. $view->categories = $this->category_dao->listCategories(true) ?: [];
  83. $view->feed = $feed;
  84. $day = date('Y-m-d');
  85. $filename = "feed_{$day}_" . $feed->categoryId() . '_' . $feed->id() . '.json';
  86. $view->list_title = _t('sub.import_export.feed_list', $feed->name());
  87. $view->type = 'feed/' . $feed->id();
  88. $entriesId = $this->entry_dao->listIdsWhere(
  89. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $max_number_entries
  90. ) ?? [];
  91. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  92. // The following is a streamable query, i.e. must be last
  93. $view->entries = $this->entry_dao->listWhere(
  94. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $max_number_entries
  95. );
  96. return [
  97. $filename,
  98. $view->helperToString('export/articles')
  99. ];
  100. }
  101. /**
  102. * Generate the entries file content for all the feeds.
  103. * @param int $max_number_entries
  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) {
  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', '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. }