xref: /webtrees/app/Services/TreeService.php (revision f25fc0f929f69ab8124cf0cecde45e457db7574a)
15afbc57aSGreg Roach<?php
25afbc57aSGreg Roach
35afbc57aSGreg Roach/**
45afbc57aSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
65afbc57aSGreg Roach * This program is free software: you can redistribute it and/or modify
75afbc57aSGreg Roach * it under the terms of the GNU General Public License as published by
85afbc57aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
95afbc57aSGreg Roach * (at your option) any later version.
105afbc57aSGreg Roach * This program is distributed in the hope that it will be useful,
115afbc57aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
125afbc57aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135afbc57aSGreg Roach * GNU General Public License for more details.
145afbc57aSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
165afbc57aSGreg Roach */
17fcfa147eSGreg Roach
185afbc57aSGreg Roachdeclare(strict_types=1);
195afbc57aSGreg Roach
205afbc57aSGreg Roachnamespace Fisharebest\Webtrees\Services;
215afbc57aSGreg Roach
22960991e4SGreg Roachuse DomainException;
235afbc57aSGreg Roachuse Fisharebest\Webtrees\Auth;
241fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
256f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
262c685d76SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
275afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N;
289d173e09SGreg Roachuse Fisharebest\Webtrees\Registry;
295afbc57aSGreg Roachuse Fisharebest\Webtrees\Site;
305afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree;
315afbc57aSGreg Roachuse Illuminate\Database\Query\Builder;
325afbc57aSGreg Roachuse Illuminate\Database\Query\Expression;
335afbc57aSGreg Roachuse Illuminate\Database\Query\JoinClause;
345afbc57aSGreg Roachuse Illuminate\Support\Collection;
355cd281f4SGreg Roachuse Psr\Http\Message\StreamInterface;
365afbc57aSGreg Roach
371c6adce8SGreg Roachuse function fclose;
381c6adce8SGreg Roachuse function feof;
391c6adce8SGreg Roachuse function fread;
401c6adce8SGreg Roachuse function max;
411c6adce8SGreg Roachuse function stream_filter_append;
421c6adce8SGreg Roachuse function strrpos;
435cd281f4SGreg Roachuse function substr;
445afbc57aSGreg Roach
451c6adce8SGreg Roachuse const STREAM_FILTER_READ;
461c6adce8SGreg Roach
475afbc57aSGreg Roach/**
485afbc57aSGreg Roach * Tree management and queries.
495afbc57aSGreg Roach */
505afbc57aSGreg Roachclass TreeService
515afbc57aSGreg Roach{
525afbc57aSGreg Roach    // The most likely surname tradition for a given language.
535afbc57aSGreg Roach    private const DEFAULT_SURNAME_TRADITIONS = [
545afbc57aSGreg Roach        'es'    => 'spanish',
555afbc57aSGreg Roach        'is'    => 'icelandic',
565afbc57aSGreg Roach        'lt'    => 'lithuanian',
575afbc57aSGreg Roach        'pl'    => 'polish',
585afbc57aSGreg Roach        'pt'    => 'portuguese',
595afbc57aSGreg Roach        'pt-BR' => 'portuguese',
605afbc57aSGreg Roach    ];
615afbc57aSGreg Roach
622c685d76SGreg Roach    private GedcomImportService $gedcom_import_service;
632c685d76SGreg Roach
642c685d76SGreg Roach    /**
652c685d76SGreg Roach     * @param GedcomImportService $gedcom_import_service
662c685d76SGreg Roach     */
672c685d76SGreg Roach    public function __construct(GedcomImportService $gedcom_import_service)
682c685d76SGreg Roach    {
692c685d76SGreg Roach        $this->gedcom_import_service = $gedcom_import_service;
702c685d76SGreg Roach    }
712c685d76SGreg Roach
725afbc57aSGreg Roach    /**
735afbc57aSGreg Roach     * All the trees that the current user has permission to access.
745afbc57aSGreg Roach     *
7576d39c55SGreg Roach     * @return Collection<array-key,Tree>
765afbc57aSGreg Roach     */
775afbc57aSGreg Roach    public function all(): Collection
785afbc57aSGreg Roach    {
796b9cb339SGreg Roach        return Registry::cache()->array()->remember('all-trees', static function (): Collection {
805afbc57aSGreg Roach            // All trees
815afbc57aSGreg Roach            $query = DB::table('gedcom')
825afbc57aSGreg Roach                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
835afbc57aSGreg Roach                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
845afbc57aSGreg Roach                        ->where('gedcom_setting.setting_name', '=', 'title');
855afbc57aSGreg Roach                })
865afbc57aSGreg Roach                ->where('gedcom.gedcom_id', '>', 0)
875afbc57aSGreg Roach                ->select([
885afbc57aSGreg Roach                    'gedcom.gedcom_id AS tree_id',
895afbc57aSGreg Roach                    'gedcom.gedcom_name AS tree_name',
905afbc57aSGreg Roach                    'gedcom_setting.setting_value AS tree_title',
915afbc57aSGreg Roach                ])
925afbc57aSGreg Roach                ->orderBy('gedcom.sort_order')
935afbc57aSGreg Roach                ->orderBy('gedcom_setting.setting_value');
945afbc57aSGreg Roach
955afbc57aSGreg Roach            // Non-admins may not see all trees
965afbc57aSGreg Roach            if (!Auth::isAdmin()) {
975afbc57aSGreg Roach                $query
985afbc57aSGreg Roach                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
996b736a8bSGreg Roach                        $join
1006b736a8bSGreg Roach                            ->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
101cc194e3fSGreg Roach                            ->where('gs2.setting_name', '=', 'imported');
1025afbc57aSGreg Roach                    })
1035afbc57aSGreg Roach                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
1046b736a8bSGreg Roach                        $join
1056b736a8bSGreg Roach                            ->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
106cc194e3fSGreg Roach                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
1075afbc57aSGreg Roach                    })
1085afbc57aSGreg Roach                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
1096b736a8bSGreg Roach                        $join
1106b736a8bSGreg Roach                            ->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
111cc194e3fSGreg Roach                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
112cc194e3fSGreg Roach                            ->where('user_gedcom_setting.setting_name', '=', UserInterface::PREF_TREE_ROLE);
1135afbc57aSGreg Roach                    })
1145afbc57aSGreg Roach                    ->where(static function (Builder $query): void {
1155afbc57aSGreg Roach                        $query
1165afbc57aSGreg Roach                            // Managers
1171fe542e9SGreg Roach                            ->where('user_gedcom_setting.setting_value', '=', UserInterface::ROLE_MANAGER)
1185afbc57aSGreg Roach                            // Members
1195afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1205afbc57aSGreg Roach                                $query
1215afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1225afbc57aSGreg Roach                                    ->where('gs3.setting_value', '=', '1')
1231fe542e9SGreg Roach                                    ->where('user_gedcom_setting.setting_value', '<>', UserInterface::ROLE_VISITOR);
1245afbc57aSGreg Roach                            })
1255afbc57aSGreg Roach                            // Public trees
1265afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1275afbc57aSGreg Roach                                $query
1285afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1295afbc57aSGreg Roach                                    ->where('gs3.setting_value', '<>', '1');
1305afbc57aSGreg Roach                            });
1315afbc57aSGreg Roach                    });
1325afbc57aSGreg Roach            }
1335afbc57aSGreg Roach
1345afbc57aSGreg Roach            return $query
1355afbc57aSGreg Roach                ->get()
136*f25fc0f9SGreg Roach                ->mapWithKeys(static fn(object $row): array => [$row->tree_name => Tree::rowMapper()($row)]);
1375afbc57aSGreg Roach        });
1385afbc57aSGreg Roach    }
1395afbc57aSGreg Roach
1405afbc57aSGreg Roach    /**
1411e653452SGreg Roach     * Find a tree by its ID.
1425afbc57aSGreg Roach     *
1431e653452SGreg Roach     * @param int $id
1445afbc57aSGreg Roach     *
1451e653452SGreg Roach     * @return Tree
1465afbc57aSGreg Roach     */
1471e653452SGreg Roach    public function find(int $id): Tree
1485afbc57aSGreg Roach    {
149*f25fc0f9SGreg Roach        $tree = $this->all()->first(static fn(Tree $tree): bool => $tree->id() === $id);
1501e653452SGreg Roach
151960991e4SGreg Roach        if ($tree instanceof Tree) {
1521e653452SGreg Roach            return $tree;
1531e653452SGreg Roach        }
1541e653452SGreg Roach
155960991e4SGreg Roach        throw new DomainException('Call to find() with an invalid id: ' . $id);
156960991e4SGreg Roach    }
157960991e4SGreg Roach
1581e653452SGreg Roach    /**
1591e653452SGreg Roach     * All trees, name => title
1601e653452SGreg Roach     *
16124f2a3afSGreg Roach     * @return array<string>
1621e653452SGreg Roach     */
1631e653452SGreg Roach    public function titles(): array
1641e653452SGreg Roach    {
165*f25fc0f9SGreg Roach        return $this->all()->map(static fn(Tree $tree): string => $tree->title())->all();
1665afbc57aSGreg Roach    }
1675afbc57aSGreg Roach
1685afbc57aSGreg Roach    /**
1695afbc57aSGreg Roach     * @param string $name
1705afbc57aSGreg Roach     * @param string $title
1715afbc57aSGreg Roach     *
1725afbc57aSGreg Roach     * @return Tree
1735afbc57aSGreg Roach     */
1745afbc57aSGreg Roach    public function create(string $name, string $title): Tree
1755afbc57aSGreg Roach    {
1765afbc57aSGreg Roach        DB::table('gedcom')->insert([
1775afbc57aSGreg Roach            'gedcom_name' => $name,
1785afbc57aSGreg Roach        ]);
1795afbc57aSGreg Roach
1805afbc57aSGreg Roach        $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
1815afbc57aSGreg Roach
1825afbc57aSGreg Roach        $tree = new Tree($tree_id, $name, $title);
1835afbc57aSGreg Roach
1845afbc57aSGreg Roach        $tree->setPreference('imported', '1');
1855afbc57aSGreg Roach        $tree->setPreference('title', $title);
1865afbc57aSGreg Roach
1875afbc57aSGreg Roach        // Set preferences from default tree
1885afbc57aSGreg Roach        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
1895afbc57aSGreg Roach            ['gedcom_id', 'setting_name', 'setting_value'],
1905afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
1915afbc57aSGreg Roach                $query
1925afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
1935afbc57aSGreg Roach                    ->from('gedcom_setting')
1945afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
1955afbc57aSGreg Roach            }
1965afbc57aSGreg Roach        );
1975afbc57aSGreg Roach
1985afbc57aSGreg Roach        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
1995afbc57aSGreg Roach            ['gedcom_id', 'tag_type', 'resn'],
2005afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
2015afbc57aSGreg Roach                $query
2025afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
2035afbc57aSGreg Roach                    ->from('default_resn')
2045afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
2055afbc57aSGreg Roach            }
2065afbc57aSGreg Roach        );
2075afbc57aSGreg Roach
2085afbc57aSGreg Roach        // Gedcom and privacy settings
2096b736a8bSGreg Roach        $tree->setPreference('REQUIRE_AUTHENTICATION', '');
2105afbc57aSGreg Roach        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
2115afbc57aSGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
21265cf5706SGreg Roach        $tree->setPreference('LANGUAGE', I18N::languageTag()); // Default to the current admin’s language
21365cf5706SGreg Roach        $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[I18N::languageTag()] ?? 'paternal');
2145afbc57aSGreg Roach
2155afbc57aSGreg Roach        // A tree needs at least one record.
216c48b130dSGreg Roach        $head = "0 HEAD\n1 SOUR webtrees\n1 DEST webtrees\n1 GEDC\n2 VERS 5.5.1\n2 FORM LINEAGE-LINKED\n1 CHAR UTF-8";
2172c685d76SGreg Roach        $this->gedcom_import_service->importRecord($head, $tree, true);
2185afbc57aSGreg Roach
2195afbc57aSGreg Roach        // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname.
2205afbc57aSGreg Roach        $name = I18N::translate('John /DOE/');
2215afbc57aSGreg Roach        $note = I18N::translate('Edit this individual and replace their details with your own.');
2229b5c9597SGreg Roach        $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note;
2232c685d76SGreg Roach        $this->gedcom_import_service->importRecord($indi, $tree, true);
2245afbc57aSGreg Roach
2255afbc57aSGreg Roach        return $tree;
2265afbc57aSGreg Roach    }
2275afbc57aSGreg Roach
2285afbc57aSGreg Roach    /**
2295cd281f4SGreg Roach     * Import data from a gedcom file into this tree.
2305cd281f4SGreg Roach     *
2315cd281f4SGreg Roach     * @param Tree            $tree
2325cd281f4SGreg Roach     * @param StreamInterface $stream   The GEDCOM file.
2335cd281f4SGreg Roach     * @param string          $filename The preferred filename, for export/download.
2341c6adce8SGreg Roach     * @param string          $encoding Override the encoding specified in the header.
2355cd281f4SGreg Roach     *
2365cd281f4SGreg Roach     * @return void
2375cd281f4SGreg Roach     */
2381c6adce8SGreg Roach    public function importGedcomFile(Tree $tree, StreamInterface $stream, string $filename, string $encoding): void
2395cd281f4SGreg Roach    {
2405cd281f4SGreg Roach        // Read the file in blocks of roughly 64K. Ensure that each block
2415cd281f4SGreg Roach        // contains complete gedcom records. This will ensure we don’t split
2425cd281f4SGreg Roach        // multi-byte characters, as well as simplifying the code to import
2435cd281f4SGreg Roach        // each block.
2445cd281f4SGreg Roach
2455cd281f4SGreg Roach        $file_data = '';
2465cd281f4SGreg Roach
2475cd281f4SGreg Roach        $tree->setPreference('gedcom_filename', $filename);
2485cd281f4SGreg Roach        $tree->setPreference('imported', '0');
2495cd281f4SGreg Roach
2505cd281f4SGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
2515cd281f4SGreg Roach
2521c6adce8SGreg Roach        $stream = $stream->detach();
2531c6adce8SGreg Roach
2541c6adce8SGreg Roach        // Convert to UTF-8.
2551c6adce8SGreg Roach        stream_filter_append($stream, GedcomEncodingFilter::class, STREAM_FILTER_READ, ['src_encoding' => $encoding]);
2561c6adce8SGreg Roach
2571c6adce8SGreg Roach        while (!feof($stream)) {
2581c6adce8SGreg Roach            $file_data .= fread($stream, 65536);
2591c6adce8SGreg Roach            $eol_pos = max((int) strrpos($file_data, "\r0"), (int) strrpos($file_data, "\n0"));
2601c6adce8SGreg Roach
2611c6adce8SGreg Roach            if ($eol_pos > 0) {
2625cd281f4SGreg Roach                DB::table('gedcom_chunk')->insert([
2635cd281f4SGreg Roach                    'gedcom_id'  => $tree->id(),
2641c6adce8SGreg Roach                    'chunk_data' => substr($file_data, 0, $eol_pos + 1),
2655cd281f4SGreg Roach                ]);
2665cd281f4SGreg Roach
2671c6adce8SGreg Roach                $file_data = substr($file_data, $eol_pos + 1);
2685cd281f4SGreg Roach            }
2695cd281f4SGreg Roach        }
2701c6adce8SGreg Roach
2715cd281f4SGreg Roach        DB::table('gedcom_chunk')->insert([
2725cd281f4SGreg Roach            'gedcom_id'  => $tree->id(),
2735cd281f4SGreg Roach            'chunk_data' => $file_data,
2745cd281f4SGreg Roach        ]);
2755cd281f4SGreg Roach
2761c6adce8SGreg Roach        fclose($stream);
2775cd281f4SGreg Roach    }
2785cd281f4SGreg Roach
2795cd281f4SGreg Roach    /**
2805afbc57aSGreg Roach     * @param Tree $tree
2815afbc57aSGreg Roach     */
2825afbc57aSGreg Roach    public function delete(Tree $tree): void
2835afbc57aSGreg Roach    {
2845afbc57aSGreg Roach        // If this is the default tree, then unset it
2855afbc57aSGreg Roach        if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) {
2865afbc57aSGreg Roach            Site::setPreference('DEFAULT_GEDCOM', '');
2875afbc57aSGreg Roach        }
2885afbc57aSGreg Roach
2895cd281f4SGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
2903ddb2c3fSGreg Roach        DB::table('individuals')->where('i_file', '=', $tree->id())->delete();
2913ddb2c3fSGreg Roach        DB::table('families')->where('f_file', '=', $tree->id())->delete();
2923ddb2c3fSGreg Roach        DB::table('sources')->where('s_file', '=', $tree->id())->delete();
2933ddb2c3fSGreg Roach        DB::table('other')->where('o_file', '=', $tree->id())->delete();
2943ddb2c3fSGreg Roach        DB::table('places')->where('p_file', '=', $tree->id())->delete();
2953ddb2c3fSGreg Roach        DB::table('placelinks')->where('pl_file', '=', $tree->id())->delete();
2963ddb2c3fSGreg Roach        DB::table('name')->where('n_file', '=', $tree->id())->delete();
2973ddb2c3fSGreg Roach        DB::table('dates')->where('d_file', '=', $tree->id())->delete();
2983ddb2c3fSGreg Roach        DB::table('change')->where('gedcom_id', '=', $tree->id())->delete();
2993ddb2c3fSGreg Roach        DB::table('link')->where('l_file', '=', $tree->id())->delete();
3003ddb2c3fSGreg Roach        DB::table('media_file')->where('m_file', '=', $tree->id())->delete();
3013ddb2c3fSGreg Roach        DB::table('media')->where('m_file', '=', $tree->id())->delete();
3025afbc57aSGreg Roach        DB::table('block_setting')
3035afbc57aSGreg Roach            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
3045afbc57aSGreg Roach            ->where('gedcom_id', '=', $tree->id())
3055afbc57aSGreg Roach            ->delete();
3065afbc57aSGreg Roach        DB::table('block')->where('gedcom_id', '=', $tree->id())->delete();
3075afbc57aSGreg Roach        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
3085afbc57aSGreg Roach        DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
3095afbc57aSGreg Roach        DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete();
3105afbc57aSGreg Roach        DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete();
3115afbc57aSGreg Roach        DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete();
3125afbc57aSGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
3135afbc57aSGreg Roach        DB::table('log')->where('gedcom_id', '=', $tree->id())->delete();
3145afbc57aSGreg Roach        DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete();
3155afbc57aSGreg Roach    }
3165afbc57aSGreg Roach
3175afbc57aSGreg Roach    /**
3185afbc57aSGreg Roach     * Generate a unique name for a new tree.
3195afbc57aSGreg Roach     *
3205afbc57aSGreg Roach     * @return string
3215afbc57aSGreg Roach     */
3225afbc57aSGreg Roach    public function uniqueTreeName(): string
3235afbc57aSGreg Roach    {
3245afbc57aSGreg Roach        $name   = 'tree';
3255afbc57aSGreg Roach        $number = 1;
3265afbc57aSGreg Roach
3271e653452SGreg Roach        while ($this->all()->get($name . $number) instanceof Tree) {
3285afbc57aSGreg Roach            $number++;
3295afbc57aSGreg Roach        }
3305afbc57aSGreg Roach
3315afbc57aSGreg Roach        return $name . $number;
3325afbc57aSGreg Roach    }
3335afbc57aSGreg Roach}
334