ExportService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(
  67. $type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1
  68. );
  69. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  70. // The following is a streamable query, i.e. must be last
  71. $view->entries = $this->entry_dao->listWhere(
  72. $type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1
  73. );
  74. return [
  75. "starred_{$day}.json",
  76. $view->helperToString('export/articles')
  77. ];
  78. }
  79. /**
  80. * Generate the entries file content for the given feed.
  81. *
  82. * @param integer $feed_id
  83. * @param integer $max_number_entries
  84. *
  85. * @return array|null First item is the filename, second item is the content.
  86. * It also can return null if the feed doesn’t exist.
  87. */
  88. public function generateFeedEntries($feed_id, $max_number_entries) {
  89. $feed = $this->feed_dao->searchById($feed_id);
  90. if (!$feed) {
  91. return null;
  92. }
  93. $view = new FreshRSS_View();
  94. $view->categories = $this->category_dao->listCategories(true);
  95. $view->feed = $feed;
  96. $day = date('Y-m-d');
  97. $filename = "feed_{$day}_" . $feed->categoryId() . '_' . $feed->id() . '.json';
  98. $view->list_title = _t('sub.import_export.feed_list', $feed->name());
  99. $view->type = 'feed/' . $feed->id();
  100. $entriesId = $this->entry_dao->listIdsWhere(
  101. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $max_number_entries
  102. );
  103. $view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
  104. // The following is a streamable query, i.e. must be last
  105. $view->entries = $this->entry_dao->listWhere(
  106. 'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $max_number_entries
  107. );
  108. return [
  109. $filename,
  110. $view->helperToString('export/articles')
  111. ];
  112. }
  113. /**
  114. * Generate the entries file content for all the feeds.
  115. *
  116. * @param integer $max_number_entries
  117. *
  118. * @return array Keys are filenames and values are contents.
  119. */
  120. public function generateAllFeedEntries($max_number_entries) {
  121. $feed_ids = $this->feed_dao->listFeedsIds();
  122. $exported_files = [];
  123. foreach ($feed_ids as $feed_id) {
  124. $result = $this->generateFeedEntries($feed_id, $max_number_entries);
  125. if (!$result) {
  126. continue;
  127. }
  128. list($filename, $content) = $result;
  129. $exported_files[$filename] = $content;
  130. }
  131. return $exported_files;
  132. }
  133. /**
  134. * Compress several files in a Zip file.
  135. *
  136. * @param array $files where first item is the filename, second item is the content
  137. *
  138. * @return array First item is the zip filename, second item is the zip content
  139. */
  140. public function zip($files) {
  141. $day = date('Y-m-d');
  142. $zip_filename = 'freshrss_' . $this->username . '_' . $day . '_export.zip';
  143. // From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  144. $zip_file = tempnam('/tmp', 'zip');
  145. $zip_archive = new ZipArchive();
  146. $zip_archive->open($zip_file, ZipArchive::OVERWRITE);
  147. foreach ($files as $filename => $content) {
  148. $zip_archive->addFromString($filename, $content);
  149. }
  150. $zip_archive->close();
  151. $content = file_get_contents($zip_file);
  152. unlink($zip_file);
  153. return [
  154. $zip_filename,
  155. $content,
  156. ];
  157. }
  158. }