15afbc57aSGreg Roach<?php 25afbc57aSGreg Roach 35afbc57aSGreg Roach/** 45afbc57aSGreg Roach * webtrees: online genealogy 51fe542e9SGreg Roach * Copyright (C) 2021 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; 24*1c6adce8SGreg Roachuse Fisharebest\Webtrees\Encodings\UTF8; 255afbc57aSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 265afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N; 279d173e09SGreg Roachuse Fisharebest\Webtrees\Registry; 285afbc57aSGreg Roachuse Fisharebest\Webtrees\Site; 29*1c6adce8SGreg Roachuse Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter; 305afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree; 315afbc57aSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 325afbc57aSGreg Roachuse Illuminate\Database\Query\Builder; 335afbc57aSGreg Roachuse Illuminate\Database\Query\Expression; 345afbc57aSGreg Roachuse Illuminate\Database\Query\JoinClause; 355afbc57aSGreg Roachuse Illuminate\Support\Collection; 365cd281f4SGreg Roachuse Psr\Http\Message\StreamInterface; 371e653452SGreg Roachuse RuntimeException; 385afbc57aSGreg Roach 3990a2f718SGreg Roachuse function assert; 40*1c6adce8SGreg Roachuse function fclose; 41*1c6adce8SGreg Roachuse function feof; 42*1c6adce8SGreg Roachuse function fread; 43*1c6adce8SGreg Roachuse function max; 44*1c6adce8SGreg Roachuse function stream_filter_append; 455cd281f4SGreg Roachuse function strlen; 46*1c6adce8SGreg Roachuse function strrpos; 475cd281f4SGreg Roachuse function substr; 485afbc57aSGreg Roach 49*1c6adce8SGreg Roachuse const STREAM_FILTER_READ; 50*1c6adce8SGreg Roach 515afbc57aSGreg Roach/** 525afbc57aSGreg Roach * Tree management and queries. 535afbc57aSGreg Roach */ 545afbc57aSGreg Roachclass TreeService 555afbc57aSGreg Roach{ 565afbc57aSGreg Roach // The most likely surname tradition for a given language. 575afbc57aSGreg Roach private const DEFAULT_SURNAME_TRADITIONS = [ 585afbc57aSGreg Roach 'es' => 'spanish', 595afbc57aSGreg Roach 'is' => 'icelandic', 605afbc57aSGreg Roach 'lt' => 'lithuanian', 615afbc57aSGreg Roach 'pl' => 'polish', 625afbc57aSGreg Roach 'pt' => 'portuguese', 635afbc57aSGreg Roach 'pt-BR' => 'portuguese', 645afbc57aSGreg Roach ]; 655afbc57aSGreg Roach 665afbc57aSGreg Roach /** 675afbc57aSGreg Roach * All the trees that the current user has permission to access. 685afbc57aSGreg Roach * 69b5c8fd7eSGreg Roach * @return Collection<Tree> 705afbc57aSGreg Roach */ 715afbc57aSGreg Roach public function all(): Collection 725afbc57aSGreg Roach { 736b9cb339SGreg Roach return Registry::cache()->array()->remember('all-trees', static function (): Collection { 745afbc57aSGreg Roach // All trees 755afbc57aSGreg Roach $query = DB::table('gedcom') 765afbc57aSGreg Roach ->leftJoin('gedcom_setting', static function (JoinClause $join): void { 775afbc57aSGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 785afbc57aSGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 795afbc57aSGreg Roach }) 805afbc57aSGreg Roach ->where('gedcom.gedcom_id', '>', 0) 815afbc57aSGreg Roach ->select([ 825afbc57aSGreg Roach 'gedcom.gedcom_id AS tree_id', 835afbc57aSGreg Roach 'gedcom.gedcom_name AS tree_name', 845afbc57aSGreg Roach 'gedcom_setting.setting_value AS tree_title', 855afbc57aSGreg Roach ]) 865afbc57aSGreg Roach ->orderBy('gedcom.sort_order') 875afbc57aSGreg Roach ->orderBy('gedcom_setting.setting_value'); 885afbc57aSGreg Roach 895afbc57aSGreg Roach // Non-admins may not see all trees 905afbc57aSGreg Roach if (!Auth::isAdmin()) { 915afbc57aSGreg Roach $query 925afbc57aSGreg Roach ->join('gedcom_setting AS gs2', static function (JoinClause $join): void { 936b736a8bSGreg Roach $join 946b736a8bSGreg Roach ->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 95cc194e3fSGreg Roach ->where('gs2.setting_name', '=', 'imported'); 965afbc57aSGreg Roach }) 975afbc57aSGreg Roach ->join('gedcom_setting AS gs3', static function (JoinClause $join): void { 986b736a8bSGreg Roach $join 996b736a8bSGreg Roach ->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 100cc194e3fSGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 1015afbc57aSGreg Roach }) 1025afbc57aSGreg Roach ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void { 1036b736a8bSGreg Roach $join 1046b736a8bSGreg Roach ->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 105cc194e3fSGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 106cc194e3fSGreg Roach ->where('user_gedcom_setting.setting_name', '=', UserInterface::PREF_TREE_ROLE); 1075afbc57aSGreg Roach }) 1085afbc57aSGreg Roach ->where(static function (Builder $query): void { 1095afbc57aSGreg Roach $query 1105afbc57aSGreg Roach // Managers 1111fe542e9SGreg Roach ->where('user_gedcom_setting.setting_value', '=', UserInterface::ROLE_MANAGER) 1125afbc57aSGreg Roach // Members 1135afbc57aSGreg Roach ->orWhere(static function (Builder $query): void { 1145afbc57aSGreg Roach $query 1155afbc57aSGreg Roach ->where('gs2.setting_value', '=', '1') 1165afbc57aSGreg Roach ->where('gs3.setting_value', '=', '1') 1171fe542e9SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', UserInterface::ROLE_VISITOR); 1185afbc57aSGreg Roach }) 1195afbc57aSGreg Roach // Public trees 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'); 1245afbc57aSGreg Roach }); 1255afbc57aSGreg Roach }); 1265afbc57aSGreg Roach } 1275afbc57aSGreg Roach 1285afbc57aSGreg Roach return $query 1295afbc57aSGreg Roach ->get() 130f70bcff5SGreg Roach ->mapWithKeys(static function (object $row): array { 1311e653452SGreg Roach return [$row->tree_name => Tree::rowMapper()($row)]; 1325afbc57aSGreg Roach }); 1335afbc57aSGreg Roach }); 1345afbc57aSGreg Roach } 1355afbc57aSGreg Roach 1365afbc57aSGreg Roach /** 1371e653452SGreg Roach * Find a tree by its ID. 1385afbc57aSGreg Roach * 1391e653452SGreg Roach * @param int $id 1405afbc57aSGreg Roach * 1411e653452SGreg Roach * @return Tree 1425afbc57aSGreg Roach */ 1431e653452SGreg Roach public function find(int $id): Tree 1445afbc57aSGreg Roach { 1451e653452SGreg Roach $tree = $this->all()->first(static function (Tree $tree) use ($id): bool { 1461e653452SGreg Roach return $tree->id() === $id; 1475afbc57aSGreg Roach }); 1481e653452SGreg Roach 1491e653452SGreg Roach assert($tree instanceof Tree, new RuntimeException()); 1501e653452SGreg Roach 1511e653452SGreg Roach return $tree; 1521e653452SGreg Roach } 1531e653452SGreg Roach 1541e653452SGreg Roach /** 1551e653452SGreg Roach * All trees, name => title 1561e653452SGreg Roach * 15724f2a3afSGreg Roach * @return array<string> 1581e653452SGreg Roach */ 1591e653452SGreg Roach public function titles(): array 1601e653452SGreg Roach { 1611e653452SGreg Roach return $this->all()->map(static function (Tree $tree): string { 1621e653452SGreg Roach return $tree->title(); 1631e653452SGreg Roach })->all(); 1645afbc57aSGreg Roach } 1655afbc57aSGreg Roach 1665afbc57aSGreg Roach /** 1675afbc57aSGreg Roach * @param string $name 1685afbc57aSGreg Roach * @param string $title 1695afbc57aSGreg Roach * 1705afbc57aSGreg Roach * @return Tree 1715afbc57aSGreg Roach */ 1725afbc57aSGreg Roach public function create(string $name, string $title): Tree 1735afbc57aSGreg Roach { 1745afbc57aSGreg Roach DB::table('gedcom')->insert([ 1755afbc57aSGreg Roach 'gedcom_name' => $name, 1765afbc57aSGreg Roach ]); 1775afbc57aSGreg Roach 1785afbc57aSGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 1795afbc57aSGreg Roach 1805afbc57aSGreg Roach $tree = new Tree($tree_id, $name, $title); 1815afbc57aSGreg Roach 1825afbc57aSGreg Roach $tree->setPreference('imported', '1'); 1835afbc57aSGreg Roach $tree->setPreference('title', $title); 1845afbc57aSGreg Roach 1855afbc57aSGreg Roach // Set preferences from default tree 1865afbc57aSGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 1875afbc57aSGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 1885afbc57aSGreg Roach static function (Builder $query) use ($tree_id): void { 1895afbc57aSGreg Roach $query 1905afbc57aSGreg Roach ->select([new Expression($tree_id), 'setting_name', 'setting_value']) 1915afbc57aSGreg Roach ->from('gedcom_setting') 1925afbc57aSGreg Roach ->where('gedcom_id', '=', -1); 1935afbc57aSGreg Roach } 1945afbc57aSGreg Roach ); 1955afbc57aSGreg Roach 1965afbc57aSGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 1975afbc57aSGreg Roach ['gedcom_id', 'tag_type', 'resn'], 1985afbc57aSGreg Roach static function (Builder $query) use ($tree_id): void { 1995afbc57aSGreg Roach $query 2005afbc57aSGreg Roach ->select([new Expression($tree_id), 'tag_type', 'resn']) 2015afbc57aSGreg Roach ->from('default_resn') 2025afbc57aSGreg Roach ->where('gedcom_id', '=', -1); 2035afbc57aSGreg Roach } 2045afbc57aSGreg Roach ); 2055afbc57aSGreg Roach 2065afbc57aSGreg Roach // Gedcom and privacy settings 2076b736a8bSGreg Roach $tree->setPreference('REQUIRE_AUTHENTICATION', ''); 2085afbc57aSGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 2095afbc57aSGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 21065cf5706SGreg Roach $tree->setPreference('LANGUAGE', I18N::languageTag()); // Default to the current admin’s language 21165cf5706SGreg Roach $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[I18N::languageTag()] ?? 'paternal'); 2125afbc57aSGreg Roach 2135afbc57aSGreg Roach // A tree needs at least one record. 2149b5c9597SGreg 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"; 2159b5c9597SGreg Roach FunctionsImport::importRecord($head, $tree, true); 2165afbc57aSGreg Roach 2175afbc57aSGreg Roach // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. 2185afbc57aSGreg Roach $name = I18N::translate('John /DOE/'); 2195afbc57aSGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 2209b5c9597SGreg Roach $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note; 2219b5c9597SGreg Roach FunctionsImport::importRecord($indi, $tree, true); 2225afbc57aSGreg Roach 2235afbc57aSGreg Roach return $tree; 2245afbc57aSGreg Roach } 2255afbc57aSGreg Roach 2265afbc57aSGreg Roach /** 2275cd281f4SGreg Roach * Import data from a gedcom file into this tree. 2285cd281f4SGreg Roach * 2295cd281f4SGreg Roach * @param Tree $tree 2305cd281f4SGreg Roach * @param StreamInterface $stream The GEDCOM file. 2315cd281f4SGreg Roach * @param string $filename The preferred filename, for export/download. 232*1c6adce8SGreg Roach * @param string $encoding Override the encoding specified in the header. 2335cd281f4SGreg Roach * 2345cd281f4SGreg Roach * @return void 2355cd281f4SGreg Roach */ 236*1c6adce8SGreg Roach public function importGedcomFile(Tree $tree, StreamInterface $stream, string $filename, string $encoding): void 2375cd281f4SGreg Roach { 2385cd281f4SGreg Roach // Read the file in blocks of roughly 64K. Ensure that each block 2395cd281f4SGreg Roach // contains complete gedcom records. This will ensure we don’t split 2405cd281f4SGreg Roach // multi-byte characters, as well as simplifying the code to import 2415cd281f4SGreg Roach // each block. 2425cd281f4SGreg Roach 2435cd281f4SGreg Roach $file_data = ''; 2445cd281f4SGreg Roach 2455cd281f4SGreg Roach $tree->setPreference('gedcom_filename', $filename); 2465cd281f4SGreg Roach $tree->setPreference('imported', '0'); 2475cd281f4SGreg Roach 2485cd281f4SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete(); 2495cd281f4SGreg Roach 250*1c6adce8SGreg Roach $stream = $stream->detach(); 251*1c6adce8SGreg Roach 252*1c6adce8SGreg Roach // Convert to UTF-8. 253*1c6adce8SGreg Roach stream_filter_append($stream, GedcomEncodingFilter::class, STREAM_FILTER_READ, ['src_encoding' => $encoding]); 254*1c6adce8SGreg Roach 255*1c6adce8SGreg Roach while (!feof($stream)) { 256*1c6adce8SGreg Roach $file_data .= fread($stream, 65536); 257*1c6adce8SGreg Roach $eol_pos = max((int) strrpos($file_data, "\r0"), (int) strrpos($file_data, "\n0")); 258*1c6adce8SGreg Roach 259*1c6adce8SGreg Roach if ($eol_pos > 0) { 2605cd281f4SGreg Roach DB::table('gedcom_chunk')->insert([ 2615cd281f4SGreg Roach 'gedcom_id' => $tree->id(), 262*1c6adce8SGreg Roach 'chunk_data' => substr($file_data, 0, $eol_pos + 1), 2635cd281f4SGreg Roach ]); 2645cd281f4SGreg Roach 265*1c6adce8SGreg Roach $file_data = substr($file_data, $eol_pos + 1); 2665cd281f4SGreg Roach } 2675cd281f4SGreg Roach } 268*1c6adce8SGreg Roach 2695cd281f4SGreg Roach DB::table('gedcom_chunk')->insert([ 2705cd281f4SGreg Roach 'gedcom_id' => $tree->id(), 2715cd281f4SGreg Roach 'chunk_data' => $file_data, 2725cd281f4SGreg Roach ]); 2735cd281f4SGreg Roach 274*1c6adce8SGreg Roach fclose($stream); 2755cd281f4SGreg Roach } 2765cd281f4SGreg Roach 2775cd281f4SGreg Roach /** 2785afbc57aSGreg Roach * @param Tree $tree 2795afbc57aSGreg Roach */ 2805afbc57aSGreg Roach public function delete(Tree $tree): void 2815afbc57aSGreg Roach { 2825afbc57aSGreg Roach // If this is the default tree, then unset it 2835afbc57aSGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) { 2845afbc57aSGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 2855afbc57aSGreg Roach } 2865afbc57aSGreg Roach 2875cd281f4SGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete(); 2885cd281f4SGreg Roach 2895cd281f4SGreg Roach $this->deleteGenealogyData($tree, false); 2905afbc57aSGreg Roach 2915afbc57aSGreg Roach DB::table('block_setting') 2925afbc57aSGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 2935afbc57aSGreg Roach ->where('gedcom_id', '=', $tree->id()) 2945afbc57aSGreg Roach ->delete(); 2955afbc57aSGreg Roach DB::table('block')->where('gedcom_id', '=', $tree->id())->delete(); 2965afbc57aSGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete(); 2975afbc57aSGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete(); 2985afbc57aSGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete(); 2995afbc57aSGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete(); 3005afbc57aSGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete(); 3015afbc57aSGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete(); 3025afbc57aSGreg Roach DB::table('log')->where('gedcom_id', '=', $tree->id())->delete(); 3035afbc57aSGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete(); 3045afbc57aSGreg Roach } 3055afbc57aSGreg Roach 3065afbc57aSGreg Roach /** 3075cd281f4SGreg Roach * Delete all the genealogy data from a tree - in preparation for importing 3085cd281f4SGreg Roach * new data. Optionally retain the media data, for when the user has been 3095cd281f4SGreg Roach * editing their data offline using an application which deletes (or does not 3105cd281f4SGreg Roach * support) media data. 3115cd281f4SGreg Roach * 3125cd281f4SGreg Roach * @param Tree $tree 3135cd281f4SGreg Roach * @param bool $keep_media 3145cd281f4SGreg Roach * 3155cd281f4SGreg Roach * @return void 3165cd281f4SGreg Roach */ 3175cd281f4SGreg Roach public function deleteGenealogyData(Tree $tree, bool $keep_media): void 3185cd281f4SGreg Roach { 3195cd281f4SGreg Roach DB::table('individuals')->where('i_file', '=', $tree->id())->delete(); 3205cd281f4SGreg Roach DB::table('families')->where('f_file', '=', $tree->id())->delete(); 3215cd281f4SGreg Roach DB::table('sources')->where('s_file', '=', $tree->id())->delete(); 3225cd281f4SGreg Roach DB::table('other')->where('o_file', '=', $tree->id())->delete(); 3235cd281f4SGreg Roach DB::table('places')->where('p_file', '=', $tree->id())->delete(); 3245cd281f4SGreg Roach DB::table('placelinks')->where('pl_file', '=', $tree->id())->delete(); 3255cd281f4SGreg Roach DB::table('name')->where('n_file', '=', $tree->id())->delete(); 3265cd281f4SGreg Roach DB::table('dates')->where('d_file', '=', $tree->id())->delete(); 3275cd281f4SGreg Roach DB::table('change')->where('gedcom_id', '=', $tree->id())->delete(); 3285cd281f4SGreg Roach 3295cd281f4SGreg Roach if ($keep_media) { 3305cd281f4SGreg Roach DB::table('link')->where('l_file', '=', $tree->id()) 3315cd281f4SGreg Roach ->where('l_type', '<>', 'OBJE') 3325cd281f4SGreg Roach ->delete(); 3335cd281f4SGreg Roach } else { 3345cd281f4SGreg Roach DB::table('link')->where('l_file', '=', $tree->id())->delete(); 3355cd281f4SGreg Roach DB::table('media_file')->where('m_file', '=', $tree->id())->delete(); 3365cd281f4SGreg Roach DB::table('media')->where('m_file', '=', $tree->id())->delete(); 3375cd281f4SGreg Roach } 3385cd281f4SGreg Roach } 3395cd281f4SGreg Roach 3405cd281f4SGreg Roach /** 3415afbc57aSGreg Roach * Generate a unique name for a new tree. 3425afbc57aSGreg Roach * 3435afbc57aSGreg Roach * @return string 3445afbc57aSGreg Roach */ 3455afbc57aSGreg Roach public function uniqueTreeName(): string 3465afbc57aSGreg Roach { 3475afbc57aSGreg Roach $name = 'tree'; 3485afbc57aSGreg Roach $number = 1; 3495afbc57aSGreg Roach 3501e653452SGreg Roach while ($this->all()->get($name . $number) instanceof Tree) { 3515afbc57aSGreg Roach $number++; 3525afbc57aSGreg Roach } 3535afbc57aSGreg Roach 3545afbc57aSGreg Roach return $name . $number; 3555afbc57aSGreg Roach } 3565afbc57aSGreg Roach} 357