xref: /webtrees/app/Services/SearchService.php (revision 788f881d1edc37597655c820c8babac60c767a55)
132cd2800SGreg Roach<?php
23976b470SGreg Roach
332cd2800SGreg Roach/**
432cd2800SGreg Roach * webtrees: online genealogy
5b5961194SGreg Roach * Copyright (C) 2020 webtrees development team
632cd2800SGreg Roach * This program is free software: you can redistribute it and/or modify
732cd2800SGreg Roach * it under the terms of the GNU General Public License as published by
832cd2800SGreg Roach * the Free Software Foundation, either version 3 of the License, or
932cd2800SGreg Roach * (at your option) any later version.
1032cd2800SGreg Roach * This program is distributed in the hope that it will be useful,
1132cd2800SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1232cd2800SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1332cd2800SGreg Roach * GNU General Public License for more details.
1432cd2800SGreg Roach * You should have received a copy of the GNU General Public License
1532cd2800SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1632cd2800SGreg Roach */
17fcfa147eSGreg Roach
1832cd2800SGreg Roachdeclare(strict_types=1);
1932cd2800SGreg Roach
2032cd2800SGreg Roachnamespace Fisharebest\Webtrees\Services;
2132cd2800SGreg Roach
2232cd2800SGreg Roachuse Closure;
23dfb2cda2SGreg Roachuse Fisharebest\Webtrees\Date;
24d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpServiceUnavailableException;
25a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
2632cd2800SGreg Roachuse Fisharebest\Webtrees\Family;
27a7a24840SGreg Roachuse Fisharebest\Webtrees\Gedcom;
2832cd2800SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2952a8ef61SGreg Roachuse Fisharebest\Webtrees\I18N;
3032cd2800SGreg Roachuse Fisharebest\Webtrees\Individual;
3132cd2800SGreg Roachuse Fisharebest\Webtrees\Media;
3232cd2800SGreg Roachuse Fisharebest\Webtrees\Note;
33b68caec6SGreg Roachuse Fisharebest\Webtrees\Place;
3432cd2800SGreg Roachuse Fisharebest\Webtrees\Repository;
352d686e68SGreg Roachuse Fisharebest\Webtrees\Soundex;
3632cd2800SGreg Roachuse Fisharebest\Webtrees\Source;
37b5c8fd7eSGreg Roachuse Fisharebest\Webtrees\Submitter;
3832cd2800SGreg Roachuse Fisharebest\Webtrees\Tree;
3932cd2800SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
4032cd2800SGreg Roachuse Illuminate\Database\Query\Builder;
41a7a24840SGreg Roachuse Illuminate\Database\Query\Expression;
4232cd2800SGreg Roachuse Illuminate\Database\Query\JoinClause;
4332cd2800SGreg Roachuse Illuminate\Support\Collection;
4432cd2800SGreg Roachuse stdClass;
453976b470SGreg Roach
46b5961194SGreg Roachuse function addcslashes;
47b4adec9eSGreg Roachuse function array_filter;
48b4adec9eSGreg Roachuse function array_map;
4956946b7aSGreg Roachuse function array_unique;
5056946b7aSGreg Roachuse function explode;
51b4adec9eSGreg Roachuse function implode;
52a7a24840SGreg Roachuse function mb_stripos;
53b4adec9eSGreg Roachuse function preg_match;
54b4adec9eSGreg Roachuse function preg_quote;
55b4adec9eSGreg Roachuse function preg_replace;
56b4adec9eSGreg Roach
57b4adec9eSGreg Roachuse const PHP_INT_MAX;
5832cd2800SGreg Roach
5932cd2800SGreg Roach/**
6032cd2800SGreg Roach * Search trees for genealogy records.
6132cd2800SGreg Roach */
6232cd2800SGreg Roachclass SearchService
6332cd2800SGreg Roach{
64645e3037SGreg Roach    // Do not attempt to show search results larger than this/
65645e3037SGreg Roach    protected const MAX_SEARCH_RESULTS = 5000;
66645e3037SGreg Roach
67d5ad3db0SGreg Roach    /** @var TreeService */
68d5ad3db0SGreg Roach    private $tree_service;
69d5ad3db0SGreg Roach
70d5ad3db0SGreg Roach    /**
71d5ad3db0SGreg Roach     * SearchService constructor.
72d5ad3db0SGreg Roach     *
73d5ad3db0SGreg Roach     * @param TreeService $tree_service
74d5ad3db0SGreg Roach     */
753959eeb6SGreg Roach    public function __construct(
763959eeb6SGreg Roach        TreeService $tree_service
773959eeb6SGreg Roach    ) {
78d5ad3db0SGreg Roach        $this->tree_service = $tree_service;
79d5ad3db0SGreg Roach    }
80d5ad3db0SGreg Roach
81a7a24840SGreg Roach    /**
82a7a24840SGreg Roach     * @param Tree[]   $trees
83a7a24840SGreg Roach     * @param string[] $search
84a7a24840SGreg Roach     *
85b5c8fd7eSGreg Roach     * @return Collection<Family>
86a7a24840SGreg Roach     */
87a7a24840SGreg Roach    public function searchFamilies(array $trees, array $search): Collection
88a7a24840SGreg Roach    {
89a7a24840SGreg Roach        $query = DB::table('families');
90a7a24840SGreg Roach
91a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
92a7a24840SGreg Roach        $this->whereSearch($query, 'f_gedcom', $search);
93a7a24840SGreg Roach
94a7a24840SGreg Roach        return $query
95a7a24840SGreg Roach            ->get()
9652a8ef61SGreg Roach            ->each($this->rowLimiter())
97d5ad3db0SGreg Roach            ->map($this->familyRowMapper())
98a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
997f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
100a7a24840SGreg Roach    }
101a7a24840SGreg Roach
10232cd2800SGreg Roach    /**
10332cd2800SGreg Roach     * Search for families by name.
10432cd2800SGreg Roach     *
105a7a24840SGreg Roach     * @param Tree[]   $trees
106a7a24840SGreg Roach     * @param string[] $search
10732cd2800SGreg Roach     * @param int      $offset
10832cd2800SGreg Roach     * @param int      $limit
10932cd2800SGreg Roach     *
110b5c8fd7eSGreg Roach     * @return Collection<Family>
11132cd2800SGreg Roach     */
112a7a24840SGreg Roach    public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
11332cd2800SGreg Roach    {
11432cd2800SGreg Roach        $query = DB::table('families')
115ac499332SGreg Roach            ->leftJoin('name AS husb_name', static function (JoinClause $join): void {
11632cd2800SGreg Roach                $join
11732cd2800SGreg Roach                    ->on('husb_name.n_file', '=', 'families.f_file')
118ac499332SGreg Roach                    ->on('husb_name.n_id', '=', 'families.f_husb')
119ac499332SGreg Roach                    ->where('husb_name.n_type', '<>', '_MARNM');
12032cd2800SGreg Roach            })
121ac499332SGreg Roach            ->leftJoin('name AS wife_name', static function (JoinClause $join): void {
12232cd2800SGreg Roach                $join
12332cd2800SGreg Roach                    ->on('wife_name.n_file', '=', 'families.f_file')
124ac499332SGreg Roach                    ->on('wife_name.n_id', '=', 'families.f_wife')
125ac499332SGreg Roach                    ->where('wife_name.n_type', '<>', '_MARNM');
126ac499332SGreg Roach            });
127a7a24840SGreg Roach
128a7a24840SGreg Roach        $prefix = DB::connection()->getTablePrefix();
129a69f5655SGreg Roach        $field  = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')");
130a7a24840SGreg Roach
131a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
132a7a24840SGreg Roach        $this->whereSearch($query, $field, $search);
133a7a24840SGreg Roach
134a7a24840SGreg Roach        $query
13532cd2800SGreg Roach            ->orderBy('husb_name.n_sort')
13632cd2800SGreg Roach            ->orderBy('wife_name.n_sort')
137c0804649SGreg Roach            ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort'])
13832cd2800SGreg Roach            ->distinct();
13932cd2800SGreg Roach
140d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
141a7a24840SGreg Roach    }
142a7a24840SGreg Roach
143a7a24840SGreg Roach    /**
144dbe53437SGreg Roach     * @param Place $place
145dbe53437SGreg Roach     *
146dbe53437SGreg Roach     * @return Collection<Family>
147dbe53437SGreg Roach     */
148dbe53437SGreg Roach    public function searchFamiliesInPlace(Place $place): Collection
149dbe53437SGreg Roach    {
150dbe53437SGreg Roach        return DB::table('families')
151dbe53437SGreg Roach            ->join('placelinks', static function (JoinClause $query) {
152dbe53437SGreg Roach                $query
153dbe53437SGreg Roach                    ->on('families.f_file', '=', 'placelinks.pl_file')
154dbe53437SGreg Roach                    ->on('families.f_id', '=', 'placelinks.pl_gid');
155dbe53437SGreg Roach            })
156dbe53437SGreg Roach            ->where('f_file', '=', $place->tree()->id())
157dbe53437SGreg Roach            ->where('pl_p_id', '=', $place->id())
158dbe53437SGreg Roach            ->select(['families.*'])
159dbe53437SGreg Roach            ->get()
160dbe53437SGreg Roach            ->each($this->rowLimiter())
161dbe53437SGreg Roach            ->map($this->familyRowMapper())
162dbe53437SGreg Roach            ->filter(GedcomRecord::accessFilter());
163dbe53437SGreg Roach    }
164dbe53437SGreg Roach
165dbe53437SGreg Roach    /**
166a7a24840SGreg Roach     * @param Tree[]   $trees
167a7a24840SGreg Roach     * @param string[] $search
168a7a24840SGreg Roach     *
169b5c8fd7eSGreg Roach     * @return Collection<Individual>
170a7a24840SGreg Roach     */
171a7a24840SGreg Roach    public function searchIndividuals(array $trees, array $search): Collection
172a7a24840SGreg Roach    {
173a7a24840SGreg Roach        $query = DB::table('individuals');
174a7a24840SGreg Roach
175a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
176a7a24840SGreg Roach        $this->whereSearch($query, 'i_gedcom', $search);
177a7a24840SGreg Roach
178a7a24840SGreg Roach        return $query
179a7a24840SGreg Roach            ->get()
18052a8ef61SGreg Roach            ->each($this->rowLimiter())
181d5ad3db0SGreg Roach            ->map($this->individualRowMapper())
182a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
1837f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
18432cd2800SGreg Roach    }
18532cd2800SGreg Roach
18632cd2800SGreg Roach    /**
18732cd2800SGreg Roach     * Search for individuals by name.
18832cd2800SGreg Roach     *
189a7a24840SGreg Roach     * @param Tree[]   $trees
190a7a24840SGreg Roach     * @param string[] $search
19132cd2800SGreg Roach     * @param int      $offset
19232cd2800SGreg Roach     * @param int      $limit
19332cd2800SGreg Roach     *
194b5c8fd7eSGreg Roach     * @return Collection<Individual>
19532cd2800SGreg Roach     */
196a7a24840SGreg Roach    public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
19732cd2800SGreg Roach    {
19832cd2800SGreg Roach        $query = DB::table('individuals')
1990b5fd0a6SGreg Roach            ->join('name', static function (JoinClause $join): void {
20032cd2800SGreg Roach                $join
20132cd2800SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
202a7a24840SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
20332cd2800SGreg Roach            })
204e84cf2deSGreg Roach            ->orderBy('n_sort')
205d78d61f7SGreg Roach            ->distinct()
206685de081SGreg Roach            ->select(['individuals.*', 'n_sort']);
20732cd2800SGreg Roach
208a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
209a7a24840SGreg Roach        $this->whereSearch($query, 'n_full', $search);
210a7a24840SGreg Roach
211d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
21232cd2800SGreg Roach    }
21332cd2800SGreg Roach
21432cd2800SGreg Roach    /**
215dbe53437SGreg Roach     * @param Place $place
216dbe53437SGreg Roach     *
217dbe53437SGreg Roach     * @return Collection<Individual>
218dbe53437SGreg Roach     */
219dbe53437SGreg Roach    public function searchIndividualsInPlace(Place $place): Collection
220dbe53437SGreg Roach    {
221dbe53437SGreg Roach        return DB::table('individuals')
222dbe53437SGreg Roach            ->join('placelinks', static function (JoinClause $join) {
223dbe53437SGreg Roach                $join
224dbe53437SGreg Roach                    ->on('i_file', '=', 'pl_file')
225dbe53437SGreg Roach                    ->on('i_id', '=', 'pl_gid');
226dbe53437SGreg Roach            })
227dbe53437SGreg Roach            ->where('i_file', '=', $place->tree()->id())
228dbe53437SGreg Roach            ->where('pl_p_id', '=', $place->id())
229dbe53437SGreg Roach            ->select(['individuals.*'])
230dbe53437SGreg Roach            ->get()
231dbe53437SGreg Roach            ->each($this->rowLimiter())
232dbe53437SGreg Roach            ->map($this->individualRowMapper())
233dbe53437SGreg Roach            ->filter(GedcomRecord::accessFilter());
234dbe53437SGreg Roach    }
235dbe53437SGreg Roach
236dbe53437SGreg Roach    /**
23732cd2800SGreg Roach     * Search for media objects.
23832cd2800SGreg Roach     *
239a7a24840SGreg Roach     * @param Tree[]   $trees
240a7a24840SGreg Roach     * @param string[] $search
24132cd2800SGreg Roach     * @param int      $offset
24232cd2800SGreg Roach     * @param int      $limit
24332cd2800SGreg Roach     *
244b5c8fd7eSGreg Roach     * @return Collection<Media>
24532cd2800SGreg Roach     */
246a7a24840SGreg Roach    public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
24732cd2800SGreg Roach    {
248a7a24840SGreg Roach        $query = DB::table('media');
24932cd2800SGreg Roach
250a7a24840SGreg Roach        $this->whereTrees($query, 'media.m_file', $trees);
251a7a24840SGreg Roach        $this->whereSearch($query, 'm_gedcom', $search);
252a7a24840SGreg Roach
253d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
25432cd2800SGreg Roach    }
25532cd2800SGreg Roach
25632cd2800SGreg Roach    /**
25732cd2800SGreg Roach     * Search for notes.
25832cd2800SGreg Roach     *
259a7a24840SGreg Roach     * @param Tree[]   $trees
260a7a24840SGreg Roach     * @param string[] $search
26132cd2800SGreg Roach     * @param int      $offset
26232cd2800SGreg Roach     * @param int      $limit
26332cd2800SGreg Roach     *
264b5c8fd7eSGreg Roach     * @return Collection<Note>
26532cd2800SGreg Roach     */
266a7a24840SGreg Roach    public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
26732cd2800SGreg Roach    {
26832cd2800SGreg Roach        $query = DB::table('other')
269a7a24840SGreg Roach            ->where('o_type', '=', 'NOTE');
27032cd2800SGreg Roach
271a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
272a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
273a7a24840SGreg Roach
274d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
27532cd2800SGreg Roach    }
27632cd2800SGreg Roach
27732cd2800SGreg Roach    /**
27832cd2800SGreg Roach     * Search for repositories.
27932cd2800SGreg Roach     *
280a7a24840SGreg Roach     * @param Tree[]   $trees
281a7a24840SGreg Roach     * @param string[] $search
28232cd2800SGreg Roach     * @param int      $offset
28332cd2800SGreg Roach     * @param int      $limit
28432cd2800SGreg Roach     *
285b5c8fd7eSGreg Roach     * @return Collection<Repository>
28632cd2800SGreg Roach     */
287a7a24840SGreg Roach    public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
28832cd2800SGreg Roach    {
28932cd2800SGreg Roach        $query = DB::table('other')
290a7a24840SGreg Roach            ->where('o_type', '=', 'REPO');
29132cd2800SGreg Roach
292a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
293a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
294a7a24840SGreg Roach
295d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
29632cd2800SGreg Roach    }
29732cd2800SGreg Roach
29832cd2800SGreg Roach    /**
299a7a24840SGreg Roach     * Search for sources.
30032cd2800SGreg Roach     *
301a7a24840SGreg Roach     * @param Tree[]   $trees
302a7a24840SGreg Roach     * @param string[] $search
30332cd2800SGreg Roach     * @param int      $offset
30432cd2800SGreg Roach     * @param int      $limit
30532cd2800SGreg Roach     *
306b5c8fd7eSGreg Roach     * @return Collection<Source>
30732cd2800SGreg Roach     */
308a7a24840SGreg Roach    public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
309a7a24840SGreg Roach    {
310a7a24840SGreg Roach        $query = DB::table('sources');
311a7a24840SGreg Roach
312a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
313a7a24840SGreg Roach        $this->whereSearch($query, 's_gedcom', $search);
314a7a24840SGreg Roach
315d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
316a7a24840SGreg Roach    }
317a7a24840SGreg Roach
318a7a24840SGreg Roach    /**
319a7a24840SGreg Roach     * Search for sources by name.
320a7a24840SGreg Roach     *
321a7a24840SGreg Roach     * @param Tree[]   $trees
322a7a24840SGreg Roach     * @param string[] $search
323a7a24840SGreg Roach     * @param int      $offset
324a7a24840SGreg Roach     * @param int      $limit
325a7a24840SGreg Roach     *
326b5c8fd7eSGreg Roach     * @return Collection<Source>
327a7a24840SGreg Roach     */
328a7a24840SGreg Roach    public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
32932cd2800SGreg Roach    {
33032cd2800SGreg Roach        $query = DB::table('sources')
331c0804649SGreg Roach            ->orderBy('s_name');
33232cd2800SGreg Roach
333a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
334a7a24840SGreg Roach        $this->whereSearch($query, 's_name', $search);
335a7a24840SGreg Roach
336d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
33732cd2800SGreg Roach    }
33832cd2800SGreg Roach
33932cd2800SGreg Roach    /**
34032cd2800SGreg Roach     * Search for submitters.
34132cd2800SGreg Roach     *
342a7a24840SGreg Roach     * @param Tree[]   $trees
343a7a24840SGreg Roach     * @param string[] $search
34432cd2800SGreg Roach     * @param int      $offset
34532cd2800SGreg Roach     * @param int      $limit
34632cd2800SGreg Roach     *
347b5c8fd7eSGreg Roach     * @return Collection<Submitter>
34832cd2800SGreg Roach     */
349a7a24840SGreg Roach    public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
35032cd2800SGreg Roach    {
35132cd2800SGreg Roach        $query = DB::table('other')
352a7a24840SGreg Roach            ->where('o_type', '=', 'SUBM');
35332cd2800SGreg Roach
354a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
355a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
356a7a24840SGreg Roach
357d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
35832cd2800SGreg Roach    }
35932cd2800SGreg Roach
36032cd2800SGreg Roach    /**
361b68caec6SGreg Roach     * Search for places.
362b68caec6SGreg Roach     *
363b68caec6SGreg Roach     * @param Tree   $tree
364b68caec6SGreg Roach     * @param string $search
365b68caec6SGreg Roach     * @param int    $offset
366b68caec6SGreg Roach     * @param int    $limit
367b68caec6SGreg Roach     *
368b5c8fd7eSGreg Roach     * @return Collection<Place>
369b68caec6SGreg Roach     */
370b68caec6SGreg Roach    public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
371b68caec6SGreg Roach    {
372b68caec6SGreg Roach        $query = DB::table('places AS p0')
373b68caec6SGreg Roach            ->where('p0.p_file', '=', $tree->id())
374b68caec6SGreg Roach            ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id')
375b68caec6SGreg Roach            ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id')
376b68caec6SGreg Roach            ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id')
377b68caec6SGreg Roach            ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id')
378b68caec6SGreg Roach            ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id')
379b68caec6SGreg Roach            ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id')
380b68caec6SGreg Roach            ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id')
381b68caec6SGreg Roach            ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id')
382b68caec6SGreg Roach            ->orderBy('p0.p_place')
383b68caec6SGreg Roach            ->orderBy('p1.p_place')
384b68caec6SGreg Roach            ->orderBy('p2.p_place')
385b68caec6SGreg Roach            ->orderBy('p3.p_place')
386b68caec6SGreg Roach            ->orderBy('p4.p_place')
387b68caec6SGreg Roach            ->orderBy('p5.p_place')
388b68caec6SGreg Roach            ->orderBy('p6.p_place')
389b68caec6SGreg Roach            ->orderBy('p7.p_place')
390b68caec6SGreg Roach            ->orderBy('p8.p_place')
391b68caec6SGreg Roach            ->select([
392b68caec6SGreg Roach                'p0.p_place AS place0',
393b68caec6SGreg Roach                'p1.p_place AS place1',
394b68caec6SGreg Roach                'p2.p_place AS place2',
395b68caec6SGreg Roach                'p3.p_place AS place3',
396b68caec6SGreg Roach                'p4.p_place AS place4',
397b68caec6SGreg Roach                'p5.p_place AS place5',
398b68caec6SGreg Roach                'p6.p_place AS place6',
399b68caec6SGreg Roach                'p7.p_place AS place7',
400b68caec6SGreg Roach                'p8.p_place AS place8',
401b68caec6SGreg Roach            ]);
402b68caec6SGreg Roach
403b68caec6SGreg Roach        // Filter each level of the hierarchy.
404b68caec6SGreg Roach        foreach (explode(',', $search, 9) as $level => $string) {
405b5961194SGreg Roach            $query->where('p' . $level . '.p_place', 'LIKE', '%' . addcslashes($string, '\\%_') . '%');
406b68caec6SGreg Roach        }
407b68caec6SGreg Roach
4086c2179e2SGreg Roach        $row_mapper = static function (stdClass $row) use ($tree): Place {
409b68caec6SGreg Roach            $place = implode(', ', array_filter((array) $row));
410b68caec6SGreg Roach
411b68caec6SGreg Roach            return new Place($place, $tree);
412b68caec6SGreg Roach        };
413b68caec6SGreg Roach
4146c2179e2SGreg Roach        $filter = static function (): bool {
415a7a24840SGreg Roach            return true;
416a7a24840SGreg Roach        };
417b68caec6SGreg Roach
418a7a24840SGreg Roach        return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit);
419a7a24840SGreg Roach    }
420b68caec6SGreg Roach
421b68caec6SGreg Roach    /**
422a5dbc5b2SGreg Roach     * @param Tree[]   $trees
423dfb2cda2SGreg Roach     * @param string[] $fields
424dfb2cda2SGreg Roach     * @param string[] $modifiers
425dfb2cda2SGreg Roach     *
426b5c8fd7eSGreg Roach     * @return Collection<Individual>
427dfb2cda2SGreg Roach     */
428dfb2cda2SGreg Roach    public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection
429dfb2cda2SGreg Roach    {
430dfb2cda2SGreg Roach        $fields = array_filter($fields);
431dfb2cda2SGreg Roach
432dfb2cda2SGreg Roach        $query = DB::table('individuals')
433dfb2cda2SGreg Roach            ->select(['individuals.*'])
434dfb2cda2SGreg Roach            ->distinct();
435dfb2cda2SGreg Roach
436dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
437dfb2cda2SGreg Roach
438dfb2cda2SGreg Roach        // Join the following tables
439dfb2cda2SGreg Roach        $father_name   = false;
440dfb2cda2SGreg Roach        $mother_name   = false;
441dfb2cda2SGreg Roach        $spouse_family = false;
442dfb2cda2SGreg Roach        $indi_name     = false;
44356946b7aSGreg Roach        $indi_dates    = [];
44456946b7aSGreg Roach        $fam_dates     = [];
445dfb2cda2SGreg Roach        $indi_plac     = false;
446dfb2cda2SGreg Roach        $fam_plac      = false;
447dfb2cda2SGreg Roach
448dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
449dfb2cda2SGreg Roach            if ($field_value !== '') {
45056946b7aSGreg Roach                // Fields can have up to 4 parts, but we only need the first 3 to identify
45156946b7aSGreg Roach                // which tables to select
45256946b7aSGreg Roach                $field_parts = explode(':', $field_name . '::');
45356946b7aSGreg Roach
45456946b7aSGreg Roach                if ($field_parts[0] === 'FAMC') {
45556946b7aSGreg Roach                    // Parent name - FAMC:[HUSB|WIFE]:NAME:[GIVN|SURN]
45656946b7aSGreg Roach                    if ($field_parts[1] === 'HUSB') {
457dfb2cda2SGreg Roach                        $father_name = true;
45831dd1910SGreg Roach                    } else {
459dfb2cda2SGreg Roach                        $mother_name = true;
460dfb2cda2SGreg Roach                    }
46156946b7aSGreg Roach                } elseif ($field_parts[0] === 'NAME') {
46256946b7aSGreg Roach                    // Individual name - NAME:[GIVN|SURN]
46356946b7aSGreg Roach                    $indi_name = true;
46456946b7aSGreg Roach                } elseif ($field_parts[0] === 'FAMS') {
46556946b7aSGreg Roach                    // Family facts - FAMS:NOTE or FAMS:[FACT]:[DATE|PLAC]
466dfb2cda2SGreg Roach                    $spouse_family = true;
46756946b7aSGreg Roach                    if ($field_parts[2] === 'DATE') {
46856946b7aSGreg Roach                        $fam_dates[] = $field_parts[1];
46956946b7aSGreg Roach                    } elseif ($field_parts[2] === 'PLAC') {
47056946b7aSGreg Roach                        $fam_plac = true;
47156946b7aSGreg Roach                    }
472dfb2cda2SGreg Roach                } else {
47356946b7aSGreg Roach                    // Individual facts - [FACT] or [FACT]:[DATE|PLAC]
47456946b7aSGreg Roach                    if ($field_parts[1] === 'DATE') {
47556946b7aSGreg Roach                        $indi_dates[] = $field_parts[0];
47656946b7aSGreg Roach                    } elseif ($field_parts[1] === 'PLAC') {
477dfb2cda2SGreg Roach                        $indi_plac = true;
478dfb2cda2SGreg Roach                    }
479dfb2cda2SGreg Roach                }
480dfb2cda2SGreg Roach            }
481dfb2cda2SGreg Roach        }
482dfb2cda2SGreg Roach
483dfb2cda2SGreg Roach        if ($father_name || $mother_name) {
4840b5fd0a6SGreg Roach            $query->join('link AS l1', static function (JoinClause $join): void {
485dfb2cda2SGreg Roach                $join
486dfb2cda2SGreg Roach                    ->on('l1.l_file', '=', 'individuals.i_file')
487dfb2cda2SGreg Roach                    ->on('l1.l_from', '=', 'individuals.i_id')
488dfb2cda2SGreg Roach                    ->where('l1.l_type', '=', 'FAMC');
489dfb2cda2SGreg Roach            });
490dfb2cda2SGreg Roach
491dfb2cda2SGreg Roach            if ($father_name) {
4920b5fd0a6SGreg Roach                $query->join('link AS l2', static function (JoinClause $join): void {
493dfb2cda2SGreg Roach                    $join
494dfb2cda2SGreg Roach                        ->on('l2.l_file', '=', 'l1.l_file')
495dfb2cda2SGreg Roach                        ->on('l2.l_from', '=', 'l1.l_to')
496dfb2cda2SGreg Roach                        ->where('l2.l_type', '=', 'HUSB');
497dfb2cda2SGreg Roach                });
4980b5fd0a6SGreg Roach                $query->join('name AS father_name', static function (JoinClause $join): void {
499dfb2cda2SGreg Roach                    $join
500dfb2cda2SGreg Roach                        ->on('father_name.n_file', '=', 'l2.l_file')
501dfb2cda2SGreg Roach                        ->on('father_name.n_id', '=', 'l2.l_to');
502dfb2cda2SGreg Roach                });
503dfb2cda2SGreg Roach            }
504dfb2cda2SGreg Roach
505dfb2cda2SGreg Roach            if ($mother_name) {
5060b5fd0a6SGreg Roach                $query->join('link AS l3', static function (JoinClause $join): void {
507dfb2cda2SGreg Roach                    $join
508dfb2cda2SGreg Roach                        ->on('l3.l_file', '=', 'l1.l_file')
509dfb2cda2SGreg Roach                        ->on('l3.l_from', '=', 'l1.l_to')
510dfb2cda2SGreg Roach                        ->where('l3.l_type', '=', 'WIFE');
511dfb2cda2SGreg Roach                });
5120b5fd0a6SGreg Roach                $query->join('name AS mother_name', static function (JoinClause $join): void {
513dfb2cda2SGreg Roach                    $join
514dfb2cda2SGreg Roach                        ->on('mother_name.n_file', '=', 'l3.l_file')
515dfb2cda2SGreg Roach                        ->on('mother_name.n_id', '=', 'l3.l_to');
516dfb2cda2SGreg Roach                });
517dfb2cda2SGreg Roach            }
518dfb2cda2SGreg Roach        }
519dfb2cda2SGreg Roach
520dfb2cda2SGreg Roach        if ($spouse_family) {
5210b5fd0a6SGreg Roach            $query->join('link AS l4', static function (JoinClause $join): void {
522dfb2cda2SGreg Roach                $join
523dfb2cda2SGreg Roach                    ->on('l4.l_file', '=', 'individuals.i_file')
524dfb2cda2SGreg Roach                    ->on('l4.l_from', '=', 'individuals.i_id')
525dfb2cda2SGreg Roach                    ->where('l4.l_type', '=', 'FAMS');
526dfb2cda2SGreg Roach            });
5270b5fd0a6SGreg Roach            $query->join('families AS spouse_families', static function (JoinClause $join): void {
528dfb2cda2SGreg Roach                $join
529dfb2cda2SGreg Roach                    ->on('spouse_families.f_file', '=', 'l4.l_file')
530dfb2cda2SGreg Roach                    ->on('spouse_families.f_id', '=', 'l4.l_to');
531dfb2cda2SGreg Roach            });
532dfb2cda2SGreg Roach        }
533dfb2cda2SGreg Roach
534dfb2cda2SGreg Roach        if ($indi_name) {
5350b5fd0a6SGreg Roach            $query->join('name AS individual_name', static function (JoinClause $join): void {
536dfb2cda2SGreg Roach                $join
537dfb2cda2SGreg Roach                    ->on('individual_name.n_file', '=', 'individuals.i_file')
538dfb2cda2SGreg Roach                    ->on('individual_name.n_id', '=', 'individuals.i_id');
539dfb2cda2SGreg Roach            });
540dfb2cda2SGreg Roach        }
541dfb2cda2SGreg Roach
54256946b7aSGreg Roach        foreach (array_unique($indi_dates) as $indi_date) {
54356946b7aSGreg Roach            $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void {
544dfb2cda2SGreg Roach                $join
54556946b7aSGreg Roach                    ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file')
54656946b7aSGreg Roach                    ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id');
547dfb2cda2SGreg Roach            });
548dfb2cda2SGreg Roach        }
549dfb2cda2SGreg Roach
55056946b7aSGreg Roach        foreach (array_unique($fam_dates) as $fam_date) {
55156946b7aSGreg Roach            $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void {
552dfb2cda2SGreg Roach                $join
553b06d667bSGreg Roach                    ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.f_file')
554b06d667bSGreg Roach                    ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.f_id');
555dfb2cda2SGreg Roach            });
556dfb2cda2SGreg Roach        }
557dfb2cda2SGreg Roach
558dfb2cda2SGreg Roach        if ($indi_plac) {
5590b5fd0a6SGreg Roach            $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void {
560dfb2cda2SGreg Roach                $join
561dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_file', '=', 'individuals.i_file')
562dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id');
563dfb2cda2SGreg Roach            });
5640b5fd0a6SGreg Roach            $query->join('places AS individual_places', static function (JoinClause $join): void {
565dfb2cda2SGreg Roach                $join
566dfb2cda2SGreg Roach                    ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file')
567dfb2cda2SGreg Roach                    ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id');
568dfb2cda2SGreg Roach            });
569dfb2cda2SGreg Roach        }
570dfb2cda2SGreg Roach
571dfb2cda2SGreg Roach        if ($fam_plac) {
5720b5fd0a6SGreg Roach            $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void {
573dfb2cda2SGreg Roach                $join
574dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file')
575dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id');
576dfb2cda2SGreg Roach            });
5770b5fd0a6SGreg Roach            $query->join('places AS family_places', static function (JoinClause $join): void {
578dfb2cda2SGreg Roach                $join
579dfb2cda2SGreg Roach                    ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file')
580dfb2cda2SGreg Roach                    ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id');
581dfb2cda2SGreg Roach            });
582dfb2cda2SGreg Roach        }
583dfb2cda2SGreg Roach
584dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
58556946b7aSGreg Roach            $parts = explode(':', $field_name . ':::');
586dfb2cda2SGreg Roach            if ($parts[0] === 'NAME') {
587dfb2cda2SGreg Roach                // NAME:*
588dfb2cda2SGreg Roach                switch ($parts[1]) {
589dfb2cda2SGreg Roach                    case 'GIVN':
590dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
591dfb2cda2SGreg Roach                            case 'EXACT':
592dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', '=', $field_value);
593dfb2cda2SGreg Roach                                break;
594dfb2cda2SGreg Roach                            case 'BEGINS':
595dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', $field_value . '%');
596dfb2cda2SGreg Roach                                break;
597dfb2cda2SGreg Roach                            case 'CONTAINS':
598dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
599dfb2cda2SGreg Roach                                break;
600dfb2cda2SGreg Roach                            case 'SDX_STD':
601dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
602dfb2cda2SGreg Roach                                if ($sdx !== '') {
603dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx);
604dfb2cda2SGreg Roach                                } else {
605dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
606dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
607dfb2cda2SGreg Roach                                }
608dfb2cda2SGreg Roach                                break;
609dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
610dfb2cda2SGreg Roach                            case 'SDX_DM':
611dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
612dfb2cda2SGreg Roach                                if ($sdx !== '') {
613dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx);
614dfb2cda2SGreg Roach                                } else {
615dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
616dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
617dfb2cda2SGreg Roach                                }
618dfb2cda2SGreg Roach                                break;
619dfb2cda2SGreg Roach                        }
620*788f881dSGreg Roach                        unset($fields[$field_name]);
621dfb2cda2SGreg Roach                        break;
622dfb2cda2SGreg Roach                    case 'SURN':
623dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
624dfb2cda2SGreg Roach                            case 'EXACT':
625fc6d23eeSGreg Roach                                $query->where(function (Builder $query) use ($field_value): void {
626fc6d23eeSGreg Roach                                    $query
627fc6d23eeSGreg Roach                                        ->where('individual_name.n_surn', '=', $field_value)
628fc6d23eeSGreg Roach                                        ->orWhere('individual_name.n_surname', '=', $field_value);
629fc6d23eeSGreg Roach                                });
630dfb2cda2SGreg Roach                                break;
631dfb2cda2SGreg Roach                            case 'BEGINS':
632fc6d23eeSGreg Roach                                $query->where(function (Builder $query) use ($field_value): void {
633fc6d23eeSGreg Roach                                    $query
634fc6d23eeSGreg Roach                                        ->where('individual_name.n_surn', 'LIKE', $field_value . '%')
6359875d474SGreg Roach                                        ->orWhere('individual_name.n_surname', 'LIKE', $field_value . '%');
636fc6d23eeSGreg Roach                                });
637dfb2cda2SGreg Roach                                break;
638dfb2cda2SGreg Roach                            case 'CONTAINS':
639fc6d23eeSGreg Roach                                $query->where(function (Builder $query) use ($field_value): void {
640fc6d23eeSGreg Roach                                    $query
641fc6d23eeSGreg Roach                                        ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%')
6429875d474SGreg Roach                                        ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%');
643fc6d23eeSGreg Roach                                });
644dfb2cda2SGreg Roach                                break;
645dfb2cda2SGreg Roach                            case 'SDX_STD':
646dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
647dfb2cda2SGreg Roach                                if ($sdx !== '') {
648dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx);
649dfb2cda2SGreg Roach                                } else {
650dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
651fc6d23eeSGreg Roach                                    $query->where(function (Builder $query) use ($field_value): void {
652fc6d23eeSGreg Roach                                        $query
653fc6d23eeSGreg Roach                                            ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%')
6549875d474SGreg Roach                                            ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%');
655fc6d23eeSGreg Roach                                    });
656dfb2cda2SGreg Roach                                }
657dfb2cda2SGreg Roach                                break;
658dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
659dfb2cda2SGreg Roach                            case 'SDX_DM':
660dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
661dfb2cda2SGreg Roach                                if ($sdx !== '') {
662dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx);
663dfb2cda2SGreg Roach                                } else {
664dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
6659875d474SGreg Roach                                    $query->where(function (Builder $query) use ($field_value): void {
6669875d474SGreg Roach                                        $query
6679875d474SGreg Roach                                            ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%')
6689875d474SGreg Roach                                            ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%');
6699875d474SGreg Roach                                    });
670dfb2cda2SGreg Roach                                }
671dfb2cda2SGreg Roach                                break;
672dfb2cda2SGreg Roach                        }
673b4adec9eSGreg Roach                        unset($fields[$field_name]);
674dfb2cda2SGreg Roach                        break;
675dfb2cda2SGreg Roach                    case 'NICK':
676dfb2cda2SGreg Roach                    case '_MARNM':
677dfb2cda2SGreg Roach                    case '_HEB':
678dfb2cda2SGreg Roach                    case '_AKA':
679b4adec9eSGreg Roach                        $like = "%\n1 " . $parts[0] . "%\n2 " . $parts[1] . ' %' . preg_quote($field_value, '/') . '%';
680b4adec9eSGreg Roach                        $query->where('individuals.i_gedcom', 'LIKE', $like);
681dfb2cda2SGreg Roach                        break;
682dfb2cda2SGreg Roach                }
683dfb2cda2SGreg Roach            } elseif ($parts[1] === 'DATE') {
684dfb2cda2SGreg Roach                // *:DATE
685dfb2cda2SGreg Roach                $date = new Date($field_value);
686dfb2cda2SGreg Roach                if ($date->isOK()) {
687dfb2cda2SGreg Roach                    $delta = 365 * ($modifiers[$field_name] ?? 0);
688dfb2cda2SGreg Roach                    $query
68956946b7aSGreg Roach                        ->where('date_' . $parts[0] . '.d_fact', '=', $parts[0])
69056946b7aSGreg Roach                        ->where('date_' . $parts[0] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
69156946b7aSGreg Roach                        ->where('date_' . $parts[0] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta);
692dfb2cda2SGreg Roach                }
693dfb2cda2SGreg Roach                unset($fields[$field_name]);
694dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') {
695dfb2cda2SGreg Roach                // FAMS:*:DATE
696dfb2cda2SGreg Roach                $date = new Date($field_value);
697dfb2cda2SGreg Roach                if ($date->isOK()) {
698dfb2cda2SGreg Roach                    $delta = 365 * $modifiers[$field_name];
699dfb2cda2SGreg Roach                    $query
70056946b7aSGreg Roach                        ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1])
70156946b7aSGreg Roach                        ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
70256946b7aSGreg Roach                        ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta);
703dfb2cda2SGreg Roach                }
704dfb2cda2SGreg Roach                unset($fields[$field_name]);
705dfb2cda2SGreg Roach            } elseif ($parts[1] === 'PLAC') {
706dfb2cda2SGreg Roach                // *:PLAC
707dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
708dfb2cda2SGreg Roach                $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%');
709dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
710dfb2cda2SGreg Roach                // FAMS:*:PLAC
711dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
712dfb2cda2SGreg Roach                $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%');
713dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') {
714dfb2cda2SGreg Roach                $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name';
715dfb2cda2SGreg Roach                // NAME:*
716dfb2cda2SGreg Roach                switch ($parts[3]) {
717dfb2cda2SGreg Roach                    case 'GIVN':
718dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
719dfb2cda2SGreg Roach                            case 'EXACT':
720dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', '=', $field_value);
721dfb2cda2SGreg Roach                                break;
722dfb2cda2SGreg Roach                            case 'BEGINS':
723dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', $field_value . '%');
724dfb2cda2SGreg Roach                                break;
725dfb2cda2SGreg Roach                            case 'CONTAINS':
726dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
727dfb2cda2SGreg Roach                                break;
728dfb2cda2SGreg Roach                            case 'SDX_STD':
729dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
730dfb2cda2SGreg Roach                                if ($sdx !== '') {
731dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx);
732dfb2cda2SGreg Roach                                } else {
733dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
734dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
735dfb2cda2SGreg Roach                                }
736dfb2cda2SGreg Roach                                break;
737dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
738dfb2cda2SGreg Roach                            case 'SDX_DM':
739dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
740dfb2cda2SGreg Roach                                if ($sdx !== '') {
741dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx);
742dfb2cda2SGreg Roach                                } else {
743dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
744dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
745dfb2cda2SGreg Roach                                }
746dfb2cda2SGreg Roach                                break;
747dfb2cda2SGreg Roach                        }
748dfb2cda2SGreg Roach                        break;
749dfb2cda2SGreg Roach                    case 'SURN':
750dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
751dfb2cda2SGreg Roach                            case 'EXACT':
752dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', '=', $field_value);
753dfb2cda2SGreg Roach                                break;
754dfb2cda2SGreg Roach                            case 'BEGINS':
755dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', $field_value . '%');
756dfb2cda2SGreg Roach                                break;
757dfb2cda2SGreg Roach                            case 'CONTAINS':
758dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
759dfb2cda2SGreg Roach                                break;
760dfb2cda2SGreg Roach                            case 'SDX_STD':
761dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
762dfb2cda2SGreg Roach                                if ($sdx !== '') {
763dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx);
764dfb2cda2SGreg Roach                                } else {
765dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
766dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
767dfb2cda2SGreg Roach                                }
768dfb2cda2SGreg Roach                                break;
769dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
770dfb2cda2SGreg Roach                            case 'SDX_DM':
771dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
772dfb2cda2SGreg Roach                                if ($sdx !== '') {
773dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx);
774dfb2cda2SGreg Roach                                } else {
775dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
776dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
777dfb2cda2SGreg Roach                                }
778dfb2cda2SGreg Roach                                break;
779dfb2cda2SGreg Roach                        }
780dfb2cda2SGreg Roach                        break;
781dfb2cda2SGreg Roach                }
782dfb2cda2SGreg Roach                unset($fields[$field_name]);
783dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS') {
784dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
785dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
7863c480c6bSGreg Roach                $query->where('spouse_families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%');
787dfb2cda2SGreg Roach            } elseif ($parts[1] === 'TYPE') {
788dfb2cda2SGreg Roach                // e.g. FACT:TYPE or EVEN:TYPE
789dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
790b4adec9eSGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . "%\n2 TYPE %" . $field_value . '%');
791dfb2cda2SGreg Roach            } else {
792dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
793dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
794c5457947SGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%' . $parts[1] . '%' . $field_value . '%');
795dfb2cda2SGreg Roach            }
796dfb2cda2SGreg Roach        }
797b4adec9eSGreg Roach
798dfb2cda2SGreg Roach        return $query
799dfb2cda2SGreg Roach            ->get()
80052a8ef61SGreg Roach            ->each($this->rowLimiter())
801d5ad3db0SGreg Roach            ->map($this->individualRowMapper())
802dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter())
8030b5fd0a6SGreg Roach            ->filter(static function (Individual $individual) use ($fields): bool {
804c5457947SGreg Roach                // Check for searches which were only partially matched by SQL
805dfb2cda2SGreg Roach                foreach ($fields as $field_name => $field_value) {
806c5457947SGreg Roach                    $parts = explode(':', $field_name . '::::');
807dfb2cda2SGreg Roach
808b4adec9eSGreg Roach                    // NAME:*
809b4adec9eSGreg Roach                    if ($parts[0] === 'NAME') {
810b4adec9eSGreg Roach                        $regex = '/\n1 NAME.*(?:\n2.*)*\n2 ' . $parts[1] . ' .*' . preg_quote($field_value, '/') . '/i';
811b4adec9eSGreg Roach
812b4adec9eSGreg Roach                        if (preg_match($regex, $individual->gedcom())) {
813b4adec9eSGreg Roach                            continue;
814b4adec9eSGreg Roach                        }
815b4adec9eSGreg Roach
816b4adec9eSGreg Roach                        return false;
817b4adec9eSGreg Roach                    }
818b4adec9eSGreg Roach
819b4adec9eSGreg Roach                    $regex = '/' . preg_quote($field_value, '/') . '/i';
820b4adec9eSGreg Roach
821dfb2cda2SGreg Roach                    // *:PLAC
822c5457947SGreg Roach                    if ($parts[1] === 'PLAC') {
823a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
824392561bbSGreg Roach                            if (preg_match($regex, $fact->place()->gedcomName())) {
825c5457947SGreg Roach                                continue 2;
826dfb2cda2SGreg Roach                            }
827a41e65f0SGreg Roach                        }
828c5457947SGreg Roach                        return false;
829c5457947SGreg Roach                    }
830c5457947SGreg Roach
831dfb2cda2SGreg Roach                    // FAMS:*:PLAC
832c5457947SGreg Roach                    if ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
83339ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
834a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
835392561bbSGreg Roach                                if (preg_match($regex, $fact->place()->gedcomName())) {
836c5457947SGreg Roach                                    continue 2;
837a41e65f0SGreg Roach                                }
838dfb2cda2SGreg Roach                            }
839dfb2cda2SGreg Roach                        }
840c5457947SGreg Roach                        return false;
841c5457947SGreg Roach                    }
842c5457947SGreg Roach
843dfb2cda2SGreg Roach                    // e.g. searches for occupation, religion, note, etc.
844c5457947SGreg Roach                    if ($parts[0] === 'FAMS') {
84539ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
846a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
847a41e65f0SGreg Roach                                if (preg_match($regex, $fact->value())) {
848c5457947SGreg Roach                                    continue 3;
849a41e65f0SGreg Roach                                }
850dfb2cda2SGreg Roach                            }
851dfb2cda2SGreg Roach                        }
852c5457947SGreg Roach                        return false;
853c5457947SGreg Roach                    }
854c5457947SGreg Roach
855dfb2cda2SGreg Roach                    // e.g. FACT:TYPE or EVEN:TYPE
856c5457947SGreg Roach                    if ($parts[1] === 'TYPE' || $parts[1] === '_WT_USER') {
857a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
858c5457947SGreg Roach                            if (preg_match($regex, $fact->attribute($parts[1]))) {
859c5457947SGreg Roach                                continue 2;
860dfb2cda2SGreg Roach                            }
861dfb2cda2SGreg Roach                        }
862dfb2cda2SGreg Roach
863dfb2cda2SGreg Roach                        return false;
864dfb2cda2SGreg Roach                    }
865c5457947SGreg Roach                }
866dfb2cda2SGreg Roach
867dfb2cda2SGreg Roach                return true;
868dfb2cda2SGreg Roach            });
869dfb2cda2SGreg Roach    }
870dfb2cda2SGreg Roach
871dfb2cda2SGreg Roach    /**
872dfb2cda2SGreg Roach     * @param string $soundex
873dfb2cda2SGreg Roach     * @param string $lastname
874dfb2cda2SGreg Roach     * @param string $firstname
875dfb2cda2SGreg Roach     * @param string $place
876dfb2cda2SGreg Roach     * @param Tree[] $search_trees
877dfb2cda2SGreg Roach     *
878b5c8fd7eSGreg Roach     * @return Collection<Individual>
879dfb2cda2SGreg Roach     */
880dfb2cda2SGreg Roach    public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection
881dfb2cda2SGreg Roach    {
882dfb2cda2SGreg Roach        switch ($soundex) {
883dfb2cda2SGreg Roach            default:
884dfb2cda2SGreg Roach            case 'Russell':
885dfb2cda2SGreg Roach                $givn_sdx   = Soundex::russell($firstname);
886dfb2cda2SGreg Roach                $surn_sdx   = Soundex::russell($lastname);
887dfb2cda2SGreg Roach                $plac_sdx   = Soundex::russell($place);
888dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_std';
889dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_std';
890dfb2cda2SGreg Roach                $plac_field = 'p_std_soundex';
891dfb2cda2SGreg Roach                break;
892dfb2cda2SGreg Roach            case 'DaitchM':
893dfb2cda2SGreg Roach                $givn_sdx   = Soundex::daitchMokotoff($firstname);
894dfb2cda2SGreg Roach                $surn_sdx   = Soundex::daitchMokotoff($lastname);
895dfb2cda2SGreg Roach                $plac_sdx   = Soundex::daitchMokotoff($place);
896dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_dm';
897dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_dm';
898dfb2cda2SGreg Roach                $plac_field = 'p_dm_soundex';
899dfb2cda2SGreg Roach                break;
900dfb2cda2SGreg Roach        }
901dfb2cda2SGreg Roach
902dfb2cda2SGreg Roach        // Nothing to search for? Return nothing.
903dfb2cda2SGreg Roach        if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') {
9043fda39a7SGreg Roach            return new Collection();
905dfb2cda2SGreg Roach        }
906dfb2cda2SGreg Roach
907dfb2cda2SGreg Roach        $query = DB::table('individuals')
908dfb2cda2SGreg Roach            ->select(['individuals.*'])
909dfb2cda2SGreg Roach            ->distinct();
910dfb2cda2SGreg Roach
911dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $search_trees);
912dfb2cda2SGreg Roach
913dfb2cda2SGreg Roach        if ($plac_sdx !== '') {
9140b5fd0a6SGreg Roach            $query->join('placelinks', static function (JoinClause $join): void {
915dfb2cda2SGreg Roach                $join
916dfb2cda2SGreg Roach                    ->on('placelinks.pl_file', '=', 'individuals.i_file')
917dfb2cda2SGreg Roach                    ->on('placelinks.pl_gid', '=', 'individuals.i_id');
918dfb2cda2SGreg Roach            });
9190b5fd0a6SGreg Roach            $query->join('places', static function (JoinClause $join): void {
920dfb2cda2SGreg Roach                $join
921dfb2cda2SGreg Roach                    ->on('places.p_file', '=', 'placelinks.pl_file')
922dfb2cda2SGreg Roach                    ->on('places.p_id', '=', 'placelinks.pl_p_id');
923dfb2cda2SGreg Roach            });
924dfb2cda2SGreg Roach
925dfb2cda2SGreg Roach            $this->wherePhonetic($query, $plac_field, $plac_sdx);
926dfb2cda2SGreg Roach        }
927dfb2cda2SGreg Roach
928dfb2cda2SGreg Roach        if ($givn_sdx !== '' || $surn_sdx !== '') {
9290b5fd0a6SGreg Roach            $query->join('name', static function (JoinClause $join): void {
930dfb2cda2SGreg Roach                $join
931dfb2cda2SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
932dfb2cda2SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
933dfb2cda2SGreg Roach            });
934dfb2cda2SGreg Roach
935dfb2cda2SGreg Roach            $this->wherePhonetic($query, $givn_field, $givn_sdx);
936dfb2cda2SGreg Roach            $this->wherePhonetic($query, $surn_field, $surn_sdx);
937dfb2cda2SGreg Roach        }
938dfb2cda2SGreg Roach
939dfb2cda2SGreg Roach        return $query
940dfb2cda2SGreg Roach            ->get()
94152a8ef61SGreg Roach            ->each($this->rowLimiter())
942d5ad3db0SGreg Roach            ->map($this->individualRowMapper())
943dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter());
944dfb2cda2SGreg Roach    }
945dfb2cda2SGreg Roach
946dfb2cda2SGreg Roach    /**
94732cd2800SGreg Roach     * Paginate a search query.
94832cd2800SGreg Roach     *
94932cd2800SGreg Roach     * @param Builder $query      Searches the database for the desired records.
95032cd2800SGreg Roach     * @param Closure $row_mapper Converts a row from the query into a record.
951a7a24840SGreg Roach     * @param Closure $row_filter
95232cd2800SGreg Roach     * @param int     $offset     Skip this many rows.
95332cd2800SGreg Roach     * @param int     $limit      Take this many rows.
95432cd2800SGreg Roach     *
955b5c8fd7eSGreg Roach     * @return Collection<mixed>
95632cd2800SGreg Roach     */
957a7a24840SGreg Roach    private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection
95832cd2800SGreg Roach    {
95932cd2800SGreg Roach        $collection = new Collection();
96032cd2800SGreg Roach
96132cd2800SGreg Roach        foreach ($query->cursor() as $row) {
96232cd2800SGreg Roach            $record = $row_mapper($row);
963b68caec6SGreg Roach            // If the object has a method "canShow()", then use it to filter for privacy.
964a7a24840SGreg Roach            if ($row_filter($record)) {
96532cd2800SGreg Roach                if ($offset > 0) {
96632cd2800SGreg Roach                    $offset--;
96732cd2800SGreg Roach                } else {
96832cd2800SGreg Roach                    if ($limit > 0) {
96932cd2800SGreg Roach                        $collection->push($record);
97032cd2800SGreg Roach                    }
97132cd2800SGreg Roach
97232cd2800SGreg Roach                    $limit--;
97332cd2800SGreg Roach
97432cd2800SGreg Roach                    if ($limit === 0) {
97532cd2800SGreg Roach                        break;
97632cd2800SGreg Roach                    }
97732cd2800SGreg Roach                }
97832cd2800SGreg Roach            }
97932cd2800SGreg Roach        }
98032cd2800SGreg Roach
981e0458bdcSGreg Roach
98232cd2800SGreg Roach        return $collection;
98332cd2800SGreg Roach    }
984a7a24840SGreg Roach
985a7a24840SGreg Roach    /**
986a7a24840SGreg Roach     * Apply search filters to a SQL query column.  Apply collation rules to MySQL.
987a7a24840SGreg Roach     *
988a7a24840SGreg Roach     * @param Builder           $query
989a7a24840SGreg Roach     * @param Expression|string $field
990a7a24840SGreg Roach     * @param string[]          $search_terms
991a7a24840SGreg Roach     */
992a7a24840SGreg Roach    private function whereSearch(Builder $query, $field, array $search_terms): void
993a7a24840SGreg Roach    {
994a7a24840SGreg Roach        if ($field instanceof Expression) {
995a7a24840SGreg Roach            $field = $field->getValue();
996a7a24840SGreg Roach        }
997a7a24840SGreg Roach
998a7a24840SGreg Roach        foreach ($search_terms as $search_term) {
999b5961194SGreg Roach            $query->where(new Expression($field), 'LIKE', '%' . addcslashes($search_term, '\\%_') . '%');
1000a7a24840SGreg Roach        }
1001a7a24840SGreg Roach    }
1002a7a24840SGreg Roach
1003a7a24840SGreg Roach    /**
10042d686e68SGreg Roach     * Apply soundex search filters to a SQL query column.
10052d686e68SGreg Roach     *
10062d686e68SGreg Roach     * @param Builder           $query
10072d686e68SGreg Roach     * @param Expression|string $field
10082d686e68SGreg Roach     * @param string            $soundex
10092d686e68SGreg Roach     */
10102d686e68SGreg Roach    private function wherePhonetic(Builder $query, $field, string $soundex): void
10112d686e68SGreg Roach    {
10122d686e68SGreg Roach        if ($soundex !== '') {
10130b5fd0a6SGreg Roach            $query->where(static function (Builder $query) use ($soundex, $field): void {
10142d686e68SGreg Roach                foreach (explode(':', $soundex) as $sdx) {
10152d686e68SGreg Roach                    $query->orWhere($field, 'LIKE', '%' . $sdx . '%');
10162d686e68SGreg Roach                }
10172d686e68SGreg Roach            });
10182d686e68SGreg Roach        }
10192d686e68SGreg Roach    }
10202d686e68SGreg Roach
10212d686e68SGreg Roach    /**
1022a7a24840SGreg Roach     * @param Builder $query
1023a7a24840SGreg Roach     * @param string  $tree_id_field
1024a7a24840SGreg Roach     * @param Tree[]  $trees
1025a7a24840SGreg Roach     */
1026a7a24840SGreg Roach    private function whereTrees(Builder $query, string $tree_id_field, array $trees): void
1027a7a24840SGreg Roach    {
10280b5fd0a6SGreg Roach        $tree_ids = array_map(static function (Tree $tree): int {
1029a7a24840SGreg Roach            return $tree->id();
1030a7a24840SGreg Roach        }, $trees);
1031a7a24840SGreg Roach
1032a7a24840SGreg Roach        $query->whereIn($tree_id_field, $tree_ids);
1033a7a24840SGreg Roach    }
1034a7a24840SGreg Roach
1035d5ad3db0SGreg Roach    /**
1036d5ad3db0SGreg Roach     * Find the media object that uses a particular media file.
1037d5ad3db0SGreg Roach     *
1038d5ad3db0SGreg Roach     * @param string $file
1039d5ad3db0SGreg Roach     *
1040d5ad3db0SGreg Roach     * @return Media[]
1041d5ad3db0SGreg Roach     */
1042d5ad3db0SGreg Roach    public function findMediaObjectsForMediaFile(string $file): array
1043d5ad3db0SGreg Roach    {
1044d5ad3db0SGreg Roach        return DB::table('media')
1045d5ad3db0SGreg Roach            ->join('media_file', static function (JoinClause $join): void {
1046d5ad3db0SGreg Roach                $join
1047d5ad3db0SGreg Roach                    ->on('media_file.m_file', '=', 'media.m_file')
1048d5ad3db0SGreg Roach                    ->on('media_file.m_id', '=', 'media.m_id');
1049d5ad3db0SGreg Roach            })
1050d5ad3db0SGreg Roach            ->join('gedcom_setting', 'media.m_file', '=', 'gedcom_setting.gedcom_id')
1051d5ad3db0SGreg Roach            ->where(new Expression('setting_value || multimedia_file_refn'), '=', $file)
1052d5ad3db0SGreg Roach            ->select(['media.*'])
1053d5ad3db0SGreg Roach            ->distinct()
1054d5ad3db0SGreg Roach            ->get()
1055d5ad3db0SGreg Roach            ->map($this->mediaRowMapper())
1056d5ad3db0SGreg Roach            ->all();
1057d5ad3db0SGreg Roach    }
1058d5ad3db0SGreg Roach
1059a7a24840SGreg Roach    /**
1060a7a24840SGreg Roach     * A closure to filter records by privacy-filtered GEDCOM data.
1061a7a24840SGreg Roach     *
10625d4b7ec2SGreg Roach     * @param array<string> $search_terms
1063a7a24840SGreg Roach     *
1064a7a24840SGreg Roach     * @return Closure
1065a7a24840SGreg Roach     */
1066a7a24840SGreg Roach    private function rawGedcomFilter(array $search_terms): Closure
1067a7a24840SGreg Roach    {
10686c2179e2SGreg Roach        return static function (GedcomRecord $record) use ($search_terms): bool {
1069a7a24840SGreg Roach            // Ignore non-genealogy fields
1070faa5e163SGreg Roach            $gedcom = preg_replace('/\n\d (?:_UID|_WT_USER) .*/', '', $record->gedcom());
1071a7a24840SGreg Roach
1072a7a24840SGreg Roach            // Ignore matches in links
1073a7a24840SGreg Roach            $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom);
1074a7a24840SGreg Roach
1075a7a24840SGreg Roach            // Re-apply the filtering
1076a7a24840SGreg Roach            foreach ($search_terms as $search_term) {
1077a7a24840SGreg Roach                if (mb_stripos($gedcom, $search_term) === false) {
1078a7a24840SGreg Roach                    return false;
1079a7a24840SGreg Roach                }
1080a7a24840SGreg Roach            }
1081a7a24840SGreg Roach
1082a7a24840SGreg Roach            return true;
1083a7a24840SGreg Roach        };
1084a7a24840SGreg Roach    }
108552a8ef61SGreg Roach
108652a8ef61SGreg Roach    /**
108752a8ef61SGreg Roach     * Searching for short or common text can give more results than the system can process.
108852a8ef61SGreg Roach     *
108952a8ef61SGreg Roach     * @param int $limit
109052a8ef61SGreg Roach     *
109152a8ef61SGreg Roach     * @return Closure
109252a8ef61SGreg Roach     */
1093645e3037SGreg Roach    private function rowLimiter(int $limit = self::MAX_SEARCH_RESULTS): Closure
109452a8ef61SGreg Roach    {
10956c2179e2SGreg Roach        return static function () use ($limit): void {
109652a8ef61SGreg Roach            static $n = 0;
109752a8ef61SGreg Roach
109852a8ef61SGreg Roach            if (++$n > $limit) {
109952a8ef61SGreg Roach                $message = I18N::translate('The search returned too many results.');
110052a8ef61SGreg Roach
1101d501c45dSGreg Roach                throw new HttpServiceUnavailableException($message);
110252a8ef61SGreg Roach            }
110352a8ef61SGreg Roach        };
110452a8ef61SGreg Roach    }
1105d5ad3db0SGreg Roach
1106d5ad3db0SGreg Roach    /**
1107d5ad3db0SGreg Roach     * Convert a row from any tree in the families table into a family object.
1108d5ad3db0SGreg Roach     *
1109d5ad3db0SGreg Roach     * @return Closure
1110d5ad3db0SGreg Roach     */
1111d5ad3db0SGreg Roach    private function familyRowMapper(): Closure
1112d5ad3db0SGreg Roach    {
1113d5ad3db0SGreg Roach        return function (stdClass $row): Family {
1114d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->f_file);
1115d5ad3db0SGreg Roach
1116a091ac74SGreg Roach            return Factory::family()->mapper($tree)($row);
1117d5ad3db0SGreg Roach        };
1118d5ad3db0SGreg Roach    }
1119d5ad3db0SGreg Roach
1120d5ad3db0SGreg Roach    /**
1121d5ad3db0SGreg Roach     * Convert a row from any tree in the individuals table into an individual object.
1122d5ad3db0SGreg Roach     *
1123d5ad3db0SGreg Roach     * @return Closure
1124d5ad3db0SGreg Roach     */
1125d5ad3db0SGreg Roach    private function individualRowMapper(): Closure
1126d5ad3db0SGreg Roach    {
1127d5ad3db0SGreg Roach        return function (stdClass $row): Individual {
1128d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->i_file);
1129d5ad3db0SGreg Roach
1130a091ac74SGreg Roach            return Factory::individual()->mapper($tree)($row);
1131d5ad3db0SGreg Roach        };
1132d5ad3db0SGreg Roach    }
1133d5ad3db0SGreg Roach
1134d5ad3db0SGreg Roach    /**
1135d5ad3db0SGreg Roach     * Convert a row from any tree in the media table into an media object.
1136d5ad3db0SGreg Roach     *
1137d5ad3db0SGreg Roach     * @return Closure
1138d5ad3db0SGreg Roach     */
1139d5ad3db0SGreg Roach    private function mediaRowMapper(): Closure
1140d5ad3db0SGreg Roach    {
1141d5ad3db0SGreg Roach        return function (stdClass $row): Media {
1142d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->m_file);
1143d5ad3db0SGreg Roach
1144a091ac74SGreg Roach            return Factory::media()->mapper($tree)($row);
1145d5ad3db0SGreg Roach        };
1146d5ad3db0SGreg Roach    }
1147d5ad3db0SGreg Roach
1148d5ad3db0SGreg Roach    /**
1149d5ad3db0SGreg Roach     * Convert a row from any tree in the other table into a note object.
1150d5ad3db0SGreg Roach     *
1151d5ad3db0SGreg Roach     * @return Closure
1152d5ad3db0SGreg Roach     */
1153d5ad3db0SGreg Roach    private function noteRowMapper(): Closure
1154d5ad3db0SGreg Roach    {
1155d5ad3db0SGreg Roach        return function (stdClass $row): Note {
1156d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->o_file);
1157d5ad3db0SGreg Roach
1158a091ac74SGreg Roach            return Factory::note()->mapper($tree)($row);
1159d5ad3db0SGreg Roach        };
1160d5ad3db0SGreg Roach    }
1161d5ad3db0SGreg Roach
1162d5ad3db0SGreg Roach    /**
1163d5ad3db0SGreg Roach     * Convert a row from any tree in the other table into a repository object.
1164d5ad3db0SGreg Roach     *
1165d5ad3db0SGreg Roach     * @return Closure
1166d5ad3db0SGreg Roach     */
1167d5ad3db0SGreg Roach    private function repositoryRowMapper(): Closure
1168d5ad3db0SGreg Roach    {
1169d5ad3db0SGreg Roach        return function (stdClass $row): Repository {
1170d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->o_file);
1171d5ad3db0SGreg Roach
1172a091ac74SGreg Roach            return Factory::repository()->mapper($tree)($row);
1173d5ad3db0SGreg Roach        };
1174d5ad3db0SGreg Roach    }
1175d5ad3db0SGreg Roach
1176d5ad3db0SGreg Roach    /**
1177d5ad3db0SGreg Roach     * Convert a row from any tree in the sources table into a source object.
1178d5ad3db0SGreg Roach     *
1179d5ad3db0SGreg Roach     * @return Closure
1180d5ad3db0SGreg Roach     */
1181d5ad3db0SGreg Roach    private function sourceRowMapper(): Closure
1182d5ad3db0SGreg Roach    {
1183d5ad3db0SGreg Roach        return function (stdClass $row): Source {
1184d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->s_file);
1185d5ad3db0SGreg Roach
1186a091ac74SGreg Roach            return Factory::source()->mapper($tree)($row);
1187d5ad3db0SGreg Roach        };
1188d5ad3db0SGreg Roach    }
1189d5ad3db0SGreg Roach
1190d5ad3db0SGreg Roach    /**
1191d5ad3db0SGreg Roach     * Convert a row from any tree in the other table into a submitter object.
1192d5ad3db0SGreg Roach     *
1193d5ad3db0SGreg Roach     * @return Closure
1194d5ad3db0SGreg Roach     */
1195d5ad3db0SGreg Roach    private function submitterRowMapper(): Closure
1196d5ad3db0SGreg Roach    {
11973959eeb6SGreg Roach        return function (stdClass $row): Submitter {
1198d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->o_file);
1199d5ad3db0SGreg Roach
1200a091ac74SGreg Roach            return Factory::submitter()->mapper($tree)($row);
1201d5ad3db0SGreg Roach        };
1202d5ad3db0SGreg Roach    }
120332cd2800SGreg Roach}
1204