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\Family; 23use Fisharebest\Webtrees\Http\ViewResponseTrait; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Location; 26use Fisharebest\Webtrees\Note; 27use Fisharebest\Webtrees\Repository; 28use Fisharebest\Webtrees\Services\SearchService; 29use Fisharebest\Webtrees\Services\TreeService; 30use Fisharebest\Webtrees\Site; 31use Fisharebest\Webtrees\Tree; 32use Fisharebest\Webtrees\Validator; 33use Illuminate\Database\Capsule\Manager as DB; 34use Illuminate\Support\Collection; 35use Psr\Http\Message\ResponseInterface; 36use Psr\Http\Message\ServerRequestInterface; 37use Psr\Http\Server\RequestHandlerInterface; 38 39use function in_array; 40use function preg_replace; 41use function redirect; 42use function trim; 43 44use const PREG_SET_ORDER; 45 46/** 47 * Search for genealogy data 48 */ 49class SearchGeneralPage implements RequestHandlerInterface 50{ 51 use ViewResponseTrait; 52 53 private SearchService $search_service; 54 55 private TreeService $tree_service; 56 57 /** 58 * SearchController constructor. 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 if ($search_individuals) { 145 $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms); 146 } 147 148 if ($search_families) { 149 $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms); 150 $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms); 151 152 $families = $tmp1->merge($tmp2)->unique(static function (Family $family): string { 153 return $family->xref() . '@' . $family->tree()->id(); 154 }); 155 } 156 157 if ($search_repositories) { 158 $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); 159 } 160 161 if ($search_sources) { 162 $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); 163 } 164 165 if ($search_notes) { 166 $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); 167 } 168 169 if ($search_locations) { 170 $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms); 171 } 172 } 173 174 // If only 1 item is returned, automatically forward to that item 175 if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { 176 return redirect($individuals->first()->url()); 177 } 178 179 if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { 180 return redirect($families->first()->url()); 181 } 182 183 if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) { 184 return redirect($sources->first()->url()); 185 } 186 187 if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) { 188 return redirect($notes->first()->url()); 189 } 190 191 if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) { 192 return redirect($locations->first()->url()); 193 } 194 195 $title = I18N::translate('General search'); 196 197 return $this->viewResponse('search-general-page', [ 198 'all_trees' => $all_trees, 199 'exist_locations' => $exist_locations, 200 'exist_notes' => $exist_notes, 201 'exist_repositories' => $exist_repositories, 202 'exist_sources' => $exist_sources, 203 'families' => $families, 204 'individuals' => $individuals, 205 'locations' => $locations, 206 'notes' => $notes, 207 'query' => $query, 208 'repositories' => $repositories, 209 'search_families' => $search_families, 210 'search_individuals' => $search_individuals, 211 'search_locations' => $search_locations, 212 'search_notes' => $search_notes, 213 'search_repositories' => $search_repositories, 214 'search_sources' => $search_sources, 215 'search_trees' => $search_trees, 216 'sources' => $sources, 217 'title' => $title, 218 'tree' => $tree, 219 ]); 220 } 221 222 /** 223 * Convert the query into an array of search terms 224 * 225 * @param string $query 226 * 227 * @return array<string> 228 */ 229 private function extractSearchTerms(string $query): array 230 { 231 $search_terms = []; 232 233 // Words in double quotes stay together 234 preg_match_all('/"([^"]+)"/', $query, $matches, PREG_SET_ORDER); 235 foreach ($matches as $match) { 236 $search_terms[] = trim($match[1]); 237 // Remove this string from the search query 238 $query = strtr($query, [$match[0] => '']); 239 } 240 241 // Treat CJK characters as separate words, not as characters. 242 $query = preg_replace('/\p{Han}/u', '$0 ', $query); 243 244 // Other words get treated separately 245 preg_match_all('/[\S]+/', $query, $matches, PREG_SET_ORDER); 246 foreach ($matches as $match) { 247 $search_terms[] = $match[0]; 248 } 249 250 return $search_terms; 251 } 252} 253