1f5402f3dSGreg Roach<?php 2f5402f3dSGreg Roach 3f5402f3dSGreg Roach/** 4f5402f3dSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 228c627a69SGreg Roachuse Fisharebest\Webtrees\Family; 23f5402f3dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 24f5402f3dSGreg Roachuse Fisharebest\Webtrees\I18N; 25058ba724SGreg Roachuse Fisharebest\Webtrees\Location; 26058ba724SGreg Roachuse Fisharebest\Webtrees\Note; 27058ba724SGreg Roachuse Fisharebest\Webtrees\Repository; 28f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 29f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 30f5402f3dSGreg Roachuse Fisharebest\Webtrees\Site; 31f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree; 32*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 33058ba724SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 34f5402f3dSGreg Roachuse Illuminate\Support\Collection; 35f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface; 36f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 37f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 38f5402f3dSGreg Roach 395525e3bcSGreg Roachuse function preg_replace; 405525e3bcSGreg Roachuse function redirect; 41f5402f3dSGreg Roachuse function trim; 42f5402f3dSGreg Roach 438b624174SGreg Roachuse const PREG_SET_ORDER; 448b624174SGreg Roach 45f5402f3dSGreg Roach/** 46f5402f3dSGreg Roach * Search for genealogy data 47f5402f3dSGreg Roach */ 48f5402f3dSGreg Roachclass SearchGeneralPage implements RequestHandlerInterface 49f5402f3dSGreg Roach{ 50f5402f3dSGreg Roach use ViewResponseTrait; 51f5402f3dSGreg Roach 52c4943cffSGreg Roach private SearchService $search_service; 53f5402f3dSGreg Roach 54c4943cffSGreg Roach private TreeService $tree_service; 55f5402f3dSGreg Roach 56f5402f3dSGreg Roach /** 57f5402f3dSGreg Roach * SearchController constructor. 58f5402f3dSGreg Roach * 59f5402f3dSGreg Roach * @param SearchService $search_service 60f5402f3dSGreg Roach * @param TreeService $tree_service 61f5402f3dSGreg Roach */ 62f5402f3dSGreg Roach public function __construct(SearchService $search_service, TreeService $tree_service) 63f5402f3dSGreg Roach { 64f5402f3dSGreg Roach $this->search_service = $search_service; 65f5402f3dSGreg Roach $this->tree_service = $tree_service; 66f5402f3dSGreg Roach } 67f5402f3dSGreg Roach 68f5402f3dSGreg Roach /** 69f5402f3dSGreg Roach * The standard search. 70f5402f3dSGreg Roach * 71f5402f3dSGreg Roach * @param ServerRequestInterface $request 72f5402f3dSGreg Roach * 73f5402f3dSGreg Roach * @return ResponseInterface 74f5402f3dSGreg Roach */ 75f5402f3dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 76f5402f3dSGreg Roach { 77*b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 78f5402f3dSGreg Roach 79f5402f3dSGreg Roach $params = $request->getQueryParams(); 80f5402f3dSGreg Roach $query = $params['query'] ?? ''; 81f5402f3dSGreg Roach 82f5402f3dSGreg Roach // What type of records to search? 83f5402f3dSGreg Roach $search_individuals = (bool) ($params['search_individuals'] ?? false); 84f5402f3dSGreg Roach $search_families = (bool) ($params['search_families'] ?? false); 85058ba724SGreg Roach $search_locations = (bool) ($params['search_locations'] ?? false); 86f5402f3dSGreg Roach $search_repositories = (bool) ($params['search_repositories'] ?? false); 87f5402f3dSGreg Roach $search_sources = (bool) ($params['search_sources'] ?? false); 88f5402f3dSGreg Roach $search_notes = (bool) ($params['search_notes'] ?? false); 89f5402f3dSGreg Roach 90058ba724SGreg Roach $exist_notes = DB::table('other') 91058ba724SGreg Roach ->where('o_file', '=', $tree->id()) 92058ba724SGreg Roach ->where('o_type', '=', Note::RECORD_TYPE) 93058ba724SGreg Roach ->exists(); 94058ba724SGreg Roach 95058ba724SGreg Roach $exist_locations = DB::table('other') 96058ba724SGreg Roach ->where('o_file', '=', $tree->id()) 97058ba724SGreg Roach ->where('o_type', '=', Location::RECORD_TYPE) 98058ba724SGreg Roach ->exists(); 99058ba724SGreg Roach 100058ba724SGreg Roach $exist_repositories = DB::table('other') 101058ba724SGreg Roach ->where('o_file', '=', $tree->id()) 102058ba724SGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE) 103058ba724SGreg Roach ->exists(); 104058ba724SGreg Roach 105058ba724SGreg Roach $exist_sources = DB::table('sources') 106058ba724SGreg Roach ->where('s_file', '=', $tree->id()) 107058ba724SGreg Roach ->exists(); 108058ba724SGreg Roach 10997b27c9dSGreg Roach // Default to families and individuals only 110f5402f3dSGreg Roach if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { 11197b27c9dSGreg Roach $search_families = true; 112f5402f3dSGreg Roach $search_individuals = true; 113f5402f3dSGreg Roach } 114f5402f3dSGreg Roach 115f5402f3dSGreg Roach // What to search for? 116f5402f3dSGreg Roach $search_terms = $this->extractSearchTerms($query); 117f5402f3dSGreg Roach 118fceda430SGreg Roach // What trees to search? 119f5402f3dSGreg Roach if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { 120ca2d6785SGreg Roach $all_trees = $this->tree_service->all(); 121f5402f3dSGreg Roach } else { 122ca2d6785SGreg Roach $all_trees = new Collection([$tree]); 123f5402f3dSGreg Roach } 124f5402f3dSGreg Roach 125ca2d6785SGreg Roach $search_tree_names = new Collection($params['search_trees'] ?? []); 126f5402f3dSGreg Roach 127ca2d6785SGreg Roach $search_trees = $all_trees 128ca2d6785SGreg Roach ->filter(static function (Tree $tree) use ($search_tree_names): bool { 129ca2d6785SGreg Roach return $search_tree_names->containsStrict($tree->name()); 130f5402f3dSGreg Roach }); 131f5402f3dSGreg Roach 132ca2d6785SGreg Roach if ($search_trees->isEmpty()) { 133ca2d6785SGreg Roach $search_trees->add($tree); 134f5402f3dSGreg Roach } 135f5402f3dSGreg Roach 136f5402f3dSGreg Roach // Do the search 137f5402f3dSGreg Roach $individuals = new Collection(); 138f5402f3dSGreg Roach $families = new Collection(); 139058ba724SGreg Roach $locations = new Collection(); 140f5402f3dSGreg Roach $repositories = new Collection(); 141f5402f3dSGreg Roach $sources = new Collection(); 142f5402f3dSGreg Roach $notes = new Collection(); 143f5402f3dSGreg Roach 144f5402f3dSGreg Roach if ($search_terms !== []) { 145f5402f3dSGreg Roach if ($search_individuals) { 146ca2d6785SGreg Roach $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms); 147f5402f3dSGreg Roach } 148f5402f3dSGreg Roach 149f5402f3dSGreg Roach if ($search_families) { 150ca2d6785SGreg Roach $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms); 151ca2d6785SGreg Roach $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms); 152f5402f3dSGreg Roach 1538c627a69SGreg Roach $families = $tmp1->merge($tmp2)->unique(static function (Family $family): string { 1548c627a69SGreg Roach return $family->xref() . '@' . $family->tree()->id(); 1558c627a69SGreg Roach }); 156f5402f3dSGreg Roach } 157f5402f3dSGreg Roach 158f5402f3dSGreg Roach if ($search_repositories) { 159ca2d6785SGreg Roach $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); 160f5402f3dSGreg Roach } 161f5402f3dSGreg Roach 162f5402f3dSGreg Roach if ($search_sources) { 163ca2d6785SGreg Roach $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); 164f5402f3dSGreg Roach } 165f5402f3dSGreg Roach 166f5402f3dSGreg Roach if ($search_notes) { 167ca2d6785SGreg Roach $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); 168f5402f3dSGreg Roach } 169058ba724SGreg Roach 170058ba724SGreg Roach if ($search_locations) { 171058ba724SGreg Roach $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms); 172058ba724SGreg Roach } 173f5402f3dSGreg Roach } 174f5402f3dSGreg Roach 175f5402f3dSGreg Roach // If only 1 item is returned, automatically forward to that item 176058ba724SGreg Roach if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { 177f5402f3dSGreg Roach return redirect($individuals->first()->url()); 178f5402f3dSGreg Roach } 179f5402f3dSGreg Roach 180058ba724SGreg Roach if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { 181f5402f3dSGreg Roach return redirect($families->first()->url()); 182f5402f3dSGreg Roach } 183f5402f3dSGreg Roach 184058ba724SGreg Roach if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) { 185f5402f3dSGreg Roach return redirect($sources->first()->url()); 186f5402f3dSGreg Roach } 187f5402f3dSGreg Roach 188058ba724SGreg Roach if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) { 189f5402f3dSGreg Roach return redirect($notes->first()->url()); 190f5402f3dSGreg Roach } 191f5402f3dSGreg Roach 192058ba724SGreg Roach if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) { 193058ba724SGreg Roach return redirect($locations->first()->url()); 194058ba724SGreg Roach } 195058ba724SGreg Roach 196f5402f3dSGreg Roach $title = I18N::translate('General search'); 197f5402f3dSGreg Roach 198f5402f3dSGreg Roach return $this->viewResponse('search-general-page', [ 199f5402f3dSGreg Roach 'all_trees' => $all_trees, 200058ba724SGreg Roach 'exist_locations' => $exist_locations, 201058ba724SGreg Roach 'exist_notes' => $exist_notes, 202058ba724SGreg Roach 'exist_repositories' => $exist_repositories, 203058ba724SGreg Roach 'exist_sources' => $exist_sources, 204f5402f3dSGreg Roach 'families' => $families, 205f5402f3dSGreg Roach 'individuals' => $individuals, 206058ba724SGreg Roach 'locations' => $locations, 207f5402f3dSGreg Roach 'notes' => $notes, 208f5402f3dSGreg Roach 'query' => $query, 209f5402f3dSGreg Roach 'repositories' => $repositories, 210f5402f3dSGreg Roach 'search_families' => $search_families, 211f5402f3dSGreg Roach 'search_individuals' => $search_individuals, 212058ba724SGreg Roach 'search_locations' => $search_locations, 213f5402f3dSGreg Roach 'search_notes' => $search_notes, 214f5402f3dSGreg Roach 'search_repositories' => $search_repositories, 215f5402f3dSGreg Roach 'search_sources' => $search_sources, 216f5402f3dSGreg Roach 'search_trees' => $search_trees, 217f5402f3dSGreg Roach 'sources' => $sources, 218f5402f3dSGreg Roach 'title' => $title, 219f5402f3dSGreg Roach 'tree' => $tree, 220f5402f3dSGreg Roach ]); 221f5402f3dSGreg Roach } 222f5402f3dSGreg Roach 223f5402f3dSGreg Roach /** 224f5402f3dSGreg Roach * Convert the query into an array of search terms 225f5402f3dSGreg Roach * 226f5402f3dSGreg Roach * @param string $query 227f5402f3dSGreg Roach * 22824f2a3afSGreg Roach * @return array<string> 229f5402f3dSGreg Roach */ 230f5402f3dSGreg Roach private function extractSearchTerms(string $query): array 231f5402f3dSGreg Roach { 232f5402f3dSGreg Roach $search_terms = []; 233f5402f3dSGreg Roach 234f5402f3dSGreg Roach // Words in double quotes stay together 2358b624174SGreg Roach preg_match_all('/"([^"]+)"/', $query, $matches, PREG_SET_ORDER); 2368b624174SGreg Roach foreach ($matches as $match) { 237f5402f3dSGreg Roach $search_terms[] = trim($match[1]); 2388b624174SGreg Roach // Remove this string from the search query 2398b624174SGreg Roach $query = strtr($query, [$match[0] => '']); 240f5402f3dSGreg Roach } 241f5402f3dSGreg Roach 2425525e3bcSGreg Roach // Treat CJK characters as separate words, not as characters. 2435525e3bcSGreg Roach $query = preg_replace('/\p{Han}/u', '$0 ', $query); 2445525e3bcSGreg Roach 245f5402f3dSGreg Roach // Other words get treated separately 2468b624174SGreg Roach preg_match_all('/[\S]+/', $query, $matches, PREG_SET_ORDER); 2478b624174SGreg Roach foreach ($matches as $match) { 2488b624174SGreg Roach $search_terms[] = $match[0]; 249f5402f3dSGreg Roach } 250f5402f3dSGreg Roach 251f5402f3dSGreg Roach return $search_terms; 252f5402f3dSGreg Roach } 253f5402f3dSGreg Roach} 254