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