ExportService.php 5.2 KB

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