1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Algorithm\ConnectedComponent; 23use Fisharebest\Webtrees\Http\ViewResponseTrait; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Tree; 28use Fisharebest\Webtrees\User; 29use Illuminate\Database\Capsule\Manager as DB; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function assert; 35 36/** 37 * Find groups of unrelated individuals. 38 */ 39class UnconnectedPage implements RequestHandlerInterface 40{ 41 use ViewResponseTrait; 42 43 /** 44 * @param ServerRequestInterface $request 45 * 46 * @return ResponseInterface 47 */ 48 public function handle(ServerRequestInterface $request): ResponseInterface 49 { 50 $tree = $request->getAttribute('tree'); 51 assert($tree instanceof Tree); 52 53 $user = $request->getAttribute('user'); 54 assert($user instanceof User); 55 56 $aliases = (bool) ($request->getQueryParams()['aliases'] ?? false); 57 $associates = (bool) ($request->getQueryParams()['associates'] ?? false); 58 59 // Connect individuals using these links. 60 $links = ['FAMS', 'FAMC']; 61 62 if ($aliases) { 63 $links[] = 'ALIA'; 64 } 65 66 if ($associates) { 67 $links[] = 'ASSO'; 68 $links[] = '_ASSO'; 69 } 70 71 $rows = DB::table('link') 72 ->where('l_file', '=', $tree->id()) 73 ->whereIn('l_type', $links) 74 ->select(['l_from', 'l_to']) 75 ->get(); 76 77 $graph = DB::table('individuals') 78 ->where('i_file', '=', $tree->id()) 79 ->pluck('i_id') 80 ->mapWithKeys(static function (string $xref): array { 81 return [$xref => []]; 82 }) 83 ->all(); 84 85 foreach ($rows as $row) { 86 $graph[$row->l_from][$row->l_to] = 1; 87 $graph[$row->l_to][$row->l_from] = 1; 88 } 89 90 $algorithm = new ConnectedComponent($graph); 91 $components = $algorithm->findConnectedComponents(); 92 $root = $tree->significantIndividual($user); 93 $xref = $root->xref(); 94 95 /** @var Individual[][] */ 96 $individual_groups = []; 97 98 foreach ($components as $component) { 99 if (!in_array($xref, $component, true)) { 100 $individual_groups[] = DB::table('individuals') 101 ->where('i_file', '=', $tree->id()) 102 ->whereIn('i_id', $component) 103 ->get() 104 ->map(Registry::individualFactory()->mapper($tree)) 105 ->filter(); 106 } 107 } 108 109 $title = I18N::translate('Find unrelated individuals') . ' — ' . e($tree->title()); 110 111 $this->layout = 'layouts/administration'; 112 113 return $this->viewResponse('admin/trees-unconnected', [ 114 'aliases' => $aliases, 115 'associates' => $associates, 116 'root' => $root, 117 'individual_groups' => $individual_groups, 118 'title' => $title, 119 'tree' => $tree, 120 ]); 121 } 122} 123