ExportService.php 5.2 KB

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