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