1bb03c9f0SGreg Roach<?php 2bb03c9f0SGreg Roach 3bb03c9f0SGreg Roach/** 4bb03c9f0SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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; 246f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 25bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Family; 26bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Gedcom; 27bb03c9f0SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 28bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Header; 29bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Individual; 301dfec52bSGreg Roachuse Fisharebest\Webtrees\Location; 31bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Media; 32bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Note; 336b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 34bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Repository; 35bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Source; 36bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Submission; 37bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Submitter; 38bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree; 39bb03c9f0SGreg Roachuse InvalidArgumentException; 40bb03c9f0SGreg Roach 41bb03c9f0SGreg Roach/** 42bb03c9f0SGreg Roach * Make a GedcomRecord object. 43bb03c9f0SGreg Roach */ 44bb03c9f0SGreg Roachclass GedcomRecordFactory extends AbstractGedcomRecordFactory implements GedcomRecordFactoryInterface 45bb03c9f0SGreg Roach{ 46bb03c9f0SGreg Roach /** 47bb03c9f0SGreg Roach * Create a GedcomRecord object. 48bb03c9f0SGreg Roach */ 49*1ff45046SGreg Roach public function make(string $xref, Tree $tree, string|null $gedcom = null): GedcomRecord|null 50bb03c9f0SGreg Roach { 511dfec52bSGreg Roach // We know the type of the record. Return it directly. 521dfec52bSGreg Roach if ($gedcom !== null && preg_match('/^0(?: @[^@]+@)? ([A-Z_]+)/', $gedcom, $match)) { 531dfec52bSGreg Roach switch ($match[1]) { 541dfec52bSGreg Roach case Family::RECORD_TYPE: 556b9cb339SGreg Roach return Registry::familyFactory()->make($xref, $tree, $gedcom); 561dfec52bSGreg Roach case Header::RECORD_TYPE: 576b9cb339SGreg Roach return Registry::headerFactory()->make($xref, $tree, $gedcom); 581dfec52bSGreg Roach case Individual::RECORD_TYPE: 596b9cb339SGreg Roach return Registry::individualFactory()->make($xref, $tree, $gedcom); 601dfec52bSGreg Roach case Location::RECORD_TYPE: 616b9cb339SGreg Roach return Registry::locationFactory()->make($xref, $tree, $gedcom); 621dfec52bSGreg Roach case Media::RECORD_TYPE: 636b9cb339SGreg Roach return Registry::mediaFactory()->make($xref, $tree, $gedcom); 641dfec52bSGreg Roach case Note::RECORD_TYPE: 656b9cb339SGreg Roach return Registry::noteFactory()->make($xref, $tree, $gedcom); 661dfec52bSGreg Roach case Repository::RECORD_TYPE: 676b9cb339SGreg Roach return Registry::repositoryFactory()->make($xref, $tree, $gedcom); 681dfec52bSGreg Roach case Source::RECORD_TYPE: 696b9cb339SGreg Roach return Registry::sourceFactory()->make($xref, $tree, $gedcom); 701dfec52bSGreg Roach case Submitter::RECORD_TYPE: 716b9cb339SGreg Roach return Registry::submitterFactory()->make($xref, $tree, $gedcom); 721dfec52bSGreg Roach case Submission::RECORD_TYPE: 736b9cb339SGreg Roach return Registry::submissionFactory()->make($xref, $tree, $gedcom); 741dfec52bSGreg Roach } 751dfec52bSGreg Roach } 761dfec52bSGreg Roach 77bb03c9f0SGreg Roach // We do not know the type of the record. Try them all in turn. 78bb03c9f0SGreg Roach return 796b9cb339SGreg Roach Registry::familyFactory()->make($xref, $tree, $gedcom) ?? 806b9cb339SGreg Roach Registry::individualFactory()->make($xref, $tree, $gedcom) ?? 816b9cb339SGreg Roach Registry::mediaFactory()->make($xref, $tree, $gedcom) ?? 826b9cb339SGreg Roach Registry::noteFactory()->make($xref, $tree, $gedcom) ?? 836b9cb339SGreg Roach Registry::repositoryFactory()->make($xref, $tree, $gedcom) ?? 846b9cb339SGreg Roach Registry::sourceFactory()->make($xref, $tree, $gedcom) ?? 856b9cb339SGreg Roach Registry::submitterFactory()->make($xref, $tree, $gedcom) ?? 866b9cb339SGreg Roach Registry::submissionFactory()->make($xref, $tree, $gedcom) ?? 876b9cb339SGreg Roach Registry::locationFactory()->make($xref, $tree, $gedcom) ?? 886b9cb339SGreg Roach Registry::headerFactory()->make($xref, $tree, $gedcom) ?? 899991924fSGreg Roach Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 903529c469SGreg Roach $gedcom ??= $this->gedcom($xref, $tree); 91bb03c9f0SGreg Roach 92bb03c9f0SGreg Roach $pending = $this->pendingChanges($tree)->get($xref); 93bb03c9f0SGreg Roach 94bb03c9f0SGreg Roach if ($gedcom === null && $pending === null) { 95bb03c9f0SGreg Roach return null; 96bb03c9f0SGreg Roach } 97bb03c9f0SGreg Roach 98bb03c9f0SGreg Roach $xref = $this->extractXref($gedcom ?? $pending, $xref); 99bb03c9f0SGreg Roach $type = $this->extractType($gedcom ?? $pending); 100bb03c9f0SGreg Roach 101bb03c9f0SGreg Roach return $this->newGedcomRecord($type, $xref, $gedcom ?? '', $pending, $tree); 102bb03c9f0SGreg Roach }); 103bb03c9f0SGreg Roach } 104bb03c9f0SGreg Roach 105bb03c9f0SGreg Roach /** 106bb03c9f0SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 107bb03c9f0SGreg Roach * 108bb03c9f0SGreg Roach * @param string $xref 109bb03c9f0SGreg Roach * @param string $gedcom an empty string for new/pending records 110bb03c9f0SGreg Roach * @param string|null $pending null for a record with no pending edits, 111bb03c9f0SGreg Roach * empty string for records with pending deletions 112bb03c9f0SGreg Roach * @param Tree $tree 113bb03c9f0SGreg Roach * 114bb03c9f0SGreg Roach * @return GedcomRecord 115bb03c9f0SGreg Roach */ 116*1ff45046SGreg Roach public function new(string $xref, string $gedcom, string|null $pending, Tree $tree): GedcomRecord 117bb03c9f0SGreg Roach { 118bb03c9f0SGreg Roach return new GedcomRecord($xref, $gedcom, $pending, $tree); 119bb03c9f0SGreg Roach } 120bb03c9f0SGreg Roach 121bb03c9f0SGreg Roach /** 122bb03c9f0SGreg Roach * Create a GedcomRecord object from a row in the database. 123bb03c9f0SGreg Roach * 124bb03c9f0SGreg Roach * @param Tree $tree 125bb03c9f0SGreg Roach * 126c6921a17SGreg Roach * @return Closure(object):GedcomRecord 127bb03c9f0SGreg Roach */ 128bb03c9f0SGreg Roach public function mapper(Tree $tree): Closure 129bb03c9f0SGreg Roach { 130743491b8SGreg Roach return fn (object $row): GedcomRecord => $this->make($row->o_id, $tree, $row->o_gedcom); 131bb03c9f0SGreg Roach } 132bb03c9f0SGreg Roach 133bb03c9f0SGreg Roach /** 134bb03c9f0SGreg Roach * @param string $type 135bb03c9f0SGreg Roach * @param string $xref 136bb03c9f0SGreg Roach * @param string $gedcom 137bb03c9f0SGreg Roach * @param string|null $pending 138bb03c9f0SGreg Roach * @param Tree $tree 139bb03c9f0SGreg Roach * 140bb03c9f0SGreg Roach * @return GedcomRecord 141bb03c9f0SGreg Roach */ 142*1ff45046SGreg Roach private function newGedcomRecord(string $type, string $xref, string $gedcom, string|null $pending, Tree $tree): GedcomRecord 143bb03c9f0SGreg Roach { 144bb03c9f0SGreg Roach switch ($type) { 145bb03c9f0SGreg Roach case Family::RECORD_TYPE: 1466b9cb339SGreg Roach return Registry::familyFactory()->new($xref, $gedcom, $pending, $tree); 147bb03c9f0SGreg Roach 148bb03c9f0SGreg Roach case Header::RECORD_TYPE: 1496b9cb339SGreg Roach return Registry::headerFactory()->new($xref, $gedcom, $pending, $tree); 150bb03c9f0SGreg Roach 151bb03c9f0SGreg Roach case Individual::RECORD_TYPE: 1526b9cb339SGreg Roach return Registry::individualFactory()->new($xref, $gedcom, $pending, $tree); 153bb03c9f0SGreg Roach 154bb03c9f0SGreg Roach case Media::RECORD_TYPE: 1556b9cb339SGreg Roach return Registry::mediaFactory()->new($xref, $gedcom, $pending, $tree); 156bb03c9f0SGreg Roach 157bb03c9f0SGreg Roach case Note::RECORD_TYPE: 1586b9cb339SGreg Roach return Registry::noteFactory()->new($xref, $gedcom, $pending, $tree); 159bb03c9f0SGreg Roach 160bb03c9f0SGreg Roach case Repository::RECORD_TYPE: 1616b9cb339SGreg Roach return Registry::repositoryFactory()->new($xref, $gedcom, $pending, $tree); 162bb03c9f0SGreg Roach 163bb03c9f0SGreg Roach case Source::RECORD_TYPE: 1646b9cb339SGreg Roach return Registry::sourceFactory()->new($xref, $gedcom, $pending, $tree); 165bb03c9f0SGreg Roach 166bb03c9f0SGreg Roach case Submission::RECORD_TYPE: 1676b9cb339SGreg Roach return Registry::submissionFactory()->new($xref, $gedcom, $pending, $tree); 168bb03c9f0SGreg Roach 169bb03c9f0SGreg Roach case Submitter::RECORD_TYPE: 1706b9cb339SGreg Roach return Registry::submitterFactory()->new($xref, $gedcom, $pending, $tree); 171bb03c9f0SGreg Roach 172bb03c9f0SGreg Roach default: 173bb03c9f0SGreg Roach return $this->new($xref, $gedcom, $pending, $tree); 174bb03c9f0SGreg Roach } 175bb03c9f0SGreg Roach } 176bb03c9f0SGreg Roach 177bb03c9f0SGreg Roach /** 178bb03c9f0SGreg Roach * Extract the type of a GEDCOM record 179bb03c9f0SGreg Roach * 180bb03c9f0SGreg Roach * @param string $gedcom 181bb03c9f0SGreg Roach * 182bb03c9f0SGreg Roach * @return string 183bb03c9f0SGreg Roach * @throws InvalidArgumentException 184bb03c9f0SGreg Roach */ 185bb03c9f0SGreg Roach private function extractType(string $gedcom): string 186bb03c9f0SGreg Roach { 187bb03c9f0SGreg Roach if (preg_match('/^0(?: @' . Gedcom::REGEX_XREF . '@)? ([_A-Z0-9]+)/', $gedcom, $match)) { 188bb03c9f0SGreg Roach return $match[1]; 189bb03c9f0SGreg Roach } 190bb03c9f0SGreg Roach 191bb03c9f0SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM record: ' . $gedcom); 192bb03c9f0SGreg Roach } 193bb03c9f0SGreg Roach 194bb03c9f0SGreg Roach /** 195bb03c9f0SGreg Roach * Fetch GEDCOM data from the database. 196bb03c9f0SGreg Roach * 197bb03c9f0SGreg Roach * @param string $xref 198bb03c9f0SGreg Roach * @param Tree $tree 199bb03c9f0SGreg Roach * 200bb03c9f0SGreg Roach * @return string|null 201bb03c9f0SGreg Roach */ 202*1ff45046SGreg Roach private function gedcom(string $xref, Tree $tree): string|null 203bb03c9f0SGreg Roach { 204bb03c9f0SGreg Roach return DB::table('other') 205bb03c9f0SGreg Roach ->where('o_id', '=', $xref) 206bb03c9f0SGreg Roach ->where('o_file', '=', $tree->id()) 207bb03c9f0SGreg Roach ->whereNotIn('o_type', [ 208bb03c9f0SGreg Roach Header::RECORD_TYPE, 209bb03c9f0SGreg Roach Note::RECORD_TYPE, 210bb03c9f0SGreg Roach Repository::RECORD_TYPE, 211bb03c9f0SGreg Roach Submission::RECORD_TYPE, 212bb03c9f0SGreg Roach Submitter::RECORD_TYPE, 213bb03c9f0SGreg Roach ]) 214bb03c9f0SGreg Roach ->value('o_gedcom'); 215bb03c9f0SGreg Roach } 216bb03c9f0SGreg Roach} 217