ExportService.php 5.0 KB

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