xref: /webtrees/app/Http/RequestHandlers/CheckTree.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg 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/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
23e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\AbstractXrefElement;
2416ecfcafSGreg Roachuse Fisharebest\Webtrees\Elements\MultimediaFileReference;
2571166947SGreg Roachuse Fisharebest\Webtrees\Elements\MultimediaFormat;
26e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\SubmitterText;
27e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
28e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefFamily;
29e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefIndividual;
30e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefLocation;
31e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefMedia;
32e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefNote;
33e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefRepository;
34e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefSource;
35e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefSubmission;
36e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefSubmitter;
37e669bb4bSGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory;
3871166947SGreg Roachuse Fisharebest\Webtrees\Factories\ImageFactory;
39e669bb4bSGreg Roachuse Fisharebest\Webtrees\Family;
406fd01894SGreg Roachuse Fisharebest\Webtrees\Gedcom;
416fd01894SGreg Roachuse Fisharebest\Webtrees\Header;
426fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
436fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
44e669bb4bSGreg Roachuse Fisharebest\Webtrees\Individual;
45e669bb4bSGreg Roachuse Fisharebest\Webtrees\Location;
46e669bb4bSGreg Roachuse Fisharebest\Webtrees\Media;
4771166947SGreg Roachuse Fisharebest\Webtrees\Mime;
48e669bb4bSGreg Roachuse Fisharebest\Webtrees\Note;
49e669bb4bSGreg Roachuse Fisharebest\Webtrees\Repository;
50e669bb4bSGreg Roachuse Fisharebest\Webtrees\Services\TimeoutService;
51e669bb4bSGreg Roachuse Fisharebest\Webtrees\Source;
52e669bb4bSGreg Roachuse Fisharebest\Webtrees\Submission;
53e669bb4bSGreg Roachuse Fisharebest\Webtrees\Submitter;
546fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
55b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
566fd01894SGreg Roachuse Illuminate\Database\Query\Expression;
576fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
586fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
596fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
606fd01894SGreg Roach
6165f30ad2SGreg Roachuse function array_key_exists;
62e669bb4bSGreg Roachuse function array_slice;
636fd01894SGreg Roachuse function e;
64e669bb4bSGreg Roachuse function implode;
656fd01894SGreg Roachuse function preg_match;
666fd01894SGreg Roachuse function route;
671259996fSGreg Roachuse function str_contains;
68e669bb4bSGreg Roachuse function str_starts_with;
696fd01894SGreg Roachuse function strtoupper;
70e669bb4bSGreg Roachuse function substr_count;
716fd01894SGreg Roach
726fd01894SGreg Roach/**
736fd01894SGreg Roach * Check a tree for errors.
746fd01894SGreg Roach */
756fd01894SGreg Roachclass CheckTree implements RequestHandlerInterface
766fd01894SGreg Roach{
776fd01894SGreg Roach    use ViewResponseTrait;
786fd01894SGreg Roach
79e669bb4bSGreg Roach    private Gedcom $gedcom;
80e669bb4bSGreg Roach
81e669bb4bSGreg Roach    private TimeoutService $timeout_service;
82e669bb4bSGreg Roach
83e669bb4bSGreg Roach    /**
84e669bb4bSGreg Roach     * @param Gedcom         $gedcom
85e669bb4bSGreg Roach     * @param TimeoutService $timeout_service
86e669bb4bSGreg Roach     */
87e669bb4bSGreg Roach    public function __construct(Gedcom $gedcom, TimeoutService $timeout_service)
88e669bb4bSGreg Roach    {
89e669bb4bSGreg Roach        $this->gedcom          = $gedcom;
90e669bb4bSGreg Roach        $this->timeout_service = $timeout_service;
91e669bb4bSGreg Roach    }
92e669bb4bSGreg Roach
936fd01894SGreg Roach    /**
946fd01894SGreg Roach     * @param ServerRequestInterface $request
956fd01894SGreg Roach     *
966fd01894SGreg Roach     * @return ResponseInterface
976fd01894SGreg Roach     */
986fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
996fd01894SGreg Roach    {
1006fd01894SGreg Roach        $this->layout = 'layouts/administration';
1016fd01894SGreg Roach
102b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
103e669bb4bSGreg Roach        $skip_to = Validator::queryParams($request)->string('skip_to', '');
1046fd01894SGreg Roach
1056fd01894SGreg Roach        // We need to work with raw GEDCOM data, as we are looking for errors
1066fd01894SGreg Roach        // which may prevent the GedcomRecord objects from working.
1076fd01894SGreg Roach
1086fd01894SGreg Roach        $q1 = DB::table('individuals')
1096fd01894SGreg Roach            ->where('i_file', '=', $tree->id())
1106fd01894SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom', new Expression("'INDI' AS type")]);
1116fd01894SGreg Roach        $q2 = DB::table('families')
1126fd01894SGreg Roach            ->where('f_file', '=', $tree->id())
1136fd01894SGreg Roach            ->select(['f_id AS xref', 'f_gedcom AS gedcom', new Expression("'FAM' AS type")]);
1146fd01894SGreg Roach        $q3 = DB::table('media')
1156fd01894SGreg Roach            ->where('m_file', '=', $tree->id())
1166fd01894SGreg Roach            ->select(['m_id AS xref', 'm_gedcom AS gedcom', new Expression("'OBJE' AS type")]);
1176fd01894SGreg Roach        $q4 = DB::table('sources')
1186fd01894SGreg Roach            ->where('s_file', '=', $tree->id())
1196fd01894SGreg Roach            ->select(['s_id AS xref', 's_gedcom AS gedcom', new Expression("'SOUR' AS type")]);
1206fd01894SGreg Roach        $q5 = DB::table('other')
1216fd01894SGreg Roach            ->where('o_file', '=', $tree->id())
1226fd01894SGreg Roach            ->select(['o_id AS xref', 'o_gedcom AS gedcom', 'o_type']);
1236fd01894SGreg Roach        $q6 = DB::table('change')
1246fd01894SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1256fd01894SGreg Roach            ->where('status', '=', 'pending')
1266fd01894SGreg Roach            ->orderBy('change_id')
1276fd01894SGreg Roach            ->select(['xref', 'new_gedcom AS gedcom', new Expression("'' AS type")]);
1286fd01894SGreg Roach
1296fd01894SGreg Roach        $rows = $q1
1306fd01894SGreg Roach            ->unionAll($q2)
1316fd01894SGreg Roach            ->unionAll($q3)
1326fd01894SGreg Roach            ->unionAll($q4)
1336fd01894SGreg Roach            ->unionAll($q5)
1346fd01894SGreg Roach            ->unionAll($q6)
1356fd01894SGreg Roach            ->get()
136f70bcff5SGreg Roach            ->map(static function (object $row): object {
1376fd01894SGreg Roach                // Extract type for pending record
138a59ab093SGreg Roach                if ($row->type === '' && str_starts_with($row->gedcom, '0 HEAD')) {
139a59ab093SGreg Roach                    $row->type = 'HEAD';
140a59ab093SGreg Roach                }
141a59ab093SGreg Roach
142a59ab093SGreg Roach                if ($row->type === '' && preg_match('/^0 @[^@]*@ ([_A-Z0-9]+)/', $row->gedcom, $match) === 1) {
1436fd01894SGreg Roach                    $row->type = $match[1];
1446fd01894SGreg Roach                }
1456fd01894SGreg Roach
1466fd01894SGreg Roach                return $row;
1476fd01894SGreg Roach            });
1486fd01894SGreg Roach
1496fd01894SGreg Roach        $records = [];
150e669bb4bSGreg Roach        $xrefs   = [];
1516fd01894SGreg Roach
1526fd01894SGreg Roach        foreach ($rows as $row) {
1536fd01894SGreg Roach            if ($row->gedcom !== '') {
1546fd01894SGreg Roach                // existing or updated record
1556fd01894SGreg Roach                $records[$row->xref] = $row;
1566fd01894SGreg Roach            } else {
1576fd01894SGreg Roach                // deleted record
1586fd01894SGreg Roach                unset($records[$row->xref]);
1596fd01894SGreg Roach            }
160e669bb4bSGreg Roach
161e669bb4bSGreg Roach            $xrefs[strtoupper($row->xref)] = $row->xref;
1626fd01894SGreg Roach        }
1636fd01894SGreg Roach
164e669bb4bSGreg Roach        unset($rows);
1656fd01894SGreg Roach
1666fd01894SGreg Roach        $errors   = [];
1676fd01894SGreg Roach        $warnings = [];
16812224cf3SGreg Roach        $infos    = [];
1696fd01894SGreg Roach
170e669bb4bSGreg Roach        $element_factory = new ElementFactory();
171e669bb4bSGreg Roach        $this->gedcom->registerTags($element_factory, false);
172e669bb4bSGreg Roach
1736fd01894SGreg Roach        foreach ($records as $record) {
174e669bb4bSGreg Roach            // If we are nearly out of time, then stop processing here
175e669bb4bSGreg Roach            if ($skip_to === $record->xref) {
176e669bb4bSGreg Roach                $skip_to = '';
177e669bb4bSGreg Roach            } elseif ($skip_to !== '') {
178e669bb4bSGreg Roach                continue;
179e669bb4bSGreg Roach            } elseif ($this->timeout_service->isTimeNearlyUp()) {
180e669bb4bSGreg Roach                $skip_to = $record->xref;
181e669bb4bSGreg Roach                break;
1826fd01894SGreg Roach            }
1836fd01894SGreg Roach
184e669bb4bSGreg Roach            $lines = explode("\n", $record->gedcom);
185e669bb4bSGreg Roach            array_shift($lines);
1866fd01894SGreg Roach
187e669bb4bSGreg Roach            $last_level = 0;
188e669bb4bSGreg Roach            $hierarchy  = [$record->type];
1896fd01894SGreg Roach
190e669bb4bSGreg Roach            foreach ($lines as $line_number => $line) {
191e669bb4bSGreg Roach                if (preg_match('/^(\d+) (\w+) ?(.*)/', $line, $match) !== 1) {
19244f4e696SGreg Roach                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM record.'), '');
193e669bb4bSGreg Roach                    break;
194e669bb4bSGreg Roach                }
195e669bb4bSGreg Roach
196e669bb4bSGreg Roach                $level = (int) $match[1];
197e669bb4bSGreg Roach                if ($level > $last_level + 1) {
19844f4e696SGreg Roach                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM level number.'), '');
199e669bb4bSGreg Roach                    break;
200e669bb4bSGreg Roach                }
201e669bb4bSGreg Roach
202e669bb4bSGreg Roach                $tag               = $match[2];
203e669bb4bSGreg Roach                $value             = $match[3];
204e669bb4bSGreg Roach                $hierarchy[$level] = $tag;
205e669bb4bSGreg Roach                $full_tag          = implode(':', array_slice($hierarchy, 0, 1 + $level));
2062eea773eSGreg Roach                $element           = $element_factory->make($full_tag);
207e669bb4bSGreg Roach                $last_level        = $level;
208e669bb4bSGreg Roach
209e669bb4bSGreg Roach                if ($tag === 'CONT') {
210e669bb4bSGreg Roach                    $element = new SubmitterText('CONT');
211e669bb4bSGreg Roach                }
212e669bb4bSGreg Roach
213e669bb4bSGreg Roach                if ($element instanceof UnknownElement) {
2141259996fSGreg Roach                    if (str_starts_with($tag, '_') || str_starts_with($full_tag, '_') || str_contains($full_tag, ':_')) {
215e669bb4bSGreg Roach                        $message    = I18N::translate('Custom GEDCOM tags are discouraged. Try to use only standard GEDCOM tags.');
21644f4e696SGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag);
2176fd01894SGreg Roach                    } else {
218e669bb4bSGreg Roach                        $message  = I18N::translate('Invalid GEDCOM tag.') . ' ' . $full_tag;
21944f4e696SGreg Roach                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag);
2206fd01894SGreg Roach                    }
221e669bb4bSGreg Roach                } elseif ($element instanceof AbstractXrefElement) {
222e669bb4bSGreg Roach                    if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $value, $match) === 1) {
223e669bb4bSGreg Roach                        $xref1  = $match[1];
224e669bb4bSGreg Roach                        $xref2  = $xrefs[strtoupper($xref1)] ?? null;
225e669bb4bSGreg Roach                        $linked = $records[$xref2] ?? null;
226e669bb4bSGreg Roach
227e669bb4bSGreg Roach                        if ($linked === null) {
228e669bb4bSGreg Roach                            $message  = I18N::translate('%s does not exist.', e($xref1));
2298993d39aSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $tag . '-' . $xref1);
230e669bb4bSGreg Roach                        } elseif ($element instanceof XrefFamily && $linked->type !== Family::RECORD_TYPE) {
231e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Family::RECORD_TYPE);
23244f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
233e669bb4bSGreg Roach                        } elseif ($element instanceof XrefIndividual && $linked->type !== Individual::RECORD_TYPE) {
234e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Individual::RECORD_TYPE);
23544f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
236e669bb4bSGreg Roach                        } elseif ($element instanceof XrefMedia && $linked->type !== Media::RECORD_TYPE) {
237e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Media::RECORD_TYPE);
23844f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
239e669bb4bSGreg Roach                        } elseif ($element instanceof XrefNote && $linked->type !== Note::RECORD_TYPE) {
240e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Note::RECORD_TYPE);
24144f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
242e669bb4bSGreg Roach                        } elseif ($element instanceof XrefSource && $linked->type !== Source::RECORD_TYPE) {
243e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Source::RECORD_TYPE);
24444f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
245e669bb4bSGreg Roach                        } elseif ($element instanceof XrefRepository && $linked->type !== Repository::RECORD_TYPE) {
246e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Repository::RECORD_TYPE);
24744f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
248e669bb4bSGreg Roach                        } elseif ($element instanceof XrefSubmitter && $linked->type !== Submitter::RECORD_TYPE) {
249e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submitter::RECORD_TYPE);
25044f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
251e669bb4bSGreg Roach                        } elseif ($element instanceof XrefSubmission && $linked->type !== Submission::RECORD_TYPE) {
252e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submission::RECORD_TYPE);
25344f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
254e669bb4bSGreg Roach                        } elseif ($element instanceof XrefLocation && $linked->type !== Location::RECORD_TYPE) {
255e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Location::RECORD_TYPE);
25644f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
257e669bb4bSGreg Roach                        } elseif (($full_tag === 'FAM:HUSB' || $full_tag === 'FAM:WIFE') && !str_contains($linked->gedcom, "\n1 FAMS @" . $record->xref . '@')) {
258e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
259e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
260e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
26144f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-FAMS');
262e669bb4bSGreg Roach                        } elseif ($full_tag === 'FAM:CHIL' && !str_contains($linked->gedcom, "\n1 FAMC @" . $record->xref . '@')) {
263e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
264e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
265e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
26644f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-FAMC');
267e669bb4bSGreg Roach                        } elseif ($full_tag === 'INDI:FAMC' && !str_contains($linked->gedcom, "\n1 CHIL @" . $record->xref . '@')) {
268e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
269e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
270e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
27144f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-CHIL');
272e669bb4bSGreg Roach                        } elseif ($full_tag === 'INDI:FAMS' && !str_contains($linked->gedcom, "\n1 HUSB @" . $record->xref . '@') && !str_contains($linked->gedcom, "\n1 WIFE @" . $record->xref . '@')) {
273e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
274e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
275e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
27644f4e696SGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-HUSB-WIFE');
277e669bb4bSGreg Roach                        } elseif ($xref1 !== $xref2) {
278e669bb4bSGreg Roach                            $message    = I18N::translate('%1$s does not exist. Did you mean %2$s?', e($xref1), e($xref2));
2798993d39aSGreg Roach                            $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $tag . '-' . $xref1);
280e669bb4bSGreg Roach                        }
281e669bb4bSGreg Roach                    } elseif ($tag === 'SOUR') {
282e669bb4bSGreg Roach                        $message    = I18N::translate('Inline-source records are discouraged.');
28344f4e696SGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-inline');
284e669bb4bSGreg Roach                    } else {
285877a2084SGreg Roach                        $message  = I18N::translate('Invalid GEDCOM value.');
2864b2988b9SGreg Roach                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-value-' . e($value));
287e669bb4bSGreg Roach                    }
288e669bb4bSGreg Roach                } elseif ($element->canonical($value) !== $value) {
289e669bb4bSGreg Roach                    $expected = e($element->canonical($value));
290e669bb4bSGreg Roach                    $actual   = strtr(e($value), ["\t" => '&rarr;']);
291e669bb4bSGreg Roach                    $message  = I18N::translate('“%1$s” should be “%2$s”.', $actual, $expected);
292aab1cd2dSGreg Roach                    if (strtoupper($element->canonical($value)) !== strtoupper($value)) {
293aab1cd2dSGreg Roach                        // This will be relevant for GEDCOM 7.0.  It's not relevant now, and causes confusion.
29444f4e696SGreg Roach                        $infos[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-value');
295aab1cd2dSGreg Roach                    }
29671166947SGreg Roach                } elseif ($element instanceof MultimediaFormat) {
29771166947SGreg Roach                    $mime = Mime::TYPES[$value] ?? Mime::DEFAULT_TYPE;
29871166947SGreg Roach
29971166947SGreg Roach                    if ($mime === Mime::DEFAULT_TYPE) {
30071166947SGreg Roach                        $message    = I18N::translate('webtrees does not recognise this file format.');
3014b2988b9SGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-' . e($value));
30265f30ad2SGreg Roach                    } elseif (str_starts_with($mime, 'image/') && !array_key_exists($mime, ImageFactory::SUPPORTED_FORMATS)) {
30371166947SGreg Roach                        $message    = I18N::translate('webtrees cannot create thumbnails for this file format.');
3044b2988b9SGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-' . e($value));
30571166947SGreg Roach                    }
30616ecfcafSGreg Roach                } elseif ($element instanceof MultimediaFileReference && $value === 'gedcom.ged') {
30716ecfcafSGreg Roach                    $message  = I18N::translate('This filename is not compatible with the GEDZIP file format.');
3084b2988b9SGreg Roach                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '_' . e($value));
309e669bb4bSGreg Roach                }
310e669bb4bSGreg Roach            }
311e669bb4bSGreg Roach
312e669bb4bSGreg Roach            if ($record->type === Family::RECORD_TYPE) {
313e669bb4bSGreg Roach                if (substr_count($record->gedcom, "\n1 HUSB @") > 1) {
314e669bb4bSGreg Roach                    $message  = I18N::translate('%s occurs too many times.', 'FAM:HUSB');
315c393fec4SGreg Roach                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message, 'FAM:HUSB-count');
316e669bb4bSGreg Roach                }
317e669bb4bSGreg Roach                if (substr_count($record->gedcom, "\n1 WIFE @") > 1) {
318e669bb4bSGreg Roach                    $message  = I18N::translate('%s occurs too many times.', 'FAM:WIFE');
319c393fec4SGreg Roach                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message, 'FAM:WIFE-count');
3206fd01894SGreg Roach                }
3216fd01894SGreg Roach            }
3226fd01894SGreg Roach        }
3236fd01894SGreg Roach
3246fd01894SGreg Roach        $title = I18N::translate('Check for errors') . ' — ' . e($tree->title());
3256fd01894SGreg Roach
326e669bb4bSGreg Roach        if ($skip_to === '') {
327e669bb4bSGreg Roach            $more_url = '';
328e669bb4bSGreg Roach        } else {
329e669bb4bSGreg Roach            $more_url = route(self::class, ['tree' => $tree->name(), 'skip_to' => $skip_to]);
330e669bb4bSGreg Roach        }
331e669bb4bSGreg Roach
3326fd01894SGreg Roach        return $this->viewResponse('admin/trees-check', [
3336fd01894SGreg Roach            'errors'   => $errors,
33412224cf3SGreg Roach            'infos'    => $infos,
335e669bb4bSGreg Roach            'more_url' => $more_url,
3366fd01894SGreg Roach            'title'    => $title,
3376fd01894SGreg Roach            'tree'     => $tree,
3386fd01894SGreg Roach            'warnings' => $warnings,
3396fd01894SGreg Roach        ]);
3406fd01894SGreg Roach    }
3416fd01894SGreg Roach
3426fd01894SGreg Roach    /**
3436fd01894SGreg Roach     * @param string $type
3446fd01894SGreg Roach     *
3456fd01894SGreg Roach     * @return string
3466fd01894SGreg Roach     */
347e669bb4bSGreg Roach    private function recordType(string $type): string
3486fd01894SGreg Roach    {
349e669bb4bSGreg Roach        $types = [
350e669bb4bSGreg Roach            Family::RECORD_TYPE     => I18N::translate('Family'),
351e669bb4bSGreg Roach            Header::RECORD_TYPE     => I18N::translate('Header'),
352e669bb4bSGreg Roach            Individual::RECORD_TYPE => I18N::translate('Individual'),
353e669bb4bSGreg Roach            Location::RECORD_TYPE   => I18N::translate('Location'),
354e669bb4bSGreg Roach            Media::RECORD_TYPE      => I18N::translate('Media object'),
355e669bb4bSGreg Roach            Note::RECORD_TYPE       => I18N::translate('Note'),
356e669bb4bSGreg Roach            Repository::RECORD_TYPE => I18N::translate('Repository'),
357e669bb4bSGreg Roach            Source::RECORD_TYPE     => I18N::translate('Source'),
358e669bb4bSGreg Roach            Submission::RECORD_TYPE => I18N::translate('Submission'),
359e669bb4bSGreg Roach            Submitter::RECORD_TYPE  => I18N::translate('Submitter'),
360e669bb4bSGreg Roach        ];
361e669bb4bSGreg Roach
362e669bb4bSGreg Roach        return $types[$type] ?? e($type);
363e669bb4bSGreg Roach    }
364e669bb4bSGreg Roach
365e669bb4bSGreg Roach    /**
366e669bb4bSGreg Roach     * @param Tree   $tree
367e669bb4bSGreg Roach     * @param string $xref
368e669bb4bSGreg Roach     *
369e669bb4bSGreg Roach     * @return string
370e669bb4bSGreg Roach     */
371e669bb4bSGreg Roach    private function recordLink(Tree $tree, string $xref): string
372e669bb4bSGreg Roach    {
373e669bb4bSGreg Roach        $url = route(GedcomRecordPage::class, ['xref' => $xref, 'tree' => $tree->name()]);
374e669bb4bSGreg Roach
375e669bb4bSGreg Roach        return '<a href="' . e($url) . '">' . e($xref) . '</a>';
3766fd01894SGreg Roach    }
3776fd01894SGreg Roach
3786fd01894SGreg Roach    /**
379e669bb4bSGreg Roach     * @param Tree   $tree
380e669bb4bSGreg Roach     * @param string $xref
381e669bb4bSGreg Roach     * @param string $type1
382e669bb4bSGreg Roach     * @param string $type2
383e669bb4bSGreg Roach     *
384e669bb4bSGreg Roach     * @return string
385e669bb4bSGreg Roach     */
386e669bb4bSGreg Roach    private function linkErrorMessage(Tree $tree, string $xref, string $type1, string $type2): string
387e669bb4bSGreg Roach    {
388e669bb4bSGreg Roach        $link  = $this->recordLink($tree, $xref);
389e669bb4bSGreg Roach        $type1 = $this->recordType($type1);
390e669bb4bSGreg Roach        $type2 = $this->recordType($type2);
391e669bb4bSGreg Roach
392e669bb4bSGreg Roach        return I18N::translate('%1$s is a %2$s but a %3$s is expected.', $link, $type1, $type2);
3936fd01894SGreg Roach    }
39444f4e696SGreg Roach
39544f4e696SGreg Roach    /**
39644f4e696SGreg Roach     * Format a link to a record.
39744f4e696SGreg Roach     *
39844f4e696SGreg Roach     * @param Tree   $tree
39944f4e696SGreg Roach     * @param string $type
40044f4e696SGreg Roach     * @param string $xref
40144f4e696SGreg Roach     * @param int    $line_number
40244f4e696SGreg Roach     * @param string $line
40344f4e696SGreg Roach     * @param string $message
40444f4e696SGreg Roach     * @param string $tag
40544f4e696SGreg Roach     *
40644f4e696SGreg Roach     * @return object
40744f4e696SGreg Roach     */
40844f4e696SGreg Roach    private function lineError(
40944f4e696SGreg Roach        Tree $tree,
41044f4e696SGreg Roach        string $type,
41144f4e696SGreg Roach        string $xref,
41244f4e696SGreg Roach        int $line_number,
41344f4e696SGreg Roach        string $line,
41444f4e696SGreg Roach        string $message,
41544f4e696SGreg Roach        string $tag
41644f4e696SGreg Roach    ): object {
41744f4e696SGreg Roach        $message =
41844f4e696SGreg Roach            I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) .
41944f4e696SGreg Roach            ' — ' .
42044f4e696SGreg Roach            I18N::translate('%1$s: %2$s', I18N::translate('Line number'), I18N::number($line_number)) .
42144f4e696SGreg Roach            ' — ' .
42244f4e696SGreg Roach            '<code>' . e($line) . '</code>' .
42344f4e696SGreg Roach            '<br>' . $message;
42444f4e696SGreg Roach
42544f4e696SGreg Roach        return (object) [
42644f4e696SGreg Roach            'message' => $message,
42744f4e696SGreg Roach            'tag'     => $tag,
42844f4e696SGreg Roach        ];
42944f4e696SGreg Roach    }
43044f4e696SGreg Roach
43144f4e696SGreg Roach    /**
43244f4e696SGreg Roach     * Format a link to a record.
43344f4e696SGreg Roach     *
43444f4e696SGreg Roach     * @param Tree   $tree
43544f4e696SGreg Roach     * @param string $type
43644f4e696SGreg Roach     * @param string $xref
43744f4e696SGreg Roach     * @param string $message
43844f4e696SGreg Roach     * @param string $tag
43944f4e696SGreg Roach     *
44044f4e696SGreg Roach     * @return object
44144f4e696SGreg Roach     */
44244f4e696SGreg Roach    private function recordError(Tree $tree, string $type, string $xref, string $message, string $tag): object
44344f4e696SGreg Roach    {
44444f4e696SGreg Roach        $message = I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) . ' — ' . $message;
44544f4e696SGreg Roach
44644f4e696SGreg Roach        return (object) [
44744f4e696SGreg Roach            'message' => $message,
44844f4e696SGreg Roach            'tag'     => $tag,
44944f4e696SGreg Roach        ];
45044f4e696SGreg Roach    }
4516fd01894SGreg Roach}
452