1bb03c9f0SGreg Roach<?php 2bb03c9f0SGreg Roach 3bb03c9f0SGreg Roach/** 4bb03c9f0SGreg Roach * webtrees: online genealogy 55bfc6897SGreg 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 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 * 49bb03c9f0SGreg Roach * @param string $xref 50bb03c9f0SGreg Roach * @param Tree $tree 51bb03c9f0SGreg Roach * @param string|null $gedcom 52bb03c9f0SGreg Roach * 53bb03c9f0SGreg Roach * @return GedcomRecord|null 54bb03c9f0SGreg Roach */ 55bb03c9f0SGreg Roach public function make(string $xref, Tree $tree, string $gedcom = null): ?GedcomRecord 56bb03c9f0SGreg Roach { 571dfec52bSGreg Roach // We know the type of the record. Return it directly. 581dfec52bSGreg Roach if ($gedcom !== null && preg_match('/^0(?: @[^@]+@)? ([A-Z_]+)/', $gedcom, $match)) { 591dfec52bSGreg Roach switch ($match[1]) { 601dfec52bSGreg Roach case Family::RECORD_TYPE: 616b9cb339SGreg Roach return Registry::familyFactory()->make($xref, $tree, $gedcom); 621dfec52bSGreg Roach case Header::RECORD_TYPE: 636b9cb339SGreg Roach return Registry::headerFactory()->make($xref, $tree, $gedcom); 641dfec52bSGreg Roach case Individual::RECORD_TYPE: 656b9cb339SGreg Roach return Registry::individualFactory()->make($xref, $tree, $gedcom); 661dfec52bSGreg Roach case Location::RECORD_TYPE: 676b9cb339SGreg Roach return Registry::locationFactory()->make($xref, $tree, $gedcom); 681dfec52bSGreg Roach case Media::RECORD_TYPE: 696b9cb339SGreg Roach return Registry::mediaFactory()->make($xref, $tree, $gedcom); 701dfec52bSGreg Roach case Note::RECORD_TYPE: 716b9cb339SGreg Roach return Registry::noteFactory()->make($xref, $tree, $gedcom); 721dfec52bSGreg Roach case Repository::RECORD_TYPE: 736b9cb339SGreg Roach return Registry::repositoryFactory()->make($xref, $tree, $gedcom); 741dfec52bSGreg Roach case Source::RECORD_TYPE: 756b9cb339SGreg Roach return Registry::sourceFactory()->make($xref, $tree, $gedcom); 761dfec52bSGreg Roach case Submitter::RECORD_TYPE: 776b9cb339SGreg Roach return Registry::submitterFactory()->make($xref, $tree, $gedcom); 781dfec52bSGreg Roach case Submission::RECORD_TYPE: 796b9cb339SGreg Roach return Registry::submissionFactory()->make($xref, $tree, $gedcom); 801dfec52bSGreg Roach } 811dfec52bSGreg Roach } 821dfec52bSGreg Roach 83bb03c9f0SGreg Roach // We do not know the type of the record. Try them all in turn. 84bb03c9f0SGreg Roach return 856b9cb339SGreg Roach Registry::familyFactory()->make($xref, $tree, $gedcom) ?? 866b9cb339SGreg Roach Registry::individualFactory()->make($xref, $tree, $gedcom) ?? 876b9cb339SGreg Roach Registry::mediaFactory()->make($xref, $tree, $gedcom) ?? 886b9cb339SGreg Roach Registry::noteFactory()->make($xref, $tree, $gedcom) ?? 896b9cb339SGreg Roach Registry::repositoryFactory()->make($xref, $tree, $gedcom) ?? 906b9cb339SGreg Roach Registry::sourceFactory()->make($xref, $tree, $gedcom) ?? 916b9cb339SGreg Roach Registry::submitterFactory()->make($xref, $tree, $gedcom) ?? 926b9cb339SGreg Roach Registry::submissionFactory()->make($xref, $tree, $gedcom) ?? 936b9cb339SGreg Roach Registry::locationFactory()->make($xref, $tree, $gedcom) ?? 946b9cb339SGreg Roach Registry::headerFactory()->make($xref, $tree, $gedcom) ?? 956b9cb339SGreg Roach Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 96bb03c9f0SGreg Roach $gedcom = $gedcom ?? $this->gedcom($xref, $tree); 97bb03c9f0SGreg Roach 98bb03c9f0SGreg Roach $pending = $this->pendingChanges($tree)->get($xref); 99bb03c9f0SGreg Roach 100bb03c9f0SGreg Roach if ($gedcom === null && $pending === null) { 101bb03c9f0SGreg Roach return null; 102bb03c9f0SGreg Roach } 103bb03c9f0SGreg Roach 104bb03c9f0SGreg Roach $xref = $this->extractXref($gedcom ?? $pending, $xref); 105bb03c9f0SGreg Roach $type = $this->extractType($gedcom ?? $pending); 106bb03c9f0SGreg Roach 107bb03c9f0SGreg Roach return $this->newGedcomRecord($type, $xref, $gedcom ?? '', $pending, $tree); 108bb03c9f0SGreg Roach }); 109bb03c9f0SGreg Roach } 110bb03c9f0SGreg Roach 111bb03c9f0SGreg Roach /** 112bb03c9f0SGreg Roach * Create a GedcomRecord object from raw GEDCOM data. 113bb03c9f0SGreg Roach * 114bb03c9f0SGreg Roach * @param string $xref 115bb03c9f0SGreg Roach * @param string $gedcom an empty string for new/pending records 116bb03c9f0SGreg Roach * @param string|null $pending null for a record with no pending edits, 117bb03c9f0SGreg Roach * empty string for records with pending deletions 118bb03c9f0SGreg Roach * @param Tree $tree 119bb03c9f0SGreg Roach * 120bb03c9f0SGreg Roach * @return GedcomRecord 121bb03c9f0SGreg Roach */ 122bb03c9f0SGreg Roach public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): GedcomRecord 123bb03c9f0SGreg Roach { 124bb03c9f0SGreg Roach return new GedcomRecord($xref, $gedcom, $pending, $tree); 125bb03c9f0SGreg Roach } 126bb03c9f0SGreg Roach 127bb03c9f0SGreg Roach /** 128bb03c9f0SGreg Roach * Create a GedcomRecord object from a row in the database. 129bb03c9f0SGreg Roach * 130bb03c9f0SGreg Roach * @param Tree $tree 131bb03c9f0SGreg Roach * 132bb03c9f0SGreg Roach * @return Closure 133bb03c9f0SGreg Roach */ 134bb03c9f0SGreg Roach public function mapper(Tree $tree): Closure 135bb03c9f0SGreg Roach { 136*743491b8SGreg Roach return fn (object $row): GedcomRecord => $this->make($row->o_id, $tree, $row->o_gedcom); 137bb03c9f0SGreg Roach } 138bb03c9f0SGreg Roach 139bb03c9f0SGreg Roach /** 140bb03c9f0SGreg Roach * @param string $type 141bb03c9f0SGreg Roach * @param string $xref 142bb03c9f0SGreg Roach * @param string $gedcom 143bb03c9f0SGreg Roach * @param string|null $pending 144bb03c9f0SGreg Roach * @param Tree $tree 145bb03c9f0SGreg Roach * 146bb03c9f0SGreg Roach * @return GedcomRecord 147bb03c9f0SGreg Roach */ 148bb03c9f0SGreg Roach private function newGedcomRecord(string $type, string $xref, string $gedcom, ?string $pending, Tree $tree): GedcomRecord 149bb03c9f0SGreg Roach { 150bb03c9f0SGreg Roach switch ($type) { 151bb03c9f0SGreg Roach case Family::RECORD_TYPE: 1526b9cb339SGreg Roach return Registry::familyFactory()->new($xref, $gedcom, $pending, $tree); 153bb03c9f0SGreg Roach 154bb03c9f0SGreg Roach case Header::RECORD_TYPE: 1556b9cb339SGreg Roach return Registry::headerFactory()->new($xref, $gedcom, $pending, $tree); 156bb03c9f0SGreg Roach 157bb03c9f0SGreg Roach case Individual::RECORD_TYPE: 1586b9cb339SGreg Roach return Registry::individualFactory()->new($xref, $gedcom, $pending, $tree); 159bb03c9f0SGreg Roach 160bb03c9f0SGreg Roach case Media::RECORD_TYPE: 1616b9cb339SGreg Roach return Registry::mediaFactory()->new($xref, $gedcom, $pending, $tree); 162bb03c9f0SGreg Roach 163bb03c9f0SGreg Roach case Note::RECORD_TYPE: 1646b9cb339SGreg Roach return Registry::noteFactory()->new($xref, $gedcom, $pending, $tree); 165bb03c9f0SGreg Roach 166bb03c9f0SGreg Roach case Repository::RECORD_TYPE: 1676b9cb339SGreg Roach return Registry::repositoryFactory()->new($xref, $gedcom, $pending, $tree); 168bb03c9f0SGreg Roach 169bb03c9f0SGreg Roach case Source::RECORD_TYPE: 1706b9cb339SGreg Roach return Registry::sourceFactory()->new($xref, $gedcom, $pending, $tree); 171bb03c9f0SGreg Roach 172bb03c9f0SGreg Roach case Submission::RECORD_TYPE: 1736b9cb339SGreg Roach return Registry::submissionFactory()->new($xref, $gedcom, $pending, $tree); 174bb03c9f0SGreg Roach 175bb03c9f0SGreg Roach case Submitter::RECORD_TYPE: 1766b9cb339SGreg Roach return Registry::submitterFactory()->new($xref, $gedcom, $pending, $tree); 177bb03c9f0SGreg Roach 178bb03c9f0SGreg Roach default: 179bb03c9f0SGreg Roach return $this->new($xref, $gedcom, $pending, $tree); 180bb03c9f0SGreg Roach } 181bb03c9f0SGreg Roach } 182bb03c9f0SGreg Roach 183bb03c9f0SGreg Roach /** 184bb03c9f0SGreg Roach * Extract the type of a GEDCOM record 185bb03c9f0SGreg Roach * 186bb03c9f0SGreg Roach * @param string $gedcom 187bb03c9f0SGreg Roach * 188bb03c9f0SGreg Roach * @return string 189bb03c9f0SGreg Roach * @throws InvalidArgumentException 190bb03c9f0SGreg Roach */ 191bb03c9f0SGreg Roach private function extractType(string $gedcom): string 192bb03c9f0SGreg Roach { 193bb03c9f0SGreg Roach if (preg_match('/^0(?: @' . Gedcom::REGEX_XREF . '@)? ([_A-Z0-9]+)/', $gedcom, $match)) { 194bb03c9f0SGreg Roach return $match[1]; 195bb03c9f0SGreg Roach } 196bb03c9f0SGreg Roach 197bb03c9f0SGreg Roach throw new InvalidArgumentException('Invalid GEDCOM record: ' . $gedcom); 198bb03c9f0SGreg Roach } 199bb03c9f0SGreg Roach 200bb03c9f0SGreg Roach /** 201bb03c9f0SGreg Roach * Fetch GEDCOM data from the database. 202bb03c9f0SGreg Roach * 203bb03c9f0SGreg Roach * @param string $xref 204bb03c9f0SGreg Roach * @param Tree $tree 205bb03c9f0SGreg Roach * 206bb03c9f0SGreg Roach * @return string|null 207bb03c9f0SGreg Roach */ 208bb03c9f0SGreg Roach private function gedcom(string $xref, Tree $tree): ?string 209bb03c9f0SGreg Roach { 210bb03c9f0SGreg Roach return DB::table('other') 211bb03c9f0SGreg Roach ->where('o_id', '=', $xref) 212bb03c9f0SGreg Roach ->where('o_file', '=', $tree->id()) 213bb03c9f0SGreg Roach ->whereNotIn('o_type', [ 214bb03c9f0SGreg Roach Header::RECORD_TYPE, 215bb03c9f0SGreg Roach Note::RECORD_TYPE, 216bb03c9f0SGreg Roach Repository::RECORD_TYPE, 217bb03c9f0SGreg Roach Submission::RECORD_TYPE, 218bb03c9f0SGreg Roach Submitter::RECORD_TYPE, 219bb03c9f0SGreg Roach ]) 220bb03c9f0SGreg Roach ->value('o_gedcom'); 221bb03c9f0SGreg Roach } 222bb03c9f0SGreg Roach} 223