xref: /webtrees/app/Http/RequestHandlers/SearchGeneralPage.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1f5402f3dSGreg Roach<?php
2f5402f3dSGreg Roach
3f5402f3dSGreg Roach/**
4f5402f3dSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6f5402f3dSGreg Roach * This program is free software: you can redistribute it and/or modify
7f5402f3dSGreg Roach * it under the terms of the GNU General Public License as published by
8f5402f3dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f5402f3dSGreg Roach * (at your option) any later version.
10f5402f3dSGreg Roach * This program is distributed in the hope that it will be useful,
11f5402f3dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f5402f3dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f5402f3dSGreg Roach * GNU General Public License for more details.
14f5402f3dSGreg 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/>.
16f5402f3dSGreg Roach */
17f5402f3dSGreg Roach
18f5402f3dSGreg Roachdeclare(strict_types=1);
19f5402f3dSGreg Roach
20f5402f3dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21f5402f3dSGreg Roach
2245ee34f2SGreg Roachuse Fisharebest\Webtrees\Auth;
236f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
248c627a69SGreg Roachuse Fisharebest\Webtrees\Family;
25f5402f3dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26f5402f3dSGreg Roachuse Fisharebest\Webtrees\I18N;
27058ba724SGreg Roachuse Fisharebest\Webtrees\Location;
2845ee34f2SGreg Roachuse Fisharebest\Webtrees\Log;
29058ba724SGreg Roachuse Fisharebest\Webtrees\Note;
30058ba724SGreg Roachuse Fisharebest\Webtrees\Repository;
31f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
32f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
33f5402f3dSGreg Roachuse Fisharebest\Webtrees\Site;
34f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree;
35b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
36f5402f3dSGreg Roachuse Illuminate\Support\Collection;
37f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface;
38f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
39f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
40f5402f3dSGreg Roach
41748dbe15SGreg Roachuse function in_array;
425525e3bcSGreg Roachuse function preg_replace;
435525e3bcSGreg Roachuse function redirect;
44f5402f3dSGreg Roachuse function trim;
45f5402f3dSGreg Roach
468b624174SGreg Roachuse const PREG_SET_ORDER;
478b624174SGreg Roach
48f5402f3dSGreg Roach/**
49f5402f3dSGreg Roach * Search for genealogy data
50f5402f3dSGreg Roach */
51f5402f3dSGreg Roachclass SearchGeneralPage implements RequestHandlerInterface
52f5402f3dSGreg Roach{
53f5402f3dSGreg Roach    use ViewResponseTrait;
54f5402f3dSGreg Roach
55c4943cffSGreg Roach    private SearchService $search_service;
56f5402f3dSGreg Roach
57c4943cffSGreg Roach    private TreeService $tree_service;
58f5402f3dSGreg Roach
59f5402f3dSGreg Roach    /**
60f5402f3dSGreg Roach     * @param SearchService $search_service
61f5402f3dSGreg Roach     * @param TreeService   $tree_service
62f5402f3dSGreg Roach     */
63f5402f3dSGreg Roach    public function __construct(SearchService $search_service, TreeService $tree_service)
64f5402f3dSGreg Roach    {
65f5402f3dSGreg Roach        $this->search_service = $search_service;
66f5402f3dSGreg Roach        $this->tree_service   = $tree_service;
67f5402f3dSGreg Roach    }
68f5402f3dSGreg Roach
69f5402f3dSGreg Roach    /**
70f5402f3dSGreg Roach     * The standard search.
71f5402f3dSGreg Roach     *
72f5402f3dSGreg Roach     * @param ServerRequestInterface $request
73f5402f3dSGreg Roach     *
74f5402f3dSGreg Roach     * @return ResponseInterface
75f5402f3dSGreg Roach     */
76f5402f3dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
77f5402f3dSGreg Roach    {
78b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
79f5402f3dSGreg Roach
80748dbe15SGreg Roach        $query = Validator::queryParams($request)->string('query', '');
81f5402f3dSGreg Roach
82f5402f3dSGreg Roach        // What type of records to search?
83748dbe15SGreg Roach        $search_individuals  = Validator::queryParams($request)->boolean('search_individuals', false);
84748dbe15SGreg Roach        $search_families     = Validator::queryParams($request)->boolean('search_families', false);
85748dbe15SGreg Roach        $search_locations    = Validator::queryParams($request)->boolean('search_locations', false);
86748dbe15SGreg Roach        $search_repositories = Validator::queryParams($request)->boolean('search_repositories', false);
87748dbe15SGreg Roach        $search_sources      = Validator::queryParams($request)->boolean('search_sources', false);
88748dbe15SGreg Roach        $search_notes        = Validator::queryParams($request)->boolean('search_notes', false);
89748dbe15SGreg Roach
90748dbe15SGreg Roach        // Where to search
91748dbe15SGreg Roach        $search_tree_names = Validator::queryParams($request)->array('search_trees');
92f5402f3dSGreg Roach
93058ba724SGreg Roach        $exist_notes = DB::table('other')
94058ba724SGreg Roach            ->where('o_file', '=', $tree->id())
95058ba724SGreg Roach            ->where('o_type', '=', Note::RECORD_TYPE)
96058ba724SGreg Roach            ->exists();
97058ba724SGreg Roach
98058ba724SGreg Roach        $exist_locations = DB::table('other')
99058ba724SGreg Roach            ->where('o_file', '=', $tree->id())
100058ba724SGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
101058ba724SGreg Roach            ->exists();
102058ba724SGreg Roach
103058ba724SGreg Roach        $exist_repositories = DB::table('other')
104058ba724SGreg Roach            ->where('o_file', '=', $tree->id())
105058ba724SGreg Roach            ->where('o_type', '=', Repository::RECORD_TYPE)
106058ba724SGreg Roach            ->exists();
107058ba724SGreg Roach
108058ba724SGreg Roach        $exist_sources = DB::table('sources')
109058ba724SGreg Roach            ->where('s_file', '=', $tree->id())
110058ba724SGreg Roach            ->exists();
111058ba724SGreg Roach
11297b27c9dSGreg Roach        // Default to families and individuals only
113f5402f3dSGreg Roach        if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) {
11497b27c9dSGreg Roach            $search_families    = true;
115f5402f3dSGreg Roach            $search_individuals = true;
116f5402f3dSGreg Roach        }
117f5402f3dSGreg Roach
118f5402f3dSGreg Roach        // What to search for?
119f5402f3dSGreg Roach        $search_terms = $this->extractSearchTerms($query);
120f5402f3dSGreg Roach
121fceda430SGreg Roach        // What trees to search?
122f5402f3dSGreg Roach        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
123ca2d6785SGreg Roach            $all_trees = $this->tree_service->all();
124f5402f3dSGreg Roach        } else {
125ca2d6785SGreg Roach            $all_trees = new Collection([$tree]);
126f5402f3dSGreg Roach        }
127f5402f3dSGreg Roach
128ca2d6785SGreg Roach        $search_trees = $all_trees
129748dbe15SGreg Roach            ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true));
130f5402f3dSGreg Roach
131ca2d6785SGreg Roach        if ($search_trees->isEmpty()) {
132ca2d6785SGreg Roach            $search_trees->add($tree);
133f5402f3dSGreg Roach        }
134f5402f3dSGreg Roach
135f5402f3dSGreg Roach        // Do the search
136f5402f3dSGreg Roach        $individuals  = new Collection();
137f5402f3dSGreg Roach        $families     = new Collection();
138058ba724SGreg Roach        $locations    = new Collection();
139f5402f3dSGreg Roach        $repositories = new Collection();
140f5402f3dSGreg Roach        $sources      = new Collection();
141f5402f3dSGreg Roach        $notes        = new Collection();
142f5402f3dSGreg Roach
143f5402f3dSGreg Roach        if ($search_terms !== []) {
14445ee34f2SGreg Roach            // Log search requests for visitors
14545ee34f2SGreg Roach            if (Auth::id() === null) {
14645ee34f2SGreg Roach                Log::addSearchLog('General: ' . $query, $search_trees->all());
14745ee34f2SGreg Roach            }
14845ee34f2SGreg Roach
149f5402f3dSGreg Roach            if ($search_individuals) {
150ca2d6785SGreg Roach                $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms);
151f5402f3dSGreg Roach            }
152f5402f3dSGreg Roach
153f5402f3dSGreg Roach            if ($search_families) {
154ca2d6785SGreg Roach                $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms);
155ca2d6785SGreg Roach                $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms);
156f5402f3dSGreg Roach
157*f25fc0f9SGreg Roach                $families = $tmp1->merge($tmp2)->unique(static fn (Family $family): string => $family->xref() . '@' . $family->tree()->id());
158f5402f3dSGreg Roach            }
159f5402f3dSGreg Roach
160f5402f3dSGreg Roach            if ($search_repositories) {
161ca2d6785SGreg Roach                $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms);
162f5402f3dSGreg Roach            }
163f5402f3dSGreg Roach
164f5402f3dSGreg Roach            if ($search_sources) {
165ca2d6785SGreg Roach                $sources = $this->search_service->searchSources($search_trees->all(), $search_terms);
166f5402f3dSGreg Roach            }
167f5402f3dSGreg Roach
168f5402f3dSGreg Roach            if ($search_notes) {
169ca2d6785SGreg Roach                $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms);
170f5402f3dSGreg Roach            }
171058ba724SGreg Roach
172058ba724SGreg Roach            if ($search_locations) {
173058ba724SGreg Roach                $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms);
174058ba724SGreg Roach            }
175f5402f3dSGreg Roach        }
176f5402f3dSGreg Roach
177f5402f3dSGreg Roach        // If only 1 item is returned, automatically forward to that item
178058ba724SGreg Roach        if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) {
179f5402f3dSGreg Roach            return redirect($individuals->first()->url());
180f5402f3dSGreg Roach        }
181f5402f3dSGreg Roach
182058ba724SGreg Roach        if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) {
183f5402f3dSGreg Roach            return redirect($families->first()->url());
184f5402f3dSGreg Roach        }
185f5402f3dSGreg Roach
186058ba724SGreg Roach        if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) {
187f5402f3dSGreg Roach            return redirect($sources->first()->url());
188f5402f3dSGreg Roach        }
189f5402f3dSGreg Roach
190058ba724SGreg Roach        if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) {
191f5402f3dSGreg Roach            return redirect($notes->first()->url());
192f5402f3dSGreg Roach        }
193f5402f3dSGreg Roach
194058ba724SGreg Roach        if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) {
195058ba724SGreg Roach            return redirect($locations->first()->url());
196058ba724SGreg Roach        }
197058ba724SGreg Roach
198f5402f3dSGreg Roach        $title = I18N::translate('General search');
199f5402f3dSGreg Roach
200f5402f3dSGreg Roach        return $this->viewResponse('search-general-page', [
201f5402f3dSGreg Roach            'all_trees'           => $all_trees,
202058ba724SGreg Roach            'exist_locations'     => $exist_locations,
203058ba724SGreg Roach            'exist_notes'         => $exist_notes,
204058ba724SGreg Roach            'exist_repositories'  => $exist_repositories,
205058ba724SGreg Roach            'exist_sources'       => $exist_sources,
206f5402f3dSGreg Roach            'families'            => $families,
207f5402f3dSGreg Roach            'individuals'         => $individuals,
208058ba724SGreg Roach            'locations'           => $locations,
209f5402f3dSGreg Roach            'notes'               => $notes,
210f5402f3dSGreg Roach            'query'               => $query,
211f5402f3dSGreg Roach            'repositories'        => $repositories,
212f5402f3dSGreg Roach            'search_families'     => $search_families,
213f5402f3dSGreg Roach            'search_individuals'  => $search_individuals,
214058ba724SGreg Roach            'search_locations'    => $search_locations,
215f5402f3dSGreg Roach            'search_notes'        => $search_notes,
216f5402f3dSGreg Roach            'search_repositories' => $search_repositories,
217f5402f3dSGreg Roach            'search_sources'      => $search_sources,
218f5402f3dSGreg Roach            'search_trees'        => $search_trees,
219f5402f3dSGreg Roach            'sources'             => $sources,
220f5402f3dSGreg Roach            'title'               => $title,
221f5402f3dSGreg Roach            'tree'                => $tree,
222f5402f3dSGreg Roach        ]);
223f5402f3dSGreg Roach    }
224f5402f3dSGreg Roach
225f5402f3dSGreg Roach    /**
226f5402f3dSGreg Roach     * Convert the query into an array of search terms
227f5402f3dSGreg Roach     *
228f5402f3dSGreg Roach     * @param string $query
229f5402f3dSGreg Roach     *
23024f2a3afSGreg Roach     * @return array<string>
231f5402f3dSGreg Roach     */
232f5402f3dSGreg Roach    private function extractSearchTerms(string $query): array
233f5402f3dSGreg Roach    {
234f5402f3dSGreg Roach        $search_terms = [];
235f5402f3dSGreg Roach
236f5402f3dSGreg Roach        // Words in double quotes stay together
2378b624174SGreg Roach        preg_match_all('/"([^"]+)"/', $query, $matches, PREG_SET_ORDER);
2388b624174SGreg Roach        foreach ($matches as $match) {
239f5402f3dSGreg Roach            $search_terms[] = trim($match[1]);
2408b624174SGreg Roach            // Remove this string from the search query
2418b624174SGreg Roach            $query = strtr($query, [$match[0] => '']);
242f5402f3dSGreg Roach        }
243f5402f3dSGreg Roach
2445525e3bcSGreg Roach        // Treat CJK characters as separate words, not as characters.
2455525e3bcSGreg Roach        $query = preg_replace('/\p{Han}/u', '$0 ', $query);
2465525e3bcSGreg Roach
247f5402f3dSGreg Roach        // Other words get treated separately
2488b624174SGreg Roach        preg_match_all('/[\S]+/', $query, $matches, PREG_SET_ORDER);
2498b624174SGreg Roach        foreach ($matches as $match) {
2508b624174SGreg Roach            $search_terms[] = $match[0];
251f5402f3dSGreg Roach        }
252f5402f3dSGreg Roach
253f5402f3dSGreg Roach        return $search_terms;
254f5402f3dSGreg Roach    }
255f5402f3dSGreg Roach}
256