xref: /webtrees/app/Http/RequestHandlers/CheckTree.php (revision e669bb4bc4fd05c009a4ba2795f411cb9d3260d7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Elements\AbstractXrefElement;
23use Fisharebest\Webtrees\Elements\SubmitterText;
24use Fisharebest\Webtrees\Elements\UnknownElement;
25use Fisharebest\Webtrees\Elements\XrefFamily;
26use Fisharebest\Webtrees\Elements\XrefIndividual;
27use Fisharebest\Webtrees\Elements\XrefLocation;
28use Fisharebest\Webtrees\Elements\XrefMedia;
29use Fisharebest\Webtrees\Elements\XrefNote;
30use Fisharebest\Webtrees\Elements\XrefRepository;
31use Fisharebest\Webtrees\Elements\XrefSource;
32use Fisharebest\Webtrees\Elements\XrefSubmission;
33use Fisharebest\Webtrees\Elements\XrefSubmitter;
34use Fisharebest\Webtrees\Factories\ElementFactory;
35use Fisharebest\Webtrees\Family;
36use Fisharebest\Webtrees\Gedcom;
37use Fisharebest\Webtrees\Header;
38use Fisharebest\Webtrees\Http\ViewResponseTrait;
39use Fisharebest\Webtrees\I18N;
40use Fisharebest\Webtrees\Individual;
41use Fisharebest\Webtrees\Location;
42use Fisharebest\Webtrees\Media;
43use Fisharebest\Webtrees\Note;
44use Fisharebest\Webtrees\Registry;
45use Fisharebest\Webtrees\Repository;
46use Fisharebest\Webtrees\Services\TimeoutService;
47use Fisharebest\Webtrees\Source;
48use Fisharebest\Webtrees\Submission;
49use Fisharebest\Webtrees\Submitter;
50use Fisharebest\Webtrees\Tree;
51use Fisharebest\Webtrees\Validator;
52use Illuminate\Database\Capsule\Manager as DB;
53use Illuminate\Database\Query\Expression;
54use Psr\Http\Message\ResponseInterface;
55use Psr\Http\Message\ServerRequestInterface;
56use Psr\Http\Server\RequestHandlerInterface;
57
58use function array_slice;
59use function e;
60use function implode;
61use function preg_match;
62use function route;
63use function str_starts_with;
64use function strtoupper;
65use function substr_count;
66
67/**
68 * Check a tree for errors.
69 */
70class CheckTree implements RequestHandlerInterface
71{
72    use ViewResponseTrait;
73
74    private Gedcom $gedcom;
75
76    private TimeoutService $timeout_service;
77
78    /**
79     * @param Gedcom         $gedcom
80     * @param TimeoutService $timeout_service
81     */
82    public function __construct(Gedcom $gedcom, TimeoutService $timeout_service)
83    {
84        $this->gedcom          = $gedcom;
85        $this->timeout_service = $timeout_service;
86    }
87
88    /**
89     * @param ServerRequestInterface $request
90     *
91     * @return ResponseInterface
92     */
93    public function handle(ServerRequestInterface $request): ResponseInterface
94    {
95        $this->layout = 'layouts/administration';
96
97        $tree    = Validator::attributes($request)->tree();
98        $skip_to = Validator::queryParams($request)->string('skip_to', '');
99
100        // We need to work with raw GEDCOM data, as we are looking for errors
101        // which may prevent the GedcomRecord objects from working.
102
103        $q1 = DB::table('individuals')
104            ->where('i_file', '=', $tree->id())
105            ->select(['i_id AS xref', 'i_gedcom AS gedcom', new Expression("'INDI' AS type")]);
106        $q2 = DB::table('families')
107            ->where('f_file', '=', $tree->id())
108            ->select(['f_id AS xref', 'f_gedcom AS gedcom', new Expression("'FAM' AS type")]);
109        $q3 = DB::table('media')
110            ->where('m_file', '=', $tree->id())
111            ->select(['m_id AS xref', 'm_gedcom AS gedcom', new Expression("'OBJE' AS type")]);
112        $q4 = DB::table('sources')
113            ->where('s_file', '=', $tree->id())
114            ->select(['s_id AS xref', 's_gedcom AS gedcom', new Expression("'SOUR' AS type")]);
115        $q5 = DB::table('other')
116            ->where('o_file', '=', $tree->id())
117            ->select(['o_id AS xref', 'o_gedcom AS gedcom', 'o_type']);
118        $q6 = DB::table('change')
119            ->where('gedcom_id', '=', $tree->id())
120            ->where('status', '=', 'pending')
121            ->orderBy('change_id')
122            ->select(['xref', 'new_gedcom AS gedcom', new Expression("'' AS type")]);
123
124        $rows = $q1
125            ->unionAll($q2)
126            ->unionAll($q3)
127            ->unionAll($q4)
128            ->unionAll($q5)
129            ->unionAll($q6)
130            ->get()
131            ->map(static function (object $row): object {
132                // Extract type for pending record
133                if ($row->type === '' && preg_match('/^0 @[^@]*@ ([_A-Z0-9]+)/', $row->gedcom, $match) === 1) {
134                    $row->type = $match[1];
135                }
136
137                return $row;
138            });
139
140        $records = [];
141        $xrefs   = [];
142
143        foreach ($rows as $row) {
144            if ($row->gedcom !== '') {
145                // existing or updated record
146                $records[$row->xref] = $row;
147            } else {
148                // deleted record
149                unset($records[$row->xref]);
150            }
151
152            $xrefs[strtoupper($row->xref)] = $row->xref;
153        }
154
155        unset($rows);
156
157        $errors   = [];
158        $warnings = [];
159
160        $element_factory = new ElementFactory();
161        $this->gedcom->registerTags($element_factory, false);
162
163        foreach ($records as $record) {
164            // If we are nearly out of time, then stop processing here
165            if ($skip_to === $record->xref) {
166                $skip_to = '';
167            } elseif ($skip_to !== '') {
168                continue;
169            } elseif ($this->timeout_service->isTimeNearlyUp()) {
170                $skip_to = $record->xref;
171                break;
172            }
173
174            $lines = explode("\n", $record->gedcom);
175            array_shift($lines);
176
177            $last_level = 0;
178            $hierarchy  = [$record->type];
179
180            foreach ($lines as $line_number => $line) {
181                if (preg_match('/^(\d+) (\w+) ?(.*)/', $line, $match) !== 1) {
182                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM record'));
183                    break;
184                }
185
186                $level = (int) $match[1];
187                if ($level > $last_level + 1) {
188                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM line number'));
189                    break;
190                }
191
192                $tag               = $match[2];
193                $value             = $match[3];
194                $hierarchy[$level] = $tag;
195                $full_tag          = implode(':', array_slice($hierarchy, 0, 1 + $level));
196                $element           = Registry::elementFactory()->make($full_tag);
197                $last_level        = $level;
198
199                if ($tag === 'CONT') {
200                    $element = new SubmitterText('CONT');
201                }
202
203                if ($element instanceof UnknownElement) {
204                    if (str_starts_with($tag, '_')) {
205                        $message    = I18N::translate('Custom GEDCOM tags are discouraged. Try to use only standard GEDCOM tags.');
206                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
207                    } else {
208                        $message  = I18N::translate('Invalid GEDCOM tag.') . ' ' . $full_tag;
209                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
210                    }
211                } elseif ($element instanceof AbstractXrefElement) {
212                    if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $value, $match) === 1) {
213                        $xref1  = $match[1];
214                        $xref2  = $xrefs[strtoupper($xref1)] ?? null;
215                        $linked = $records[$xref2] ?? null;
216
217                        if ($linked === null) {
218                            $message  = I18N::translate('%s does not exist.', e($xref1));
219                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
220                        } elseif ($element instanceof XrefFamily && $linked->type !== Family::RECORD_TYPE) {
221                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Family::RECORD_TYPE);
222                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
223                        } elseif ($element instanceof XrefIndividual && $linked->type !== Individual::RECORD_TYPE) {
224                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Individual::RECORD_TYPE);
225                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
226                        } elseif ($element instanceof XrefMedia && $linked->type !== Media::RECORD_TYPE) {
227                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Media::RECORD_TYPE);
228                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
229                        } elseif ($element instanceof XrefNote && $linked->type !== Note::RECORD_TYPE) {
230                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Note::RECORD_TYPE);
231                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
232                        } elseif ($element instanceof XrefSource && $linked->type !== Source::RECORD_TYPE) {
233                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Source::RECORD_TYPE);
234                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
235                        } elseif ($element instanceof XrefRepository && $linked->type !== Repository::RECORD_TYPE) {
236                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Repository::RECORD_TYPE);
237                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
238                        } elseif ($element instanceof XrefSubmitter && $linked->type !== Submitter::RECORD_TYPE) {
239                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submitter::RECORD_TYPE);
240                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
241                        } elseif ($element instanceof XrefSubmission && $linked->type !== Submission::RECORD_TYPE) {
242                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submission::RECORD_TYPE);
243                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
244                        } elseif ($element instanceof XrefLocation && $linked->type !== Location::RECORD_TYPE) {
245                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Location::RECORD_TYPE);
246                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
247                        } elseif (($full_tag === 'FAM:HUSB' || $full_tag === 'FAM:WIFE') && !str_contains($linked->gedcom, "\n1 FAMS @" . $record->xref . '@')) {
248                            $link1    = $this->recordLink($tree, $linked->xref);
249                            $link2    = $this->recordLink($tree, $record->xref);
250                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
251                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
252                        } elseif ($full_tag === 'FAM:CHIL' && !str_contains($linked->gedcom, "\n1 FAMC @" . $record->xref . '@')) {
253                            $link1    = $this->recordLink($tree, $linked->xref);
254                            $link2    = $this->recordLink($tree, $record->xref);
255                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
256                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
257                        } elseif ($full_tag === 'INDI:FAMC' && !str_contains($linked->gedcom, "\n1 CHIL @" . $record->xref . '@')) {
258                            $link1    = $this->recordLink($tree, $linked->xref);
259                            $link2    = $this->recordLink($tree, $record->xref);
260                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
261                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
262                        } elseif ($full_tag === 'INDI:FAMS' && !str_contains($linked->gedcom, "\n1 HUSB @" . $record->xref . '@') && !str_contains($linked->gedcom, "\n1 WIFE @" . $record->xref . '@')) {
263                            $link1    = $this->recordLink($tree, $linked->xref);
264                            $link2    = $this->recordLink($tree, $record->xref);
265                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
266                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
267                        } elseif ($xref1 !== $xref2) {
268                            $message    = I18N::translate('%1$s does not exist. Did you mean %2$s?', e($xref1), e($xref2));
269                            $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
270                        }
271                    } elseif ($tag === 'SOUR') {
272                        $message  = I18N::translate('Inline-source records are discouraged.');
273                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
274                    } else {
275                        $message  = I18N::translate('Invalid GEDCOM value');
276                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
277                    }
278                } elseif ($element->canonical($value) !== $value) {
279                    $expected   = e($element->canonical($value));
280                    $actual     = strtr(e($value), ["\t" => '&rarr;']);
281                    $message    = I18N::translate('“%1$s” should be “%2$s”.', $actual, $expected);
282                    $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message);
283                }
284            }
285
286            if ($record->type === Family::RECORD_TYPE) {
287                if (substr_count($record->gedcom, "\n1 HUSB @") > 1) {
288                    $message  = I18N::translate('%s occurs too many times.', 'FAM:HUSB');
289                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message);
290                }
291                if (substr_count($record->gedcom, "\n1 WIFE @") > 1) {
292                    $message  = I18N::translate('%s occurs too many times.', 'FAM:WIFE');
293                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message);
294                }
295            }
296        }
297
298        $title = I18N::translate('Check for errors') . ' — ' . e($tree->title());
299
300        if ($skip_to === '') {
301            $more_url = '';
302        } else {
303            $more_url = route(self::class, ['tree' => $tree->name(), 'skip_to' => $skip_to]);
304        }
305
306        return $this->viewResponse('admin/trees-check', [
307            'errors'   => $errors,
308            'more_url' => $more_url,
309            'title'    => $title,
310            'tree'     => $tree,
311            'warnings' => $warnings,
312        ]);
313    }
314
315    /**
316     * @param string $type
317     *
318     * @return string
319     */
320    private function recordType(string $type): string
321    {
322        $types = [
323            Family::RECORD_TYPE     => I18N::translate('Family'),
324            Header::RECORD_TYPE     => I18N::translate('Header'),
325            Individual::RECORD_TYPE => I18N::translate('Individual'),
326            Location::RECORD_TYPE   => I18N::translate('Location'),
327            Media::RECORD_TYPE      => I18N::translate('Media object'),
328            Note::RECORD_TYPE       => I18N::translate('Note'),
329            Repository::RECORD_TYPE => I18N::translate('Repository'),
330            Source::RECORD_TYPE     => I18N::translate('Source'),
331            Submission::RECORD_TYPE => I18N::translate('Submission'),
332            Submitter::RECORD_TYPE  => I18N::translate('Submitter'),
333        ];
334
335        return $types[$type] ?? e($type);
336    }
337
338    /**
339     * @param Tree   $tree
340     * @param string $xref
341     *
342     * @return string
343     */
344    private function recordLink(Tree $tree, string $xref): string
345    {
346        $url = route(GedcomRecordPage::class, ['xref' => $xref, 'tree' => $tree->name()]);
347
348        return '<a href="' . e($url) . '">' . e($xref) . '</a>';
349    }
350
351    /**
352     * Format a link to a record.
353     *
354     * @param Tree   $tree
355     * @param string $type
356     * @param string $xref
357     * @param int    $line_number
358     * @param string $line
359     * @param string $message
360     *
361     * @return string
362     */
363    private function lineError(Tree $tree, string $type, string $xref, int $line_number, string $line, string $message): string
364    {
365        return
366            I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) .
367            ' — ' .
368            I18N::translate('%1$s: %2$s', I18N::translate('Line number'), I18N::number($line_number)) .
369            ' — ' .
370            '<code>' . e($line) . '</code>' .
371            '<br>' . $message;
372    }
373
374    /**
375     * Format a link to a record.
376     *
377     * @param Tree   $tree
378     * @param string $type
379     * @param string $xref
380     * @param string $message
381     *
382     * @return string
383     */
384    private function recordError(Tree $tree, string $type, string $xref, string $message): string
385    {
386        return I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) . ' — ' . $message;
387    }
388
389    /**
390     * @param Tree   $tree
391     * @param string $xref
392     * @param string $type1
393     * @param string $type2
394     *
395     * @return string
396     */
397    private function linkErrorMessage(Tree $tree, string $xref, string $type1, string $type2): string
398    {
399        $link  = $this->recordLink($tree, $xref);
400        $type1 = $this->recordType($type1);
401        $type2 = $this->recordType($type2);
402
403        return I18N::translate('%1$s is a %2$s but a %3$s is expected.', $link, $type1, $type2);
404    }
405}
406