xref: /webtrees/app/Http/RequestHandlers/CheckTree.php (revision 16ecfcafdc113f0b4074e256aa90a4c870e7b2f0)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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
22e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\AbstractXrefElement;
23*16ecfcafSGreg Roachuse Fisharebest\Webtrees\Elements\MultimediaFileReference;
2471166947SGreg Roachuse Fisharebest\Webtrees\Elements\MultimediaFormat;
25e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\SubmitterText;
26e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\UnknownElement;
27e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefFamily;
28e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefIndividual;
29e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefLocation;
30e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefMedia;
31e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefNote;
32e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefRepository;
33e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefSource;
34e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefSubmission;
35e669bb4bSGreg Roachuse Fisharebest\Webtrees\Elements\XrefSubmitter;
36e669bb4bSGreg Roachuse Fisharebest\Webtrees\Factories\ElementFactory;
3771166947SGreg Roachuse Fisharebest\Webtrees\Factories\ImageFactory;
38e669bb4bSGreg Roachuse Fisharebest\Webtrees\Family;
396fd01894SGreg Roachuse Fisharebest\Webtrees\Gedcom;
406fd01894SGreg Roachuse Fisharebest\Webtrees\Header;
416fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
426fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
43e669bb4bSGreg Roachuse Fisharebest\Webtrees\Individual;
44e669bb4bSGreg Roachuse Fisharebest\Webtrees\Location;
45e669bb4bSGreg Roachuse Fisharebest\Webtrees\Media;
4671166947SGreg Roachuse Fisharebest\Webtrees\Mime;
47e669bb4bSGreg Roachuse Fisharebest\Webtrees\Note;
48e669bb4bSGreg Roachuse Fisharebest\Webtrees\Registry;
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\Capsule\Manager as DB;
576fd01894SGreg Roachuse Illuminate\Database\Query\Expression;
586fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
596fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
606fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
616fd01894SGreg Roach
6265f30ad2SGreg Roachuse function array_key_exists;
63e669bb4bSGreg Roachuse function array_slice;
646fd01894SGreg Roachuse function e;
65e669bb4bSGreg Roachuse function implode;
6671166947SGreg Roachuse function in_array;
676fd01894SGreg Roachuse function preg_match;
686fd01894SGreg Roachuse function route;
69e669bb4bSGreg Roachuse function str_starts_with;
706fd01894SGreg Roachuse function strtoupper;
71e669bb4bSGreg Roachuse function substr_count;
726fd01894SGreg Roach
736fd01894SGreg Roach/**
746fd01894SGreg Roach * Check a tree for errors.
756fd01894SGreg Roach */
766fd01894SGreg Roachclass CheckTree implements RequestHandlerInterface
776fd01894SGreg Roach{
786fd01894SGreg Roach    use ViewResponseTrait;
796fd01894SGreg Roach
80e669bb4bSGreg Roach    private Gedcom $gedcom;
81e669bb4bSGreg Roach
82e669bb4bSGreg Roach    private TimeoutService $timeout_service;
83e669bb4bSGreg Roach
84e669bb4bSGreg Roach    /**
85e669bb4bSGreg Roach     * @param Gedcom         $gedcom
86e669bb4bSGreg Roach     * @param TimeoutService $timeout_service
87e669bb4bSGreg Roach     */
88e669bb4bSGreg Roach    public function __construct(Gedcom $gedcom, TimeoutService $timeout_service)
89e669bb4bSGreg Roach    {
90e669bb4bSGreg Roach        $this->gedcom          = $gedcom;
91e669bb4bSGreg Roach        $this->timeout_service = $timeout_service;
92e669bb4bSGreg Roach    }
93e669bb4bSGreg Roach
946fd01894SGreg Roach    /**
956fd01894SGreg Roach     * @param ServerRequestInterface $request
966fd01894SGreg Roach     *
976fd01894SGreg Roach     * @return ResponseInterface
986fd01894SGreg Roach     */
996fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
1006fd01894SGreg Roach    {
1016fd01894SGreg Roach        $this->layout = 'layouts/administration';
1026fd01894SGreg Roach
103b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
104e669bb4bSGreg Roach        $skip_to = Validator::queryParams($request)->string('skip_to', '');
1056fd01894SGreg Roach
1066fd01894SGreg Roach        // We need to work with raw GEDCOM data, as we are looking for errors
1076fd01894SGreg Roach        // which may prevent the GedcomRecord objects from working.
1086fd01894SGreg Roach
1096fd01894SGreg Roach        $q1 = DB::table('individuals')
1106fd01894SGreg Roach            ->where('i_file', '=', $tree->id())
1116fd01894SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom', new Expression("'INDI' AS type")]);
1126fd01894SGreg Roach        $q2 = DB::table('families')
1136fd01894SGreg Roach            ->where('f_file', '=', $tree->id())
1146fd01894SGreg Roach            ->select(['f_id AS xref', 'f_gedcom AS gedcom', new Expression("'FAM' AS type")]);
1156fd01894SGreg Roach        $q3 = DB::table('media')
1166fd01894SGreg Roach            ->where('m_file', '=', $tree->id())
1176fd01894SGreg Roach            ->select(['m_id AS xref', 'm_gedcom AS gedcom', new Expression("'OBJE' AS type")]);
1186fd01894SGreg Roach        $q4 = DB::table('sources')
1196fd01894SGreg Roach            ->where('s_file', '=', $tree->id())
1206fd01894SGreg Roach            ->select(['s_id AS xref', 's_gedcom AS gedcom', new Expression("'SOUR' AS type")]);
1216fd01894SGreg Roach        $q5 = DB::table('other')
1226fd01894SGreg Roach            ->where('o_file', '=', $tree->id())
1236fd01894SGreg Roach            ->select(['o_id AS xref', 'o_gedcom AS gedcom', 'o_type']);
1246fd01894SGreg Roach        $q6 = DB::table('change')
1256fd01894SGreg Roach            ->where('gedcom_id', '=', $tree->id())
1266fd01894SGreg Roach            ->where('status', '=', 'pending')
1276fd01894SGreg Roach            ->orderBy('change_id')
1286fd01894SGreg Roach            ->select(['xref', 'new_gedcom AS gedcom', new Expression("'' AS type")]);
1296fd01894SGreg Roach
1306fd01894SGreg Roach        $rows = $q1
1316fd01894SGreg Roach            ->unionAll($q2)
1326fd01894SGreg Roach            ->unionAll($q3)
1336fd01894SGreg Roach            ->unionAll($q4)
1346fd01894SGreg Roach            ->unionAll($q5)
1356fd01894SGreg Roach            ->unionAll($q6)
1366fd01894SGreg Roach            ->get()
137f70bcff5SGreg Roach            ->map(static function (object $row): object {
1386fd01894SGreg Roach                // Extract type for pending record
139e669bb4bSGreg Roach                if ($row->type === '' && preg_match('/^0 @[^@]*@ ([_A-Z0-9]+)/', $row->gedcom, $match) === 1) {
1406fd01894SGreg Roach                    $row->type = $match[1];
1416fd01894SGreg Roach                }
1426fd01894SGreg Roach
1436fd01894SGreg Roach                return $row;
1446fd01894SGreg Roach            });
1456fd01894SGreg Roach
1466fd01894SGreg Roach        $records = [];
147e669bb4bSGreg Roach        $xrefs   = [];
1486fd01894SGreg Roach
1496fd01894SGreg Roach        foreach ($rows as $row) {
1506fd01894SGreg Roach            if ($row->gedcom !== '') {
1516fd01894SGreg Roach                // existing or updated record
1526fd01894SGreg Roach                $records[$row->xref] = $row;
1536fd01894SGreg Roach            } else {
1546fd01894SGreg Roach                // deleted record
1556fd01894SGreg Roach                unset($records[$row->xref]);
1566fd01894SGreg Roach            }
157e669bb4bSGreg Roach
158e669bb4bSGreg Roach            $xrefs[strtoupper($row->xref)] = $row->xref;
1596fd01894SGreg Roach        }
1606fd01894SGreg Roach
161e669bb4bSGreg Roach        unset($rows);
1626fd01894SGreg Roach
1636fd01894SGreg Roach        $errors   = [];
1646fd01894SGreg Roach        $warnings = [];
16512224cf3SGreg Roach        $infos    = [];
1666fd01894SGreg Roach
167e669bb4bSGreg Roach        $element_factory = new ElementFactory();
168e669bb4bSGreg Roach        $this->gedcom->registerTags($element_factory, false);
169e669bb4bSGreg Roach
1706fd01894SGreg Roach        foreach ($records as $record) {
171e669bb4bSGreg Roach            // If we are nearly out of time, then stop processing here
172e669bb4bSGreg Roach            if ($skip_to === $record->xref) {
173e669bb4bSGreg Roach                $skip_to = '';
174e669bb4bSGreg Roach            } elseif ($skip_to !== '') {
175e669bb4bSGreg Roach                continue;
176e669bb4bSGreg Roach            } elseif ($this->timeout_service->isTimeNearlyUp()) {
177e669bb4bSGreg Roach                $skip_to = $record->xref;
178e669bb4bSGreg Roach                break;
1796fd01894SGreg Roach            }
1806fd01894SGreg Roach
181e669bb4bSGreg Roach            $lines = explode("\n", $record->gedcom);
182e669bb4bSGreg Roach            array_shift($lines);
1836fd01894SGreg Roach
184e669bb4bSGreg Roach            $last_level = 0;
185e669bb4bSGreg Roach            $hierarchy  = [$record->type];
1866fd01894SGreg Roach
187e669bb4bSGreg Roach            foreach ($lines as $line_number => $line) {
188e669bb4bSGreg Roach                if (preg_match('/^(\d+) (\w+) ?(.*)/', $line, $match) !== 1) {
189e669bb4bSGreg Roach                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM record'));
190e669bb4bSGreg Roach                    break;
191e669bb4bSGreg Roach                }
192e669bb4bSGreg Roach
193e669bb4bSGreg Roach                $level = (int) $match[1];
194e669bb4bSGreg Roach                if ($level > $last_level + 1) {
195e669bb4bSGreg Roach                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM line number'));
196e669bb4bSGreg Roach                    break;
197e669bb4bSGreg Roach                }
198e669bb4bSGreg Roach
199e669bb4bSGreg Roach                $tag               = $match[2];
200e669bb4bSGreg Roach                $value             = $match[3];
201e669bb4bSGreg Roach                $hierarchy[$level] = $tag;
202e669bb4bSGreg Roach                $full_tag          = implode(':', array_slice($hierarchy, 0, 1 + $level));
203e669bb4bSGreg Roach                $element           = Registry::elementFactory()->make($full_tag);
204e669bb4bSGreg Roach                $last_level        = $level;
205e669bb4bSGreg Roach
206e669bb4bSGreg Roach                if ($tag === 'CONT') {
207e669bb4bSGreg Roach                    $element = new SubmitterText('CONT');
208e669bb4bSGreg Roach                }
209e669bb4bSGreg Roach
210e669bb4bSGreg Roach                if ($element instanceof UnknownElement) {
211e669bb4bSGreg Roach                    if (str_starts_with($tag, '_')) {
212e669bb4bSGreg Roach                        $message    = I18N::translate('Custom GEDCOM tags are discouraged. Try to use only standard GEDCOM tags.');
213e669bb4bSGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
2146fd01894SGreg Roach                    } else {
215e669bb4bSGreg Roach                        $message  = I18N::translate('Invalid GEDCOM tag.') . ' ' . $full_tag;
216e669bb4bSGreg Roach                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
2176fd01894SGreg Roach                    }
218e669bb4bSGreg Roach                } elseif ($element instanceof AbstractXrefElement) {
219e669bb4bSGreg Roach                    if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $value, $match) === 1) {
220e669bb4bSGreg Roach                        $xref1  = $match[1];
221e669bb4bSGreg Roach                        $xref2  = $xrefs[strtoupper($xref1)] ?? null;
222e669bb4bSGreg Roach                        $linked = $records[$xref2] ?? null;
223e669bb4bSGreg Roach
224e669bb4bSGreg Roach                        if ($linked === null) {
225e669bb4bSGreg Roach                            $message  = I18N::translate('%s does not exist.', e($xref1));
226e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
227e669bb4bSGreg Roach                        } elseif ($element instanceof XrefFamily && $linked->type !== Family::RECORD_TYPE) {
228e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Family::RECORD_TYPE);
229e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
230e669bb4bSGreg Roach                        } elseif ($element instanceof XrefIndividual && $linked->type !== Individual::RECORD_TYPE) {
231e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Individual::RECORD_TYPE);
232e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
233e669bb4bSGreg Roach                        } elseif ($element instanceof XrefMedia && $linked->type !== Media::RECORD_TYPE) {
234e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Media::RECORD_TYPE);
235e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
236e669bb4bSGreg Roach                        } elseif ($element instanceof XrefNote && $linked->type !== Note::RECORD_TYPE) {
237e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Note::RECORD_TYPE);
238e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
239e669bb4bSGreg Roach                        } elseif ($element instanceof XrefSource && $linked->type !== Source::RECORD_TYPE) {
240e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Source::RECORD_TYPE);
241e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
242e669bb4bSGreg Roach                        } elseif ($element instanceof XrefRepository && $linked->type !== Repository::RECORD_TYPE) {
243e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Repository::RECORD_TYPE);
244e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
245e669bb4bSGreg Roach                        } elseif ($element instanceof XrefSubmitter && $linked->type !== Submitter::RECORD_TYPE) {
246e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submitter::RECORD_TYPE);
247e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
248e669bb4bSGreg Roach                        } elseif ($element instanceof XrefSubmission && $linked->type !== Submission::RECORD_TYPE) {
249e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submission::RECORD_TYPE);
250e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
251e669bb4bSGreg Roach                        } elseif ($element instanceof XrefLocation && $linked->type !== Location::RECORD_TYPE) {
252e669bb4bSGreg Roach                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Location::RECORD_TYPE);
253e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
254e669bb4bSGreg Roach                        } elseif (($full_tag === 'FAM:HUSB' || $full_tag === 'FAM:WIFE') && !str_contains($linked->gedcom, "\n1 FAMS @" . $record->xref . '@')) {
255e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
256e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
257e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
258e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
259e669bb4bSGreg Roach                        } elseif ($full_tag === 'FAM:CHIL' && !str_contains($linked->gedcom, "\n1 FAMC @" . $record->xref . '@')) {
260e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
261e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
262e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
263e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
264e669bb4bSGreg Roach                        } elseif ($full_tag === 'INDI:FAMC' && !str_contains($linked->gedcom, "\n1 CHIL @" . $record->xref . '@')) {
265e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
266e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
267e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
268e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
269e669bb4bSGreg Roach                        } elseif ($full_tag === 'INDI:FAMS' && !str_contains($linked->gedcom, "\n1 HUSB @" . $record->xref . '@') && !str_contains($linked->gedcom, "\n1 WIFE @" . $record->xref . '@')) {
270e669bb4bSGreg Roach                            $link1    = $this->recordLink($tree, $linked->xref);
271e669bb4bSGreg Roach                            $link2    = $this->recordLink($tree, $record->xref);
272e669bb4bSGreg Roach                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
273e669bb4bSGreg Roach                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
274e669bb4bSGreg Roach                        } elseif ($xref1 !== $xref2) {
275e669bb4bSGreg Roach                            $message    = I18N::translate('%1$s does not exist. Did you mean %2$s?', e($xref1), e($xref2));
276e669bb4bSGreg Roach                            $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
277e669bb4bSGreg Roach                        }
278e669bb4bSGreg Roach                    } elseif ($tag === 'SOUR') {
279e669bb4bSGreg Roach                        $message    = I18N::translate('Inline-source records are discouraged.');
280e669bb4bSGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
281e669bb4bSGreg Roach                    } else {
282e669bb4bSGreg Roach                        $message  = I18N::translate('Invalid GEDCOM value');
283e669bb4bSGreg Roach                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
284e669bb4bSGreg Roach                    }
285e669bb4bSGreg Roach                } elseif ($element->canonical($value) !== $value) {
286e669bb4bSGreg Roach                    $expected = e($element->canonical($value));
287e669bb4bSGreg Roach                    $actual   = strtr(e($value), ["\t" => '&rarr;']);
288e669bb4bSGreg Roach                    $message  = I18N::translate('“%1$s” should be “%2$s”.', $actual, $expected);
28912224cf3SGreg Roach                    $infos[]  = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
29071166947SGreg Roach                } elseif ($element instanceof MultimediaFormat) {
29171166947SGreg Roach                    $mime = Mime::TYPES[$value] ?? Mime::DEFAULT_TYPE;
29271166947SGreg Roach
29371166947SGreg Roach                    if ($mime === Mime::DEFAULT_TYPE) {
29471166947SGreg Roach                        $message    = I18N::translate('webtrees does not recognise this file format.');
29571166947SGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
29665f30ad2SGreg Roach                    } elseif (str_starts_with($mime, 'image/') && !array_key_exists($mime, ImageFactory::SUPPORTED_FORMATS)) {
29771166947SGreg Roach                        $message    = I18N::translate('webtrees cannot create thumbnails for this file format.');
29871166947SGreg Roach                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
29971166947SGreg Roach                    }
300*16ecfcafSGreg Roach                } elseif ($element instanceof MultimediaFileReference && $value === 'gedcom.ged') {
301*16ecfcafSGreg Roach                    $message  = I18N::translate('This filename is not compatible with the GEDZIP file format.');
302*16ecfcafSGreg Roach                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
303e669bb4bSGreg Roach                }
304e669bb4bSGreg Roach            }
305e669bb4bSGreg Roach
306e669bb4bSGreg Roach            if ($record->type === Family::RECORD_TYPE) {
307e669bb4bSGreg Roach                if (substr_count($record->gedcom, "\n1 HUSB @") > 1) {
308e669bb4bSGreg Roach                    $message  = I18N::translate('%s occurs too many times.', 'FAM:HUSB');
309e669bb4bSGreg Roach                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message);
310e669bb4bSGreg Roach                }
311e669bb4bSGreg Roach                if (substr_count($record->gedcom, "\n1 WIFE @") > 1) {
312e669bb4bSGreg Roach                    $message  = I18N::translate('%s occurs too many times.', 'FAM:WIFE');
313e669bb4bSGreg Roach                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message);
3146fd01894SGreg Roach                }
3156fd01894SGreg Roach            }
3166fd01894SGreg Roach        }
3176fd01894SGreg Roach
3186fd01894SGreg Roach        $title = I18N::translate('Check for errors') . ' — ' . e($tree->title());
3196fd01894SGreg Roach
320e669bb4bSGreg Roach        if ($skip_to === '') {
321e669bb4bSGreg Roach            $more_url = '';
322e669bb4bSGreg Roach        } else {
323e669bb4bSGreg Roach            $more_url = route(self::class, ['tree' => $tree->name(), 'skip_to' => $skip_to]);
324e669bb4bSGreg Roach        }
325e669bb4bSGreg Roach
3266fd01894SGreg Roach        return $this->viewResponse('admin/trees-check', [
3276fd01894SGreg Roach            'errors'   => $errors,
32812224cf3SGreg Roach            'infos'    => $infos,
329e669bb4bSGreg Roach            'more_url' => $more_url,
3306fd01894SGreg Roach            'title'    => $title,
3316fd01894SGreg Roach            'tree'     => $tree,
3326fd01894SGreg Roach            'warnings' => $warnings,
3336fd01894SGreg Roach        ]);
3346fd01894SGreg Roach    }
3356fd01894SGreg Roach
3366fd01894SGreg Roach    /**
3376fd01894SGreg Roach     * @param string $type
3386fd01894SGreg Roach     *
3396fd01894SGreg Roach     * @return string
3406fd01894SGreg Roach     */
341e669bb4bSGreg Roach    private function recordType(string $type): string
3426fd01894SGreg Roach    {
343e669bb4bSGreg Roach        $types = [
344e669bb4bSGreg Roach            Family::RECORD_TYPE     => I18N::translate('Family'),
345e669bb4bSGreg Roach            Header::RECORD_TYPE     => I18N::translate('Header'),
346e669bb4bSGreg Roach            Individual::RECORD_TYPE => I18N::translate('Individual'),
347e669bb4bSGreg Roach            Location::RECORD_TYPE   => I18N::translate('Location'),
348e669bb4bSGreg Roach            Media::RECORD_TYPE      => I18N::translate('Media object'),
349e669bb4bSGreg Roach            Note::RECORD_TYPE       => I18N::translate('Note'),
350e669bb4bSGreg Roach            Repository::RECORD_TYPE => I18N::translate('Repository'),
351e669bb4bSGreg Roach            Source::RECORD_TYPE     => I18N::translate('Source'),
352e669bb4bSGreg Roach            Submission::RECORD_TYPE => I18N::translate('Submission'),
353e669bb4bSGreg Roach            Submitter::RECORD_TYPE  => I18N::translate('Submitter'),
354e669bb4bSGreg Roach        ];
355e669bb4bSGreg Roach
356e669bb4bSGreg Roach        return $types[$type] ?? e($type);
357e669bb4bSGreg Roach    }
358e669bb4bSGreg Roach
359e669bb4bSGreg Roach    /**
360e669bb4bSGreg Roach     * @param Tree   $tree
361e669bb4bSGreg Roach     * @param string $xref
362e669bb4bSGreg Roach     *
363e669bb4bSGreg Roach     * @return string
364e669bb4bSGreg Roach     */
365e669bb4bSGreg Roach    private function recordLink(Tree $tree, string $xref): string
366e669bb4bSGreg Roach    {
367e669bb4bSGreg Roach        $url = route(GedcomRecordPage::class, ['xref' => $xref, 'tree' => $tree->name()]);
368e669bb4bSGreg Roach
369e669bb4bSGreg Roach        return '<a href="' . e($url) . '">' . e($xref) . '</a>';
3706fd01894SGreg Roach    }
3716fd01894SGreg Roach
3726fd01894SGreg Roach    /**
3736fd01894SGreg Roach     * Format a link to a record.
3746fd01894SGreg Roach     *
3756fd01894SGreg Roach     * @param Tree   $tree
376e669bb4bSGreg Roach     * @param string $type
3776fd01894SGreg Roach     * @param string $xref
378e669bb4bSGreg Roach     * @param int    $line_number
379e669bb4bSGreg Roach     * @param string $line
380e669bb4bSGreg Roach     * @param string $message
3816fd01894SGreg Roach     *
3826fd01894SGreg Roach     * @return string
3836fd01894SGreg Roach     */
384e669bb4bSGreg Roach    private function lineError(Tree $tree, string $type, string $xref, int $line_number, string $line, string $message): string
3856fd01894SGreg Roach    {
386e669bb4bSGreg Roach        return
387e669bb4bSGreg Roach            I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) .
388e669bb4bSGreg Roach            ' — ' .
389e669bb4bSGreg Roach            I18N::translate('%1$s: %2$s', I18N::translate('Line number'), I18N::number($line_number)) .
390e669bb4bSGreg Roach            ' — ' .
391e669bb4bSGreg Roach            '<code>' . e($line) . '</code>' .
392e669bb4bSGreg Roach            '<br>' . $message;
3936fd01894SGreg Roach    }
3946fd01894SGreg Roach
3956fd01894SGreg Roach    /**
396e669bb4bSGreg Roach     * Format a link to a record.
3976fd01894SGreg Roach     *
398e669bb4bSGreg Roach     * @param Tree   $tree
3996fd01894SGreg Roach     * @param string $type
400e669bb4bSGreg Roach     * @param string $xref
401e669bb4bSGreg Roach     * @param string $message
4026fd01894SGreg Roach     *
4036fd01894SGreg Roach     * @return string
4046fd01894SGreg Roach     */
405e669bb4bSGreg Roach    private function recordError(Tree $tree, string $type, string $xref, string $message): string
4066fd01894SGreg Roach    {
407e669bb4bSGreg Roach        return I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) . ' — ' . $message;
408e669bb4bSGreg Roach    }
409e669bb4bSGreg Roach
410e669bb4bSGreg Roach    /**
411e669bb4bSGreg Roach     * @param Tree   $tree
412e669bb4bSGreg Roach     * @param string $xref
413e669bb4bSGreg Roach     * @param string $type1
414e669bb4bSGreg Roach     * @param string $type2
415e669bb4bSGreg Roach     *
416e669bb4bSGreg Roach     * @return string
417e669bb4bSGreg Roach     */
418e669bb4bSGreg Roach    private function linkErrorMessage(Tree $tree, string $xref, string $type1, string $type2): string
419e669bb4bSGreg Roach    {
420e669bb4bSGreg Roach        $link  = $this->recordLink($tree, $xref);
421e669bb4bSGreg Roach        $type1 = $this->recordType($type1);
422e669bb4bSGreg Roach        $type2 = $this->recordType($type2);
423e669bb4bSGreg Roach
424e669bb4bSGreg Roach        return I18N::translate('%1$s is a %2$s but a %3$s is expected.', $link, $type1, $type2);
4256fd01894SGreg Roach    }
4266fd01894SGreg Roach}
427