xref: /webtrees/app/Services/GedcomService.php (revision efd4768b0eab1f325771cdbc6181ff84f85f2149)
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    // User defined tags begin with an underscore
3016d6367aSGreg Roach    private const USER_DEFINED_TAG_PREFIX = '_';
318dded141SGreg Roach
328dded141SGreg Roach    // Some applications, such as FTM, use GEDCOM tag names instead of the tags.
3316d6367aSGreg Roach    private const TAG_NAMES = [
348dded141SGreg Roach        'ABBREVIATION'      => 'ABBR',
358dded141SGreg Roach        'ADDRESS'           => 'ADDR',
368dded141SGreg Roach        'ADDRESS1'          => 'ADR1',
378dded141SGreg Roach        'ADDRESS2'          => 'ADR2',
388dded141SGreg Roach        'ADOPTION'          => 'ADOP',
398dded141SGreg Roach        'AGENCY'            => 'AGNC',
408dded141SGreg Roach        'ALIAS'             => 'ALIA',
418dded141SGreg Roach        'ANCESTORS'         => 'ANCE',
428dded141SGreg Roach        'ANCES_INTEREST'    => 'ANCI',
438dded141SGreg Roach        'ANULMENT'          => 'ANUL',
448dded141SGreg Roach        'ASSOCIATES'        => 'ASSO',
458dded141SGreg Roach        'AUTHOR'            => 'AUTH',
468dded141SGreg Roach        'BAPTISM-LDS'       => 'BAPL',
478dded141SGreg Roach        'BAPTISM'           => 'BAPM',
488dded141SGreg Roach        'BAR_MITZVAH'       => 'BARM',
498dded141SGreg Roach        'BAS_MITZVAH'       => 'BASM',
508dded141SGreg Roach        'BIRTH'             => 'BIRT',
518dded141SGreg Roach        'BLESSING'          => 'BLES',
528dded141SGreg Roach        'BURIAL'            => 'BURI',
538dded141SGreg Roach        'CALL_NUMBER'       => 'CALN',
548dded141SGreg Roach        'CASTE'             => 'CAST',
558dded141SGreg Roach        'CAUSE'             => 'CAUS',
568dded141SGreg Roach        'CENSUS'            => 'CENS',
578dded141SGreg Roach        'CHANGE'            => 'CHAN',
588dded141SGreg Roach        'CHARACTER'         => 'CHAR',
598dded141SGreg Roach        'CHILD'             => 'CHIL',
608dded141SGreg Roach        'CHRISTENING'       => 'CHR',
618dded141SGreg Roach        'ADULT_CHRISTENING' => 'CHRA',
628dded141SGreg Roach        'CONCATENATION'     => 'CONC',
638dded141SGreg Roach        'CONFIRMATION'      => 'CONF',
648dded141SGreg Roach        'CONFIRMATION-LDS'  => 'CONL',
658dded141SGreg Roach        'CONTINUED'         => 'CONT',
668dded141SGreg Roach        'COPYRIGHT'         => 'COPY',
678dded141SGreg Roach        'CORPORTATE'        => 'CORP',
688dded141SGreg Roach        'CREMATION'         => 'CREM',
698dded141SGreg Roach        'COUNTRY'           => 'CTRY',
708dded141SGreg Roach        'DEATH'             => 'DEAT',
718dded141SGreg Roach        'DESCENDANTS'       => 'DESC',
728dded141SGreg Roach        'DESCENDANTS_INT'   => 'DESI',
738dded141SGreg Roach        'DESTINATION'       => 'DEST',
748dded141SGreg Roach        'DIVORCE'           => 'DIV',
758dded141SGreg Roach        'DIVORCE_FILED'     => 'DIVF',
768dded141SGreg Roach        'PHY_DESCRIPTION'   => 'DSCR',
778dded141SGreg Roach        'EDUCATION'         => 'EDUC',
788dded141SGreg Roach        'EMIGRATION'        => 'EMIG',
798dded141SGreg Roach        'ENDOWMENT'         => 'ENDL',
808dded141SGreg Roach        'ENGAGEMENT'        => 'ENGA',
818dded141SGreg Roach        'EVENT'             => 'EVEN',
828dded141SGreg Roach        'FAMILY'            => 'FAM',
838dded141SGreg Roach        'FAMILY_CHILD'      => 'FAMC',
848dded141SGreg Roach        'FAMILY_FILE'       => 'FAMF',
858dded141SGreg Roach        'FAMILY_SPOUSE'     => 'FAMS',
868dded141SGreg Roach        'FACIMILIE'         => 'FAX',
878dded141SGreg Roach        'FIRST_COMMUNION'   => 'FCOM',
888dded141SGreg Roach        'FORMAT'            => 'FORM',
898dded141SGreg Roach        'PHONETIC'          => 'FONE',
908dded141SGreg Roach        'GEDCOM'            => 'GEDC',
918dded141SGreg Roach        'GIVEN_NAME'        => 'GIVN',
928dded141SGreg Roach        'GRADUATION'        => 'GRAD',
938dded141SGreg Roach        'HEADER'            => 'HEAD',
948dded141SGreg Roach        'HUSBAND'           => 'HUSB',
958dded141SGreg Roach        'IDENT_NUMBER'      => 'IDNO',
968dded141SGreg Roach        'IMMIGRATION'       => 'IMMI',
978dded141SGreg Roach        'INDIVIDUAL'        => 'INDI',
988dded141SGreg Roach        'LANGUAGE'          => 'LANG',
998dded141SGreg Roach        'LATITUDE'          => 'LATI',
1008dded141SGreg Roach        'LONGITUDE'         => 'LONG',
1018dded141SGreg Roach        'MARRIAGE_BANN'     => 'MARB',
1028dded141SGreg Roach        'MARR_CONTRACT'     => 'MARC',
1038dded141SGreg Roach        'MARR_LICENSE'      => 'MARL',
1048dded141SGreg Roach        'MARRIAGE'          => 'MARR',
1058dded141SGreg Roach        'MEDIA'             => 'MEDI',
1068dded141SGreg Roach        'NATIONALITY'       => 'NATI',
1078dded141SGreg Roach        'NATURALIZATION'    => 'NATU',
1088dded141SGreg Roach        'CHILDREN_COUNT'    => 'NCHI',
1098dded141SGreg Roach        'NICKNAME'          => 'NICK',
1108dded141SGreg Roach        'MARRIAGE_COUNT'    => 'NMR',
1118dded141SGreg Roach        'NAME_PREFIX'       => 'NPFX',
1128dded141SGreg Roach        'NAME_SUFFIX'       => 'NSFX',
1138dded141SGreg Roach        'OBJECT'            => 'OBJE',
1148dded141SGreg Roach        'OCCUPATION'        => 'OCCU',
1158dded141SGreg Roach        'ORDINANCE'         => 'ORDI',
1168dded141SGreg Roach        'ORDINATION'        => 'ORDN',
1178dded141SGreg Roach        'PEDIGREE'          => 'PEDI',
1188dded141SGreg Roach        'PHONE'             => 'PHON',
1198dded141SGreg Roach        'PLACE'             => 'PLAC',
1208dded141SGreg Roach        'POSTAL_CODE'       => 'POST',
1218dded141SGreg Roach        'PROBATE'           => 'PROB',
1228dded141SGreg Roach        'PROPERTY'          => 'PROP',
123c1afbf58SGreg Roach        'PUBLICATION'       => 'PUBL',
1248dded141SGreg Roach        'QUALITY_OF_DATA'   => 'QUAY',
1258dded141SGreg Roach        'REFERENCE'         => 'REFN',
1268dded141SGreg Roach        'RELATIONSHIP'      => 'RELA',
1278dded141SGreg Roach        'RELIGION'          => 'RELI',
1288dded141SGreg Roach        'REPOSITORY'        => 'REPO',
1298dded141SGreg Roach        'RESIDENCE'         => 'RESI',
1308dded141SGreg Roach        'RESTRICTION'       => 'RESN',
1318dded141SGreg Roach        'RETIREMENT'        => 'RETI',
1328dded141SGreg Roach        'REC_FILE_NUMBER'   => 'RFN',
1338dded141SGreg Roach        'REC_ID_NUMBER'     => 'RIN',
1348dded141SGreg Roach        'ROMANIZED'         => 'ROMN',
1358dded141SGreg Roach        'SEALING_CHILD'     => 'SLGC',
1368dded141SGreg Roach        'SEALING_SPOUSE'    => 'SLGS',
1378dded141SGreg Roach        'SOURCE'            => 'SOUR',
1388dded141SGreg Roach        'SURN_PREFIX'       => 'SPFX',
1398dded141SGreg Roach        'SOC_SEC_NUMBER'    => 'SSN',
1408dded141SGreg Roach        'STATE'             => 'STAE',
1418dded141SGreg Roach        'STATUS'            => 'STAT',
1428dded141SGreg Roach        'SUBMITTER'         => 'SUBM',
1438dded141SGreg Roach        'SUBMISSION'        => 'SUBN',
1448dded141SGreg Roach        'SURNAME'           => 'SURN',
1458dded141SGreg Roach        'TEMPLE'            => 'TEMP',
1468dded141SGreg Roach        'TITLE'             => 'TITL',
1478dded141SGreg Roach        'TRAILER'           => 'TRLR',
1488dded141SGreg Roach        'VERSION'           => 'VERS',
1498dded141SGreg Roach        'WEB'               => 'WWW',
1508dded141SGreg Roach        '_DEATH_OF_SPOUSE'  => 'DETS',
1518dded141SGreg Roach        '_DEGREE'           => '_DEG',
1528dded141SGreg Roach        '_MEDICAL'          => '_MCL',
1538dded141SGreg Roach        '_MILITARY_SERVICE' => '_MILT',
1548dded141SGreg Roach    ];
1558dded141SGreg Roach
156*efd4768bSGreg Roach    // Custom GEDCOM tags used by other applications, with direct synonyms
15716d6367aSGreg Roach    private const TAG_SYNONYMS = [
158b6cc30bcSGreg Roach        // Convert PhpGedView tag to webtrees
159b6cc30bcSGreg Roach        '_PGVU'     => '_WT_USER',
160b6cc30bcSGreg Roach        '_PGV_OBJS' => '_WT_OBJE_SORT',
1618dded141SGreg Roach    ];
1628dded141SGreg Roach
1638dded141SGreg Roach    // SEX tags
16416d6367aSGreg Roach    private const SEX_FEMALE  = 'F';
16516d6367aSGreg Roach    private const SEX_MALE    = 'M';
16616d6367aSGreg Roach    private const SEX_UNKNOWN = 'U';
1678dded141SGreg Roach
1688dded141SGreg Roach    /**
1698dded141SGreg Roach     * Convert a GEDCOM tag to a canonical form.
1708dded141SGreg Roach     *
1718dded141SGreg Roach     * @param string $tag
1728dded141SGreg Roach     *
1738dded141SGreg Roach     * @return string
1748dded141SGreg Roach     */
1758dded141SGreg Roach    public function canonicalTag(string $tag): string
1768dded141SGreg Roach    {
1778dded141SGreg Roach        $tag = strtoupper($tag);
1788dded141SGreg Roach
179c70c3c8cSGreg Roach        $tag = self::TAG_NAMES[$tag] ?? self::TAG_SYNONYMS[$tag] ?? $tag;
1808dded141SGreg Roach
1818dded141SGreg Roach        return $tag;
1828dded141SGreg Roach    }
1838dded141SGreg Roach
1848dded141SGreg Roach    /**
1858dded141SGreg Roach     * @param string $tag
1868dded141SGreg Roach     *
1878dded141SGreg Roach     * @return bool
1888dded141SGreg Roach     */
1898dded141SGreg Roach    public function isUserDefinedTag(string $tag): bool
1908dded141SGreg Roach    {
1914d798ef2SGreg Roach        return substr_compare($tag, self::USER_DEFINED_TAG_PREFIX, 0, 1) === 0;
1928dded141SGreg Roach    }
1938dded141SGreg Roach
1948dded141SGreg Roach    /**
1958dded141SGreg Roach     * @param string $text
1968dded141SGreg Roach     *
19790949315SGreg Roach     * @return float|null
1988dded141SGreg Roach     */
19990949315SGreg Roach    public function readLatitude(string $text): ?float
2008dded141SGreg Roach    {
20190949315SGreg Roach        return $this->readDegrees($text, Gedcom::LATITUDE_NORTH, Gedcom::LATITUDE_SOUTH);
2028dded141SGreg Roach    }
2038dded141SGreg Roach
2048dded141SGreg Roach    /**
2058dded141SGreg Roach     * @param string $text
2068dded141SGreg Roach     *
20790949315SGreg Roach     * @return float|null
2088dded141SGreg Roach     */
20990949315SGreg Roach    public function readLongitude(string $text): ?float
2108dded141SGreg Roach    {
21190949315SGreg Roach        return $this->readDegrees($text, Gedcom::LONGITUDE_EAST, Gedcom::LONGITUDE_WEST);
2128dded141SGreg Roach    }
2138dded141SGreg Roach
2148dded141SGreg Roach    /**
2158dded141SGreg Roach     * @param string $text
2168dded141SGreg Roach     * @param string $positive
2178dded141SGreg Roach     * @param string $negative
2188dded141SGreg Roach     *
21990949315SGreg Roach     * @return float|null
2208dded141SGreg Roach     */
22190949315SGreg Roach    private function readDegrees(string $text, string $positive, string $negative): ?float
2228dded141SGreg Roach    {
2238dded141SGreg Roach        $text       = trim($text);
2248dded141SGreg Roach        $hemisphere = substr($text, 0, 1);
2258dded141SGreg Roach        $degrees    = substr($text, 1);
2268dded141SGreg Roach
2278dded141SGreg Roach        // Match a valid GEDCOM format
2288dded141SGreg Roach        if (is_numeric($degrees)) {
2298dded141SGreg Roach            $hemisphere = strtoupper($hemisphere);
2308dded141SGreg Roach            $degrees    = (float) $degrees;
2318dded141SGreg Roach
2328dded141SGreg Roach            if ($hemisphere === $positive) {
2338dded141SGreg Roach                return $degrees;
2348dded141SGreg Roach            }
2358dded141SGreg Roach
2368dded141SGreg Roach            if ($hemisphere === $negative) {
2378dded141SGreg Roach                return -$degrees;
2388dded141SGreg Roach            }
2398dded141SGreg Roach        }
2408dded141SGreg Roach
2418dded141SGreg Roach        // Just a number?
2428dded141SGreg Roach        if (is_numeric($text)) {
2438dded141SGreg Roach            return (float) $text;
2448dded141SGreg Roach        }
2458dded141SGreg Roach
2468dded141SGreg Roach        // Can't match anything.
24790949315SGreg Roach        return null;
2488dded141SGreg Roach    }
2498dded141SGreg Roach
2508dded141SGreg Roach    /**
2518dded141SGreg Roach     * Although empty placenames are valid "Town, , Country", it is only meaningful
2528dded141SGreg Roach     * when structured places are used (PLAC:FORM town, county, country), and
2538dded141SGreg Roach     * structured places are discouraged.
2548dded141SGreg Roach     *
2558dded141SGreg Roach     * @param string $text
2568dded141SGreg Roach     *
25724f2a3afSGreg Roach     * @return array<string>
2588dded141SGreg Roach     */
2598dded141SGreg Roach    public function readPlace(string $text): array
2608dded141SGreg Roach    {
2618dded141SGreg Roach        $text = trim($text);
2628dded141SGreg Roach
26390949315SGreg Roach        return preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $text);
2648dded141SGreg Roach    }
2658dded141SGreg Roach
2668dded141SGreg Roach    /**
2678dded141SGreg Roach     * @param string[] $place
2688dded141SGreg Roach     *
2698dded141SGreg Roach     * @return string
2708dded141SGreg Roach     */
2718dded141SGreg Roach    public function writePlace(array $place): string
2728dded141SGreg Roach    {
27390949315SGreg Roach        return implode(Gedcom::PLACE_SEPARATOR, $place);
2748dded141SGreg Roach    }
2758dded141SGreg Roach
2768dded141SGreg Roach    /**
2778dded141SGreg Roach     * Some applications use non-standard values for unknown.
2788dded141SGreg Roach     *
2798dded141SGreg Roach     * @param string $text
2808dded141SGreg Roach     *
2818dded141SGreg Roach     * @return string
2828dded141SGreg Roach     */
2838dded141SGreg Roach    public function readSex(string $text): string
2848dded141SGreg Roach    {
2858dded141SGreg Roach        $text = strtoupper($text);
2868dded141SGreg Roach
2878dded141SGreg Roach        if ($text !== self::SEX_MALE && $text !== self::SEX_FEMALE) {
2888dded141SGreg Roach            $text = self::SEX_UNKNOWN;
2898dded141SGreg Roach        }
2908dded141SGreg Roach
2918dded141SGreg Roach        return $text;
2928dded141SGreg Roach    }
2938dded141SGreg Roach}
294