xref: /webtrees/app/Services/TreeService.php (revision 3ddb2c3f1b75331c0af6be131557ffc6c3bfb468)
15afbc57aSGreg Roach<?php
25afbc57aSGreg Roach
35afbc57aSGreg Roach/**
45afbc57aSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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
225afbc57aSGreg Roachuse Fisharebest\Webtrees\Auth;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
242c685d76SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
255afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N;
269d173e09SGreg Roachuse Fisharebest\Webtrees\Registry;
275afbc57aSGreg Roachuse Fisharebest\Webtrees\Site;
285afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree;
295afbc57aSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
305afbc57aSGreg Roachuse Illuminate\Database\Query\Builder;
315afbc57aSGreg Roachuse Illuminate\Database\Query\Expression;
325afbc57aSGreg Roachuse Illuminate\Database\Query\JoinClause;
335afbc57aSGreg Roachuse Illuminate\Support\Collection;
345cd281f4SGreg Roachuse Psr\Http\Message\StreamInterface;
351e653452SGreg Roachuse RuntimeException;
365afbc57aSGreg Roach
3790a2f718SGreg Roachuse function assert;
381c6adce8SGreg Roachuse function fclose;
391c6adce8SGreg Roachuse function feof;
401c6adce8SGreg Roachuse function fread;
411c6adce8SGreg Roachuse function max;
421c6adce8SGreg Roachuse function stream_filter_append;
431c6adce8SGreg Roachuse function strrpos;
445cd281f4SGreg Roachuse function substr;
455afbc57aSGreg Roach
461c6adce8SGreg Roachuse const STREAM_FILTER_READ;
471c6adce8SGreg Roach
485afbc57aSGreg Roach/**
495afbc57aSGreg Roach * Tree management and queries.
505afbc57aSGreg Roach */
515afbc57aSGreg Roachclass TreeService
525afbc57aSGreg Roach{
535afbc57aSGreg Roach    // The most likely surname tradition for a given language.
545afbc57aSGreg Roach    private const DEFAULT_SURNAME_TRADITIONS = [
555afbc57aSGreg Roach        'es'    => 'spanish',
565afbc57aSGreg Roach        'is'    => 'icelandic',
575afbc57aSGreg Roach        'lt'    => 'lithuanian',
585afbc57aSGreg Roach        'pl'    => 'polish',
595afbc57aSGreg Roach        'pt'    => 'portuguese',
605afbc57aSGreg Roach        'pt-BR' => 'portuguese',
615afbc57aSGreg Roach    ];
625afbc57aSGreg Roach
632c685d76SGreg Roach    private GedcomImportService $gedcom_import_service;
642c685d76SGreg Roach
652c685d76SGreg Roach    /**
662c685d76SGreg Roach     * @param GedcomImportService $gedcom_import_service
672c685d76SGreg Roach     */
682c685d76SGreg Roach    public function __construct(GedcomImportService $gedcom_import_service)
692c685d76SGreg Roach    {
702c685d76SGreg Roach        $this->gedcom_import_service = $gedcom_import_service;
712c685d76SGreg Roach    }
722c685d76SGreg Roach
735afbc57aSGreg Roach    /**
745afbc57aSGreg Roach     * All the trees that the current user has permission to access.
755afbc57aSGreg Roach     *
7676d39c55SGreg Roach     * @return Collection<array-key,Tree>
775afbc57aSGreg Roach     */
785afbc57aSGreg Roach    public function all(): Collection
795afbc57aSGreg Roach    {
806b9cb339SGreg Roach        return Registry::cache()->array()->remember('all-trees', static function (): Collection {
815afbc57aSGreg Roach            // All trees
825afbc57aSGreg Roach            $query = DB::table('gedcom')
835afbc57aSGreg Roach                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
845afbc57aSGreg Roach                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
855afbc57aSGreg Roach                        ->where('gedcom_setting.setting_name', '=', 'title');
865afbc57aSGreg Roach                })
875afbc57aSGreg Roach                ->where('gedcom.gedcom_id', '>', 0)
885afbc57aSGreg Roach                ->select([
895afbc57aSGreg Roach                    'gedcom.gedcom_id AS tree_id',
905afbc57aSGreg Roach                    'gedcom.gedcom_name AS tree_name',
915afbc57aSGreg Roach                    'gedcom_setting.setting_value AS tree_title',
925afbc57aSGreg Roach                ])
935afbc57aSGreg Roach                ->orderBy('gedcom.sort_order')
945afbc57aSGreg Roach                ->orderBy('gedcom_setting.setting_value');
955afbc57aSGreg Roach
965afbc57aSGreg Roach            // Non-admins may not see all trees
975afbc57aSGreg Roach            if (!Auth::isAdmin()) {
985afbc57aSGreg Roach                $query
995afbc57aSGreg Roach                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
1006b736a8bSGreg Roach                        $join
1016b736a8bSGreg Roach                            ->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
102cc194e3fSGreg Roach                            ->where('gs2.setting_name', '=', 'imported');
1035afbc57aSGreg Roach                    })
1045afbc57aSGreg Roach                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
1056b736a8bSGreg Roach                        $join
1066b736a8bSGreg Roach                            ->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
107cc194e3fSGreg Roach                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
1085afbc57aSGreg Roach                    })
1095afbc57aSGreg Roach                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
1106b736a8bSGreg Roach                        $join
1116b736a8bSGreg Roach                            ->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
112cc194e3fSGreg Roach                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
113cc194e3fSGreg Roach                            ->where('user_gedcom_setting.setting_name', '=', UserInterface::PREF_TREE_ROLE);
1145afbc57aSGreg Roach                    })
1155afbc57aSGreg Roach                    ->where(static function (Builder $query): void {
1165afbc57aSGreg Roach                        $query
1175afbc57aSGreg Roach                            // Managers
1181fe542e9SGreg Roach                            ->where('user_gedcom_setting.setting_value', '=', UserInterface::ROLE_MANAGER)
1195afbc57aSGreg Roach                            // Members
1205afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1215afbc57aSGreg Roach                                $query
1225afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1235afbc57aSGreg Roach                                    ->where('gs3.setting_value', '=', '1')
1241fe542e9SGreg Roach                                    ->where('user_gedcom_setting.setting_value', '<>', UserInterface::ROLE_VISITOR);
1255afbc57aSGreg Roach                            })
1265afbc57aSGreg Roach                            // Public trees
1275afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1285afbc57aSGreg Roach                                $query
1295afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1305afbc57aSGreg Roach                                    ->where('gs3.setting_value', '<>', '1');
1315afbc57aSGreg Roach                            });
1325afbc57aSGreg Roach                    });
1335afbc57aSGreg Roach            }
1345afbc57aSGreg Roach
1355afbc57aSGreg Roach            return $query
1365afbc57aSGreg Roach                ->get()
137f70bcff5SGreg Roach                ->mapWithKeys(static function (object $row): array {
1381e653452SGreg Roach                    return [$row->tree_name => Tree::rowMapper()($row)];
1395afbc57aSGreg Roach                });
1405afbc57aSGreg Roach        });
1415afbc57aSGreg Roach    }
1425afbc57aSGreg Roach
1435afbc57aSGreg Roach    /**
1441e653452SGreg Roach     * Find a tree by its ID.
1455afbc57aSGreg Roach     *
1461e653452SGreg Roach     * @param int $id
1475afbc57aSGreg Roach     *
1481e653452SGreg Roach     * @return Tree
1495afbc57aSGreg Roach     */
1501e653452SGreg Roach    public function find(int $id): Tree
1515afbc57aSGreg Roach    {
1521e653452SGreg Roach        $tree = $this->all()->first(static function (Tree $tree) use ($id): bool {
1531e653452SGreg Roach            return $tree->id() === $id;
1545afbc57aSGreg Roach        });
1551e653452SGreg Roach
1561e653452SGreg Roach        assert($tree instanceof Tree, new RuntimeException());
1571e653452SGreg Roach
1581e653452SGreg Roach        return $tree;
1591e653452SGreg Roach    }
1601e653452SGreg Roach
1611e653452SGreg Roach    /**
1621e653452SGreg Roach     * All trees, name => title
1631e653452SGreg Roach     *
16424f2a3afSGreg Roach     * @return array<string>
1651e653452SGreg Roach     */
1661e653452SGreg Roach    public function titles(): array
1671e653452SGreg Roach    {
1681e653452SGreg Roach        return $this->all()->map(static function (Tree $tree): string {
1691e653452SGreg Roach            return $tree->title();
1701e653452SGreg Roach        })->all();
1715afbc57aSGreg Roach    }
1725afbc57aSGreg Roach
1735afbc57aSGreg Roach    /**
1745afbc57aSGreg Roach     * @param string $name
1755afbc57aSGreg Roach     * @param string $title
1765afbc57aSGreg Roach     *
1775afbc57aSGreg Roach     * @return Tree
1785afbc57aSGreg Roach     */
1795afbc57aSGreg Roach    public function create(string $name, string $title): Tree
1805afbc57aSGreg Roach    {
1815afbc57aSGreg Roach        DB::table('gedcom')->insert([
1825afbc57aSGreg Roach            'gedcom_name' => $name,
1835afbc57aSGreg Roach        ]);
1845afbc57aSGreg Roach
1855afbc57aSGreg Roach        $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
1865afbc57aSGreg Roach
1875afbc57aSGreg Roach        $tree = new Tree($tree_id, $name, $title);
1885afbc57aSGreg Roach
1895afbc57aSGreg Roach        $tree->setPreference('imported', '1');
1905afbc57aSGreg Roach        $tree->setPreference('title', $title);
1915afbc57aSGreg Roach
1925afbc57aSGreg Roach        // Set preferences from default tree
1935afbc57aSGreg Roach        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
1945afbc57aSGreg Roach            ['gedcom_id', 'setting_name', 'setting_value'],
1955afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
1965afbc57aSGreg Roach                $query
1975afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
1985afbc57aSGreg Roach                    ->from('gedcom_setting')
1995afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
2005afbc57aSGreg Roach            }
2015afbc57aSGreg Roach        );
2025afbc57aSGreg Roach
2035afbc57aSGreg Roach        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
2045afbc57aSGreg Roach            ['gedcom_id', 'tag_type', 'resn'],
2055afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
2065afbc57aSGreg Roach                $query
2075afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
2085afbc57aSGreg Roach                    ->from('default_resn')
2095afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
2105afbc57aSGreg Roach            }
2115afbc57aSGreg Roach        );
2125afbc57aSGreg Roach
2135afbc57aSGreg Roach        // Gedcom and privacy settings
2146b736a8bSGreg Roach        $tree->setPreference('REQUIRE_AUTHENTICATION', '');
2155afbc57aSGreg Roach        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
2165afbc57aSGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
21765cf5706SGreg Roach        $tree->setPreference('LANGUAGE', I18N::languageTag()); // Default to the current admin’s language
21865cf5706SGreg Roach        $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[I18N::languageTag()] ?? 'paternal');
2195afbc57aSGreg Roach
2205afbc57aSGreg Roach        // A tree needs at least one record.
2219b5c9597SGreg Roach        $head = "0 HEAD\n1 SOUR webtrees\n2 DEST webtrees\n1 GEDC\n2 VERS 5.5.1\n2 FORM LINEAGE-LINKED\n1 CHAR UTF-8";
2222c685d76SGreg Roach        $this->gedcom_import_service->importRecord($head, $tree, true);
2235afbc57aSGreg Roach
2245afbc57aSGreg Roach        // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname.
2255afbc57aSGreg Roach        $name = I18N::translate('John /DOE/');
2265afbc57aSGreg Roach        $note = I18N::translate('Edit this individual and replace their details with your own.');
2279b5c9597SGreg Roach        $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note;
2282c685d76SGreg Roach        $this->gedcom_import_service->importRecord($indi, $tree, true);
2295afbc57aSGreg Roach
2305afbc57aSGreg Roach        return $tree;
2315afbc57aSGreg Roach    }
2325afbc57aSGreg Roach
2335afbc57aSGreg Roach    /**
2345cd281f4SGreg Roach     * Import data from a gedcom file into this tree.
2355cd281f4SGreg Roach     *
2365cd281f4SGreg Roach     * @param Tree            $tree
2375cd281f4SGreg Roach     * @param StreamInterface $stream   The GEDCOM file.
2385cd281f4SGreg Roach     * @param string          $filename The preferred filename, for export/download.
2391c6adce8SGreg Roach     * @param string          $encoding Override the encoding specified in the header.
2405cd281f4SGreg Roach     *
2415cd281f4SGreg Roach     * @return void
2425cd281f4SGreg Roach     */
2431c6adce8SGreg Roach    public function importGedcomFile(Tree $tree, StreamInterface $stream, string $filename, string $encoding): void
2445cd281f4SGreg Roach    {
2455cd281f4SGreg Roach        // Read the file in blocks of roughly 64K. Ensure that each block
2465cd281f4SGreg Roach        // contains complete gedcom records. This will ensure we don’t split
2475cd281f4SGreg Roach        // multi-byte characters, as well as simplifying the code to import
2485cd281f4SGreg Roach        // each block.
2495cd281f4SGreg Roach
2505cd281f4SGreg Roach        $file_data = '';
2515cd281f4SGreg Roach
2525cd281f4SGreg Roach        $tree->setPreference('gedcom_filename', $filename);
2535cd281f4SGreg Roach        $tree->setPreference('imported', '0');
2545cd281f4SGreg Roach
2555cd281f4SGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
2565cd281f4SGreg Roach
2571c6adce8SGreg Roach        $stream = $stream->detach();
2581c6adce8SGreg Roach
2591c6adce8SGreg Roach        // Convert to UTF-8.
2601c6adce8SGreg Roach        stream_filter_append($stream, GedcomEncodingFilter::class, STREAM_FILTER_READ, ['src_encoding' => $encoding]);
2611c6adce8SGreg Roach
2621c6adce8SGreg Roach        while (!feof($stream)) {
2631c6adce8SGreg Roach            $file_data .= fread($stream, 65536);
2641c6adce8SGreg Roach            $eol_pos = max((int) strrpos($file_data, "\r0"), (int) strrpos($file_data, "\n0"));
2651c6adce8SGreg Roach
2661c6adce8SGreg Roach            if ($eol_pos > 0) {
2675cd281f4SGreg Roach                DB::table('gedcom_chunk')->insert([
2685cd281f4SGreg Roach                    'gedcom_id'  => $tree->id(),
2691c6adce8SGreg Roach                    'chunk_data' => substr($file_data, 0, $eol_pos + 1),
2705cd281f4SGreg Roach                ]);
2715cd281f4SGreg Roach
2721c6adce8SGreg Roach                $file_data = substr($file_data, $eol_pos + 1);
2735cd281f4SGreg Roach            }
2745cd281f4SGreg Roach        }
2751c6adce8SGreg Roach
2765cd281f4SGreg Roach        DB::table('gedcom_chunk')->insert([
2775cd281f4SGreg Roach            'gedcom_id'  => $tree->id(),
2785cd281f4SGreg Roach            'chunk_data' => $file_data,
2795cd281f4SGreg Roach        ]);
2805cd281f4SGreg Roach
2811c6adce8SGreg Roach        fclose($stream);
2825cd281f4SGreg Roach    }
2835cd281f4SGreg Roach
2845cd281f4SGreg Roach    /**
2855afbc57aSGreg Roach     * @param Tree $tree
2865afbc57aSGreg Roach     */
2875afbc57aSGreg Roach    public function delete(Tree $tree): void
2885afbc57aSGreg Roach    {
2895afbc57aSGreg Roach        // If this is the default tree, then unset it
2905afbc57aSGreg Roach        if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) {
2915afbc57aSGreg Roach            Site::setPreference('DEFAULT_GEDCOM', '');
2925afbc57aSGreg Roach        }
2935afbc57aSGreg Roach
2945cd281f4SGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
295*3ddb2c3fSGreg Roach        DB::table('individuals')->where('i_file', '=', $tree->id())->delete();
296*3ddb2c3fSGreg Roach        DB::table('families')->where('f_file', '=', $tree->id())->delete();
297*3ddb2c3fSGreg Roach        DB::table('sources')->where('s_file', '=', $tree->id())->delete();
298*3ddb2c3fSGreg Roach        DB::table('other')->where('o_file', '=', $tree->id())->delete();
299*3ddb2c3fSGreg Roach        DB::table('places')->where('p_file', '=', $tree->id())->delete();
300*3ddb2c3fSGreg Roach        DB::table('placelinks')->where('pl_file', '=', $tree->id())->delete();
301*3ddb2c3fSGreg Roach        DB::table('name')->where('n_file', '=', $tree->id())->delete();
302*3ddb2c3fSGreg Roach        DB::table('dates')->where('d_file', '=', $tree->id())->delete();
303*3ddb2c3fSGreg Roach        DB::table('change')->where('gedcom_id', '=', $tree->id())->delete();
304*3ddb2c3fSGreg Roach        DB::table('link')->where('l_file', '=', $tree->id())->delete();
305*3ddb2c3fSGreg Roach        DB::table('media_file')->where('m_file', '=', $tree->id())->delete();
306*3ddb2c3fSGreg Roach        DB::table('media')->where('m_file', '=', $tree->id())->delete();
3075afbc57aSGreg Roach        DB::table('block_setting')
3085afbc57aSGreg Roach            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
3095afbc57aSGreg Roach            ->where('gedcom_id', '=', $tree->id())
3105afbc57aSGreg Roach            ->delete();
3115afbc57aSGreg Roach        DB::table('block')->where('gedcom_id', '=', $tree->id())->delete();
3125afbc57aSGreg Roach        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
3135afbc57aSGreg Roach        DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
3145afbc57aSGreg Roach        DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete();
3155afbc57aSGreg Roach        DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete();
3165afbc57aSGreg Roach        DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete();
3175afbc57aSGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
3185afbc57aSGreg Roach        DB::table('log')->where('gedcom_id', '=', $tree->id())->delete();
3195afbc57aSGreg Roach        DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete();
3205afbc57aSGreg Roach    }
3215afbc57aSGreg Roach
3225afbc57aSGreg Roach    /**
3235afbc57aSGreg Roach     * Generate a unique name for a new tree.
3245afbc57aSGreg Roach     *
3255afbc57aSGreg Roach     * @return string
3265afbc57aSGreg Roach     */
3275afbc57aSGreg Roach    public function uniqueTreeName(): string
3285afbc57aSGreg Roach    {
3295afbc57aSGreg Roach        $name   = 'tree';
3305afbc57aSGreg Roach        $number = 1;
3315afbc57aSGreg Roach
3321e653452SGreg Roach        while ($this->all()->get($name . $number) instanceof Tree) {
3335afbc57aSGreg Roach            $number++;
3345afbc57aSGreg Roach        }
3355afbc57aSGreg Roach
3365afbc57aSGreg Roach        return $name . $number;
3375afbc57aSGreg Roach    }
3385afbc57aSGreg Roach}
339