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