1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Statistics\Repository; 21 22use Fisharebest\Webtrees\Carbon; 23use Fisharebest\Webtrees\Fact; 24use Fisharebest\Webtrees\Factory; 25use Fisharebest\Webtrees\Header; 26use Fisharebest\Webtrees\Statistics\Repository\Interfaces\GedcomRepositoryInterface; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29 30use function str_contains; 31 32/** 33 * A repository providing methods for GEDCOM related statistics. 34 */ 35class GedcomRepository implements GedcomRepositoryInterface 36{ 37 /** 38 * @var Tree 39 */ 40 private $tree; 41 42 /** 43 * Constructor. 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 string[] 56 */ 57 private function gedcomHead(): array 58 { 59 $title = ''; 60 $version = ''; 61 $source = ''; 62 63 $head = Factory::header()->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 = Factory::header()->make('HEAD', $this->tree); 143 144 if ($head instanceof Header) { 145 $fact = $head->facts(['DATE'])->first(); 146 147 if ($fact instanceof Fact) { 148 return Carbon::make($fact->value())->local()->isoFormat('LL'); 149 } 150 } 151 152 return ''; 153 } 154 155 /** 156 * @return string 157 */ 158 public function gedcomUpdated(): string 159 { 160 $row = DB::table('change') 161 ->where('gedcom_id', '=', $this->tree->id()) 162 ->where('status', '=', 'accepted') 163 ->orderBy('change_id', 'DESC') 164 ->select(['change_time']) 165 ->first(); 166 167 if ($row === null) { 168 return $this->gedcomDate(); 169 } 170 171 return Carbon::make($row->change_time)->local()->isoFormat('LL'); 172 } 173 174 /** 175 * @return string 176 */ 177 public function gedcomRootId(): string 178 { 179 return $this->tree->getPreference('PEDIGREE_ROOT_ID'); 180 } 181} 182