ExportService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 string $username;
  8. private FreshRSS_CategoryDAO $category_dao;
  9. private FreshRSS_FeedDAO $feed_dao;
  10. private FreshRSS_EntryDAO $entry_dao;
  11. private FreshRSS_TagDAO $tag_dao;
  12. public const FRSS_NAMESPACE = 'https://freshrss.org/opml';
  13. public const TYPE_HTML_XPATH = 'HTML+XPath';
  14. public const TYPE_XML_XPATH = 'XML+XPath';
  15. public const TYPE_RSS_ATOM = 'rss';
  16. /**
  17. * Initialize the service for the given user.
  18. */
  19. public function __construct(string $username) {
  20. $this->username = $username;
  21. $this->category_dao = FreshRSS_Factory::createCategoryDao($username);
  22. $this->feed_dao = FreshRSS_Factory::createFeedDao($username);
  23. $this->entry_dao = FreshRSS_Factory::createEntryDao($username);
  24. $this->tag_dao = FreshRSS_Factory::createTagDao();
  25. }
  26. /**
  27. * Generate OPML file content.
  28. * @return array{0:string,1:string} First item is the filename, second item is the content
  29. */
  30. public function generateOpml(): array {
  31. $view = new FreshRSS_View();
  32. $day = date('Y-m-d');
  33. $view->categories = $this->category_dao->listCategories(true, true) ?: [];
  34. $view->excludeMutedFeeds = false;
  35. return [
  36. "feeds_{$day}.opml.xml",
  37. $view->helperToString('export/opml')
  38. ];
  39. }
  40. /**
  41. * Generate the starred and labelled entries file content.
  42. *
  43. * Both starred and labelled entries are put into a "starred" file, that’s
  44. * why there is only one method for both.
  45. *
  46. * @phpstan-param 'S'|'T'|'ST' $type
  47. * @param string $type must be one of:
  48. * 'S' (starred/favourite),
  49. * 'T' (taggued/labelled),
  50. * 'ST' (starred or labelled)
  51. * @return array{0:string,1:string} First item is the filename, second item is the content
  52. */
  53. public function generateStarredEntries(string $type): array {
  54. $view = new FreshRSS_View();
  55. $view->categories = $this->category_dao->listCategories(true) ?: [];
  56. $day = date('Y-m-d');
  57. $view->list_title = _t('sub.import_export.starred_list');
  58. $view->type = 'starred';
  59. $entriesId = $this->entry_dao->listIdsWhere($type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1) ?? [];
  60. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  61. // The following is a streamable query, i.e. must be last
  62. $view->entries = $this->entry_dao->listWhere(
  63. $type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1
  64. );
  65. return [
  66. "starred_{$day}.json",
  67. $view->helperToString('export/articles')
  68. ];
  69. }
  70. /**
  71. * Generate the entries file content for the given feed.
  72. * @param int $feed_id
  73. * @param int $max_number_entries
  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. $feed = $this->feed_dao->searchById($feed_id);
  79. if ($feed === null) {
  80. return null;
  81. }
  82. $view = new FreshRSS_View();
  83. $view->categories = $this->category_dao->listCategories(true) ?: [];
  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, 'ASC', $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, 'ASC', $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. * @param int $max_number_entries
  105. * @return array<string,string> Keys are filenames and values are contents.
  106. */
  107. public function generateAllFeedEntries(int $max_number_entries): array {
  108. $feed_ids = $this->feed_dao->listFeedsIds();
  109. $exported_files = [];
  110. foreach ($feed_ids as $feed_id) {
  111. $result = $this->generateFeedEntries($feed_id, $max_number_entries);
  112. if (!$result) {
  113. continue;
  114. }
  115. [$filename, $content] = $result;
  116. $exported_files[$filename] = $content;
  117. }
  118. return $exported_files;
  119. }
  120. /**
  121. * Compress several files in a Zip file.
  122. * @param array<string,string> $files where the key is the filename, the value is the content
  123. * @return array{0:string,1:string|false} First item is the zip filename, second item is the zip content
  124. */
  125. public function zip(array $files): array {
  126. $day = date('Y-m-d');
  127. $zip_filename = 'freshrss_' . $this->username . '_' . $day . '_export.zip';
  128. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  129. $zip_file = tempnam('/tmp', 'zip');
  130. if ($zip_file == false) {
  131. return [$zip_filename, false];
  132. }
  133. $zip_archive = new ZipArchive();
  134. $zip_archive->open($zip_file, ZipArchive::OVERWRITE);
  135. foreach ($files as $filename => $content) {
  136. $zip_archive->addFromString($filename, $content);
  137. }
  138. $zip_archive->close();
  139. $content = file_get_contents($zip_file);
  140. unlink($zip_file);
  141. return [
  142. $zip_filename,
  143. $content,
  144. ];
  145. }
  146. }