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