ExportService.php 5.0 KB

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