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