1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Webtrees\Auth; 23use Fisharebest\Webtrees\DB; 24use Fisharebest\Webtrees\Family; 25use Fisharebest\Webtrees\Http\ViewResponseTrait; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Location; 28use Fisharebest\Webtrees\Log; 29use Fisharebest\Webtrees\Note; 30use Fisharebest\Webtrees\Repository; 31use Fisharebest\Webtrees\Services\SearchService; 32use Fisharebest\Webtrees\Services\TreeService; 33use Fisharebest\Webtrees\Site; 34use Fisharebest\Webtrees\Tree; 35use Fisharebest\Webtrees\Validator; 36use Illuminate\Support\Collection; 37use Psr\Http\Message\ResponseInterface; 38use Psr\Http\Message\ServerRequestInterface; 39use Psr\Http\Server\RequestHandlerInterface; 40 41use function in_array; 42use function preg_replace; 43use function redirect; 44use function trim; 45 46use const PREG_SET_ORDER; 47 48/** 49 * Search for genealogy data 50 */ 51class SearchGeneralPage implements RequestHandlerInterface 52{ 53 use ViewResponseTrait; 54 55 private SearchService $search_service; 56 57 private TreeService $tree_service; 58 59 /** 60 * @param SearchService $search_service 61 * @param TreeService $tree_service 62 */ 63 public function __construct(SearchService $search_service, TreeService $tree_service) 64 { 65 $this->search_service = $search_service; 66 $this->tree_service = $tree_service; 67 } 68 69 /** 70 * The standard search. 71 * 72 * @param ServerRequestInterface $request 73 * 74 * @return ResponseInterface 75 */ 76 public function handle(ServerRequestInterface $request): ResponseInterface 77 { 78 $tree = Validator::attributes($request)->tree(); 79 80 $query = Validator::queryParams($request)->string('query', ''); 81 82 // What type of records to search? 83 $search_individuals = Validator::queryParams($request)->boolean('search_individuals', false); 84 $search_families = Validator::queryParams($request)->boolean('search_families', false); 85 $search_locations = Validator::queryParams($request)->boolean('search_locations', false); 86 $search_repositories = Validator::queryParams($request)->boolean('search_repositories', false); 87 $search_sources = Validator::queryParams($request)->boolean('search_sources', false); 88 $search_notes = Validator::queryParams($request)->boolean('search_notes', false); 89 90 // Where to search 91 $search_tree_names = Validator::queryParams($request)->array('search_trees'); 92 93 $exist_notes = DB::table('other') 94 ->where('o_file', '=', $tree->id()) 95 ->where('o_type', '=', Note::RECORD_TYPE) 96 ->exists(); 97 98 $exist_locations = DB::table('other') 99 ->where('o_file', '=', $tree->id()) 100 ->where('o_type', '=', Location::RECORD_TYPE) 101 ->exists(); 102 103 $exist_repositories = DB::table('other') 104 ->where('o_file', '=', $tree->id()) 105 ->where('o_type', '=', Repository::RECORD_TYPE) 106 ->exists(); 107 108 $exist_sources = DB::table('sources') 109 ->where('s_file', '=', $tree->id()) 110 ->exists(); 111 112 // Default to families and individuals only 113 if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { 114 $search_families = true; 115 $search_individuals = true; 116 } 117 118 // What to search for? 119 $search_terms = $this->extractSearchTerms($query); 120 121 // What trees to search? 122 if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { 123 $all_trees = $this->tree_service->all(); 124 } else { 125 $all_trees = new Collection([$tree]); 126 } 127 128 $search_trees = $all_trees 129 ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true)); 130 131 if ($search_trees->isEmpty()) { 132 $search_trees->add($tree); 133 } 134 135 // Do the search 136 $individuals = new Collection(); 137 $families = new Collection(); 138 $locations = new Collection(); 139 $repositories = new Collection(); 140 $sources = new Collection(); 141 $notes = new Collection(); 142 143 if ($search_terms !== []) { 144 // Log search requests for visitors 145 if (Auth::id() === null) { 146 Log::addSearchLog('General: ' . $query, $search_trees->all()); 147 } 148 149 if ($search_individuals) { 150 $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms); 151 } 152 153 if ($search_families) { 154 $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms); 155 $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms); 156 157 $families = $tmp1->merge($tmp2)->unique(static function (Family $family): string { 158 return $family->xref() . '@' . $family->tree()->id(); 159 }); 160 } 161 162 if ($search_repositories) { 163 $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); 164 } 165 166 if ($search_sources) { 167 $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); 168 } 169 170 if ($search_notes) { 171 $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); 172 } 173 174 if ($search_locations) { 175 $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms); 176 } 177 } 178 179 // If only 1 item is returned, automatically forward to that item 180 if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { 181 return redirect($individuals->first()->url()); 182 } 183 184 if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { 185 return redirect($families->first()->url()); 186 } 187 188 if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) { 189 return redirect($sources->first()->url()); 190 } 191 192 if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) { 193 return redirect($notes->first()->url()); 194 } 195 196 if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) { 197 return redirect($locations->first()->url()); 198 } 199 200 $title = I18N::translate('General search'); 201 202 return $this->viewResponse('search-general-page', [ 203 'all_trees' => $all_trees, 204 'exist_locations' => $exist_locations, 205 'exist_notes' => $exist_notes, 206 'exist_repositories' => $exist_repositories, 207 'exist_sources' => $exist_sources, 208 'families' => $families, 209 'individuals' => $individuals, 210 'locations' => $locations, 211 'notes' => $notes, 212 'query' => $query, 213 'repositories' => $repositories, 214 'search_families' => $search_families, 215 'search_individuals' => $search_individuals, 216 'search_locations' => $search_locations, 217 'search_notes' => $search_notes, 218 'search_repositories' => $search_repositories, 219 'search_sources' => $search_sources, 220 'search_trees' => $search_trees, 221 'sources' => $sources, 222 'title' => $title, 223 'tree' => $tree, 224 ]); 225 } 226 227 /** 228 * Convert the query into an array of search terms 229 * 230 * @param string $query 231 * 232 * @return array<string> 233 */ 234 private function extractSearchTerms(string $query): array 235 { 236 $search_terms = []; 237 238 // Words in double quotes stay together 239 preg_match_all('/"([^"]+)"/', $query, $matches, PREG_SET_ORDER); 240 foreach ($matches as $match) { 241 $search_terms[] = trim($match[1]); 242 // Remove this string from the search query 243 $query = strtr($query, [$match[0] => '']); 244 } 245 246 // Treat CJK characters as separate words, not as characters. 247 $query = preg_replace('/\p{Han}/u', '$0 ', $query); 248 249 // Other words get treated separately 250 preg_match_all('/[\S]+/', $query, $matches, PREG_SET_ORDER); 251 foreach ($matches as $match) { 252 $search_terms[] = $match[0]; 253 } 254 255 return $search_terms; 256 } 257} 258