Passed
Push — main ( 693748...87efd3 )
by Greg
07:30
created

TreeImport::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.9666
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Cli\Commands;
21
22
use Fisharebest\Webtrees\DB;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Exceptions\GedcomErrorException;
24
use Fisharebest\Webtrees\Services\GedcomImportService;
25
use Fisharebest\Webtrees\Services\TreeService;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Services\TreeService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Symfony\Component\Console\Completion\CompletionInput;
27
use Symfony\Component\Console\Helper\ProgressBar;
28
use Symfony\Component\Console\Input\InputArgument;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Input\InputOption;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Symfony\Component\Console\Style\SymfonyStyle;
33
use Throwable;
34
35
use function addcslashes;
36
use function file_exists;
37
use function file_get_contents;
38
use function filesize;
39
use function fopen;
40
use function gc_collect_cycles;
41
use function preg_split;
42
use function str_replace;
43
44
final class TreeImport extends AbstractCommand
45
{
46
    public function __construct(
47
        private readonly GedcomImportService $gedcom_import_service,
48
        private readonly TreeService $tree_service,
49
    ) {
50
        parent::__construct();
51
    }
52
53
    protected function configure(): void
54
    {
55
        $this
56
            ->setName(name: 'tree-import')
57
            ->addArgument(name: 'tree-name', mode: InputArgument::REQUIRED, description: 'The name of the tree', suggestedValues: self::autoCompleteTreeName(...))
58
            ->addArgument(name: 'gedcom-file', mode: InputArgument::REQUIRED, description: 'Path to the GEDCOM file')
59
            ->addOption(name: 'encoding', mode: InputOption::VALUE_REQUIRED, description: 'Encoding of the GEDCOM file')
60
            ->addOption(name: 'keep-media', mode: InputOption::VALUE_OPTIONAL, description: 'Merge existing media with the new file')
61
            ->addOption(name: 'conc-spaces', mode: InputOption::VALUE_OPTIONAL, description: 'Add spaces when wrapping CONC tags')
62
            ->addOption(name: 'gedcom-media-path', mode: InputOption::VALUE_OPTIONAL, description: 'Strip media path from OBJE records')
63
            ->setDescription(description: 'Import a tree from a GEDCOM file');
64
    }
65
66
    /**
67
     * @return array<string>
68
     */
69
    private function autoCompleteTreeName(CompletionInput $input): array
0 ignored issues
show
Unused Code introduced by
The method autoCompleteTreeName() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
70
    {
71
        return DB::table('tree')
72
            ->where('tree_name', 'LIKE', addcslashes($input->getCompletionValue(), '%_\\') . '%')
73
            ->pluck('name')
74
            ->all();
75
    }
76
77
    protected function execute(InputInterface $input, OutputInterface $output): int
78
    {
79
        $io = new SymfonyStyle(input: $input, output: $output);
80
81
        $tree_name          = $this->stringArgument(input: $input, name: 'tree-name');
82
        $gedcom_file        = $this->stringArgument(input: $input, name: 'gedcom-file');
83
        $keep_media         = $this->boolOption(input: $input, name: 'keep-media');
84
        $word_wrapped_notes = $this->boolOption(input: $input, name: 'conc-spaces');
85
        $gedcom_media_path  = $this->stringOption(input: $input, name: 'gedcom-media-path');
86
        $encoding           = $this->stringOption(input: $input, name: 'encoding');
0 ignored issues
show
Unused Code introduced by
The assignment to $encoding is dead and can be removed.
Loading history...
87
88
        $tree = $this->tree_service->all()[$tree_name] ?? null;
89
90
        if ($tree === null) {
91
            $io->error(message: 'Tree "' . $tree_name . '" not found.');
92
93
            return self::FAILURE;
94
        }
95
96
        if (!file_exists($gedcom_file)) {
97
            $io->error(message: 'File "' . $gedcom_file . '" does not exist.');
98
99
            return self::FAILURE;
100
        }
101
102
        try {
103
            DB::connection()->beginTransaction();
104
105
            $tree->setPreference('imported', '0');
106
            $tree->setPreference('keep_media', $keep_media ? '1' : '0');
107
            $tree->setPreference('WORD_WRAPPED_NOTES', $word_wrapped_notes ? '1' : '0');
108
            $tree->setPreference('GEDCOM_MEDIA_PATH', $gedcom_media_path);
109
110
            $queries = [
111
                'individuals' => DB::table('individuals')->where('i_file', '=', $tree->id()),
112
                'families'    => DB::table('families')->where('f_file', '=', $tree->id()),
113
                'sources'     => DB::table('sources')->where('s_file', '=', $tree->id()),
114
                'other'       => DB::table('other')->where('o_file', '=', $tree->id()),
115
                'places'      => DB::table('places')->where('p_file', '=', $tree->id()),
116
                'placelinks'  => DB::table('placelinks')->where('pl_file', '=', $tree->id()),
117
                'name'        => DB::table('name')->where('n_file', '=', $tree->id()),
118
                'dates'       => DB::table('dates')->where('d_file', '=', $tree->id()),
119
                'change'      => DB::table('change')->where('gedcom_id', '=', $tree->id()),
120
            ];
121
122
123
            if ($keep_media) {
124
                $queries['link'] = DB::table('link')
125
                    ->where('l_file', '=', $tree->id())
126
                    ->where('l_type', '<>', 'OBJE');
127
            } else {
128
                $queries += [
129
                    'link'       => DB::table('link')->where('l_file', '=', $tree->id()),
130
                    'media_file' => DB::table('media_file')->where('m_file', '=', $tree->id()),
131
                    'media'      => DB::table('media')->where('m_file', '=', $tree->id()),
132
                ];
133
            }
134
135
            $io->info('Deleting old genealogy data.');
136
137
            $progress_bar = new ProgressBar($output, count($queries));
138
            $progress_bar->setFormat(' %current%/%max% [%bar%] %percent%% %memory%, %elapsed% elapsed');
139
            $progress_bar->setRedrawFrequency(1);
140
            $progress_bar->start();
141
142
            foreach ($queries as $name => $query) {
143
                $query->delete();
144
                $progress_bar->advance();
145
            }
146
147
            $progress_bar->finish();
148
            $output->writeln('');
149
150
            $io->info('Importing new genealogy data.');
151
152
            $total_bytes  = filesize($gedcom_file);
153
154
            $bytes_loaded = 0;
155
156
            $fp     = fopen($gedcom_file, 'rb');
157
            $buffer = '';
158
159
            $progress_bar = new ProgressBar($output, $total_bytes);
160
            $progress_bar->setFormat(' %current%/%max% [%bar%] %percent%%, %memory%, %elapsed% elapsed, %remaining% remaining');
161
            $progress_bar->setRedrawFrequency(1);
162
            $progress_bar->minSecondsBetweenRedraws(0.1);
163
164
            while ($bytes_loaded < $total_bytes) {
165
                $tmp = fread($fp, 8192);
166
                $buffer .= $tmp;
167
                $bytes_loaded += strlen($buffer);
168
169
                $records = preg_split('/[\r\n]+(?=0)/', $buffer);
170
                $buffer = array_pop($records);
171
172
                foreach ($records as $record) {
173
                    $this->gedcom_import_service->importRecord($record, $tree, false);
174
                }
175
176
                $progress_bar->setProgress($bytes_loaded);
177
            }
178
            $progress_bar->finish();
179
180
            $output->writeln('');
181
182
            $tree->setPreference('imported', '1');
183
184
            DB::connection()->commit();
185
        } catch (Throwable $ex) {
186
            $io->error(message: $ex->getMessage());
187
            DB::connection()->rollBack();
188
189
            return self::FAILURE;
190
        }
191
192
        return self::SUCCESS;
193
    }
194
}
195