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