xref: /webtrees/app/Http/RequestHandlers/UnconnectedPage.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 Illuminate\Support\Collection;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function strtolower;
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       = Validator::attributes($request)->tree();
51        $user       = Validator::attributes($request)->user();
52        $aliases    = (bool) ($request->getQueryParams()['aliases'] ?? false);
53        $associates = (bool) ($request->getQueryParams()['associates'] ?? false);
54
55        // Connect individuals using these links.
56        $links = ['FAMS', 'FAMC'];
57
58        if ($aliases) {
59            $links[] = 'ALIA';
60        }
61
62        if ($associates) {
63            $links[] = 'ASSO';
64            $links[] = '_ASSO';
65        }
66
67        $rows = DB::table('link')
68            ->where('l_file', '=', $tree->id())
69            ->whereIn('l_type', $links)
70            ->select(['l_from', 'l_to'])
71            ->get();
72
73        $graph = DB::table('individuals')
74            ->where('i_file', '=', $tree->id())
75            ->pluck('i_id')
76            ->mapWithKeys(static function (string $xref): array {
77                return [$xref => []];
78            })
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