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