1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 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 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 /** 178 * @param string $text 179 * 180 * @return float|null 181 */ 182 public function readLatitude(string $text): ?float 183 { 184 return $this->readDegrees($text, Gedcom::LATITUDE_NORTH, Gedcom::LATITUDE_SOUTH); 185 } 186 187 /** 188 * @param string $text 189 * 190 * @return float|null 191 */ 192 public function readLongitude(string $text): ?float 193 { 194 return $this->readDegrees($text, Gedcom::LONGITUDE_EAST, Gedcom::LONGITUDE_WEST); 195 } 196 197 /** 198 * @param string $text 199 * @param string $positive 200 * @param string $negative 201 * 202 * @return float|null 203 */ 204 private function readDegrees(string $text, string $positive, string $negative): ?float 205 { 206 $text = trim($text); 207 $hemisphere = substr($text, 0, 1); 208 $degrees = substr($text, 1); 209 210 // Match a valid GEDCOM format 211 if (is_numeric($degrees)) { 212 $hemisphere = strtoupper($hemisphere); 213 $degrees = (float) $degrees; 214 215 if ($hemisphere === $positive) { 216 return $degrees; 217 } 218 219 if ($hemisphere === $negative) { 220 return -$degrees; 221 } 222 } 223 224 // Just a number? 225 if (is_numeric($text)) { 226 return (float) $text; 227 } 228 229 // Can't match anything. 230 return null; 231 } 232} 233