xref: /webtrees/app/Cli/Commands/TreeExport.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1bb87bb9bSGreg Roach<?php
2bb87bb9bSGreg Roach
3bb87bb9bSGreg Roach/**
4bb87bb9bSGreg Roach * webtrees: online genealogy
5bb87bb9bSGreg Roach * Copyright (C) 2023 webtrees development team
6bb87bb9bSGreg Roach * This program is free software: you can redistribute it and/or modify
7bb87bb9bSGreg Roach * it under the terms of the GNU General Public License as published by
8bb87bb9bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9bb87bb9bSGreg Roach * (at your option) any later version.
10bb87bb9bSGreg Roach * This program is distributed in the hope that it will be useful,
11bb87bb9bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12bb87bb9bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13bb87bb9bSGreg Roach * GNU General Public License for more details.
14bb87bb9bSGreg Roach * You should have received a copy of the GNU General Public License
15bb87bb9bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16bb87bb9bSGreg Roach */
17bb87bb9bSGreg Roach
18bb87bb9bSGreg Roachdeclare(strict_types=1);
19bb87bb9bSGreg Roach
20bb87bb9bSGreg Roachnamespace Fisharebest\Webtrees\Cli\Commands;
21bb87bb9bSGreg Roach
22bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Auth;
23bb87bb9bSGreg Roachuse Fisharebest\Webtrees\DB;
24bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Encodings\UTF8;
25bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Services\GedcomExportService;
26bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
27bb87bb9bSGreg Roachuse Symfony\Component\Console\Command\Command;
28bb87bb9bSGreg Roachuse Symfony\Component\Console\Completion\CompletionInput;
29bb87bb9bSGreg Roachuse Symfony\Component\Console\Input\InputArgument;
30bb87bb9bSGreg Roachuse Symfony\Component\Console\Input\InputInterface;
31bb87bb9bSGreg Roachuse Symfony\Component\Console\Input\InputOption;
32bb87bb9bSGreg Roachuse Symfony\Component\Console\Output\OutputInterface;
33bb87bb9bSGreg Roachuse Symfony\Component\Console\Style\SymfonyStyle;
34bb87bb9bSGreg Roach
35bb87bb9bSGreg Roachuse function addcslashes;
36bb87bb9bSGreg Roachuse function stream_get_contents;
37bb87bb9bSGreg Roach
38bb87bb9bSGreg Roachclass TreeExport extends Command
39bb87bb9bSGreg Roach{
40bb87bb9bSGreg Roach    public function __construct(
41bb87bb9bSGreg Roach        private readonly GedcomExportService $gedcom_export_service,
42bb87bb9bSGreg Roach        private readonly TreeService $tree_service,
43bb87bb9bSGreg Roach    ) {
44bb87bb9bSGreg Roach        parent::__construct();
45bb87bb9bSGreg Roach    }
46bb87bb9bSGreg Roach
47bb87bb9bSGreg Roach    protected function configure(): void
48bb87bb9bSGreg Roach    {
49bb87bb9bSGreg Roach        $this
50bb87bb9bSGreg Roach            ->setName(name: 'tree-export')
51bb87bb9bSGreg Roach            ->addArgument(name: 'tree_name', mode: InputArgument::REQUIRED, description: 'The name of the tree', suggestedValues: self::autoCompleteTreeName(...))
52bb87bb9bSGreg Roach            ->addOption(name: 'format', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'Export format')
53bb87bb9bSGreg Roach            ->addOption(name: 'filename', shortcut: null, mode: InputOption::VALUE_REQUIRED, description: 'Export filename')
54bb87bb9bSGreg Roach            ->setDescription(description: 'Export a tree to a GEDCOM file');
55bb87bb9bSGreg Roach    }
56bb87bb9bSGreg Roach
57*7413816eSGreg Roach    /**
58*7413816eSGreg Roach     * @return array<string>
59*7413816eSGreg Roach     */
60bb87bb9bSGreg Roach    private function autoCompleteTreeName(CompletionInput $input): array
61bb87bb9bSGreg Roach    {
62bb87bb9bSGreg Roach        return DB::table('tree')
63bb87bb9bSGreg Roach            ->where('tree_name', 'LIKE', addcslashes($input->getCompletionValue(), '%_\\') . '%')
64bb87bb9bSGreg Roach            ->pluck('name')
65bb87bb9bSGreg Roach            ->all();
66bb87bb9bSGreg Roach    }
67bb87bb9bSGreg Roach
68bb87bb9bSGreg Roach    protected function execute(InputInterface $input, OutputInterface $output): int
69bb87bb9bSGreg Roach    {
70bb87bb9bSGreg Roach        $io = new SymfonyStyle(input: $input, output: $output);
71bb87bb9bSGreg Roach
72bb87bb9bSGreg Roach        $tree_name = $input->getArgument(name: 'tree_name');
73bb87bb9bSGreg Roach        $format   = $input->getOption(name: 'format');
74bb87bb9bSGreg Roach        $filename = $input->getOption(name: 'filename');
75bb87bb9bSGreg Roach
76bb87bb9bSGreg Roach        $tree = $this->tree_service->all()[$tree_name] ?? null;
77bb87bb9bSGreg Roach
78bb87bb9bSGreg Roach        if ($tree === null) {
79bb87bb9bSGreg Roach            $io->error(message: 'Tree "' . $tree_name . '" not found.');
80bb87bb9bSGreg Roach
81bb87bb9bSGreg Roach            return Command::FAILURE;
82bb87bb9bSGreg Roach        }
83bb87bb9bSGreg Roach
84bb87bb9bSGreg Roach        $stream = $this->gedcom_export_service->export(
85bb87bb9bSGreg Roach            tree: $tree,
86bb87bb9bSGreg Roach            sort_by_xref: false,
87bb87bb9bSGreg Roach            encoding: UTF8::NAME,
88bb87bb9bSGreg Roach            access_level: Auth::PRIV_HIDE,
89bb87bb9bSGreg Roach            line_endings: 'CRLF',
90bb87bb9bSGreg Roach            records: null,
91bb87bb9bSGreg Roach            zip_filesystem: null,
92bb87bb9bSGreg Roach            media_path: null,
93bb87bb9bSGreg Roach        );
94bb87bb9bSGreg Roach
95bb87bb9bSGreg Roach        echo stream_get_contents($stream);
96bb87bb9bSGreg Roach
97bb87bb9bSGreg Roach        $io->success('File exported successfully.');
98bb87bb9bSGreg Roach
99bb87bb9bSGreg Roach        return Command::SUCCESS;
100bb87bb9bSGreg Roach    }
101bb87bb9bSGreg Roach}
102