1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Statistics\Repository; 21 22use Exception; 23use Fisharebest\Webtrees\DB; 24use Fisharebest\Webtrees\Fact; 25use Fisharebest\Webtrees\Header; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Statistics\Repository\Interfaces\GedcomRepositoryInterface; 28use Fisharebest\Webtrees\Tree; 29 30use InvalidArgumentException; 31 32use function e; 33use function str_contains; 34use function strpos; 35use function substr; 36 37/** 38 * A repository providing methods for GEDCOM related statistics. 39 */ 40class GedcomRepository implements GedcomRepositoryInterface 41{ 42 private Tree $tree; 43 44 /** 45 * @param Tree $tree 46 */ 47 public function __construct(Tree $tree) 48 { 49 $this->tree = $tree; 50 } 51 52 /** 53 * Get information from the GEDCOM's HEAD record. 54 * 55 * @return array<string> 56 */ 57 private function gedcomHead(): array 58 { 59 $title = ''; 60 $version = ''; 61 $source = ''; 62 63 $head = Registry::headerFactory()->make('HEAD', $this->tree); 64 65 if ($head instanceof Header) { 66 $sour = $head->facts(['SOUR'])->first(); 67 68 if ($sour instanceof Fact) { 69 $source = $sour->value(); 70 $title = $sour->attribute('NAME'); 71 $version = $sour->attribute('VERS'); 72 } 73 } 74 75 return [ 76 $title, 77 $version, 78 $source, 79 ]; 80 } 81 82 /** 83 * @return string 84 */ 85 public function gedcomFilename(): string 86 { 87 return $this->tree->name(); 88 } 89 90 /** 91 * @return int 92 */ 93 public function gedcomId(): int 94 { 95 return $this->tree->id(); 96 } 97 98 /** 99 * @return string 100 */ 101 public function gedcomTitle(): string 102 { 103 return e($this->tree->title()); 104 } 105 106 /** 107 * @return string 108 */ 109 public function gedcomCreatedSoftware(): string 110 { 111 return $this->gedcomHead()[0]; 112 } 113 114 /** 115 * @return string 116 */ 117 public function gedcomCreatedVersion(): string 118 { 119 $head = $this->gedcomHead(); 120 121 // Fix broken version string in Family Tree Maker 122 if (str_contains($head[1], 'Family Tree Maker ')) { 123 $p = strpos($head[1], '(') + 1; 124 $p2 = strpos($head[1], ')'); 125 $head[1] = substr($head[1], $p, $p2 - $p); 126 } 127 128 // Fix EasyTree version 129 if ($head[2] === 'EasyTree') { 130 $head[1] = substr($head[1], 1); 131 } 132 133 return $head[1]; 134 } 135 136 /** 137 * @return string 138 * @throws Exception 139 */ 140 public function gedcomDate(): string 141 { 142 $head = Registry::headerFactory()->make('HEAD', $this->tree); 143 144 if ($head instanceof Header) { 145 $fact = $head->facts(['DATE'])->first(); 146 147 if ($fact instanceof Fact) { 148 try { 149 return Registry::timestampFactory()->fromString($fact->value(), 'j M Y')->isoFormat('LL'); 150 } catch (InvalidArgumentException) { 151 // HEAD:DATE invalid. 152 } 153 } 154 } 155 156 return ''; 157 } 158 159 /** 160 * @return string 161 */ 162 public function gedcomUpdated(): string 163 { 164 $row = DB::table('change') 165 ->where('gedcom_id', '=', $this->tree->id()) 166 ->where('status', '=', 'accepted') 167 ->orderBy('change_id', 'DESC') 168 ->select(['change_time']) 169 ->first(); 170 171 if ($row === null) { 172 return $this->gedcomDate(); 173 } 174 175 return Registry::timestampFactory()->fromString($row->change_time)->isoFormat('LL'); 176 } 177 178 /** 179 * @return string 180 */ 181 public function gedcomRootId(): string 182 { 183 return $this->tree->getPreference('PEDIGREE_ROOT_ID'); 184 } 185} 186