1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * webtrees: online genealogy |
||||
5 | * Copyright (C) 2025 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 | |||||
18 | declare(strict_types=1); |
||||
19 | |||||
20 | namespace Fisharebest\Webtrees\Http\RequestHandlers; |
||||
21 | |||||
22 | use Fisharebest\Webtrees\Auth; |
||||
0 ignored issues
–
show
|
|||||
23 | use Fisharebest\Webtrees\DB; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\DB was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
24 | use Fisharebest\Webtrees\Family; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Family was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
25 | use Fisharebest\Webtrees\Http\ViewResponseTrait; |
||||
26 | use Fisharebest\Webtrees\I18N; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
27 | use Fisharebest\Webtrees\Location; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Location was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
28 | use Fisharebest\Webtrees\Log; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Log was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
29 | use Fisharebest\Webtrees\Note; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Note was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
30 | use Fisharebest\Webtrees\Repository; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Repository was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
31 | use Fisharebest\Webtrees\Services\SearchService; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Services\SearchService was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
32 | use Fisharebest\Webtrees\Services\TreeService; |
||||
0 ignored issues
–
show
The type
Fisharebest\Webtrees\Services\TreeService was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
33 | use Fisharebest\Webtrees\Site; |
||||
34 | use Fisharebest\Webtrees\Tree; |
||||
35 | use Fisharebest\Webtrees\Validator; |
||||
36 | use Illuminate\Support\Collection; |
||||
37 | use Psr\Http\Message\ResponseInterface; |
||||
38 | use Psr\Http\Message\ServerRequestInterface; |
||||
39 | use Psr\Http\Server\RequestHandlerInterface; |
||||
40 | |||||
41 | use function in_array; |
||||
42 | use function preg_replace; |
||||
43 | use function redirect; |
||||
44 | use function trim; |
||||
45 | |||||
46 | use const PREG_SET_ORDER; |
||||
47 | |||||
48 | /** |
||||
49 | * Search for genealogy data |
||||
50 | */ |
||||
51 | class 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]); |
||||
0 ignored issues
–
show
array($tree) of type array<integer,Fisharebest\Webtrees\Tree> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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 fn (Family $family): string => $family->xref() . '@' . $family->tree()->id()); |
||||
158 | } |
||||
159 | |||||
160 | if ($search_repositories) { |
||||
161 | $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); |
||||
162 | } |
||||
163 | |||||
164 | if ($search_sources) { |
||||
165 | $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); |
||||
166 | } |
||||
167 | |||||
168 | if ($search_notes) { |
||||
169 | $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); |
||||
170 | } |
||||
171 | |||||
172 | if ($search_locations) { |
||||
173 | $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms); |
||||
174 | } |
||||
175 | } |
||||
176 | |||||
177 | // If only 1 item is returned, automatically forward to that item |
||||
178 | if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { |
||||
179 | return redirect($individuals->first()->url()); |
||||
180 | } |
||||
181 | |||||
182 | if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { |
||||
183 | return redirect($families->first()->url()); |
||||
184 | } |
||||
185 | |||||
186 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) { |
||||
187 | return redirect($sources->first()->url()); |
||||
188 | } |
||||
189 | |||||
190 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) { |
||||
191 | return redirect($notes->first()->url()); |
||||
192 | } |
||||
193 | |||||
194 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) { |
||||
195 | return redirect($locations->first()->url()); |
||||
196 | } |
||||
197 | |||||
198 | $title = I18N::translate('General search'); |
||||
199 | |||||
200 | return $this->viewResponse('search-general-page', [ |
||||
201 | 'all_trees' => $all_trees, |
||||
202 | 'exist_locations' => $exist_locations, |
||||
203 | 'exist_notes' => $exist_notes, |
||||
204 | 'exist_repositories' => $exist_repositories, |
||||
205 | 'exist_sources' => $exist_sources, |
||||
206 | 'families' => $families, |
||||
207 | 'individuals' => $individuals, |
||||
208 | 'locations' => $locations, |
||||
209 | 'notes' => $notes, |
||||
210 | 'query' => $query, |
||||
211 | 'repositories' => $repositories, |
||||
212 | 'search_families' => $search_families, |
||||
213 | 'search_individuals' => $search_individuals, |
||||
214 | 'search_locations' => $search_locations, |
||||
215 | 'search_notes' => $search_notes, |
||||
216 | 'search_repositories' => $search_repositories, |
||||
217 | 'search_sources' => $search_sources, |
||||
218 | 'search_trees' => $search_trees, |
||||
219 | 'sources' => $sources, |
||||
220 | 'title' => $title, |
||||
221 | 'tree' => $tree, |
||||
222 | ]); |
||||
223 | } |
||||
224 | |||||
225 | /** |
||||
226 | * Convert the query into an array of search terms |
||||
227 | * |
||||
228 | * @param string $query |
||||
229 | * |
||||
230 | * @return array<string> |
||||
231 | */ |
||||
232 | private function extractSearchTerms(string $query): array |
||||
233 | { |
||||
234 | $search_terms = []; |
||||
235 | |||||
236 | // Words in double quotes stay together |
||||
237 | preg_match_all('/"([^"]+)"/', $query, $matches, PREG_SET_ORDER); |
||||
238 | foreach ($matches as $match) { |
||||
239 | $search_terms[] = trim($match[1]); |
||||
240 | // Remove this string from the search query |
||||
241 | $query = strtr($query, [$match[0] => '']); |
||||
242 | } |
||||
243 | |||||
244 | // Treat CJK characters as separate words, not as characters. |
||||
245 | $query = preg_replace('/\p{Han}/u', '$0 ', $query); |
||||
246 | |||||
247 | // Other words get treated separately |
||||
248 | preg_match_all('/[\S]+/', $query, $matches, PREG_SET_ORDER); |
||||
249 | foreach ($matches as $match) { |
||||
250 | $search_terms[] = $match[0]; |
||||
251 | } |
||||
252 | |||||
253 | return $search_terms; |
||||
254 | } |
||||
255 | } |
||||
256 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths