. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Cli\Commands; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\DB; use Fisharebest\Webtrees\Encodings\UTF8; use Fisharebest\Webtrees\Services\GedcomExportService; use Fisharebest\Webtrees\Services\TreeService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use function addcslashes; use function stream_get_contents; class TreeExport extends Command { public function __construct( private readonly GedcomExportService $gedcom_export_service, private readonly TreeService $tree_service, ) { parent::__construct(); } protected function configure(): void { $this ->setName(name: 'tree-export') ->addArgument(name: 'tree_name', mode: InputArgument::REQUIRED, description: 'The name of the tree', suggestedValues: self::autoCompleteTreeName(...)) ->addOption(name: 'format', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'Export format') ->addOption(name: 'filename', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'Export filename') ->setDescription(description: 'Export a tree to a GEDCOM file'); } private function autoCompleteTreeName(CompletionInput $input): array { return DB::table('tree') ->where('tree_name', 'LIKE', addcslashes($input->getCompletionValue(), '%_\\') .'%') ->pluck('name') ->all(); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle(input: $input, output: $output); $tree_name = $input->getArgument(name: 'tree_name'); $format = $input->getOption(name: 'format'); $filename = $input->getOption(name: 'filename'); $tree = $this->tree_service->all()[$tree_name] ?? null; if ($tree === null) { $io->error(message: 'Tree "' . $tree_name . '" not found.'); return Command::FAILURE; } $stream = $this->gedcom_export_service->export( tree: $tree, sort_by_xref: false, encoding: UTF8::NAME, access_level: Auth::PRIV_HIDE, line_endings: 'CRLF', records: null, zip_filesystem: null, media_path: null, ); echo stream_get_contents($stream); $io->success('File exported successfully.'); return Command::SUCCESS; } }