1f5402f3dSGreg Roach<?php 2f5402f3dSGreg Roach 3f5402f3dSGreg Roach/** 4f5402f3dSGreg Roach * webtrees: online genealogy 5f5402f3dSGreg Roach * Copyright (C) 2019 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 15f5402f3dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16f5402f3dSGreg Roach */ 17f5402f3dSGreg Roach 18f5402f3dSGreg Roachdeclare(strict_types=1); 19f5402f3dSGreg Roach 20f5402f3dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21f5402f3dSGreg Roach 22f5402f3dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 23f5402f3dSGreg Roachuse Fisharebest\Webtrees\I18N; 24f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 25f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 26f5402f3dSGreg Roachuse Fisharebest\Webtrees\Site; 27f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree; 28f5402f3dSGreg Roachuse Illuminate\Support\Collection; 29f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface; 30f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 32f5402f3dSGreg Roach 335525e3bcSGreg Roachuse function array_filter; 34f5402f3dSGreg Roachuse function assert; 355525e3bcSGreg Roachuse function in_array; 36f5402f3dSGreg Roachuse function preg_match; 375525e3bcSGreg Roachuse function preg_replace; 385525e3bcSGreg Roachuse function redirect; 39f5402f3dSGreg Roachuse function str_replace; 40f5402f3dSGreg Roachuse function trim; 41f5402f3dSGreg Roach 42f5402f3dSGreg Roach/** 43f5402f3dSGreg Roach * Search for genealogy data 44f5402f3dSGreg Roach */ 45f5402f3dSGreg Roachclass SearchGeneralPage implements RequestHandlerInterface 46f5402f3dSGreg Roach{ 47f5402f3dSGreg Roach use ViewResponseTrait; 48f5402f3dSGreg Roach 49f5402f3dSGreg Roach /** @var SearchService */ 50f5402f3dSGreg Roach private $search_service; 51f5402f3dSGreg Roach 52f5402f3dSGreg Roach /** @var TreeService */ 53f5402f3dSGreg Roach private $tree_service; 54f5402f3dSGreg Roach 55f5402f3dSGreg Roach /** 56f5402f3dSGreg Roach * SearchController constructor. 57f5402f3dSGreg Roach * 58f5402f3dSGreg Roach * @param SearchService $search_service 59f5402f3dSGreg Roach * @param TreeService $tree_service 60f5402f3dSGreg Roach */ 61f5402f3dSGreg Roach public function __construct(SearchService $search_service, TreeService $tree_service) 62f5402f3dSGreg Roach { 63f5402f3dSGreg Roach $this->search_service = $search_service; 64f5402f3dSGreg Roach $this->tree_service = $tree_service; 65f5402f3dSGreg Roach } 66f5402f3dSGreg Roach 67f5402f3dSGreg Roach /** 68f5402f3dSGreg Roach * The standard search. 69f5402f3dSGreg Roach * 70f5402f3dSGreg Roach * @param ServerRequestInterface $request 71f5402f3dSGreg Roach * 72f5402f3dSGreg Roach * @return ResponseInterface 73f5402f3dSGreg Roach */ 74f5402f3dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 75f5402f3dSGreg Roach { 76f5402f3dSGreg Roach $tree = $request->getAttribute('tree'); 7775964c75SGreg Roach assert($tree instanceof 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); 85f5402f3dSGreg Roach $search_repositories = (bool) ($params['search_repositories'] ?? false); 86f5402f3dSGreg Roach $search_sources = (bool) ($params['search_sources'] ?? false); 87f5402f3dSGreg Roach $search_notes = (bool) ($params['search_notes'] ?? false); 88f5402f3dSGreg Roach 89*97b27c9dSGreg Roach // Default to families and individuals only 90f5402f3dSGreg Roach if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { 91*97b27c9dSGreg Roach $search_families = true; 92f5402f3dSGreg Roach $search_individuals = true; 93f5402f3dSGreg Roach } 94f5402f3dSGreg Roach 95f5402f3dSGreg Roach // What to search for? 96f5402f3dSGreg Roach $search_terms = $this->extractSearchTerms($query); 97f5402f3dSGreg Roach 98f5402f3dSGreg Roach // What trees to seach? 99f5402f3dSGreg Roach if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { 100f5402f3dSGreg Roach $all_trees = $this->tree_service->all()->all(); 101f5402f3dSGreg Roach } else { 102f5402f3dSGreg Roach $all_trees = [$tree]; 103f5402f3dSGreg Roach } 104f5402f3dSGreg Roach 105f5402f3dSGreg Roach $search_tree_names = $params['search_trees'] ?? []; 106f5402f3dSGreg Roach 107f5402f3dSGreg Roach $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool { 108f5402f3dSGreg Roach return in_array($tree->name(), $search_tree_names, true); 109f5402f3dSGreg Roach }); 110f5402f3dSGreg Roach 111f5402f3dSGreg Roach if ($search_trees === []) { 112f5402f3dSGreg Roach $search_trees = [$tree]; 113f5402f3dSGreg Roach } 114f5402f3dSGreg Roach 115f5402f3dSGreg Roach // Do the search 116f5402f3dSGreg Roach $individuals = new Collection(); 117f5402f3dSGreg Roach $families = new Collection(); 118f5402f3dSGreg Roach $repositories = new Collection(); 119f5402f3dSGreg Roach $sources = new Collection(); 120f5402f3dSGreg Roach $notes = new Collection(); 121f5402f3dSGreg Roach 122f5402f3dSGreg Roach if ($search_terms !== []) { 123f5402f3dSGreg Roach if ($search_individuals) { 124f5402f3dSGreg Roach $individuals = $this->search_service->searchIndividuals($search_trees, $search_terms); 125f5402f3dSGreg Roach } 126f5402f3dSGreg Roach 127f5402f3dSGreg Roach if ($search_families) { 128f5402f3dSGreg Roach $tmp1 = $this->search_service->searchFamilies($search_trees, $search_terms); 129f5402f3dSGreg Roach $tmp2 = $this->search_service->searchFamilyNames($search_trees, $search_terms); 130f5402f3dSGreg Roach 131f5402f3dSGreg Roach $families = $tmp1->merge($tmp2)->unique(); 132f5402f3dSGreg Roach } 133f5402f3dSGreg Roach 134f5402f3dSGreg Roach if ($search_repositories) { 135f5402f3dSGreg Roach $repositories = $this->search_service->searchRepositories($search_trees, $search_terms); 136f5402f3dSGreg Roach } 137f5402f3dSGreg Roach 138f5402f3dSGreg Roach if ($search_sources) { 139f5402f3dSGreg Roach $sources = $this->search_service->searchSources($search_trees, $search_terms); 140f5402f3dSGreg Roach } 141f5402f3dSGreg Roach 142f5402f3dSGreg Roach if ($search_notes) { 143f5402f3dSGreg Roach $notes = $this->search_service->searchNotes($search_trees, $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 * 190f5402f3dSGreg Roach * @return 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 197f5402f3dSGreg Roach while (preg_match('/"([^"]+)"/', $query, $match)) { 198f5402f3dSGreg Roach $search_terms[] = trim($match[1]); 199f5402f3dSGreg Roach $query = str_replace($match[0], '', $query); 200f5402f3dSGreg Roach } 201f5402f3dSGreg Roach 2025525e3bcSGreg Roach // Treat CJK characters as separate words, not as characters. 2035525e3bcSGreg Roach $query = preg_replace('/\p{Han}/u', '$0 ', $query); 2045525e3bcSGreg Roach 205f5402f3dSGreg Roach // Other words get treated separately 206f5402f3dSGreg Roach while (preg_match('/[\S]+/', $query, $match)) { 207f5402f3dSGreg Roach $search_terms[] = trim($match[0]); 208f5402f3dSGreg Roach $query = str_replace($match[0], '', $query); 209f5402f3dSGreg Roach } 210f5402f3dSGreg Roach 211f5402f3dSGreg Roach return $search_terms; 212f5402f3dSGreg Roach } 213f5402f3dSGreg Roach} 214