Issues (2498)

app/Services/TreeService.php (1 issue)

Labels
Severity
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\Services;
21
22
use DomainException;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\Contracts\UserInterface;
25
use Fisharebest\Webtrees\DB;
26
use Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
27
use Fisharebest\Webtrees\I18N;
28
use Fisharebest\Webtrees\Registry;
29
use Fisharebest\Webtrees\Site;
30
use Fisharebest\Webtrees\Tree;
31
use Illuminate\Database\Query\Builder;
32
use Illuminate\Database\Query\Expression;
33
use Illuminate\Database\Query\JoinClause;
34
use Illuminate\Support\Collection;
35
use Psr\Http\Message\StreamInterface;
36
37
use function fclose;
38
use function feof;
39
use function fread;
40
use function max;
41
use function stream_filter_append;
42
use function strrpos;
43
use function substr;
44
45
use const STREAM_FILTER_READ;
46
47
/**
48
 * Tree management and queries.
49
 */
50
class TreeService
51
{
52
    // The most likely surname tradition for a given language.
53
    private const array DEFAULT_SURNAME_TRADITIONS = [
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 53 at column 24
Loading history...
54
        'es'    => 'spanish',
55
        'is'    => 'icelandic',
56
        'lt'    => 'lithuanian',
57
        'pl'    => 'polish',
58
        'pt'    => 'portuguese',
59
        'pt-BR' => 'portuguese',
60
    ];
61
62
    public function __construct(
63
        private readonly GedcomImportService $gedcom_import_service,
64
    ) {
65
    }
66
67
    /**
68
     * All the trees that the current user has permission to access.
69
     *
70
     * @return Collection<array-key,Tree>
71
     */
72
    public function all(): Collection
73
    {
74
        return Registry::cache()->array()->remember('all-trees', static function (): Collection {
75
            // All trees
76
            $query = DB::table('gedcom')
77
                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
78
                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
79
                        ->where('gedcom_setting.setting_name', '=', 'title');
80
                })
81
                ->where('gedcom.gedcom_id', '>', 0)
82
                ->select([
83
                    'gedcom.gedcom_id AS tree_id',
84
                    'gedcom.gedcom_name AS tree_name',
85
                    'gedcom_setting.setting_value AS tree_title',
86
                ])
87
                ->orderBy('gedcom.sort_order')
88
                ->orderBy('gedcom_setting.setting_value');
89
90
            // Non-admins may not see all trees
91
            if (!Auth::isAdmin()) {
92
                $query
93
                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
94
                        $join
95
                            ->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
96
                            ->where('gs2.setting_name', '=', 'imported');
97
                    })
98
                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
99
                        $join
100
                            ->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
101
                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
102
                    })
103
                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
104
                        $join
105
                            ->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
106
                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
107
                            ->where('user_gedcom_setting.setting_name', '=', UserInterface::PREF_TREE_ROLE);
108
                    })
109
                    ->where(static function (Builder $query): void {
110
                        $query
111
                            // Managers
112
                            ->where('user_gedcom_setting.setting_value', '=', UserInterface::ROLE_MANAGER)
113
                            // Members
114
                            ->orWhere(static function (Builder $query): void {
115
                                $query
116
                                    ->where('gs2.setting_value', '=', '1')
117
                                    ->where('gs3.setting_value', '=', '1')
118
                                    ->where('user_gedcom_setting.setting_value', '<>', UserInterface::ROLE_VISITOR);
119
                            })
120
                            // Public trees
121
                            ->orWhere(static function (Builder $query): void {
122
                                $query
123
                                    ->where('gs2.setting_value', '=', '1')
124
                                    ->where('gs3.setting_value', '<>', '1');
125
                            });
126
                    });
127
            }
128
129
            return $query
130
                ->get()
131
                ->mapWithKeys(static fn (object $row): array => [$row->tree_name => Tree::rowMapper()($row)]);
132
        });
133
    }
134
135
    /**
136
     * Find a tree by its ID.
137
     *
138
     * @param int $id
139
     *
140
     * @return Tree
141
     */
142
    public function find(int $id): Tree
143
    {
144
        $tree = $this->all()->first(static fn (Tree $tree): bool => $tree->id() === $id);
145
146
        if ($tree instanceof Tree) {
147
            return $tree;
148
        }
149
150
        throw new DomainException('Call to find() with an invalid id: ' . $id);
151
    }
152
153
    /**
154
     * All trees, name => title
155
     *
156
     * @return array<string>
157
     */
158
    public function titles(): array
159
    {
160
        return $this->all()->map(static fn (Tree $tree): string => $tree->title())->all();
161
    }
162
163
    /**
164
     * @param string $name
165
     * @param string $title
166
     *
167
     * @return Tree
168
     */
169
    public function create(string $name, string $title): Tree
170
    {
171
        DB::table('gedcom')->insert([
172
            'gedcom_name' => $name,
173
        ]);
174
175
        $tree_id = DB::lastInsertId();
176
177
        $tree = new Tree($tree_id, $name, $title);
178
179
        $tree->setPreference('imported', '1');
180
        $tree->setPreference('title', $title);
181
182
        // Set preferences from default tree
183
        DB::query()->from('gedcom_setting')->insertUsing(
184
            ['gedcom_id', 'setting_name', 'setting_value'],
185
            static function (Builder $query) use ($tree_id): void {
186
                $query
187
                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
188
                    ->from('gedcom_setting')
189
                    ->where('gedcom_id', '=', -1);
190
            }
191
        );
192
193
        DB::query()->from('default_resn')->insertUsing(
194
            ['gedcom_id', 'tag_type', 'resn'],
195
            static function (Builder $query) use ($tree_id): void {
196
                $query
197
                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
198
                    ->from('default_resn')
199
                    ->where('gedcom_id', '=', -1);
200
            }
201
        );
202
203
        // Gedcom and privacy settings
204
        $tree->setPreference('REQUIRE_AUTHENTICATION', '');
205
        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
206
        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
207
        $tree->setPreference('LANGUAGE', I18N::languageTag()); // Default to the current admin’s language
208
        $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[I18N::languageTag()] ?? 'paternal');
209
210
        // A tree needs at least one record.
211
        $head = "0 HEAD\n1 SOUR webtrees\n1 DEST webtrees\n1 GEDC\n2 VERS 5.5.1\n2 FORM LINEAGE-LINKED\n1 CHAR UTF-8";
212
        $this->gedcom_import_service->importRecord($head, $tree, true);
213
214
        // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname.
215
        $name = I18N::translate('John /DOE/');
216
        $note = I18N::translate('Edit this individual and replace their details with your own.');
217
        $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note;
218
        $this->gedcom_import_service->importRecord($indi, $tree, true);
219
220
        return $tree;
221
    }
222
223
    /**
224
     * Import data from a gedcom file into this tree.
225
     *
226
     * @param Tree            $tree
227
     * @param StreamInterface $stream   The GEDCOM file.
228
     * @param string          $filename The preferred filename, for export/download.
229
     * @param string          $encoding Override the encoding specified in the header.
230
     *
231
     * @return void
232
     */
233
    public function importGedcomFile(Tree $tree, StreamInterface $stream, string $filename, string $encoding): void
234
    {
235
        // Read the file in blocks of roughly 64K. Ensure that each block
236
        // contains complete gedcom records. This will ensure we don’t split
237
        // multi-byte characters, as well as simplifying the code to import
238
        // each block.
239
240
        $file_data = '';
241
242
        $tree->setPreference('gedcom_filename', $filename);
243
        $tree->setPreference('imported', '0');
244
245
        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
246
247
        $stream = $stream->detach();
248
249
        // Convert to UTF-8.
250
        stream_filter_append($stream, GedcomEncodingFilter::class, STREAM_FILTER_READ, ['src_encoding' => $encoding]);
251
252
        while (!feof($stream)) {
253
            $file_data .= fread($stream, 65536);
254
            $eol_pos = max((int) strrpos($file_data, "\r0"), (int) strrpos($file_data, "\n0"));
255
256
            if ($eol_pos > 0) {
257
                DB::table('gedcom_chunk')->insert([
258
                    'gedcom_id'  => $tree->id(),
259
                    'chunk_data' => substr($file_data, 0, $eol_pos + 1),
260
                ]);
261
262
                $file_data = substr($file_data, $eol_pos + 1);
263
            }
264
        }
265
266
        DB::table('gedcom_chunk')->insert([
267
            'gedcom_id'  => $tree->id(),
268
            'chunk_data' => $file_data,
269
        ]);
270
271
        fclose($stream);
272
    }
273
274
    /**
275
     * @param Tree $tree
276
     */
277
    public function delete(Tree $tree): void
278
    {
279
        // If this is the default tree, then unset it
280
        if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) {
281
            Site::setPreference('DEFAULT_GEDCOM', '');
282
        }
283
284
        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
285
        DB::table('individuals')->where('i_file', '=', $tree->id())->delete();
286
        DB::table('families')->where('f_file', '=', $tree->id())->delete();
287
        DB::table('sources')->where('s_file', '=', $tree->id())->delete();
288
        DB::table('other')->where('o_file', '=', $tree->id())->delete();
289
        DB::table('places')->where('p_file', '=', $tree->id())->delete();
290
        DB::table('placelinks')->where('pl_file', '=', $tree->id())->delete();
291
        DB::table('name')->where('n_file', '=', $tree->id())->delete();
292
        DB::table('dates')->where('d_file', '=', $tree->id())->delete();
293
        DB::table('change')->where('gedcom_id', '=', $tree->id())->delete();
294
        DB::table('link')->where('l_file', '=', $tree->id())->delete();
295
        DB::table('media_file')->where('m_file', '=', $tree->id())->delete();
296
        DB::table('media')->where('m_file', '=', $tree->id())->delete();
297
        DB::table('block_setting')
298
            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
299
            ->where('gedcom_id', '=', $tree->id())
300
            ->delete();
301
        DB::table('block')->where('gedcom_id', '=', $tree->id())->delete();
302
        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
303
        DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
304
        DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete();
305
        DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete();
306
        DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete();
307
        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
308
        DB::table('log')->where('gedcom_id', '=', $tree->id())->delete();
309
        DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete();
310
    }
311
312
    /**
313
     * Generate a unique name for a new tree.
314
     *
315
     * @return string
316
     */
317
    public function uniqueTreeName(): string
318
    {
319
        $name   = 'tree';
320
        $number = 1;
321
322
        while ($this->all()->get($name . $number) instanceof Tree) {
323
            $number++;
324
        }
325
326
        return $name . $number;
327
    }
328
}
329