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\Services; 21 22use Fisharebest\Webtrees\Gedcom; 23 24/** 25 * Utilities for manipulating GEDCOM data. 26 */ 27class GedcomService 28{ 29 // Some applications, such as FTM, use GEDCOM tag names instead of the tags. 30 private const array TAG_NAMES = [ 31 'ABBREVIATION' => 'ABBR', 32 'ADDRESS' => 'ADDR', 33 'ADDRESS1' => 'ADR1', 34 'ADDRESS2' => 'ADR2', 35 'ADDRESS3' => 'ADR3', 36 'ADOPTION' => 'ADOP', 37 'AGENCY' => 'AGNC', 38 'ALIAS' => 'ALIA', 39 'ANCESTORS' => 'ANCE', 40 'ANCES_INTEREST' => 'ANCI', 41 'ANULMENT' => 'ANUL', 42 'ASSOCIATES' => 'ASSO', 43 'AUTHOR' => 'AUTH', 44 'BAPTISM-LDS' => 'BAPL', 45 'BAPTISM' => 'BAPM', 46 'BAR_MITZVAH' => 'BARM', 47 'BAS_MITZVAH' => 'BASM', 48 'BIRTH' => 'BIRT', 49 'BLESSING' => 'BLES', 50 'BURIAL' => 'BURI', 51 'CALL_NUMBER' => 'CALN', 52 'CASTE' => 'CAST', 53 'CAUSE' => 'CAUS', 54 'CENSUS' => 'CENS', 55 'CHANGE' => 'CHAN', 56 'CHARACTER' => 'CHAR', 57 'CHILD' => 'CHIL', 58 'CHRISTENING' => 'CHR', 59 'ADULT_CHRISTENING' => 'CHRA', 60 'CONCATENATION' => 'CONC', 61 'CONFIRMATION' => 'CONF', 62 'CONFIRMATION-LDS' => 'CONL', 63 'CONTINUED' => 'CONT', 64 'COPYRIGHT' => 'COPY', 65 'CORPORTATE' => 'CORP', 66 'CREMATION' => 'CREM', 67 'COUNTRY' => 'CTRY', 68 'DEATH' => 'DEAT', 69 'DESCENDANTS' => 'DESC', 70 'DESCENDANTS_INT' => 'DESI', 71 'DESTINATION' => 'DEST', 72 'DIVORCE' => 'DIV', 73 'DIVORCE_FILED' => 'DIVF', 74 'PHY_DESCRIPTION' => 'DSCR', 75 'EDUCATION' => 'EDUC', 76 'EMIGRATION' => 'EMIG', 77 'ENDOWMENT' => 'ENDL', 78 'ENGAGEMENT' => 'ENGA', 79 'EVENT' => 'EVEN', 80 'FAMILY' => 'FAM', 81 'FAMILY_CHILD' => 'FAMC', 82 'FAMILY_FILE' => 'FAMF', 83 'FAMILY_SPOUSE' => 'FAMS', 84 'FACIMILIE' => 'FAX', 85 'FIRST_COMMUNION' => 'FCOM', 86 'FORMAT' => 'FORM', 87 'PHONETIC' => 'FONE', 88 'GEDCOM' => 'GEDC', 89 'GIVEN_NAME' => 'GIVN', 90 'GRADUATION' => 'GRAD', 91 'HEADER' => 'HEAD', 92 'HUSBAND' => 'HUSB', 93 'IDENT_NUMBER' => 'IDNO', 94 'IMMIGRATION' => 'IMMI', 95 'INDIVIDUAL' => 'INDI', 96 'LANGUAGE' => 'LANG', 97 'LATITUDE' => 'LATI', 98 'LONGITUDE' => 'LONG', 99 'MARRIAGE_BANN' => 'MARB', 100 'MARR_CONTRACT' => 'MARC', 101 'MARR_LICENSE' => 'MARL', 102 'MARRIAGE' => 'MARR', 103 'MEDIA' => 'MEDI', 104 'NATIONALITY' => 'NATI', 105 'NATURALIZATION' => 'NATU', 106 'CHILDREN_COUNT' => 'NCHI', 107 'NICKNAME' => 'NICK', 108 'MARRIAGE_COUNT' => 'NMR', 109 'NAME_PREFIX' => 'NPFX', 110 'NAME_SUFFIX' => 'NSFX', 111 'OBJECT' => 'OBJE', 112 'OCCUPATION' => 'OCCU', 113 'ORDINANCE' => 'ORDI', 114 'ORDINATION' => 'ORDN', 115 'PEDIGREE' => 'PEDI', 116 'PHONE' => 'PHON', 117 'PLACE' => 'PLAC', 118 'POSTAL_CODE' => 'POST', 119 'PROBATE' => 'PROB', 120 'PROPERTY' => 'PROP', 121 'PUBLICATION' => 'PUBL', 122 'QUALITY_OF_DATA' => 'QUAY', 123 'REFERENCE' => 'REFN', 124 'RELATIONSHIP' => 'RELA', 125 'RELIGION' => 'RELI', 126 'REPOSITORY' => 'REPO', 127 'RESIDENCE' => 'RESI', 128 'RESTRICTION' => 'RESN', 129 'RETIREMENT' => 'RETI', 130 'REC_FILE_NUMBER' => 'RFN', 131 'REC_ID_NUMBER' => 'RIN', 132 'ROMANIZED' => 'ROMN', 133 'SEALING_CHILD' => 'SLGC', 134 'SEALING_SPOUSE' => 'SLGS', 135 'SOURCE' => 'SOUR', 136 'SURN_PREFIX' => 'SPFX', 137 'SOC_SEC_NUMBER' => 'SSN', 138 'STATE' => 'STAE', 139 'STATUS' => 'STAT', 140 'SUBMITTER' => 'SUBM', 141 'SUBMISSION' => 'SUBN', 142 'SURNAME' => 'SURN', 143 'TEMPLE' => 'TEMP', 144 'TITLE' => 'TITL', 145 'TRAILER' => 'TRLR', 146 'VERSION' => 'VERS', 147 'WEB' => 'WWW', 148 '_DEATH_OF_SPOUSE' => 'DETS', 149 '_DEGREE' => '_DEG', 150 '_MEDICAL' => '_MCL', 151 '_MILITARY_SERVICE' => '_MILT', 152 ]; 153 154 // Custom GEDCOM tags used by other applications, with direct synonyms 155 private const array TAG_SYNONYMS = [ 156 // Convert PhpGedView tag to webtrees 157 '_PGVU' => '_WT_USER', 158 '_PGV_OBJS' => '_WT_OBJE_SORT', 159 ]; 160 161 /** 162 * Convert a GEDCOM tag to a canonical form. 163 * 164 * @param string $tag 165 * 166 * @return string 167 */ 168 public function canonicalTag(string $tag): string 169 { 170 $tag = strtoupper($tag); 171 172 $tag = self::TAG_NAMES[$tag] ?? self::TAG_SYNONYMS[$tag] ?? $tag; 173 174 return $tag; 175 } 176 177 public function readLatitude(string $text): float|null 178 { 179 return $this->readDegrees($text, Gedcom::LATITUDE_NORTH, Gedcom::LATITUDE_SOUTH); 180 } 181 182 public function readLongitude(string $text): float|null 183 { 184 return $this->readDegrees($text, Gedcom::LONGITUDE_EAST, Gedcom::LONGITUDE_WEST); 185 } 186 187 private function readDegrees(string $text, string $positive, string $negative): float|null 188 { 189 $text = trim($text); 190 $hemisphere = substr($text, 0, 1); 191 $degrees = substr($text, 1); 192 193 // Match a valid GEDCOM format 194 if (is_numeric($degrees)) { 195 $hemisphere = strtoupper($hemisphere); 196 $degrees = (float) $degrees; 197 198 if ($hemisphere === $positive) { 199 return $degrees; 200 } 201 202 if ($hemisphere === $negative) { 203 return -$degrees; 204 } 205 } 206 207 // Just a number? 208 if (is_numeric($text)) { 209 return (float) $text; 210 } 211 212 // Can't match anything. 213 return null; 214 } 215} 216