4
0

export-zip-for-user.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require(__DIR__ . '/_cli.php');
  5. performRequirementCheck(FreshRSS_Context::systemConf()->db['type'] ?? '');
  6. $cliOptions = new class extends CliOptionsParser {
  7. public string $user;
  8. public int $maxFeedEntries;
  9. public function __construct() {
  10. $this->addRequiredOption('user', (new CliOption('user')));
  11. $this->addOption('maxFeedEntries', (new CliOption('max-feed-entries'))->typeOfInt(), '100');
  12. parent::__construct();
  13. }
  14. };
  15. if (!empty($cliOptions->errors)) {
  16. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  17. }
  18. if (!extension_loaded('zip')) {
  19. fail('FreshRSS error: Lacking php-zip extension!');
  20. }
  21. $username = cliInitUser($cliOptions->user);
  22. fwrite(STDERR, 'FreshRSS exporting ZIP for user “' . $username . "”…\n");
  23. $export_service = new FreshRSS_Export_Service($username);
  24. $number_entries = $cliOptions->maxFeedEntries;
  25. $exported_files = [];
  26. // First, we generate the OPML file
  27. [$filename, $content] = $export_service->generateOpml();
  28. $exported_files[$filename] = $content;
  29. // Then, labelled and starred entries
  30. [$filename, $content] = $export_service->generateStarredEntries('ST');
  31. $exported_files[$filename] = $content;
  32. // And a list of entries based on the complete list of feeds
  33. $feeds_exported_files = $export_service->generateAllFeedEntries($number_entries);
  34. $exported_files = array_merge($exported_files, $feeds_exported_files);
  35. // Finally, we compress all these files into a single Zip archive and we output
  36. // the content
  37. [$filename, $content] = $export_service->zip($exported_files);
  38. echo $content;
  39. invalidateHttpCache($username);
  40. done();