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