Total Complexity | 167 |
Total Lines | 1265 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like SearchService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SearchService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class SearchService |
||
67 | { |
||
68 | // Do not attempt to show search results larger than this/ |
||
69 | protected const MAX_SEARCH_RESULTS = 5000; |
||
70 | |||
71 | private TreeService $tree_service; |
||
72 | |||
73 | /** |
||
74 | * @param TreeService $tree_service |
||
75 | */ |
||
76 | public function __construct( |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param array<Tree> $trees |
||
84 | * @param array<string> $search |
||
85 | * |
||
86 | * @return Collection<int,Family> |
||
87 | */ |
||
88 | public function searchFamilies(array $trees, array $search): Collection |
||
89 | { |
||
90 | $query = DB::table('families'); |
||
91 | |||
92 | $this->whereTrees($query, 'f_file', $trees); |
||
93 | $this->whereSearch($query, 'f_gedcom', $search); |
||
94 | |||
95 | // Remove accents when searching using PHP. |
||
96 | $search = array_map(static fn (string $x): string => I18N::language()->normalize($x), $search); |
||
97 | |||
98 | return $query |
||
99 | ->get() |
||
100 | ->each($this->rowLimiter()) |
||
101 | ->map($this->familyRowMapper()) |
||
102 | ->filter(GedcomRecord::accessFilter()) |
||
103 | ->filter($this->rawGedcomFilter($search)); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Search for families by name. |
||
108 | * |
||
109 | * @param array<Tree> $trees |
||
110 | * @param array<string> $search |
||
111 | * @param int $offset |
||
112 | * @param int $limit |
||
113 | * |
||
114 | * @return Collection<int,Family> |
||
115 | */ |
||
116 | public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
117 | { |
||
118 | $query = DB::table('families') |
||
119 | ->leftJoin('name AS husb_name', static function (JoinClause $join): void { |
||
120 | $join |
||
121 | ->on('husb_name.n_file', '=', 'families.f_file') |
||
122 | ->on('husb_name.n_id', '=', 'families.f_husb') |
||
123 | ->where('husb_name.n_type', '<>', '_MARNM'); |
||
124 | }) |
||
125 | ->leftJoin('name AS wife_name', static function (JoinClause $join): void { |
||
126 | $join |
||
127 | ->on('wife_name.n_file', '=', 'families.f_file') |
||
128 | ->on('wife_name.n_id', '=', 'families.f_wife') |
||
129 | ->where('wife_name.n_type', '<>', '_MARNM'); |
||
130 | }); |
||
131 | |||
132 | $field = new Expression('COALESCE(' . DB::prefix('husb_name') . ".n_full, '') || COALESCE(" . DB::prefix('wife_name') . ".n_full, '')"); |
||
133 | |||
134 | $this->whereTrees($query, 'f_file', $trees); |
||
135 | $this->whereSearch($query, $field, $search); |
||
136 | |||
137 | $query |
||
138 | ->orderBy('husb_name.n_sort') |
||
139 | ->orderBy('wife_name.n_sort') |
||
140 | ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort']); |
||
141 | |||
142 | return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param Place $place |
||
147 | * |
||
148 | * @return Collection<int,Family> |
||
149 | */ |
||
150 | public function searchFamiliesInPlace(Place $place): Collection |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param array<Tree> $trees |
||
169 | * @param array<string> $search |
||
170 | * |
||
171 | * @return Collection<int,Individual> |
||
172 | */ |
||
173 | public function searchIndividuals(array $trees, array $search): Collection |
||
174 | { |
||
175 | $query = DB::table('individuals'); |
||
176 | |||
177 | $this->whereTrees($query, 'i_file', $trees); |
||
178 | $this->whereSearch($query, 'i_gedcom', $search); |
||
179 | |||
180 | // Remove accents when searching using PHP. |
||
181 | $search = array_map(static fn (string $x): string => I18N::language()->normalize($x), $search); |
||
182 | |||
183 | return $query |
||
184 | ->get() |
||
185 | ->each($this->rowLimiter()) |
||
186 | ->map($this->individualRowMapper()) |
||
187 | ->filter(GedcomRecord::accessFilter()) |
||
188 | ->filter($this->rawGedcomFilter($search)); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Search for individuals by name. |
||
193 | * |
||
194 | * @param array<Tree> $trees |
||
195 | * @param array<string> $search |
||
196 | * @param int $offset |
||
197 | * @param int $limit |
||
198 | * |
||
199 | * @return Collection<int,Individual> |
||
200 | */ |
||
201 | public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
202 | { |
||
203 | $query = DB::table('individuals') |
||
204 | ->join('name', static function (JoinClause $join): void { |
||
205 | $join |
||
206 | ->on('name.n_file', '=', 'individuals.i_file') |
||
207 | ->on('name.n_id', '=', 'individuals.i_id'); |
||
208 | }) |
||
209 | ->orderBy('n_sort') |
||
210 | ->select(['individuals.*', 'n_sort']); |
||
211 | |||
212 | $this->whereTrees($query, 'i_file', $trees); |
||
213 | $this->whereSearch($query, 'n_full', $search); |
||
214 | |||
215 | return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param Place $place |
||
220 | * |
||
221 | * @return Collection<int,Individual> |
||
222 | */ |
||
223 | public function searchIndividualsInPlace(Place $place): Collection |
||
224 | { |
||
225 | return DB::table('individuals') |
||
226 | ->join('placelinks', static function (JoinClause $join) { |
||
227 | $join |
||
228 | ->on('i_file', '=', 'pl_file') |
||
229 | ->on('i_id', '=', 'pl_gid'); |
||
230 | }) |
||
231 | ->where('i_file', '=', $place->tree()->id()) |
||
232 | ->where('pl_p_id', '=', $place->id()) |
||
233 | ->select(['individuals.*']) |
||
234 | ->get() |
||
235 | ->each($this->rowLimiter()) |
||
236 | ->map($this->individualRowMapper()) |
||
237 | ->filter(GedcomRecord::accessFilter()); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Search for submissions. |
||
242 | * |
||
243 | * @param array<Tree> $trees |
||
244 | * @param array<string> $search |
||
245 | * @param int $offset |
||
246 | * @param int $limit |
||
247 | * |
||
248 | * @return Collection<int,Location> |
||
249 | */ |
||
250 | public function searchLocations(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
251 | { |
||
252 | $query = DB::table('other') |
||
253 | ->where('o_type', '=', Location::RECORD_TYPE); |
||
254 | |||
255 | $this->whereTrees($query, 'o_file', $trees); |
||
256 | $this->whereSearch($query, 'o_gedcom', $search); |
||
257 | |||
258 | return $this->paginateQuery($query, $this->locationRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Search for media objects. |
||
263 | * |
||
264 | * @param array<Tree> $trees |
||
265 | * @param array<string> $search |
||
266 | * @param int $offset |
||
267 | * @param int $limit |
||
268 | * |
||
269 | * @return Collection<int,Media> |
||
270 | */ |
||
271 | public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
272 | { |
||
273 | $query = DB::table('media'); |
||
274 | |||
275 | $this->whereTrees($query, 'media.m_file', $trees); |
||
276 | $this->whereSearch($query, 'm_gedcom', $search); |
||
277 | |||
278 | return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Search for notes. |
||
283 | * |
||
284 | * @param array<Tree> $trees |
||
285 | * @param array<string> $search |
||
286 | * @param int $offset |
||
287 | * @param int $limit |
||
288 | * |
||
289 | * @return Collection<int,Note> |
||
290 | */ |
||
291 | public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
292 | { |
||
293 | $query = DB::table('other') |
||
294 | ->where('o_type', '=', Note::RECORD_TYPE); |
||
295 | |||
296 | $this->whereTrees($query, 'o_file', $trees); |
||
297 | $this->whereSearch($query, 'o_gedcom', $search); |
||
298 | |||
299 | return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Search for notes. |
||
304 | * |
||
305 | * @param array<Tree> $trees |
||
306 | * @param array<string> $search |
||
307 | * @param int $offset |
||
308 | * @param int $limit |
||
309 | * |
||
310 | * @return Collection<int,SharedNote> |
||
311 | */ |
||
312 | public function searchSharedNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
313 | { |
||
314 | $query = DB::table('other') |
||
315 | ->where('o_type', '=', SharedNote::RECORD_TYPE); |
||
316 | |||
317 | $this->whereTrees($query, 'o_file', $trees); |
||
318 | $this->whereSearch($query, 'o_gedcom', $search); |
||
319 | |||
320 | return $this->paginateQuery($query, $this->sharedNoteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Search for repositories. |
||
325 | * |
||
326 | * @param array<Tree> $trees |
||
327 | * @param array<string> $search |
||
328 | * @param int $offset |
||
329 | * @param int $limit |
||
330 | * |
||
331 | * @return Collection<int,Repository> |
||
332 | */ |
||
333 | public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
334 | { |
||
335 | $query = DB::table('other') |
||
336 | ->where('o_type', '=', Repository::RECORD_TYPE); |
||
337 | |||
338 | $this->whereTrees($query, 'o_file', $trees); |
||
339 | $this->whereSearch($query, 'o_gedcom', $search); |
||
340 | |||
341 | return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Search for sources. |
||
346 | * |
||
347 | * @param array<Tree> $trees |
||
348 | * @param array<string> $search |
||
349 | * @param int $offset |
||
350 | * @param int $limit |
||
351 | * |
||
352 | * @return Collection<int,Source> |
||
353 | */ |
||
354 | public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
355 | { |
||
356 | $query = DB::table('sources'); |
||
357 | |||
358 | $this->whereTrees($query, 's_file', $trees); |
||
359 | $this->whereSearch($query, 's_gedcom', $search); |
||
360 | |||
361 | return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Search for sources by name. |
||
366 | * |
||
367 | * @param array<Tree> $trees |
||
368 | * @param array<string> $search |
||
369 | * @param int $offset |
||
370 | * @param int $limit |
||
371 | * |
||
372 | * @return Collection<int,Source> |
||
373 | */ |
||
374 | public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
375 | { |
||
376 | $query = DB::table('sources') |
||
377 | ->orderBy('s_name'); |
||
378 | |||
379 | $this->whereTrees($query, 's_file', $trees); |
||
380 | $this->whereSearch($query, 's_name', $search); |
||
381 | |||
382 | return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Search for sources. |
||
387 | * |
||
388 | * @param array<Tree> $trees |
||
389 | * @param array<string> $search |
||
390 | * @param int $offset |
||
391 | * @param int $limit |
||
392 | * |
||
393 | * @return Collection<int,string> |
||
394 | */ |
||
395 | public function searchSurnames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
396 | { |
||
397 | $query = DB::table('name'); |
||
398 | |||
399 | $this->whereTrees($query, 'n_file', $trees); |
||
400 | $this->whereSearch($query, 'n_surname', $search); |
||
401 | |||
402 | return $query |
||
403 | ->groupBy(['n_surname']) |
||
404 | ->orderBy('n_surname') |
||
405 | ->skip($offset) |
||
406 | ->take($limit) |
||
407 | ->pluck('n_surname'); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Search for submissions. |
||
412 | * |
||
413 | * @param array<Tree> $trees |
||
414 | * @param array<string> $search |
||
415 | * @param int $offset |
||
416 | * @param int $limit |
||
417 | * |
||
418 | * @return Collection<int,Submission> |
||
419 | */ |
||
420 | public function searchSubmissions(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
421 | { |
||
422 | $query = DB::table('other') |
||
423 | ->where('o_type', '=', Submission::RECORD_TYPE); |
||
424 | |||
425 | $this->whereTrees($query, 'o_file', $trees); |
||
426 | $this->whereSearch($query, 'o_gedcom', $search); |
||
427 | |||
428 | return $this->paginateQuery($query, $this->submissionRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Search for submitters. |
||
433 | * |
||
434 | * @param array<Tree> $trees |
||
435 | * @param array<string> $search |
||
436 | * @param int $offset |
||
437 | * @param int $limit |
||
438 | * |
||
439 | * @return Collection<int,Submitter> |
||
440 | */ |
||
441 | public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
442 | { |
||
443 | $query = DB::table('other') |
||
444 | ->where('o_type', '=', Submitter::RECORD_TYPE); |
||
445 | |||
446 | $this->whereTrees($query, 'o_file', $trees); |
||
447 | $this->whereSearch($query, 'o_gedcom', $search); |
||
448 | |||
449 | return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Search for places. |
||
454 | * |
||
455 | * @param Tree $tree |
||
456 | * @param string $search |
||
457 | * @param int $offset |
||
458 | * @param int $limit |
||
459 | * |
||
460 | * @return Collection<int,Place> |
||
461 | */ |
||
462 | public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection |
||
463 | { |
||
464 | $query = DB::table('places AS p0') |
||
465 | ->where('p0.p_file', '=', $tree->id()) |
||
466 | ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id') |
||
467 | ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id') |
||
468 | ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id') |
||
469 | ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id') |
||
470 | ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id') |
||
471 | ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id') |
||
472 | ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id') |
||
473 | ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id') |
||
474 | ->orderBy('p0.p_place') |
||
475 | ->orderBy('p1.p_place') |
||
476 | ->orderBy('p2.p_place') |
||
477 | ->orderBy('p3.p_place') |
||
478 | ->orderBy('p4.p_place') |
||
479 | ->orderBy('p5.p_place') |
||
480 | ->orderBy('p6.p_place') |
||
481 | ->orderBy('p7.p_place') |
||
482 | ->orderBy('p8.p_place') |
||
483 | ->select([ |
||
484 | 'p0.p_place AS place0', |
||
485 | 'p1.p_place AS place1', |
||
486 | 'p2.p_place AS place2', |
||
487 | 'p3.p_place AS place3', |
||
488 | 'p4.p_place AS place4', |
||
489 | 'p5.p_place AS place5', |
||
490 | 'p6.p_place AS place6', |
||
491 | 'p7.p_place AS place7', |
||
492 | 'p8.p_place AS place8', |
||
493 | ]); |
||
494 | |||
495 | // Filter each level of the hierarchy. |
||
496 | foreach (explode(',', $search, 9) as $level => $string) { |
||
497 | $query->where('p' . $level . '.p_place', DB::iLike(), '%' . addcslashes($string, '\\%_') . '%'); |
||
498 | } |
||
499 | |||
500 | $row_mapper = static function (object $row) use ($tree): Place { |
||
501 | $place = implode(', ', array_filter((array) $row)); |
||
502 | |||
503 | return new Place($place, $tree); |
||
504 | }; |
||
505 | |||
506 | $filter = static fn (): bool => true; |
||
507 | |||
508 | return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @param Tree $tree |
||
513 | * @param array<string,string> $fields |
||
514 | * @param array<string,string> $modifiers |
||
515 | * |
||
516 | * @return Collection<int,Individual> |
||
517 | */ |
||
518 | public function searchIndividualsAdvanced(Tree $tree, array $fields, array $modifiers): Collection |
||
519 | { |
||
520 | $fields = array_filter($fields, static fn (string $x): bool => $x !== ''); |
||
521 | |||
522 | $query = DB::table('individuals') |
||
523 | ->where('i_file', '=', $tree->id()) |
||
524 | ->select(['individuals.*']) |
||
525 | ->distinct(); |
||
526 | |||
527 | // Join the following tables |
||
528 | $father_name = false; |
||
529 | $mother_name = false; |
||
530 | $spouse_family = false; |
||
531 | $indi_name = false; |
||
532 | $indi_dates = []; |
||
533 | $fam_dates = []; |
||
534 | $indi_plac = false; |
||
535 | $fam_plac = false; |
||
536 | |||
537 | foreach ($fields as $field_name => $field_value) { |
||
538 | if (str_starts_with($field_name, 'FATHER:NAME')) { |
||
539 | $father_name = true; |
||
540 | } elseif (str_starts_with($field_name, 'MOTHER:NAME')) { |
||
541 | $mother_name = true; |
||
542 | } elseif (str_starts_with($field_name, 'INDI:NAME:GIVN')) { |
||
543 | $indi_name = true; |
||
544 | } elseif (str_starts_with($field_name, 'INDI:NAME:SURN')) { |
||
545 | $indi_name = true; |
||
546 | } elseif (str_starts_with($field_name, 'FAM:')) { |
||
547 | $spouse_family = true; |
||
548 | if (str_ends_with($field_name, ':DATE')) { |
||
549 | $fam_dates[] = explode(':', $field_name)[1]; |
||
550 | } elseif (str_ends_with($field_name, ':PLAC')) { |
||
551 | $fam_plac = true; |
||
552 | } |
||
553 | } elseif (str_starts_with($field_name, 'INDI:')) { |
||
554 | if (str_ends_with($field_name, ':DATE')) { |
||
555 | $indi_dates[] = explode(':', $field_name)[1]; |
||
556 | } elseif (str_ends_with($field_name, ':PLAC')) { |
||
557 | $indi_plac = true; |
||
558 | } |
||
559 | } |
||
560 | } |
||
561 | |||
562 | if ($father_name || $mother_name) { |
||
563 | $query->join('link AS l1', static function (JoinClause $join): void { |
||
564 | $join |
||
565 | ->on('l1.l_file', '=', 'individuals.i_file') |
||
566 | ->on('l1.l_from', '=', 'individuals.i_id') |
||
567 | ->where('l1.l_type', '=', 'FAMC'); |
||
568 | }); |
||
569 | |||
570 | if ($father_name) { |
||
571 | $query->join('link AS l2', static function (JoinClause $join): void { |
||
572 | $join |
||
573 | ->on('l2.l_file', '=', 'l1.l_file') |
||
574 | ->on('l2.l_from', '=', 'l1.l_to') |
||
575 | ->where('l2.l_type', '=', 'HUSB'); |
||
576 | }); |
||
577 | $query->join('name AS father_name', static function (JoinClause $join): void { |
||
578 | $join |
||
579 | ->on('father_name.n_file', '=', 'l2.l_file') |
||
580 | ->on('father_name.n_id', '=', 'l2.l_to'); |
||
581 | }); |
||
582 | } |
||
583 | |||
584 | if ($mother_name) { |
||
585 | $query->join('link AS l3', static function (JoinClause $join): void { |
||
586 | $join |
||
587 | ->on('l3.l_file', '=', 'l1.l_file') |
||
588 | ->on('l3.l_from', '=', 'l1.l_to') |
||
589 | ->where('l3.l_type', '=', 'WIFE'); |
||
590 | }); |
||
591 | $query->join('name AS mother_name', static function (JoinClause $join): void { |
||
592 | $join |
||
593 | ->on('mother_name.n_file', '=', 'l3.l_file') |
||
594 | ->on('mother_name.n_id', '=', 'l3.l_to'); |
||
595 | }); |
||
596 | } |
||
597 | } |
||
598 | |||
599 | if ($spouse_family) { |
||
600 | $query->join('link AS l4', static function (JoinClause $join): void { |
||
601 | $join |
||
602 | ->on('l4.l_file', '=', 'individuals.i_file') |
||
603 | ->on('l4.l_from', '=', 'individuals.i_id') |
||
604 | ->where('l4.l_type', '=', 'FAMS'); |
||
605 | }); |
||
606 | $query->join('families AS spouse_families', static function (JoinClause $join): void { |
||
607 | $join |
||
608 | ->on('spouse_families.f_file', '=', 'l4.l_file') |
||
609 | ->on('spouse_families.f_id', '=', 'l4.l_to'); |
||
610 | }); |
||
611 | } |
||
612 | |||
613 | if ($indi_name) { |
||
614 | $query->join('name AS individual_name', static function (JoinClause $join): void { |
||
615 | $join |
||
616 | ->on('individual_name.n_file', '=', 'individuals.i_file') |
||
617 | ->on('individual_name.n_id', '=', 'individuals.i_id'); |
||
618 | }); |
||
619 | } |
||
620 | |||
621 | foreach (array_unique($indi_dates) as $indi_date) { |
||
622 | $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void { |
||
623 | $join |
||
624 | ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file') |
||
625 | ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id'); |
||
626 | }); |
||
627 | } |
||
628 | |||
629 | foreach (array_unique($fam_dates) as $fam_date) { |
||
630 | $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void { |
||
631 | $join |
||
632 | ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.f_file') |
||
633 | ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.f_id'); |
||
634 | }); |
||
635 | } |
||
636 | |||
637 | if ($indi_plac) { |
||
638 | $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { |
||
639 | $join |
||
640 | ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') |
||
641 | ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); |
||
642 | }); |
||
643 | $query->join('places AS individual_places', static function (JoinClause $join): void { |
||
644 | $join |
||
645 | ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') |
||
646 | ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); |
||
647 | }); |
||
648 | } |
||
649 | |||
650 | if ($fam_plac) { |
||
651 | $query->join('placelinks AS family_placelinks', static function (JoinClause $join): void { |
||
652 | $join |
||
653 | ->on('family_placelinks.pl_file', '=', 'spouse_families.f_file') |
||
654 | ->on('family_placelinks.pl_gid', '=', 'spouse_families.f_id'); |
||
655 | }); |
||
656 | $query->join('places AS family_places', static function (JoinClause $join): void { |
||
657 | $join |
||
658 | ->on('family_places.p_file', '=', 'family_placelinks.pl_file') |
||
659 | ->on('family_places.p_id', '=', 'family_placelinks.pl_p_id'); |
||
660 | }); |
||
661 | } |
||
662 | |||
663 | foreach ($fields as $field_name => $field_value) { |
||
664 | $parts = explode(':', $field_name . ':::'); |
||
665 | if (str_starts_with($field_name, 'INDI:NAME:')) { |
||
666 | switch ($field_name) { |
||
667 | case 'INDI:NAME:GIVN': |
||
668 | switch ($modifiers[$field_name]) { |
||
669 | case 'EXACT': |
||
670 | $query->where('individual_name.n_givn', '=', $field_value); |
||
671 | break; |
||
672 | case 'BEGINS': |
||
673 | $query->where('individual_name.n_givn', DB::iLike(), $field_value . '%'); |
||
674 | break; |
||
675 | case 'CONTAINS': |
||
676 | $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
677 | break; |
||
678 | case 'SDX_STD': |
||
679 | $sdx = Soundex::russell($field_value); |
||
680 | if ($sdx !== '') { |
||
681 | $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx); |
||
682 | } else { |
||
683 | // No phonetic content? Use a substring match |
||
684 | $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
685 | } |
||
686 | break; |
||
687 | case 'SDX': // SDX uses DM by default. |
||
688 | case 'SDX_DM': |
||
689 | $sdx = Soundex::daitchMokotoff($field_value); |
||
690 | if ($sdx !== '') { |
||
691 | $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx); |
||
692 | } else { |
||
693 | // No phonetic content? Use a substring match |
||
694 | $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
695 | } |
||
696 | break; |
||
697 | } |
||
698 | unset($fields[$field_name]); |
||
699 | break; |
||
700 | case 'INDI:NAME:SURN': |
||
701 | switch ($modifiers[$field_name]) { |
||
702 | case 'EXACT': |
||
703 | $query->where(function (Builder $query) use ($field_value): void { |
||
704 | $query |
||
705 | ->where('individual_name.n_surn', '=', $field_value) |
||
706 | ->orWhere('individual_name.n_surname', '=', $field_value); |
||
707 | }); |
||
708 | break; |
||
709 | case 'BEGINS': |
||
710 | $query->where(function (Builder $query) use ($field_value): void { |
||
711 | $query |
||
712 | ->where('individual_name.n_surn', DB::iLike(), $field_value . '%') |
||
713 | ->orWhere('individual_name.n_surname', DB::iLike(), $field_value . '%'); |
||
714 | }); |
||
715 | break; |
||
716 | case 'CONTAINS': |
||
717 | $query->where(function (Builder $query) use ($field_value): void { |
||
718 | $query |
||
719 | ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') |
||
720 | ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); |
||
721 | }); |
||
722 | break; |
||
723 | case 'SDX_STD': |
||
724 | $sdx = Soundex::russell($field_value); |
||
725 | if ($sdx !== '') { |
||
726 | $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); |
||
727 | } else { |
||
728 | // No phonetic content? Use a substring match |
||
729 | $query->where(function (Builder $query) use ($field_value): void { |
||
730 | $query |
||
731 | ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') |
||
732 | ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); |
||
733 | }); |
||
734 | } |
||
735 | break; |
||
736 | case 'SDX': // SDX uses DM by default. |
||
737 | case 'SDX_DM': |
||
738 | $sdx = Soundex::daitchMokotoff($field_value); |
||
739 | if ($sdx !== '') { |
||
740 | $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); |
||
741 | } else { |
||
742 | // No phonetic content? Use a substring match |
||
743 | $query->where(function (Builder $query) use ($field_value): void { |
||
744 | $query |
||
745 | ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') |
||
746 | ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); |
||
747 | }); |
||
748 | } |
||
749 | break; |
||
750 | } |
||
751 | unset($fields[$field_name]); |
||
752 | break; |
||
753 | case 'INDI:NAME:NICK': |
||
754 | case 'INDI:NAME:_MARNM': |
||
755 | case 'INDI:NAME:_HEB': |
||
756 | case 'INDI:NAME:_AKA': |
||
757 | $like = "%\n1 NAME%\n2 " . $parts[2] . ' %' . preg_quote($field_value, '/') . '%'; |
||
758 | $query->where('individuals.i_gedcom', DB::iLike(), $like); |
||
759 | break; |
||
760 | } |
||
761 | } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':DATE')) { |
||
762 | $date = new Date($field_value); |
||
763 | if ($date->isOK()) { |
||
764 | $delta = 365 * (int) ($modifiers[$field_name] ?? 0); |
||
765 | $query |
||
766 | ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) |
||
767 | ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) |
||
768 | ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); |
||
769 | } |
||
770 | unset($fields[$field_name]); |
||
771 | } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':DATE')) { |
||
772 | $date = new Date($field_value); |
||
773 | if ($date->isOK()) { |
||
774 | $delta = 365 * (int) ($modifiers[$field_name] ?? 0); |
||
775 | $query |
||
776 | ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) |
||
777 | ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) |
||
778 | ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); |
||
779 | } |
||
780 | unset($fields[$field_name]); |
||
781 | } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { |
||
782 | // SQL can only link a place to a person/family, not to an event. |
||
783 | $query->where('individual_places.p_id', '=', $field_value); |
||
784 | } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { |
||
785 | // SQL can only link a place to a person/family, not to an event. |
||
786 | $query->where('family_places.p_id', '=', $field_value); |
||
787 | } elseif (str_starts_with($field_name, 'MOTHER:NAME:') || str_starts_with($field_name, 'FATHER:NAME:')) { |
||
788 | $table = str_starts_with($field_name, 'FATHER:NAME:') ? 'father_name' : 'mother_name'; |
||
789 | switch ($parts[2]) { |
||
790 | case 'GIVN': |
||
791 | switch ($modifiers[$field_name]) { |
||
792 | case 'EXACT': |
||
793 | $query->where($table . '.n_givn', '=', $field_value); |
||
794 | break; |
||
795 | case 'BEGINS': |
||
796 | $query->where($table . '.n_givn', DB::iLike(), $field_value . '%'); |
||
797 | break; |
||
798 | case 'CONTAINS': |
||
799 | $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
800 | break; |
||
801 | case 'SDX_STD': |
||
802 | $sdx = Soundex::russell($field_value); |
||
803 | if ($sdx !== '') { |
||
804 | $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); |
||
805 | } else { |
||
806 | // No phonetic content? Use a substring match |
||
807 | $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
808 | } |
||
809 | break; |
||
810 | case 'SDX': // SDX uses DM by default. |
||
811 | case 'SDX_DM': |
||
812 | $sdx = Soundex::daitchMokotoff($field_value); |
||
813 | if ($sdx !== '') { |
||
814 | $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); |
||
815 | } else { |
||
816 | // No phonetic content? Use a substring match |
||
817 | $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
818 | } |
||
819 | break; |
||
820 | } |
||
821 | break; |
||
822 | case 'SURN': |
||
823 | switch ($modifiers[$field_name]) { |
||
824 | case 'EXACT': |
||
825 | $query->where($table . '.n_surn', '=', $field_value); |
||
826 | break; |
||
827 | case 'BEGINS': |
||
828 | $query->where($table . '.n_surn', DB::iLike(), $field_value . '%'); |
||
829 | break; |
||
830 | case 'CONTAINS': |
||
831 | $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); |
||
832 | break; |
||
833 | case 'SDX_STD': |
||
834 | $sdx = Soundex::russell($field_value); |
||
835 | if ($sdx !== '') { |
||
836 | $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); |
||
837 | } else { |
||
838 | // No phonetic content? Use a substring match |
||
839 | $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); |
||
840 | } |
||
841 | break; |
||
842 | case 'SDX': // SDX uses DM by default. |
||
843 | case 'SDX_DM': |
||
844 | $sdx = Soundex::daitchMokotoff($field_value); |
||
845 | if ($sdx !== '') { |
||
846 | $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); |
||
847 | } else { |
||
848 | // No phonetic content? Use a substring match |
||
849 | $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); |
||
850 | } |
||
851 | break; |
||
852 | } |
||
853 | break; |
||
854 | } |
||
855 | unset($fields[$field_name]); |
||
856 | } elseif (str_starts_with($field_name, 'FAM:')) { |
||
857 | // e.g. searches for occupation, religion, note, etc. |
||
858 | // Initial matching only. Need PHP to apply filter. |
||
859 | $query->where('spouse_families.f_gedcom', DB::iLike(), "%\n1 " . $parts[1] . ' %' . $field_value . '%'); |
||
860 | } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':TYPE')) { |
||
861 | // Initial matching only. Need PHP to apply filter. |
||
862 | $query->where('individuals.i_gedcom', DB::iLike(), "%\n1 " . $parts[1] . "%\n2 TYPE %" . $field_value . '%'); |
||
863 | } elseif (str_starts_with($field_name, 'INDI:')) { |
||
864 | // e.g. searches for occupation, religion, note, etc. |
||
865 | // Initial matching only. Need PHP to apply filter. |
||
866 | $query->where('individuals.i_gedcom', DB::iLike(), "%\n1 " . $parts[1] . '%' . $parts[2] . '%' . $field_value . '%'); |
||
867 | } |
||
868 | } |
||
869 | |||
870 | return $query |
||
871 | ->get() |
||
872 | ->each($this->rowLimiter()) |
||
873 | ->map($this->individualRowMapper()) |
||
874 | ->filter(GedcomRecord::accessFilter()) |
||
875 | ->filter(static function (Individual $individual) use ($fields, $tree): bool { |
||
876 | // Check for searches which were only partially matched by SQL |
||
877 | foreach ($fields as $field_name => $field_value) { |
||
878 | $parts = explode(':', $field_name . '::::'); |
||
879 | |||
880 | if (str_starts_with($field_name, 'INDI:NAME:') && $field_name !== 'INDI:NAME:GIVN' && $field_name !== 'INDI:NAME:SURN') { |
||
881 | $regex = '/\n1 NAME.*(?:\n2.*)*\n2 ' . $parts[2] . ' .*' . preg_quote($field_value, '/') . '/i'; |
||
882 | |||
883 | if (preg_match($regex, $individual->gedcom()) === 1) { |
||
884 | continue; |
||
885 | } |
||
886 | |||
887 | return false; |
||
888 | } |
||
889 | |||
890 | if (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { |
||
891 | $place = Place::find((int) $field_value, $tree); |
||
892 | |||
893 | foreach ($individual->facts([$parts[1]]) as $fact) { |
||
894 | if ($fact->place()->gedcomName() === $place->gedcomName() || str_ends_with($fact->place()->gedcomName(), ', ' . $place->gedcomName())) { |
||
895 | continue 2; |
||
896 | } |
||
897 | } |
||
898 | return false; |
||
899 | } |
||
900 | |||
901 | if (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { |
||
902 | $place = Place::find((int) $field_value, $tree); |
||
903 | |||
904 | foreach ($individual->spouseFamilies() as $family) { |
||
905 | foreach ($family->facts([$parts[1]]) as $fact) { |
||
906 | if ($fact->place()->gedcomName() === $place->gedcomName() || str_ends_with($fact->place()->gedcomName(), ', ' . $place->gedcomName())) { |
||
907 | continue 3; |
||
908 | } |
||
909 | } |
||
910 | } |
||
911 | return false; |
||
912 | } |
||
913 | |||
914 | $regex = '/' . preg_quote($field_value, '/') . '/i'; |
||
915 | |||
916 | if ($field_name === 'INDI:FACT:TYPE' || $field_name === 'INDI:EVEN:TYPE' || $field_name === 'INDI:CHAN:_WT_USER') { |
||
917 | foreach ($individual->facts([$parts[1]]) as $fact) { |
||
918 | if (preg_match($regex, $fact->attribute($parts[2])) === 1) { |
||
919 | continue 2; |
||
920 | } |
||
921 | } |
||
922 | |||
923 | return false; |
||
924 | } |
||
925 | |||
926 | if (str_starts_with($field_name, 'INDI:')) { |
||
927 | foreach ($individual->facts([$parts[1]]) as $fact) { |
||
928 | if (preg_match($regex, $fact->value()) === 1) { |
||
929 | continue 2; |
||
930 | } |
||
931 | } |
||
932 | |||
933 | return false; |
||
934 | } |
||
935 | |||
936 | if (str_starts_with($field_name, 'FAM:')) { |
||
937 | foreach ($individual->spouseFamilies() as $family) { |
||
938 | foreach ($family->facts([$parts[1]]) as $fact) { |
||
939 | if (preg_match($regex, $fact->value()) === 1) { |
||
940 | continue 3; |
||
941 | } |
||
942 | } |
||
943 | } |
||
944 | return false; |
||
945 | } |
||
946 | } |
||
947 | |||
948 | return true; |
||
949 | }); |
||
950 | } |
||
951 | |||
952 | /** |
||
953 | * @param string $soundex |
||
954 | * @param string $lastname |
||
955 | * @param string $firstname |
||
956 | * @param string $place |
||
957 | * @param array<Tree> $search_trees |
||
958 | * |
||
959 | * @return Collection<int,Individual> |
||
960 | */ |
||
961 | public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection |
||
962 | { |
||
963 | switch ($soundex) { |
||
964 | default: |
||
965 | case 'Russell': |
||
966 | $givn_sdx = Soundex::russell($firstname); |
||
967 | $surn_sdx = Soundex::russell($lastname); |
||
968 | $plac_sdx = Soundex::russell($place); |
||
969 | $givn_field = 'n_soundex_givn_std'; |
||
970 | $surn_field = 'n_soundex_surn_std'; |
||
971 | $plac_field = 'p_std_soundex'; |
||
972 | break; |
||
973 | case 'DaitchM': |
||
974 | $givn_sdx = Soundex::daitchMokotoff($firstname); |
||
975 | $surn_sdx = Soundex::daitchMokotoff($lastname); |
||
976 | $plac_sdx = Soundex::daitchMokotoff($place); |
||
977 | $givn_field = 'n_soundex_givn_dm'; |
||
978 | $surn_field = 'n_soundex_surn_dm'; |
||
979 | $plac_field = 'p_dm_soundex'; |
||
980 | break; |
||
981 | } |
||
982 | |||
983 | // Nothing to search for? Return nothing. |
||
984 | if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') { |
||
985 | return new Collection(); |
||
986 | } |
||
987 | |||
988 | $query = DB::table('individuals') |
||
989 | ->select(['individuals.*']) |
||
990 | ->distinct(); |
||
991 | |||
992 | $this->whereTrees($query, 'i_file', $search_trees); |
||
993 | |||
994 | if ($plac_sdx !== '') { |
||
995 | $query->join('placelinks', static function (JoinClause $join): void { |
||
996 | $join |
||
997 | ->on('placelinks.pl_file', '=', 'individuals.i_file') |
||
998 | ->on('placelinks.pl_gid', '=', 'individuals.i_id'); |
||
999 | }); |
||
1000 | $query->join('places', static function (JoinClause $join): void { |
||
1001 | $join |
||
1002 | ->on('places.p_file', '=', 'placelinks.pl_file') |
||
1003 | ->on('places.p_id', '=', 'placelinks.pl_p_id'); |
||
1004 | }); |
||
1005 | |||
1006 | $this->wherePhonetic($query, $plac_field, $plac_sdx); |
||
1007 | } |
||
1008 | |||
1009 | if ($givn_sdx !== '' || $surn_sdx !== '') { |
||
1010 | $query->join('name', static function (JoinClause $join): void { |
||
1011 | $join |
||
1012 | ->on('name.n_file', '=', 'individuals.i_file') |
||
1013 | ->on('name.n_id', '=', 'individuals.i_id'); |
||
1014 | }); |
||
1015 | |||
1016 | $this->wherePhonetic($query, $givn_field, $givn_sdx); |
||
1017 | $this->wherePhonetic($query, $surn_field, $surn_sdx); |
||
1018 | } |
||
1019 | |||
1020 | return $query |
||
1021 | ->get() |
||
1022 | ->each($this->rowLimiter()) |
||
1023 | ->map($this->individualRowMapper()) |
||
1024 | ->filter(GedcomRecord::accessFilter()); |
||
1025 | } |
||
1026 | |||
1027 | /** |
||
1028 | * Paginate a search query. |
||
1029 | * |
||
1030 | * @param Builder $query Searches the database for the desired records. |
||
1031 | * @param Closure $row_mapper Converts a row from the query into a record. |
||
1032 | * @param Closure $row_filter |
||
1033 | * @param int $offset Skip this many rows. |
||
1034 | * @param int $limit Take this many rows. |
||
1035 | * |
||
1036 | * @return Collection<int,mixed> |
||
1037 | */ |
||
1038 | private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection |
||
1039 | { |
||
1040 | $collection = new Collection(); |
||
1041 | |||
1042 | foreach ($query->cursor() as $row) { |
||
1043 | $record = $row_mapper($row); |
||
1044 | // searchIndividualNames() and searchFamilyNames() can return duplicate rows, |
||
1045 | // where individuals have multiple names - and we need to sort results by name. |
||
1046 | if ($collection->containsStrict($record)) { |
||
1047 | continue; |
||
1048 | } |
||
1049 | // If the object has a method "canShow()", then use it to filter for privacy. |
||
1050 | if ($row_filter($record)) { |
||
1051 | if ($offset > 0) { |
||
1052 | $offset--; |
||
1053 | } else { |
||
1054 | if ($limit > 0) { |
||
1055 | $collection->push($record); |
||
1056 | } |
||
1057 | |||
1058 | $limit--; |
||
1059 | |||
1060 | if ($limit === 0) { |
||
1061 | break; |
||
1062 | } |
||
1063 | } |
||
1064 | } |
||
1065 | } |
||
1066 | |||
1067 | return $collection; |
||
1068 | } |
||
1069 | |||
1070 | /** |
||
1071 | * Apply search filters to a SQL query column. Apply collation rules to MySQL. |
||
1072 | * |
||
1073 | * @param Builder $query |
||
1074 | * @param Expression|string $column |
||
1075 | * @param array<string> $search_terms |
||
1076 | */ |
||
1077 | private function whereSearch(Builder $query, $column, array $search_terms): void |
||
1078 | { |
||
1079 | foreach ($search_terms as $search_term) { |
||
1080 | $query->where($column, DB::iLike(), '%' . addcslashes($search_term, '\\%_') . '%'); |
||
1081 | } |
||
1082 | } |
||
1083 | |||
1084 | /** |
||
1085 | * Apply soundex search filters to a SQL query column. |
||
1086 | * |
||
1087 | * @param Builder $query |
||
1088 | * @param Expression|string $field |
||
1089 | * @param string $soundex |
||
1090 | */ |
||
1091 | private function wherePhonetic(Builder $query, $field, string $soundex): void |
||
1092 | { |
||
1093 | if ($soundex !== '') { |
||
1094 | $query->where(function (Builder $query) use ($soundex, $field): void { |
||
1095 | foreach (explode(':', $soundex) as $sdx) { |
||
1096 | $query->orWhere($field, DB::iLike(), '%' . $sdx . '%'); |
||
1097 | } |
||
1098 | }); |
||
1099 | } |
||
1100 | } |
||
1101 | |||
1102 | /** |
||
1103 | * @param Builder $query |
||
1104 | * @param string $tree_id_field |
||
1105 | * @param array<Tree> $trees |
||
1106 | */ |
||
1107 | private function whereTrees(Builder $query, string $tree_id_field, array $trees): void |
||
1108 | { |
||
1109 | $tree_ids = array_map(static fn (Tree $tree): int => $tree->id(), $trees); |
||
1110 | |||
1111 | $query->whereIn($tree_id_field, $tree_ids); |
||
1112 | } |
||
1113 | |||
1114 | /** |
||
1115 | * Find the media object that uses a particular media file. |
||
1116 | * |
||
1117 | * @param string $file |
||
1118 | * |
||
1119 | * @return array<Media> |
||
1120 | */ |
||
1121 | public function findMediaObjectsForMediaFile(string $file): array |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * A closure to filter records by privacy-filtered GEDCOM data. |
||
1140 | * |
||
1141 | * @param array<string> $search_terms |
||
1142 | * |
||
1143 | * @return Closure(GedcomRecord):bool |
||
1144 | */ |
||
1145 | private function rawGedcomFilter(array $search_terms): Closure |
||
1146 | { |
||
1147 | return static function (GedcomRecord $record) use ($search_terms): bool { |
||
1148 | // Ignore non-genealogy fields |
||
1149 | $regex = '/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9A-F]{36})$/i'; |
||
1150 | |||
1151 | if (array_filter($search_terms, static fn (string $term): bool => preg_match($regex, $term) === 1) !== []) { |
||
1152 | $gedcom = preg_replace('/\n\d _WT_USER .*/', '', $record->gedcom()); |
||
1153 | } else { |
||
1154 | $gedcom = preg_replace('/\n\d (UID|_UID|_WT_USER) .*/', '', $record->gedcom()); |
||
1155 | } |
||
1156 | |||
1157 | // Ignore matches in links |
||
1158 | $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom); |
||
1159 | |||
1160 | // Remove accents when searching using PHP. |
||
1161 | $gedcom = I18N::language()->normalize($gedcom); |
||
1162 | |||
1163 | // Re-apply the filtering |
||
1164 | foreach ($search_terms as $search_term) { |
||
1165 | if (mb_stripos($gedcom, $search_term) === false) { |
||
1166 | return false; |
||
1167 | } |
||
1168 | } |
||
1169 | |||
1170 | return true; |
||
1171 | }; |
||
1172 | } |
||
1173 | |||
1174 | /** |
||
1175 | * Searching for short or common text can give more results than the system can process. |
||
1176 | * |
||
1177 | * @param int $limit |
||
1178 | * |
||
1179 | * @return Closure():void |
||
1180 | */ |
||
1181 | private function rowLimiter(int $limit = self::MAX_SEARCH_RESULTS): Closure |
||
1182 | { |
||
1183 | return static function () use ($limit): void { |
||
1184 | static $n = 0; |
||
1185 | |||
1186 | if (++$n > $limit) { |
||
1187 | $message = I18N::translate('The search returned too many results.'); |
||
1188 | |||
1189 | throw new HttpServiceUnavailableException($message); |
||
1190 | } |
||
1191 | }; |
||
1192 | } |
||
1193 | |||
1194 | /** |
||
1195 | * Convert a row from any tree in the families table into a family object. |
||
1196 | * |
||
1197 | * @return Closure(object):Family |
||
1198 | */ |
||
1199 | private function familyRowMapper(): Closure |
||
1200 | { |
||
1201 | return function (object $row): Family { |
||
1202 | $tree = $this->tree_service->find((int) $row->f_file); |
||
1203 | |||
1204 | return Registry::familyFactory()->mapper($tree)($row); |
||
1205 | }; |
||
1206 | } |
||
1207 | |||
1208 | /** |
||
1209 | * Convert a row from any tree in the individuals table into an individual object. |
||
1210 | * |
||
1211 | * @return Closure(object):Individual |
||
1212 | */ |
||
1213 | private function individualRowMapper(): Closure |
||
1214 | { |
||
1215 | return function (object $row): Individual { |
||
1216 | $tree = $this->tree_service->find((int) $row->i_file); |
||
1217 | |||
1218 | return Registry::individualFactory()->mapper($tree)($row); |
||
1219 | }; |
||
1220 | } |
||
1221 | |||
1222 | /** |
||
1223 | * Convert a row from any tree in the media table into a location object. |
||
1224 | * |
||
1225 | * @return Closure(object):Location |
||
1226 | */ |
||
1227 | private function locationRowMapper(): Closure |
||
1228 | { |
||
1229 | return function (object $row): Location { |
||
1230 | $tree = $this->tree_service->find((int) $row->o_file); |
||
1231 | |||
1232 | return Registry::locationFactory()->mapper($tree)($row); |
||
1233 | }; |
||
1234 | } |
||
1235 | |||
1236 | /** |
||
1237 | * Convert a row from any tree in the media table into an media object. |
||
1238 | * |
||
1239 | * @return Closure(object):Media |
||
1240 | */ |
||
1241 | private function mediaRowMapper(): Closure |
||
1247 | }; |
||
1248 | } |
||
1249 | |||
1250 | /** |
||
1251 | * Convert a row from any tree in the other table into a note object. |
||
1252 | * |
||
1253 | * @return Closure(object):Note |
||
1254 | */ |
||
1255 | private function noteRowMapper(): Closure |
||
1256 | { |
||
1257 | return function (object $row): Note { |
||
1258 | $tree = $this->tree_service->find((int) $row->o_file); |
||
1259 | |||
1260 | return Registry::noteFactory()->mapper($tree)($row); |
||
1261 | }; |
||
1262 | } |
||
1263 | |||
1264 | /** |
||
1265 | * Convert a row from any tree in the other table into a repository object. |
||
1266 | * |
||
1267 | * @return Closure(object):Repository |
||
1268 | */ |
||
1269 | private function repositoryRowMapper(): Closure |
||
1275 | }; |
||
1276 | } |
||
1277 | |||
1278 | /** |
||
1279 | * Convert a row from any tree in the other table into a note object. |
||
1280 | * |
||
1281 | * @return Closure(object):SharedNote |
||
1282 | */ |
||
1283 | private function sharedNoteRowMapper(): Closure |
||
1284 | { |
||
1285 | return function (object $row): Note { |
||
1286 | $tree = $this->tree_service->find((int) $row->o_file); |
||
1287 | |||
1288 | return Registry::sharedNoteFactory()->mapper($tree)($row); |
||
1289 | }; |
||
1290 | } |
||
1291 | |||
1292 | /** |
||
1293 | * Convert a row from any tree in the sources table into a source object. |
||
1294 | * |
||
1295 | * @return Closure(object):Source |
||
1296 | */ |
||
1297 | private function sourceRowMapper(): Closure |
||
1298 | { |
||
1299 | return function (object $row): Source { |
||
1300 | $tree = $this->tree_service->find((int) $row->s_file); |
||
1301 | |||
1302 | return Registry::sourceFactory()->mapper($tree)($row); |
||
1303 | }; |
||
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * Convert a row from any tree in the other table into a submission object. |
||
1308 | * |
||
1309 | * @return Closure(object):Submission |
||
1310 | */ |
||
1311 | private function submissionRowMapper(): Closure |
||
1312 | { |
||
1313 | return function (object $row): Submission { |
||
1314 | $tree = $this->tree_service->find((int) $row->o_file); |
||
1315 | |||
1316 | return Registry::submissionFactory()->mapper($tree)($row); |
||
1317 | }; |
||
1318 | } |
||
1319 | |||
1320 | /** |
||
1321 | * Convert a row from any tree in the other table into a submitter object. |
||
1322 | * |
||
1323 | * @return Closure(object):Submitter |
||
1324 | */ |
||
1325 | private function submitterRowMapper(): Closure |
||
1331 | }; |
||
1332 | } |
||
1333 | } |
||
1334 |