196716c47SGreg Roach<?php 296716c47SGreg Roach 396716c47SGreg Roach/** 496716c47SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 696716c47SGreg Roach * This program is free software: you can redistribute it and/or modify 796716c47SGreg Roach * it under the terms of the GNU General Public License as published by 896716c47SGreg Roach * the Free Software Foundation, either version 3 of the License, or 996716c47SGreg Roach * (at your option) any later version. 1096716c47SGreg Roach * This program is distributed in the hope that it will be useful, 1196716c47SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1296716c47SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1396716c47SGreg Roach * GNU General Public License for more details. 1496716c47SGreg 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/>. 1696716c47SGreg Roach */ 1796716c47SGreg Roach 1896716c47SGreg Roachdeclare(strict_types=1); 1996716c47SGreg Roach 2096716c47SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 2196716c47SGreg Roach 2296716c47SGreg Roachuse Fisharebest\Algorithm\ConnectedComponent; 2396716c47SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 2496716c47SGreg Roachuse Fisharebest\Webtrees\I18N; 2596716c47SGreg Roachuse Fisharebest\Webtrees\Individual; 266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 27*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 2896716c47SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2996716c47SGreg Roachuse Psr\Http\Message\ResponseInterface; 3096716c47SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3196716c47SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3296716c47SGreg Roach 3368c99bc7SGreg Roachuse function strtolower; 3496716c47SGreg Roach 3596716c47SGreg Roach/** 3696716c47SGreg Roach * Find groups of unrelated individuals. 3796716c47SGreg Roach */ 3896716c47SGreg Roachclass UnconnectedPage implements RequestHandlerInterface 3996716c47SGreg Roach{ 4096716c47SGreg Roach use ViewResponseTrait; 4196716c47SGreg Roach 4296716c47SGreg Roach /** 4396716c47SGreg Roach * @param ServerRequestInterface $request 4496716c47SGreg Roach * 4596716c47SGreg Roach * @return ResponseInterface 4696716c47SGreg Roach */ 4796716c47SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 4896716c47SGreg Roach { 49*b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 50*b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 51f925fcc4SGreg Roach $aliases = (bool) ($request->getQueryParams()['aliases'] ?? false); 5296716c47SGreg Roach $associates = (bool) ($request->getQueryParams()['associates'] ?? false); 5396716c47SGreg Roach 54f925fcc4SGreg Roach // Connect individuals using these links. 5596716c47SGreg Roach $links = ['FAMS', 'FAMC']; 56f925fcc4SGreg Roach 57f925fcc4SGreg Roach if ($aliases) { 58f925fcc4SGreg Roach $links[] = 'ALIA'; 59f925fcc4SGreg Roach } 60f925fcc4SGreg Roach 61f925fcc4SGreg Roach if ($associates) { 62f925fcc4SGreg Roach $links[] = 'ASSO'; 63f925fcc4SGreg Roach $links[] = '_ASSO'; 6496716c47SGreg Roach } 6596716c47SGreg Roach 6696716c47SGreg Roach $rows = DB::table('link') 6796716c47SGreg Roach ->where('l_file', '=', $tree->id()) 6896716c47SGreg Roach ->whereIn('l_type', $links) 6996716c47SGreg Roach ->select(['l_from', 'l_to']) 7096716c47SGreg Roach ->get(); 7196716c47SGreg Roach 7212e03a90SGreg Roach $graph = DB::table('individuals') 7312e03a90SGreg Roach ->where('i_file', '=', $tree->id()) 7412e03a90SGreg Roach ->pluck('i_id') 7512e03a90SGreg Roach ->mapWithKeys(static function (string $xref): array { 7612e03a90SGreg Roach return [$xref => []]; 7712e03a90SGreg Roach }) 7812e03a90SGreg Roach ->all(); 7996716c47SGreg Roach 8096716c47SGreg Roach foreach ($rows as $row) { 8196716c47SGreg Roach $graph[$row->l_from][$row->l_to] = 1; 8296716c47SGreg Roach $graph[$row->l_to][$row->l_from] = 1; 8396716c47SGreg Roach } 8496716c47SGreg Roach 8596716c47SGreg Roach $algorithm = new ConnectedComponent($graph); 8696716c47SGreg Roach $components = $algorithm->findConnectedComponents(); 8796716c47SGreg Roach $root = $tree->significantIndividual($user); 8896716c47SGreg Roach $xref = $root->xref(); 8996716c47SGreg Roach 9096716c47SGreg Roach /** @var Individual[][] */ 9196716c47SGreg Roach $individual_groups = []; 9296716c47SGreg Roach 9396716c47SGreg Roach foreach ($components as $component) { 9468c99bc7SGreg Roach // Allow for upper/lower-case mismatches, and all-numeric XREFs 9568c99bc7SGreg Roach $component = array_map(static fn ($x): string => strtolower((string) $x), $component); 9668c99bc7SGreg Roach 9768c99bc7SGreg Roach if (!in_array(strtolower($xref), $component, true)) { 9870c53a06SGreg Roach $individual_groups[] = DB::table('individuals') 9970c53a06SGreg Roach ->where('i_file', '=', $tree->id()) 10070c53a06SGreg Roach ->whereIn('i_id', $component) 10170c53a06SGreg Roach ->get() 1026b9cb339SGreg Roach ->map(Registry::individualFactory()->mapper($tree)) 10370c53a06SGreg Roach ->filter(); 10496716c47SGreg Roach } 10596716c47SGreg Roach } 10696716c47SGreg Roach 10796716c47SGreg Roach $title = I18N::translate('Find unrelated individuals') . ' — ' . e($tree->title()); 10896716c47SGreg Roach 10996716c47SGreg Roach $this->layout = 'layouts/administration'; 11096716c47SGreg Roach 11196716c47SGreg Roach return $this->viewResponse('admin/trees-unconnected', [ 112f925fcc4SGreg Roach 'aliases' => $aliases, 11396716c47SGreg Roach 'associates' => $associates, 11496716c47SGreg Roach 'root' => $root, 11596716c47SGreg Roach 'individual_groups' => $individual_groups, 11696716c47SGreg Roach 'title' => $title, 11796716c47SGreg Roach 'tree' => $tree, 11896716c47SGreg Roach ]); 11996716c47SGreg Roach } 12096716c47SGreg Roach} 121