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