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; 25f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 26f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 27f5402f3dSGreg Roachuse Fisharebest\Webtrees\Site; 28f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree; 29f5402f3dSGreg Roachuse Illuminate\Support\Collection; 30f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface; 31f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 32f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 33f5402f3dSGreg Roach 34f5402f3dSGreg Roachuse function assert; 355525e3bcSGreg Roachuse function preg_replace; 365525e3bcSGreg Roachuse function redirect; 37f5402f3dSGreg Roachuse function trim; 38f5402f3dSGreg Roach 398b624174SGreg Roachuse const PREG_SET_ORDER; 408b624174SGreg Roach 41f5402f3dSGreg Roach/** 42f5402f3dSGreg Roach * Search for genealogy data 43f5402f3dSGreg Roach */ 44f5402f3dSGreg Roachclass SearchGeneralPage implements RequestHandlerInterface 45f5402f3dSGreg Roach{ 46f5402f3dSGreg Roach use ViewResponseTrait; 47f5402f3dSGreg Roach 48*c4943cffSGreg Roach private SearchService $search_service; 49f5402f3dSGreg Roach 50*c4943cffSGreg Roach private TreeService $tree_service; 51f5402f3dSGreg Roach 52f5402f3dSGreg Roach /** 53f5402f3dSGreg Roach * SearchController constructor. 54f5402f3dSGreg Roach * 55f5402f3dSGreg Roach * @param SearchService $search_service 56f5402f3dSGreg Roach * @param TreeService $tree_service 57f5402f3dSGreg Roach */ 58f5402f3dSGreg Roach public function __construct(SearchService $search_service, TreeService $tree_service) 59f5402f3dSGreg Roach { 60f5402f3dSGreg Roach $this->search_service = $search_service; 61f5402f3dSGreg Roach $this->tree_service = $tree_service; 62f5402f3dSGreg Roach } 63f5402f3dSGreg Roach 64f5402f3dSGreg Roach /** 65f5402f3dSGreg Roach * The standard search. 66f5402f3dSGreg Roach * 67f5402f3dSGreg Roach * @param ServerRequestInterface $request 68f5402f3dSGreg Roach * 69f5402f3dSGreg Roach * @return ResponseInterface 70f5402f3dSGreg Roach */ 71f5402f3dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 72f5402f3dSGreg Roach { 73f5402f3dSGreg Roach $tree = $request->getAttribute('tree'); 7475964c75SGreg Roach assert($tree instanceof Tree); 75f5402f3dSGreg Roach 76f5402f3dSGreg Roach $params = $request->getQueryParams(); 77f5402f3dSGreg Roach $query = $params['query'] ?? ''; 78f5402f3dSGreg Roach 79f5402f3dSGreg Roach // What type of records to search? 80f5402f3dSGreg Roach $search_individuals = (bool) ($params['search_individuals'] ?? false); 81f5402f3dSGreg Roach $search_families = (bool) ($params['search_families'] ?? false); 82f5402f3dSGreg Roach $search_repositories = (bool) ($params['search_repositories'] ?? false); 83f5402f3dSGreg Roach $search_sources = (bool) ($params['search_sources'] ?? false); 84f5402f3dSGreg Roach $search_notes = (bool) ($params['search_notes'] ?? false); 85f5402f3dSGreg Roach 8697b27c9dSGreg Roach // Default to families and individuals only 87f5402f3dSGreg Roach if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { 8897b27c9dSGreg Roach $search_families = true; 89f5402f3dSGreg Roach $search_individuals = true; 90f5402f3dSGreg Roach } 91f5402f3dSGreg Roach 92f5402f3dSGreg Roach // What to search for? 93f5402f3dSGreg Roach $search_terms = $this->extractSearchTerms($query); 94f5402f3dSGreg Roach 95fceda430SGreg Roach // What trees to search? 96f5402f3dSGreg Roach if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { 97ca2d6785SGreg Roach $all_trees = $this->tree_service->all(); 98f5402f3dSGreg Roach } else { 99ca2d6785SGreg Roach $all_trees = new Collection([$tree]); 100f5402f3dSGreg Roach } 101f5402f3dSGreg Roach 102ca2d6785SGreg Roach $search_tree_names = new Collection($params['search_trees'] ?? []); 103f5402f3dSGreg Roach 104ca2d6785SGreg Roach $search_trees = $all_trees 105ca2d6785SGreg Roach ->filter(static function (Tree $tree) use ($search_tree_names): bool { 106ca2d6785SGreg Roach return $search_tree_names->containsStrict($tree->name()); 107f5402f3dSGreg Roach }); 108f5402f3dSGreg Roach 109ca2d6785SGreg Roach if ($search_trees->isEmpty()) { 110ca2d6785SGreg Roach $search_trees->add($tree); 111f5402f3dSGreg Roach } 112f5402f3dSGreg Roach 113f5402f3dSGreg Roach // Do the search 114f5402f3dSGreg Roach $individuals = new Collection(); 115f5402f3dSGreg Roach $families = new Collection(); 116f5402f3dSGreg Roach $repositories = new Collection(); 117f5402f3dSGreg Roach $sources = new Collection(); 118f5402f3dSGreg Roach $notes = new Collection(); 119f5402f3dSGreg Roach 120f5402f3dSGreg Roach if ($search_terms !== []) { 121f5402f3dSGreg Roach if ($search_individuals) { 122ca2d6785SGreg Roach $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms); 123f5402f3dSGreg Roach } 124f5402f3dSGreg Roach 125f5402f3dSGreg Roach if ($search_families) { 126ca2d6785SGreg Roach $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms); 127ca2d6785SGreg Roach $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms); 128f5402f3dSGreg Roach 1298c627a69SGreg Roach $families = $tmp1->merge($tmp2)->unique(static function (Family $family): string { 1308c627a69SGreg Roach return $family->xref() . '@' . $family->tree()->id(); 1318c627a69SGreg Roach }); 132f5402f3dSGreg Roach } 133f5402f3dSGreg Roach 134f5402f3dSGreg Roach if ($search_repositories) { 135ca2d6785SGreg Roach $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); 136f5402f3dSGreg Roach } 137f5402f3dSGreg Roach 138f5402f3dSGreg Roach if ($search_sources) { 139ca2d6785SGreg Roach $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); 140f5402f3dSGreg Roach } 141f5402f3dSGreg Roach 142f5402f3dSGreg Roach if ($search_notes) { 143ca2d6785SGreg Roach $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); 144f5402f3dSGreg Roach } 145f5402f3dSGreg Roach } 146f5402f3dSGreg Roach 147f5402f3dSGreg Roach // If only 1 item is returned, automatically forward to that item 148f5402f3dSGreg Roach if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty()) { 149f5402f3dSGreg Roach return redirect($individuals->first()->url()); 150f5402f3dSGreg Roach } 151f5402f3dSGreg Roach 152f5402f3dSGreg Roach if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty()) { 153f5402f3dSGreg Roach return redirect($families->first()->url()); 154f5402f3dSGreg Roach } 155f5402f3dSGreg Roach 156f5402f3dSGreg Roach if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty()) { 157f5402f3dSGreg Roach return redirect($sources->first()->url()); 158f5402f3dSGreg Roach } 159f5402f3dSGreg Roach 160f5402f3dSGreg Roach if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1) { 161f5402f3dSGreg Roach return redirect($notes->first()->url()); 162f5402f3dSGreg Roach } 163f5402f3dSGreg Roach 164f5402f3dSGreg Roach $title = I18N::translate('General search'); 165f5402f3dSGreg Roach 166f5402f3dSGreg Roach return $this->viewResponse('search-general-page', [ 167f5402f3dSGreg Roach 'all_trees' => $all_trees, 168f5402f3dSGreg Roach 'families' => $families, 169f5402f3dSGreg Roach 'individuals' => $individuals, 170f5402f3dSGreg Roach 'notes' => $notes, 171f5402f3dSGreg Roach 'query' => $query, 172f5402f3dSGreg Roach 'repositories' => $repositories, 173f5402f3dSGreg Roach 'search_families' => $search_families, 174f5402f3dSGreg Roach 'search_individuals' => $search_individuals, 175f5402f3dSGreg Roach 'search_notes' => $search_notes, 176f5402f3dSGreg Roach 'search_repositories' => $search_repositories, 177f5402f3dSGreg Roach 'search_sources' => $search_sources, 178f5402f3dSGreg Roach 'search_trees' => $search_trees, 179f5402f3dSGreg Roach 'sources' => $sources, 180f5402f3dSGreg Roach 'title' => $title, 181f5402f3dSGreg Roach 'tree' => $tree, 182f5402f3dSGreg Roach ]); 183f5402f3dSGreg Roach } 184f5402f3dSGreg Roach 185f5402f3dSGreg Roach /** 186f5402f3dSGreg Roach * Convert the query into an array of search terms 187f5402f3dSGreg Roach * 188f5402f3dSGreg Roach * @param string $query 189f5402f3dSGreg Roach * 19024f2a3afSGreg Roach * @return array<string> 191f5402f3dSGreg Roach */ 192f5402f3dSGreg Roach private function extractSearchTerms(string $query): array 193f5402f3dSGreg Roach { 194f5402f3dSGreg Roach $search_terms = []; 195f5402f3dSGreg Roach 196f5402f3dSGreg Roach // Words in double quotes stay together 1978b624174SGreg Roach preg_match_all('/"([^"]+)"/', $query, $matches, PREG_SET_ORDER); 1988b624174SGreg Roach foreach ($matches as $match) { 199f5402f3dSGreg Roach $search_terms[] = trim($match[1]); 2008b624174SGreg Roach // Remove this string from the search query 2018b624174SGreg Roach $query = strtr($query, [$match[0] => '']); 202f5402f3dSGreg Roach } 203f5402f3dSGreg Roach 2045525e3bcSGreg Roach // Treat CJK characters as separate words, not as characters. 2055525e3bcSGreg Roach $query = preg_replace('/\p{Han}/u', '$0 ', $query); 2065525e3bcSGreg Roach 207f5402f3dSGreg Roach // Other words get treated separately 2088b624174SGreg Roach preg_match_all('/[\S]+/', $query, $matches, PREG_SET_ORDER); 2098b624174SGreg Roach foreach ($matches as $match) { 2108b624174SGreg Roach $search_terms[] = $match[0]; 211f5402f3dSGreg Roach } 212f5402f3dSGreg Roach 213f5402f3dSGreg Roach return $search_terms; 214f5402f3dSGreg Roach } 215f5402f3dSGreg Roach} 216