1bb03c9f0SGreg Roach<?php 2bb03c9f0SGreg Roach 3bb03c9f0SGreg Roach/** 4bb03c9f0SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 6bb03c9f0SGreg Roach * This program is free software: you can redistribute it and/or modify 7bb03c9f0SGreg Roach * it under the terms of the GNU General Public License as published by 8bb03c9f0SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9bb03c9f0SGreg Roach * (at your option) any later version. 10bb03c9f0SGreg Roach * This program is distributed in the hope that it will be useful, 11bb03c9f0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12bb03c9f0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13bb03c9f0SGreg Roach * GNU General Public License for more details. 14bb03c9f0SGreg 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/>. 16bb03c9f0SGreg Roach */ 17bb03c9f0SGreg Roach 18bb03c9f0SGreg Roachdeclare(strict_types=1); 19bb03c9f0SGreg Roach 20bb03c9f0SGreg Roachnamespace Fisharebest\Webtrees\Factories; 21bb03c9f0SGreg Roach 22bb03c9f0SGreg Roachuse Closure; 23bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Contracts\GedcomRecordFactoryInterface; 24bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Family; 25bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 26bb03c9f0SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 27bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Header; 28bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Individual; 291dfec52bSGreg Roachuse Fisharebest\Webtrees\Location; 30bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Media; 31bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Note; 326b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 33bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Repository; 34bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Source; 35bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Submission; 36bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Submitter; 37bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree; 38bb03c9f0SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 39bb03c9f0SGreg Roachuse InvalidArgumentException; 40bb03c9f0SGreg Roach 41bb03c9f0SGreg Roachuse function assert; 42bb03c9f0SGreg Roach 43bb03c9f0SGreg Roach/** 44bb03c9f0SGreg Roach * Make a GedcomRecord object. 45bb03c9f0SGreg Roach */ 46bb03c9f0SGreg Roachclass GedcomRecordFactory extends AbstractGedcomRecordFactory implements GedcomRecordFactoryInterface 47bb03c9f0SGreg Roach{ 48bb03c9f0SGreg Roach /** 49bb03c9f0SGreg Roach * Create a GedcomRecord object. 50bb03c9f0SGreg Roach * 51bb03c9f0SGreg Roach * @param string $xref 52bb03c9f0SGreg Roach * @param Tree $tree 53bb03c9f0SGreg Roach * @param string|null $gedcom 54bb03c9f0SGreg Roach * 55bb03c9f0SGreg Roach * @return GedcomRecord|null 56bb03c9f0SGreg Roach */ 57bb03c9f0SGreg Roach public function make(string $xref, Tree $tree, string $gedcom = null): ?GedcomRecord 58bb03c9f0SGreg Roach { 591dfec52bSGreg Roach // We know the type of the record. Return it directly. 601dfec52bSGreg Roach if ($gedcom !== null && preg_match('/^0(?: @[^@]+@)? ([A-Z_]+)/', $gedcom, $match)) { 611dfec52bSGreg Roach switch ($match[1]) { 621dfec52bSGreg Roach case Family::RECORD_TYPE: 636b9cb339SGreg Roach return Registry::familyFactory()->make($xref, $tree, $gedcom); 641dfec52bSGreg Roach case Header::RECORD_TYPE: 656b9cb339SGreg Roach return Registry::headerFactory()->make($xref, $tree, $gedcom); 661dfec52bSGreg Roach case Individual::RECORD_TYPE: 676b9cb339SGreg Roach return Registry::individualFactory()->make($xref, $tree, $gedcom); 681dfec52bSGreg Roach case Location::RECORD_TYPE: 696b9cb339SGreg Roach return Registry::locationFactory()->make($xref, $tree, $gedcom); 701dfec52bSGreg Roach case Media::RECORD_TYPE: 716b9cb339SGreg Roach return Registry::mediaFactory()->make($xref, $tree, $gedcom); 721dfec52bSGreg Roach case Note::RECORD_TYPE: 736b9cb339SGreg Roach return Registry::noteFactory()->make($xref, $tree, $gedcom); 741dfec52bSGreg Roach case Repository::RECORD_TYPE: 756b9cb339SGreg Roach return Registry::repositoryFactory()->make($xref, $tree, $gedcom); 761dfec52bSGreg Roach case Source::RECORD_TYPE: 776b9cb339SGreg Roach return Registry::sourceFactory()->make($xref, $tree, $gedcom); 781dfec52bSGreg Roach case Submitter::RECORD_TYPE: 796b9cb339SGreg Roach return Registry::submitterFactory()->make($xref, $tree, $gedcom); 801dfec52bSGreg Roach case Submission::RECORD_TYPE: 816b9cb339SGreg Roach return Registry::submissionFactory()->make($xref, $tree, $gedcom); 821dfec52bSGreg Roach } 831dfec52bSGreg Roach } 841dfec52bSGreg Roach 85bb03c9f0SGreg Roach // We do not know the type of the record. Try them all in turn. 86bb03c9f0SGreg Roach return 876b9cb339SGreg Roach Registry::familyFactory()->make($xref, $tree, $gedcom) ?? 886b9cb339SGreg Roach Registry::individualFactory()->make($xref, $tree, $gedcom) ?? 896b9cb339SGreg Roach Registry::mediaFactory()->make($xref, $tree, $gedcom) ?? 906b9cb339SGreg Roach Registry::noteFactory()->make($xref, $tree, $gedcom) ?? 916b9cb339SGreg Roach Registry::repositoryFactory()->make($xref, $tree, $gedcom) ?? 926b9cb339SGreg Roach Registry::sourceFactory()->make($xref, $tree, $gedcom) ?? 936b9cb339SGreg Roach Registry::submitterFactory()->make($xref, $tree, $gedcom) ?? 946b9cb339SGreg Roach Registry::submissionFactory()->make($xref, $tree, $gedcom) ?? 956b9cb339SGreg Roach Registry::locationFactory()->make($xref, $tree, $gedcom) ?? 966b9cb339SGreg Roach Registry::headerFactory()->make($xref, $tree, $gedcom) ?? 976b9cb339SGreg Roach Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 98bb03c9f0SGreg Roach $gedcom = $gedcom ?? $this->gedcom($xref, $tree); 99bb03c9f0SGreg Roach 100bb03c9f0SGreg Roach $pending = $this->pendingChanges($tree)->get($xref); 101bb03c9f0SGreg Roach 102bb03c9f0SGreg Roach if ($gedcom === null && $pending === null) { 103bb03c9f0SGreg Roach return null; 104bb03c9f0SGreg Roach } 105bb03c9f0SGreg Roach 106bb03c9f0SGreg Roach $xref = $this->extractXref($gedcom ?? $pending, $xref); 107bb03c9f0SGreg Roach $type = $this->extractType($gedcom ?? $pending); 108bb03c9f0SGreg Roach 109bb03c9f0SGreg Roach return $this->newGedcomRecord($type, $xref, $gedcom ?? '', $pending, $tree); 110bb03c9f0SGreg Roach }); 111bb03c9f0SGreg Roach } 112bb03c9f0SGreg Roach 113bb03c9f0SGreg Roach /** 114bb03c9f0SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 115bb03c9f0SGreg Roach * 116bb03c9f0SGreg Roach * @param string $xref 117bb03c9f0SGreg Roach * @param string $gedcom an empty string for new/pending records 118bb03c9f0SGreg Roach * @param string|null $pending null for a record with no pending edits, 119bb03c9f0SGreg Roach * empty string for records with pending deletions 120bb03c9f0SGreg Roach * @param Tree $tree 121bb03c9f0SGreg Roach * 122bb03c9f0SGreg Roach * @return GedcomRecord 123bb03c9f0SGreg Roach */ 124bb03c9f0SGreg Roach public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): GedcomRecord 125bb03c9f0SGreg Roach { 126bb03c9f0SGreg Roach return new GedcomRecord($xref, $gedcom, $pending, $tree); 127bb03c9f0SGreg Roach } 128bb03c9f0SGreg Roach 129bb03c9f0SGreg Roach /** 130bb03c9f0SGreg Roach * Create a GedcomRecord object from a row in the database. 131bb03c9f0SGreg Roach * 132bb03c9f0SGreg Roach * @param Tree $tree 133bb03c9f0SGreg Roach * 134bb03c9f0SGreg Roach * @return Closure 135bb03c9f0SGreg Roach */ 136bb03c9f0SGreg Roach public function mapper(Tree $tree): Closure 137bb03c9f0SGreg Roach { 138fdea1dfaSGreg Roach return function (object $row) use ($tree): GedcomRecord { 139bb03c9f0SGreg Roach $record = $this->make($row->o_id, $tree, $row->o_gedcom); 140bb03c9f0SGreg Roach assert($record instanceof GedcomRecord); 141bb03c9f0SGreg Roach 142bb03c9f0SGreg Roach return $record; 143bb03c9f0SGreg Roach }; 144bb03c9f0SGreg Roach } 145bb03c9f0SGreg Roach 146bb03c9f0SGreg Roach /** 147bb03c9f0SGreg Roach * @param string $type 148bb03c9f0SGreg Roach * @param string $xref 149bb03c9f0SGreg Roach * @param string $gedcom 150bb03c9f0SGreg Roach * @param string|null $pending 151bb03c9f0SGreg Roach * @param Tree $tree 152bb03c9f0SGreg Roach * 153bb03c9f0SGreg Roach * @return GedcomRecord 154bb03c9f0SGreg Roach */ 155bb03c9f0SGreg Roach private function newGedcomRecord(string $type, string $xref, string $gedcom, ?string $pending, Tree $tree): GedcomRecord 156bb03c9f0SGreg Roach { 157bb03c9f0SGreg Roach switch ($type) { 158bb03c9f0SGreg Roach case Family::RECORD_TYPE: 1596b9cb339SGreg Roach return Registry::familyFactory()->new($xref, $gedcom, $pending, $tree); 160bb03c9f0SGreg Roach 161bb03c9f0SGreg Roach case Header::RECORD_TYPE: 1626b9cb339SGreg Roach return Registry::headerFactory()->new($xref, $gedcom, $pending, $tree); 163bb03c9f0SGreg Roach 164bb03c9f0SGreg Roach case Individual::RECORD_TYPE: 1656b9cb339SGreg Roach return Registry::individualFactory()->new($xref, $gedcom, $pending, $tree); 166bb03c9f0SGreg Roach 167bb03c9f0SGreg Roach case Media::RECORD_TYPE: 1686b9cb339SGreg Roach return Registry::mediaFactory()->new($xref, $gedcom, $pending, $tree); 169bb03c9f0SGreg Roach 170bb03c9f0SGreg Roach case Note::RECORD_TYPE: 1716b9cb339SGreg Roach return Registry::noteFactory()->new($xref, $gedcom, $pending, $tree); 172bb03c9f0SGreg Roach 173bb03c9f0SGreg Roach case Repository::RECORD_TYPE: 1746b9cb339SGreg Roach return Registry::repositoryFactory()->new($xref, $gedcom, $pending, $tree); 175bb03c9f0SGreg Roach 176bb03c9f0SGreg Roach case Source::RECORD_TYPE: 1776b9cb339SGreg Roach return Registry::sourceFactory()->new($xref, $gedcom, $pending, $tree); 178bb03c9f0SGreg Roach 179bb03c9f0SGreg Roach case Submission::RECORD_TYPE: 1806b9cb339SGreg Roach return Registry::submissionFactory()->new($xref, $gedcom, $pending, $tree); 181bb03c9f0SGreg Roach 182bb03c9f0SGreg Roach case Submitter::RECORD_TYPE: 1836b9cb339SGreg Roach return Registry::submitterFactory()->new($xref, $gedcom, $pending, $tree); 184bb03c9f0SGreg Roach 185bb03c9f0SGreg Roach default: 186bb03c9f0SGreg Roach return $this->new($xref, $gedcom, $pending, $tree); 187bb03c9f0SGreg Roach } 188bb03c9f0SGreg Roach } 189bb03c9f0SGreg Roach 190bb03c9f0SGreg Roach /** 191bb03c9f0SGreg Roach * Extract the type of a GEDCOM record 192bb03c9f0SGreg Roach * 193bb03c9f0SGreg Roach * @param string $gedcom 194bb03c9f0SGreg Roach * 195bb03c9f0SGreg Roach * @return string 196bb03c9f0SGreg Roach * @throws InvalidArgumentException 197bb03c9f0SGreg Roach */ 198bb03c9f0SGreg Roach private function extractType(string $gedcom): string 199bb03c9f0SGreg Roach { 200bb03c9f0SGreg Roach if (preg_match('/^0(?: @' . Gedcom::REGEX_XREF . '@)? ([_A-Z0-9]+)/', $gedcom, $match)) { 201bb03c9f0SGreg Roach return $match[1]; 202bb03c9f0SGreg Roach } 203bb03c9f0SGreg Roach 204bb03c9f0SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM record: ' . $gedcom); 205bb03c9f0SGreg Roach } 206bb03c9f0SGreg Roach 207bb03c9f0SGreg Roach /** 208bb03c9f0SGreg Roach * Fetch GEDCOM data from the database. 209bb03c9f0SGreg Roach * 210bb03c9f0SGreg Roach * @param string $xref 211bb03c9f0SGreg Roach * @param Tree $tree 212bb03c9f0SGreg Roach * 213bb03c9f0SGreg Roach * @return string|null 214bb03c9f0SGreg Roach */ 215bb03c9f0SGreg Roach private function gedcom(string $xref, Tree $tree): ?string 216bb03c9f0SGreg Roach { 217bb03c9f0SGreg Roach return DB::table('other') 218bb03c9f0SGreg Roach ->where('o_id', '=', $xref) 219bb03c9f0SGreg Roach ->where('o_file', '=', $tree->id()) 220bb03c9f0SGreg Roach ->whereNotIn('o_type', [ 221bb03c9f0SGreg Roach Header::RECORD_TYPE, 222bb03c9f0SGreg Roach Note::RECORD_TYPE, 223bb03c9f0SGreg Roach Repository::RECORD_TYPE, 224bb03c9f0SGreg Roach Submission::RECORD_TYPE, 225bb03c9f0SGreg Roach Submitter::RECORD_TYPE, 226bb03c9f0SGreg Roach ]) 227bb03c9f0SGreg Roach ->value('o_gedcom'); 228bb03c9f0SGreg Roach } 229bb03c9f0SGreg Roach} 230