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\RepositoryFactoryInterface; 246f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 26bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Repository; 27bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree; 28bb03c9f0SGreg Roach 29bb03c9f0SGreg Roachuse function assert; 30bb03c9f0SGreg Roachuse function preg_match; 31bb03c9f0SGreg Roach 32bb03c9f0SGreg Roach/** 33bb03c9f0SGreg Roach * Make a Repository object. 34bb03c9f0SGreg Roach */ 35bb03c9f0SGreg Roachclass RepositoryFactory extends AbstractGedcomRecordFactory implements RepositoryFactoryInterface 36bb03c9f0SGreg Roach{ 37bb03c9f0SGreg Roach private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Repository::RECORD_TYPE . '/'; 38bb03c9f0SGreg Roach 39bb03c9f0SGreg Roach /** 40bb03c9f0SGreg Roach * Create a repository. 41bb03c9f0SGreg Roach */ 42*1ff45046SGreg Roach public function make(string $xref, Tree $tree, string|null $gedcom = null): Repository|null 43bb03c9f0SGreg Roach { 449991924fSGreg Roach return Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 453529c469SGreg Roach $gedcom ??= $this->gedcom($xref, $tree); 46bb03c9f0SGreg Roach $pending = $this->pendingChanges($tree)->get($xref); 47bb03c9f0SGreg Roach 48bb03c9f0SGreg Roach if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) { 49bb03c9f0SGreg Roach return null; 50bb03c9f0SGreg Roach } 51bb03c9f0SGreg Roach 52bb03c9f0SGreg Roach $xref = $this->extractXref($gedcom ?? $pending, $xref); 53bb03c9f0SGreg Roach 54b27dac11SGreg Roach return $this->new($xref, $gedcom ?? '', $pending, $tree); 55bb03c9f0SGreg Roach }); 56bb03c9f0SGreg Roach } 57bb03c9f0SGreg Roach 58bb03c9f0SGreg Roach /** 59bb03c9f0SGreg Roach * Create a source from a row in the database. 60bb03c9f0SGreg Roach * 61bb03c9f0SGreg Roach * @param Tree $tree 62bb03c9f0SGreg Roach * 63c6921a17SGreg Roach * @return Closure(object):Repository 64bb03c9f0SGreg Roach */ 65bb03c9f0SGreg Roach public function mapper(Tree $tree): Closure 66bb03c9f0SGreg Roach { 67fdea1dfaSGreg Roach return function (object $row) use ($tree): Repository { 68bb03c9f0SGreg Roach $repository = $this->make($row->o_id, $tree, $row->o_gedcom); 69bb03c9f0SGreg Roach assert($repository instanceof Repository); 70bb03c9f0SGreg Roach 71bb03c9f0SGreg Roach return $repository; 72bb03c9f0SGreg Roach }; 73bb03c9f0SGreg Roach } 74bb03c9f0SGreg Roach 75bb03c9f0SGreg Roach /** 76bb03c9f0SGreg Roach * Create a source from raw GEDCOM data. 77bb03c9f0SGreg Roach * 78bb03c9f0SGreg Roach * @param string $xref 79bb03c9f0SGreg Roach * @param string $gedcom an empty string for new/pending records 80bb03c9f0SGreg Roach * @param string|null $pending null for a record with no pending edits, 81bb03c9f0SGreg Roach * empty string for records with pending deletions 82bb03c9f0SGreg Roach * @param Tree $tree 83bb03c9f0SGreg Roach * 84bb03c9f0SGreg Roach * @return Repository 85bb03c9f0SGreg Roach */ 86*1ff45046SGreg Roach public function new(string $xref, string $gedcom, string|null $pending, Tree $tree): Repository 87bb03c9f0SGreg Roach { 88bb03c9f0SGreg Roach return new Repository($xref, $gedcom, $pending, $tree); 89bb03c9f0SGreg Roach } 90bb03c9f0SGreg Roach 91bb03c9f0SGreg Roach /** 92bb03c9f0SGreg Roach * Fetch GEDCOM data from the database. 93bb03c9f0SGreg Roach * 94bb03c9f0SGreg Roach * @param string $xref 95bb03c9f0SGreg Roach * @param Tree $tree 96bb03c9f0SGreg Roach * 97bb03c9f0SGreg Roach * @return string|null 98bb03c9f0SGreg Roach */ 99*1ff45046SGreg Roach protected function gedcom(string $xref, Tree $tree): string|null 100bb03c9f0SGreg Roach { 101bb03c9f0SGreg Roach return DB::table('other') 102bb03c9f0SGreg Roach ->where('o_id', '=', $xref) 103bb03c9f0SGreg Roach ->where('o_file', '=', $tree->id()) 104bb03c9f0SGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE) 105bb03c9f0SGreg Roach ->value('o_gedcom'); 106bb03c9f0SGreg Roach } 107bb03c9f0SGreg Roach} 108