1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Factories; 21 22use Closure; 23use Fisharebest\Webtrees\Contracts\FamilyFactoryInterface; 24use Fisharebest\Webtrees\Family; 25use Fisharebest\Webtrees\Gedcom; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29 30use function assert; 31use function preg_match; 32 33/** 34 * Make a Family object. 35 */ 36class FamilyFactory extends AbstractGedcomRecordFactory implements FamilyFactoryInterface 37{ 38 private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Family::RECORD_TYPE . '/'; 39 40 /** 41 * Create a family. 42 * 43 * @param string $xref 44 * @param Tree $tree 45 * @param string|null $gedcom 46 * 47 * @return Family|null 48 */ 49 public function make(string $xref, Tree $tree, string $gedcom = null): ?Family 50 { 51 return Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 52 $gedcom = $gedcom ?? $this->gedcom($xref, $tree); 53 $pending = $this->pendingChanges($tree)->get($xref); 54 55 if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) { 56 return null; 57 } 58 59 $xref = $this->extractXref($gedcom ?? $pending, $xref); 60 61 // Preload all the family members using a single database query. 62 preg_match_all('/\n1 (?:HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $gedcom . "\n" . $pending, $match); 63 DB::table('individuals') 64 ->where('i_file', '=', $tree->id()) 65 ->whereIn('i_id', $match[1]) 66 ->get() 67 ->map(Registry::individualFactory()->mapper($tree)); 68 69 return $this->new($xref, $gedcom ?? '', $pending, $tree); 70 }); 71 } 72 73 /** 74 * Create a Family object from a row in the database. 75 * 76 * @param Tree $tree 77 * 78 * @return Closure 79 */ 80 public function mapper(Tree $tree): Closure 81 { 82 return function (object $row) use ($tree): Family { 83 $family = $this->make($row->f_id, $tree, $row->f_gedcom); 84 assert($family instanceof Family); 85 86 return $family; 87 }; 88 } 89 90 /** 91 * Create a Family object from raw GEDCOM data. 92 * 93 * @param string $xref 94 * @param string $gedcom an empty string for new/pending records 95 * @param string|null $pending null for a record with no pending edits, 96 * empty string for records with pending deletions 97 * @param Tree $tree 98 * 99 * @return Family 100 */ 101 public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Family 102 { 103 return new Family($xref, $gedcom, $pending, $tree); 104 } 105 106 /** 107 * Fetch GEDCOM data from the database. 108 * 109 * @param string $xref 110 * @param Tree $tree 111 * 112 * @return string|null 113 */ 114 protected function gedcom(string $xref, Tree $tree): ?string 115 { 116 return DB::table('families') 117 ->where('f_id', '=', $xref) 118 ->where('f_file', '=', $tree->id()) 119 ->value('f_gedcom'); 120 } 121} 122