export-zip-for-user.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. $params = array(
  7. 'user:',
  8. 'max-feed-entries:',
  9. );
  10. $options = getopt('', $params);
  11. if (!validateOptions($argv, $params) || empty($options['user']) || !is_string($options['user'])) {
  12. fail('Usage: ' . basename(__FILE__) . " --user username ( --max-feed-entries 100 ) > /path/to/file.zip");
  13. }
  14. if (!extension_loaded('zip')) {
  15. fail('FreshRSS error: Lacking php-zip extension!');
  16. }
  17. $username = cliInitUser($options['user']);
  18. fwrite(STDERR, 'FreshRSS exporting ZIP for user “' . $username . "”…\n");
  19. $export_service = new FreshRSS_Export_Service($username);
  20. $number_entries = empty($options['max-feed-entries']) ? 100 : intval($options['max-feed-entries']);
  21. $exported_files = [];
  22. // First, we generate the OPML file
  23. list($filename, $content) = $export_service->generateOpml();
  24. $exported_files[$filename] = $content;
  25. // Then, labelled and starred entries
  26. list($filename, $content) = $export_service->generateStarredEntries('ST');
  27. $exported_files[$filename] = $content;
  28. // And a list of entries based on the complete list of feeds
  29. $feeds_exported_files = $export_service->generateAllFeedEntries($number_entries);
  30. $exported_files = array_merge($exported_files, $feeds_exported_files);
  31. // Finally, we compress all these files into a single Zip archive and we output
  32. // the content
  33. list($filename, $content) = $export_service->zip($exported_files);
  34. echo $content;
  35. invalidateHttpCache($username);
  36. done();