xref: /webtrees/app/Services/GedcomService.php (revision e96f1efade579b645d976e8dddc6f00169bfc582)
18dded141SGreg Roach<?php
23976b470SGreg Roach
38dded141SGreg Roach/**
48dded141SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 webtrees development team
68dded141SGreg Roach * This program is free software: you can redistribute it and/or modify
78dded141SGreg Roach * it under the terms of the GNU General Public License as published by
88dded141SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98dded141SGreg Roach * (at your option) any later version.
108dded141SGreg Roach * This program is distributed in the hope that it will be useful,
118dded141SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128dded141SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138dded141SGreg Roach * GNU General Public License for more details.
148dded141SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168dded141SGreg Roach */
17fcfa147eSGreg Roach
188dded141SGreg Roachdeclare(strict_types=1);
198dded141SGreg Roach
208dded141SGreg Roachnamespace Fisharebest\Webtrees\Services;
218dded141SGreg Roach
2290949315SGreg Roachuse Fisharebest\Webtrees\Gedcom;
2390949315SGreg Roach
248dded141SGreg Roach/**
258dded141SGreg Roach * Utilities for manipulating GEDCOM data.
268dded141SGreg Roach */
278dded141SGreg Roachclass GedcomService
288dded141SGreg Roach{
298dded141SGreg Roach    // Some applications, such as FTM, use GEDCOM tag names instead of the tags.
3016d6367aSGreg Roach    private const TAG_NAMES = [
318dded141SGreg Roach        'ABBREVIATION'      => 'ABBR',
328dded141SGreg Roach        'ADDRESS'           => 'ADDR',
338dded141SGreg Roach        'ADDRESS1'          => 'ADR1',
348dded141SGreg Roach        'ADDRESS2'          => 'ADR2',
35*e96f1efaSGreg Roach        'ADDRESS3'          => 'ADR3',
368dded141SGreg Roach        'ADOPTION'          => 'ADOP',
378dded141SGreg Roach        'AGENCY'            => 'AGNC',
388dded141SGreg Roach        'ALIAS'             => 'ALIA',
398dded141SGreg Roach        'ANCESTORS'         => 'ANCE',
408dded141SGreg Roach        'ANCES_INTEREST'    => 'ANCI',
418dded141SGreg Roach        'ANULMENT'          => 'ANUL',
428dded141SGreg Roach        'ASSOCIATES'        => 'ASSO',
438dded141SGreg Roach        'AUTHOR'            => 'AUTH',
448dded141SGreg Roach        'BAPTISM-LDS'       => 'BAPL',
458dded141SGreg Roach        'BAPTISM'           => 'BAPM',
468dded141SGreg Roach        'BAR_MITZVAH'       => 'BARM',
478dded141SGreg Roach        'BAS_MITZVAH'       => 'BASM',
488dded141SGreg Roach        'BIRTH'             => 'BIRT',
498dded141SGreg Roach        'BLESSING'          => 'BLES',
508dded141SGreg Roach        'BURIAL'            => 'BURI',
518dded141SGreg Roach        'CALL_NUMBER'       => 'CALN',
528dded141SGreg Roach        'CASTE'             => 'CAST',
538dded141SGreg Roach        'CAUSE'             => 'CAUS',
548dded141SGreg Roach        'CENSUS'            => 'CENS',
558dded141SGreg Roach        'CHANGE'            => 'CHAN',
568dded141SGreg Roach        'CHARACTER'         => 'CHAR',
578dded141SGreg Roach        'CHILD'             => 'CHIL',
588dded141SGreg Roach        'CHRISTENING'       => 'CHR',
598dded141SGreg Roach        'ADULT_CHRISTENING' => 'CHRA',
608dded141SGreg Roach        'CONCATENATION'     => 'CONC',
618dded141SGreg Roach        'CONFIRMATION'      => 'CONF',
628dded141SGreg Roach        'CONFIRMATION-LDS'  => 'CONL',
638dded141SGreg Roach        'CONTINUED'         => 'CONT',
648dded141SGreg Roach        'COPYRIGHT'         => 'COPY',
658dded141SGreg Roach        'CORPORTATE'        => 'CORP',
668dded141SGreg Roach        'CREMATION'         => 'CREM',
678dded141SGreg Roach        'COUNTRY'           => 'CTRY',
688dded141SGreg Roach        'DEATH'             => 'DEAT',
698dded141SGreg Roach        'DESCENDANTS'       => 'DESC',
708dded141SGreg Roach        'DESCENDANTS_INT'   => 'DESI',
718dded141SGreg Roach        'DESTINATION'       => 'DEST',
728dded141SGreg Roach        'DIVORCE'           => 'DIV',
738dded141SGreg Roach        'DIVORCE_FILED'     => 'DIVF',
748dded141SGreg Roach        'PHY_DESCRIPTION'   => 'DSCR',
758dded141SGreg Roach        'EDUCATION'         => 'EDUC',
768dded141SGreg Roach        'EMIGRATION'        => 'EMIG',
778dded141SGreg Roach        'ENDOWMENT'         => 'ENDL',
788dded141SGreg Roach        'ENGAGEMENT'        => 'ENGA',
798dded141SGreg Roach        'EVENT'             => 'EVEN',
808dded141SGreg Roach        'FAMILY'            => 'FAM',
818dded141SGreg Roach        'FAMILY_CHILD'      => 'FAMC',
828dded141SGreg Roach        'FAMILY_FILE'       => 'FAMF',
838dded141SGreg Roach        'FAMILY_SPOUSE'     => 'FAMS',
848dded141SGreg Roach        'FACIMILIE'         => 'FAX',
858dded141SGreg Roach        'FIRST_COMMUNION'   => 'FCOM',
868dded141SGreg Roach        'FORMAT'            => 'FORM',
878dded141SGreg Roach        'PHONETIC'          => 'FONE',
888dded141SGreg Roach        'GEDCOM'            => 'GEDC',
898dded141SGreg Roach        'GIVEN_NAME'        => 'GIVN',
908dded141SGreg Roach        'GRADUATION'        => 'GRAD',
918dded141SGreg Roach        'HEADER'            => 'HEAD',
928dded141SGreg Roach        'HUSBAND'           => 'HUSB',
938dded141SGreg Roach        'IDENT_NUMBER'      => 'IDNO',
948dded141SGreg Roach        'IMMIGRATION'       => 'IMMI',
958dded141SGreg Roach        'INDIVIDUAL'        => 'INDI',
968dded141SGreg Roach        'LANGUAGE'          => 'LANG',
978dded141SGreg Roach        'LATITUDE'          => 'LATI',
988dded141SGreg Roach        'LONGITUDE'         => 'LONG',
998dded141SGreg Roach        'MARRIAGE_BANN'     => 'MARB',
1008dded141SGreg Roach        'MARR_CONTRACT'     => 'MARC',
1018dded141SGreg Roach        'MARR_LICENSE'      => 'MARL',
1028dded141SGreg Roach        'MARRIAGE'          => 'MARR',
1038dded141SGreg Roach        'MEDIA'             => 'MEDI',
1048dded141SGreg Roach        'NATIONALITY'       => 'NATI',
1058dded141SGreg Roach        'NATURALIZATION'    => 'NATU',
1068dded141SGreg Roach        'CHILDREN_COUNT'    => 'NCHI',
1078dded141SGreg Roach        'NICKNAME'          => 'NICK',
1088dded141SGreg Roach        'MARRIAGE_COUNT'    => 'NMR',
1098dded141SGreg Roach        'NAME_PREFIX'       => 'NPFX',
1108dded141SGreg Roach        'NAME_SUFFIX'       => 'NSFX',
1118dded141SGreg Roach        'OBJECT'            => 'OBJE',
1128dded141SGreg Roach        'OCCUPATION'        => 'OCCU',
1138dded141SGreg Roach        'ORDINANCE'         => 'ORDI',
1148dded141SGreg Roach        'ORDINATION'        => 'ORDN',
1158dded141SGreg Roach        'PEDIGREE'          => 'PEDI',
1168dded141SGreg Roach        'PHONE'             => 'PHON',
1178dded141SGreg Roach        'PLACE'             => 'PLAC',
1188dded141SGreg Roach        'POSTAL_CODE'       => 'POST',
1198dded141SGreg Roach        'PROBATE'           => 'PROB',
1208dded141SGreg Roach        'PROPERTY'          => 'PROP',
121c1afbf58SGreg Roach        'PUBLICATION'       => 'PUBL',
1228dded141SGreg Roach        'QUALITY_OF_DATA'   => 'QUAY',
1238dded141SGreg Roach        'REFERENCE'         => 'REFN',
1248dded141SGreg Roach        'RELATIONSHIP'      => 'RELA',
1258dded141SGreg Roach        'RELIGION'          => 'RELI',
1268dded141SGreg Roach        'REPOSITORY'        => 'REPO',
1278dded141SGreg Roach        'RESIDENCE'         => 'RESI',
1288dded141SGreg Roach        'RESTRICTION'       => 'RESN',
1298dded141SGreg Roach        'RETIREMENT'        => 'RETI',
1308dded141SGreg Roach        'REC_FILE_NUMBER'   => 'RFN',
1318dded141SGreg Roach        'REC_ID_NUMBER'     => 'RIN',
1328dded141SGreg Roach        'ROMANIZED'         => 'ROMN',
1338dded141SGreg Roach        'SEALING_CHILD'     => 'SLGC',
1348dded141SGreg Roach        'SEALING_SPOUSE'    => 'SLGS',
1358dded141SGreg Roach        'SOURCE'            => 'SOUR',
1368dded141SGreg Roach        'SURN_PREFIX'       => 'SPFX',
1378dded141SGreg Roach        'SOC_SEC_NUMBER'    => 'SSN',
1388dded141SGreg Roach        'STATE'             => 'STAE',
1398dded141SGreg Roach        'STATUS'            => 'STAT',
1408dded141SGreg Roach        'SUBMITTER'         => 'SUBM',
1418dded141SGreg Roach        'SUBMISSION'        => 'SUBN',
1428dded141SGreg Roach        'SURNAME'           => 'SURN',
1438dded141SGreg Roach        'TEMPLE'            => 'TEMP',
1448dded141SGreg Roach        'TITLE'             => 'TITL',
1458dded141SGreg Roach        'TRAILER'           => 'TRLR',
1468dded141SGreg Roach        'VERSION'           => 'VERS',
1478dded141SGreg Roach        'WEB'               => 'WWW',
1488dded141SGreg Roach        '_DEATH_OF_SPOUSE'  => 'DETS',
1498dded141SGreg Roach        '_DEGREE'           => '_DEG',
1508dded141SGreg Roach        '_MEDICAL'          => '_MCL',
1518dded141SGreg Roach        '_MILITARY_SERVICE' => '_MILT',
1528dded141SGreg Roach    ];
1538dded141SGreg Roach
154efd4768bSGreg Roach    // Custom GEDCOM tags used by other applications, with direct synonyms
15516d6367aSGreg Roach    private const TAG_SYNONYMS = [
156b6cc30bcSGreg Roach        // Convert PhpGedView tag to webtrees
157b6cc30bcSGreg Roach        '_PGVU'     => '_WT_USER',
158b6cc30bcSGreg Roach        '_PGV_OBJS' => '_WT_OBJE_SORT',
1598dded141SGreg Roach    ];
1608dded141SGreg Roach
1618dded141SGreg Roach    /**
1628dded141SGreg Roach     * Convert a GEDCOM tag to a canonical form.
1638dded141SGreg Roach     *
1648dded141SGreg Roach     * @param string $tag
1658dded141SGreg Roach     *
1668dded141SGreg Roach     * @return string
1678dded141SGreg Roach     */
1688dded141SGreg Roach    public function canonicalTag(string $tag): string
1698dded141SGreg Roach    {
1708dded141SGreg Roach        $tag = strtoupper($tag);
1718dded141SGreg Roach
172c70c3c8cSGreg Roach        $tag = self::TAG_NAMES[$tag] ?? self::TAG_SYNONYMS[$tag] ?? $tag;
1738dded141SGreg Roach
1748dded141SGreg Roach        return $tag;
1758dded141SGreg Roach    }
1768dded141SGreg Roach
1778dded141SGreg Roach    /**
1788dded141SGreg Roach     * @param string $text
1798dded141SGreg Roach     *
18090949315SGreg Roach     * @return float|null
1818dded141SGreg Roach     */
18290949315SGreg Roach    public function readLatitude(string $text): ?float
1838dded141SGreg Roach    {
18490949315SGreg Roach        return $this->readDegrees($text, Gedcom::LATITUDE_NORTH, Gedcom::LATITUDE_SOUTH);
1858dded141SGreg Roach    }
1868dded141SGreg Roach
1878dded141SGreg Roach    /**
1888dded141SGreg Roach     * @param string $text
1898dded141SGreg Roach     *
19090949315SGreg Roach     * @return float|null
1918dded141SGreg Roach     */
19290949315SGreg Roach    public function readLongitude(string $text): ?float
1938dded141SGreg Roach    {
19490949315SGreg Roach        return $this->readDegrees($text, Gedcom::LONGITUDE_EAST, Gedcom::LONGITUDE_WEST);
1958dded141SGreg Roach    }
1968dded141SGreg Roach
1978dded141SGreg Roach    /**
1988dded141SGreg Roach     * @param string $text
1998dded141SGreg Roach     * @param string $positive
2008dded141SGreg Roach     * @param string $negative
2018dded141SGreg Roach     *
20290949315SGreg Roach     * @return float|null
2038dded141SGreg Roach     */
20490949315SGreg Roach    private function readDegrees(string $text, string $positive, string $negative): ?float
2058dded141SGreg Roach    {
2068dded141SGreg Roach        $text       = trim($text);
2078dded141SGreg Roach        $hemisphere = substr($text, 0, 1);
2088dded141SGreg Roach        $degrees    = substr($text, 1);
2098dded141SGreg Roach
2108dded141SGreg Roach        // Match a valid GEDCOM format
2118dded141SGreg Roach        if (is_numeric($degrees)) {
2128dded141SGreg Roach            $hemisphere = strtoupper($hemisphere);
2138dded141SGreg Roach            $degrees    = (float) $degrees;
2148dded141SGreg Roach
2158dded141SGreg Roach            if ($hemisphere === $positive) {
2168dded141SGreg Roach                return $degrees;
2178dded141SGreg Roach            }
2188dded141SGreg Roach
2198dded141SGreg Roach            if ($hemisphere === $negative) {
2208dded141SGreg Roach                return -$degrees;
2218dded141SGreg Roach            }
2228dded141SGreg Roach        }
2238dded141SGreg Roach
2248dded141SGreg Roach        // Just a number?
2258dded141SGreg Roach        if (is_numeric($text)) {
2268dded141SGreg Roach            return (float) $text;
2278dded141SGreg Roach        }
2288dded141SGreg Roach
2298dded141SGreg Roach        // Can't match anything.
23090949315SGreg Roach        return null;
2318dded141SGreg Roach    }
2328dded141SGreg Roach}
233