ExportService.php 5.3 KB

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