xref: /webtrees/app/Services/SearchService.php (revision b4adec9ef7d036bc7dee168f827fd61e8c8d05d4)
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;
47*b4adec9eSGreg Roachuse function array_filter;
48*b4adec9eSGreg Roachuse function array_map;
4956946b7aSGreg Roachuse function array_unique;
5056946b7aSGreg Roachuse function explode;
51*b4adec9eSGreg Roachuse function implode;
52a7a24840SGreg Roachuse function mb_stripos;
53*b4adec9eSGreg Roachuse function preg_match;
54*b4adec9eSGreg Roachuse function preg_quote;
55*b4adec9eSGreg Roachuse function preg_replace;
56*b4adec9eSGreg Roach
57*b4adec9eSGreg Roachuse const PHP_INT_MAX;
5832cd2800SGreg Roach
5932cd2800SGreg Roach/**
6032cd2800SGreg Roach * Search trees for genealogy records.
6132cd2800SGreg Roach */
6232cd2800SGreg Roachclass SearchService
6332cd2800SGreg Roach{
64d5ad3db0SGreg Roach    /** @var TreeService */
65d5ad3db0SGreg Roach    private $tree_service;
66d5ad3db0SGreg Roach
67d5ad3db0SGreg Roach    /**
68d5ad3db0SGreg Roach     * SearchService constructor.
69d5ad3db0SGreg Roach     *
70d5ad3db0SGreg Roach     * @param TreeService $tree_service
71d5ad3db0SGreg Roach     */
723959eeb6SGreg Roach    public function __construct(
733959eeb6SGreg Roach        TreeService $tree_service
743959eeb6SGreg Roach    ) {
75d5ad3db0SGreg Roach        $this->tree_service = $tree_service;
76d5ad3db0SGreg Roach    }
77d5ad3db0SGreg Roach
78a7a24840SGreg Roach    /**
79a7a24840SGreg Roach     * @param Tree[]   $trees
80a7a24840SGreg Roach     * @param string[] $search
81a7a24840SGreg Roach     *
82b5c8fd7eSGreg Roach     * @return Collection<Family>
83a7a24840SGreg Roach     */
84a7a24840SGreg Roach    public function searchFamilies(array $trees, array $search): Collection
85a7a24840SGreg Roach    {
86a7a24840SGreg Roach        $query = DB::table('families');
87a7a24840SGreg Roach
88a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
89a7a24840SGreg Roach        $this->whereSearch($query, 'f_gedcom', $search);
90a7a24840SGreg Roach
91a7a24840SGreg Roach        return $query
92a7a24840SGreg Roach            ->get()
9352a8ef61SGreg Roach            ->each($this->rowLimiter())
94d5ad3db0SGreg Roach            ->map($this->familyRowMapper())
95a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
967f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
97a7a24840SGreg Roach    }
98a7a24840SGreg Roach
9932cd2800SGreg Roach    /**
10032cd2800SGreg Roach     * Search for families by name.
10132cd2800SGreg Roach     *
102a7a24840SGreg Roach     * @param Tree[]   $trees
103a7a24840SGreg Roach     * @param string[] $search
10432cd2800SGreg Roach     * @param int      $offset
10532cd2800SGreg Roach     * @param int      $limit
10632cd2800SGreg Roach     *
107b5c8fd7eSGreg Roach     * @return Collection<Family>
10832cd2800SGreg Roach     */
109a7a24840SGreg Roach    public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
11032cd2800SGreg Roach    {
11132cd2800SGreg Roach        $query = DB::table('families')
112ac499332SGreg Roach            ->leftJoin('name AS husb_name', static function (JoinClause $join): void {
11332cd2800SGreg Roach                $join
11432cd2800SGreg Roach                    ->on('husb_name.n_file', '=', 'families.f_file')
115ac499332SGreg Roach                    ->on('husb_name.n_id', '=', 'families.f_husb')
116ac499332SGreg Roach                    ->where('husb_name.n_type', '<>', '_MARNM');
11732cd2800SGreg Roach            })
118ac499332SGreg Roach            ->leftJoin('name AS wife_name', static function (JoinClause $join): void {
11932cd2800SGreg Roach                $join
12032cd2800SGreg Roach                    ->on('wife_name.n_file', '=', 'families.f_file')
121ac499332SGreg Roach                    ->on('wife_name.n_id', '=', 'families.f_wife')
122ac499332SGreg Roach                    ->where('wife_name.n_type', '<>', '_MARNM');
123ac499332SGreg Roach            });
124a7a24840SGreg Roach
125a7a24840SGreg Roach        $prefix = DB::connection()->getTablePrefix();
126a69f5655SGreg Roach        $field  = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')");
127a7a24840SGreg Roach
128a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
129a7a24840SGreg Roach        $this->whereSearch($query, $field, $search);
130a7a24840SGreg Roach
131a7a24840SGreg Roach        $query
13232cd2800SGreg Roach            ->orderBy('husb_name.n_sort')
13332cd2800SGreg Roach            ->orderBy('wife_name.n_sort')
134c0804649SGreg Roach            ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort'])
13532cd2800SGreg Roach            ->distinct();
13632cd2800SGreg Roach
137d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
138a7a24840SGreg Roach    }
139a7a24840SGreg Roach
140a7a24840SGreg Roach    /**
141dbe53437SGreg Roach     * @param Place $place
142dbe53437SGreg Roach     *
143dbe53437SGreg Roach     * @return Collection<Family>
144dbe53437SGreg Roach     */
145dbe53437SGreg Roach    public function searchFamiliesInPlace(Place $place): Collection
146dbe53437SGreg Roach    {
147dbe53437SGreg Roach        return DB::table('families')
148dbe53437SGreg Roach            ->join('placelinks', static function (JoinClause $query) {
149dbe53437SGreg Roach                $query
150dbe53437SGreg Roach                    ->on('families.f_file', '=', 'placelinks.pl_file')
151dbe53437SGreg Roach                    ->on('families.f_id', '=', 'placelinks.pl_gid');
152dbe53437SGreg Roach            })
153dbe53437SGreg Roach            ->where('f_file', '=', $place->tree()->id())
154dbe53437SGreg Roach            ->where('pl_p_id', '=', $place->id())
155dbe53437SGreg Roach            ->select(['families.*'])
156dbe53437SGreg Roach            ->get()
157dbe53437SGreg Roach            ->each($this->rowLimiter())
158dbe53437SGreg Roach            ->map($this->familyRowMapper())
159dbe53437SGreg Roach            ->filter(GedcomRecord::accessFilter());
160dbe53437SGreg Roach    }
161dbe53437SGreg Roach
162dbe53437SGreg Roach    /**
163a7a24840SGreg Roach     * @param Tree[]   $trees
164a7a24840SGreg Roach     * @param string[] $search
165a7a24840SGreg Roach     *
166b5c8fd7eSGreg Roach     * @return Collection<Individual>
167a7a24840SGreg Roach     */
168a7a24840SGreg Roach    public function searchIndividuals(array $trees, array $search): Collection
169a7a24840SGreg Roach    {
170a7a24840SGreg Roach        $query = DB::table('individuals');
171a7a24840SGreg Roach
172a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
173a7a24840SGreg Roach        $this->whereSearch($query, 'i_gedcom', $search);
174a7a24840SGreg Roach
175a7a24840SGreg Roach        return $query
176a7a24840SGreg Roach            ->get()
17752a8ef61SGreg Roach            ->each($this->rowLimiter())
178d5ad3db0SGreg Roach            ->map($this->individualRowMapper())
179a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
1807f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
18132cd2800SGreg Roach    }
18232cd2800SGreg Roach
18332cd2800SGreg Roach    /**
18432cd2800SGreg Roach     * Search for individuals by name.
18532cd2800SGreg Roach     *
186a7a24840SGreg Roach     * @param Tree[]   $trees
187a7a24840SGreg Roach     * @param string[] $search
18832cd2800SGreg Roach     * @param int      $offset
18932cd2800SGreg Roach     * @param int      $limit
19032cd2800SGreg Roach     *
191b5c8fd7eSGreg Roach     * @return Collection<Individual>
19232cd2800SGreg Roach     */
193a7a24840SGreg Roach    public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
19432cd2800SGreg Roach    {
19532cd2800SGreg Roach        $query = DB::table('individuals')
1960b5fd0a6SGreg Roach            ->join('name', static function (JoinClause $join): void {
19732cd2800SGreg Roach                $join
19832cd2800SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
199a7a24840SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
20032cd2800SGreg Roach            })
201e84cf2deSGreg Roach            ->orderBy('n_sort')
202d78d61f7SGreg Roach            ->distinct()
203685de081SGreg Roach            ->select(['individuals.*', 'n_sort']);
20432cd2800SGreg Roach
205a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
206a7a24840SGreg Roach        $this->whereSearch($query, 'n_full', $search);
207a7a24840SGreg Roach
208d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
20932cd2800SGreg Roach    }
21032cd2800SGreg Roach
21132cd2800SGreg Roach    /**
212dbe53437SGreg Roach     * @param Place $place
213dbe53437SGreg Roach     *
214dbe53437SGreg Roach     * @return Collection<Individual>
215dbe53437SGreg Roach     */
216dbe53437SGreg Roach    public function searchIndividualsInPlace(Place $place): Collection
217dbe53437SGreg Roach    {
218dbe53437SGreg Roach        return DB::table('individuals')
219dbe53437SGreg Roach            ->join('placelinks', static function (JoinClause $join) {
220dbe53437SGreg Roach                $join
221dbe53437SGreg Roach                    ->on('i_file', '=', 'pl_file')
222dbe53437SGreg Roach                    ->on('i_id', '=', 'pl_gid');
223dbe53437SGreg Roach            })
224dbe53437SGreg Roach            ->where('i_file', '=', $place->tree()->id())
225dbe53437SGreg Roach            ->where('pl_p_id', '=', $place->id())
226dbe53437SGreg Roach            ->select(['individuals.*'])
227dbe53437SGreg Roach            ->get()
228dbe53437SGreg Roach            ->each($this->rowLimiter())
229dbe53437SGreg Roach            ->map($this->individualRowMapper())
230dbe53437SGreg Roach            ->filter(GedcomRecord::accessFilter());
231dbe53437SGreg Roach    }
232dbe53437SGreg Roach
233dbe53437SGreg Roach    /**
23432cd2800SGreg Roach     * Search for media objects.
23532cd2800SGreg Roach     *
236a7a24840SGreg Roach     * @param Tree[]   $trees
237a7a24840SGreg Roach     * @param string[] $search
23832cd2800SGreg Roach     * @param int      $offset
23932cd2800SGreg Roach     * @param int      $limit
24032cd2800SGreg Roach     *
241b5c8fd7eSGreg Roach     * @return Collection<Media>
24232cd2800SGreg Roach     */
243a7a24840SGreg Roach    public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
24432cd2800SGreg Roach    {
245a7a24840SGreg Roach        $query = DB::table('media');
24632cd2800SGreg Roach
247a7a24840SGreg Roach        $this->whereTrees($query, 'media.m_file', $trees);
248a7a24840SGreg Roach        $this->whereSearch($query, 'm_gedcom', $search);
249a7a24840SGreg Roach
250d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
25132cd2800SGreg Roach    }
25232cd2800SGreg Roach
25332cd2800SGreg Roach    /**
25432cd2800SGreg Roach     * Search for notes.
25532cd2800SGreg Roach     *
256a7a24840SGreg Roach     * @param Tree[]   $trees
257a7a24840SGreg Roach     * @param string[] $search
25832cd2800SGreg Roach     * @param int      $offset
25932cd2800SGreg Roach     * @param int      $limit
26032cd2800SGreg Roach     *
261b5c8fd7eSGreg Roach     * @return Collection<Note>
26232cd2800SGreg Roach     */
263a7a24840SGreg Roach    public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
26432cd2800SGreg Roach    {
26532cd2800SGreg Roach        $query = DB::table('other')
266a7a24840SGreg Roach            ->where('o_type', '=', 'NOTE');
26732cd2800SGreg Roach
268a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
269a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
270a7a24840SGreg Roach
271d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
27232cd2800SGreg Roach    }
27332cd2800SGreg Roach
27432cd2800SGreg Roach    /**
27532cd2800SGreg Roach     * Search for repositories.
27632cd2800SGreg Roach     *
277a7a24840SGreg Roach     * @param Tree[]   $trees
278a7a24840SGreg Roach     * @param string[] $search
27932cd2800SGreg Roach     * @param int      $offset
28032cd2800SGreg Roach     * @param int      $limit
28132cd2800SGreg Roach     *
282b5c8fd7eSGreg Roach     * @return Collection<Repository>
28332cd2800SGreg Roach     */
284a7a24840SGreg Roach    public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
28532cd2800SGreg Roach    {
28632cd2800SGreg Roach        $query = DB::table('other')
287a7a24840SGreg Roach            ->where('o_type', '=', 'REPO');
28832cd2800SGreg Roach
289a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
290a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
291a7a24840SGreg Roach
292d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
29332cd2800SGreg Roach    }
29432cd2800SGreg Roach
29532cd2800SGreg Roach    /**
296a7a24840SGreg Roach     * Search for sources.
29732cd2800SGreg Roach     *
298a7a24840SGreg Roach     * @param Tree[]   $trees
299a7a24840SGreg Roach     * @param string[] $search
30032cd2800SGreg Roach     * @param int      $offset
30132cd2800SGreg Roach     * @param int      $limit
30232cd2800SGreg Roach     *
303b5c8fd7eSGreg Roach     * @return Collection<Source>
30432cd2800SGreg Roach     */
305a7a24840SGreg Roach    public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
306a7a24840SGreg Roach    {
307a7a24840SGreg Roach        $query = DB::table('sources');
308a7a24840SGreg Roach
309a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
310a7a24840SGreg Roach        $this->whereSearch($query, 's_gedcom', $search);
311a7a24840SGreg Roach
312d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
313a7a24840SGreg Roach    }
314a7a24840SGreg Roach
315a7a24840SGreg Roach    /**
316a7a24840SGreg Roach     * Search for sources by name.
317a7a24840SGreg Roach     *
318a7a24840SGreg Roach     * @param Tree[]   $trees
319a7a24840SGreg Roach     * @param string[] $search
320a7a24840SGreg Roach     * @param int      $offset
321a7a24840SGreg Roach     * @param int      $limit
322a7a24840SGreg Roach     *
323b5c8fd7eSGreg Roach     * @return Collection<Source>
324a7a24840SGreg Roach     */
325a7a24840SGreg Roach    public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
32632cd2800SGreg Roach    {
32732cd2800SGreg Roach        $query = DB::table('sources')
328c0804649SGreg Roach            ->orderBy('s_name');
32932cd2800SGreg Roach
330a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
331a7a24840SGreg Roach        $this->whereSearch($query, 's_name', $search);
332a7a24840SGreg Roach
333d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
33432cd2800SGreg Roach    }
33532cd2800SGreg Roach
33632cd2800SGreg Roach    /**
33732cd2800SGreg Roach     * Search for submitters.
33832cd2800SGreg Roach     *
339a7a24840SGreg Roach     * @param Tree[]   $trees
340a7a24840SGreg Roach     * @param string[] $search
34132cd2800SGreg Roach     * @param int      $offset
34232cd2800SGreg Roach     * @param int      $limit
34332cd2800SGreg Roach     *
344b5c8fd7eSGreg Roach     * @return Collection<Submitter>
34532cd2800SGreg Roach     */
346a7a24840SGreg Roach    public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
34732cd2800SGreg Roach    {
34832cd2800SGreg Roach        $query = DB::table('other')
349a7a24840SGreg Roach            ->where('o_type', '=', 'SUBM');
35032cd2800SGreg Roach
351a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
352a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
353a7a24840SGreg Roach
354d5ad3db0SGreg Roach        return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
35532cd2800SGreg Roach    }
35632cd2800SGreg Roach
35732cd2800SGreg Roach    /**
358b68caec6SGreg Roach     * Search for places.
359b68caec6SGreg Roach     *
360b68caec6SGreg Roach     * @param Tree   $tree
361b68caec6SGreg Roach     * @param string $search
362b68caec6SGreg Roach     * @param int    $offset
363b68caec6SGreg Roach     * @param int    $limit
364b68caec6SGreg Roach     *
365b5c8fd7eSGreg Roach     * @return Collection<Place>
366b68caec6SGreg Roach     */
367b68caec6SGreg Roach    public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
368b68caec6SGreg Roach    {
369b68caec6SGreg Roach        $query = DB::table('places AS p0')
370b68caec6SGreg Roach            ->where('p0.p_file', '=', $tree->id())
371b68caec6SGreg Roach            ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id')
372b68caec6SGreg Roach            ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id')
373b68caec6SGreg Roach            ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id')
374b68caec6SGreg Roach            ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id')
375b68caec6SGreg Roach            ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id')
376b68caec6SGreg Roach            ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id')
377b68caec6SGreg Roach            ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id')
378b68caec6SGreg Roach            ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id')
379b68caec6SGreg Roach            ->orderBy('p0.p_place')
380b68caec6SGreg Roach            ->orderBy('p1.p_place')
381b68caec6SGreg Roach            ->orderBy('p2.p_place')
382b68caec6SGreg Roach            ->orderBy('p3.p_place')
383b68caec6SGreg Roach            ->orderBy('p4.p_place')
384b68caec6SGreg Roach            ->orderBy('p5.p_place')
385b68caec6SGreg Roach            ->orderBy('p6.p_place')
386b68caec6SGreg Roach            ->orderBy('p7.p_place')
387b68caec6SGreg Roach            ->orderBy('p8.p_place')
388b68caec6SGreg Roach            ->select([
389b68caec6SGreg Roach                'p0.p_place AS place0',
390b68caec6SGreg Roach                'p1.p_place AS place1',
391b68caec6SGreg Roach                'p2.p_place AS place2',
392b68caec6SGreg Roach                'p3.p_place AS place3',
393b68caec6SGreg Roach                'p4.p_place AS place4',
394b68caec6SGreg Roach                'p5.p_place AS place5',
395b68caec6SGreg Roach                'p6.p_place AS place6',
396b68caec6SGreg Roach                'p7.p_place AS place7',
397b68caec6SGreg Roach                'p8.p_place AS place8',
398b68caec6SGreg Roach            ]);
399b68caec6SGreg Roach
400b68caec6SGreg Roach        // Filter each level of the hierarchy.
401b68caec6SGreg Roach        foreach (explode(',', $search, 9) as $level => $string) {
402b5961194SGreg Roach            $query->where('p' . $level . '.p_place', 'LIKE', '%' . addcslashes($string, '\\%_') . '%');
403b68caec6SGreg Roach        }
404b68caec6SGreg Roach
4056c2179e2SGreg Roach        $row_mapper = static function (stdClass $row) use ($tree): Place {
406b68caec6SGreg Roach            $place = implode(', ', array_filter((array) $row));
407b68caec6SGreg Roach
408b68caec6SGreg Roach            return new Place($place, $tree);
409b68caec6SGreg Roach        };
410b68caec6SGreg Roach
4116c2179e2SGreg Roach        $filter = static function (): bool {
412a7a24840SGreg Roach            return true;
413a7a24840SGreg Roach        };
414b68caec6SGreg Roach
415a7a24840SGreg Roach        return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit);
416a7a24840SGreg Roach    }
417b68caec6SGreg Roach
418b68caec6SGreg Roach    /**
419a5dbc5b2SGreg Roach     * @param Tree[]   $trees
420dfb2cda2SGreg Roach     * @param string[] $fields
421dfb2cda2SGreg Roach     * @param string[] $modifiers
422dfb2cda2SGreg Roach     *
423b5c8fd7eSGreg Roach     * @return Collection<Individual>
424dfb2cda2SGreg Roach     */
425dfb2cda2SGreg Roach    public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection
426dfb2cda2SGreg Roach    {
427dfb2cda2SGreg Roach        $fields = array_filter($fields);
428dfb2cda2SGreg Roach
429dfb2cda2SGreg Roach        $query = DB::table('individuals')
430dfb2cda2SGreg Roach            ->select(['individuals.*'])
431dfb2cda2SGreg Roach            ->distinct();
432dfb2cda2SGreg Roach
433dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
434dfb2cda2SGreg Roach
435dfb2cda2SGreg Roach        // Join the following tables
436dfb2cda2SGreg Roach        $father_name   = false;
437dfb2cda2SGreg Roach        $mother_name   = false;
438dfb2cda2SGreg Roach        $spouse_family = false;
439dfb2cda2SGreg Roach        $indi_name     = false;
44056946b7aSGreg Roach        $indi_dates    = [];
44156946b7aSGreg Roach        $fam_dates     = [];
442dfb2cda2SGreg Roach        $indi_plac     = false;
443dfb2cda2SGreg Roach        $fam_plac      = false;
444dfb2cda2SGreg Roach
445dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
446dfb2cda2SGreg Roach            if ($field_value !== '') {
44756946b7aSGreg Roach                // Fields can have up to 4 parts, but we only need the first 3 to identify
44856946b7aSGreg Roach                // which tables to select
44956946b7aSGreg Roach                $field_parts = explode(':', $field_name . '::');
45056946b7aSGreg Roach
45156946b7aSGreg Roach                if ($field_parts[0] === 'FAMC') {
45256946b7aSGreg Roach                    // Parent name - FAMC:[HUSB|WIFE]:NAME:[GIVN|SURN]
45356946b7aSGreg Roach                    if ($field_parts[1] === 'HUSB') {
454dfb2cda2SGreg Roach                        $father_name = true;
45531dd1910SGreg Roach                    } else {
456dfb2cda2SGreg Roach                        $mother_name = true;
457dfb2cda2SGreg Roach                    }
45856946b7aSGreg Roach                } elseif ($field_parts[0] === 'NAME') {
45956946b7aSGreg Roach                    // Individual name - NAME:[GIVN|SURN]
46056946b7aSGreg Roach                    $indi_name = true;
46156946b7aSGreg Roach                } elseif ($field_parts[0] === 'FAMS') {
46256946b7aSGreg Roach                    // Family facts - FAMS:NOTE or FAMS:[FACT]:[DATE|PLAC]
463dfb2cda2SGreg Roach                    $spouse_family = true;
46456946b7aSGreg Roach                    if ($field_parts[2] === 'DATE') {
46556946b7aSGreg Roach                        $fam_dates[] = $field_parts[1];
46656946b7aSGreg Roach                    } elseif ($field_parts[2] === 'PLAC') {
46756946b7aSGreg Roach                        $fam_plac = true;
46856946b7aSGreg Roach                    }
469dfb2cda2SGreg Roach                } else {
47056946b7aSGreg Roach                    // Individual facts - [FACT] or [FACT]:[DATE|PLAC]
47156946b7aSGreg Roach                    if ($field_parts[1] === 'DATE') {
47256946b7aSGreg Roach                        $indi_dates[] = $field_parts[0];
47356946b7aSGreg Roach                    } elseif ($field_parts[1] === 'PLAC') {
474dfb2cda2SGreg Roach                        $indi_plac = true;
475dfb2cda2SGreg Roach                    }
476dfb2cda2SGreg Roach                }
477dfb2cda2SGreg Roach            }
478dfb2cda2SGreg Roach        }
479dfb2cda2SGreg Roach
480dfb2cda2SGreg Roach        if ($father_name || $mother_name) {
4810b5fd0a6SGreg Roach            $query->join('link AS l1', static function (JoinClause $join): void {
482dfb2cda2SGreg Roach                $join
483dfb2cda2SGreg Roach                    ->on('l1.l_file', '=', 'individuals.i_file')
484dfb2cda2SGreg Roach                    ->on('l1.l_from', '=', 'individuals.i_id')
485dfb2cda2SGreg Roach                    ->where('l1.l_type', '=', 'FAMC');
486dfb2cda2SGreg Roach            });
487dfb2cda2SGreg Roach
488dfb2cda2SGreg Roach            if ($father_name) {
4890b5fd0a6SGreg Roach                $query->join('link AS l2', static function (JoinClause $join): void {
490dfb2cda2SGreg Roach                    $join
491dfb2cda2SGreg Roach                        ->on('l2.l_file', '=', 'l1.l_file')
492dfb2cda2SGreg Roach                        ->on('l2.l_from', '=', 'l1.l_to')
493dfb2cda2SGreg Roach                        ->where('l2.l_type', '=', 'HUSB');
494dfb2cda2SGreg Roach                });
4950b5fd0a6SGreg Roach                $query->join('name AS father_name', static function (JoinClause $join): void {
496dfb2cda2SGreg Roach                    $join
497dfb2cda2SGreg Roach                        ->on('father_name.n_file', '=', 'l2.l_file')
498dfb2cda2SGreg Roach                        ->on('father_name.n_id', '=', 'l2.l_to');
499dfb2cda2SGreg Roach                });
500dfb2cda2SGreg Roach            }
501dfb2cda2SGreg Roach
502dfb2cda2SGreg Roach            if ($mother_name) {
5030b5fd0a6SGreg Roach                $query->join('link AS l3', static function (JoinClause $join): void {
504dfb2cda2SGreg Roach                    $join
505dfb2cda2SGreg Roach                        ->on('l3.l_file', '=', 'l1.l_file')
506dfb2cda2SGreg Roach                        ->on('l3.l_from', '=', 'l1.l_to')
507dfb2cda2SGreg Roach                        ->where('l3.l_type', '=', 'WIFE');
508dfb2cda2SGreg Roach                });
5090b5fd0a6SGreg Roach                $query->join('name AS mother_name', static function (JoinClause $join): void {
510dfb2cda2SGreg Roach                    $join
511dfb2cda2SGreg Roach                        ->on('mother_name.n_file', '=', 'l3.l_file')
512dfb2cda2SGreg Roach                        ->on('mother_name.n_id', '=', 'l3.l_to');
513dfb2cda2SGreg Roach                });
514dfb2cda2SGreg Roach            }
515dfb2cda2SGreg Roach        }
516dfb2cda2SGreg Roach
517dfb2cda2SGreg Roach        if ($spouse_family) {
5180b5fd0a6SGreg Roach            $query->join('link AS l4', static function (JoinClause $join): void {
519dfb2cda2SGreg Roach                $join
520dfb2cda2SGreg Roach                    ->on('l4.l_file', '=', 'individuals.i_file')
521dfb2cda2SGreg Roach                    ->on('l4.l_from', '=', 'individuals.i_id')
522dfb2cda2SGreg Roach                    ->where('l4.l_type', '=', 'FAMS');
523dfb2cda2SGreg Roach            });
5240b5fd0a6SGreg Roach            $query->join('families AS spouse_families', static function (JoinClause $join): void {
525dfb2cda2SGreg Roach                $join
526dfb2cda2SGreg Roach                    ->on('spouse_families.f_file', '=', 'l4.l_file')
527dfb2cda2SGreg Roach                    ->on('spouse_families.f_id', '=', 'l4.l_to');
528dfb2cda2SGreg Roach            });
529dfb2cda2SGreg Roach        }
530dfb2cda2SGreg Roach
531dfb2cda2SGreg Roach        if ($indi_name) {
5320b5fd0a6SGreg Roach            $query->join('name AS individual_name', static function (JoinClause $join): void {
533dfb2cda2SGreg Roach                $join
534dfb2cda2SGreg Roach                    ->on('individual_name.n_file', '=', 'individuals.i_file')
535dfb2cda2SGreg Roach                    ->on('individual_name.n_id', '=', 'individuals.i_id');
536dfb2cda2SGreg Roach            });
537dfb2cda2SGreg Roach        }
538dfb2cda2SGreg Roach
53956946b7aSGreg Roach        foreach (array_unique($indi_dates) as $indi_date) {
54056946b7aSGreg Roach            $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void {
541dfb2cda2SGreg Roach                $join
54256946b7aSGreg Roach                    ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file')
54356946b7aSGreg Roach                    ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id');
544dfb2cda2SGreg Roach            });
545dfb2cda2SGreg Roach        }
546dfb2cda2SGreg Roach
54756946b7aSGreg Roach        foreach (array_unique($fam_dates) as $fam_date) {
54856946b7aSGreg Roach            $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void {
549dfb2cda2SGreg Roach                $join
550b06d667bSGreg Roach                    ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.f_file')
551b06d667bSGreg Roach                    ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.f_id');
552dfb2cda2SGreg Roach            });
553dfb2cda2SGreg Roach        }
554dfb2cda2SGreg Roach
555dfb2cda2SGreg Roach        if ($indi_plac) {
5560b5fd0a6SGreg Roach            $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void {
557dfb2cda2SGreg Roach                $join
558dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_file', '=', 'individuals.i_file')
559dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id');
560dfb2cda2SGreg Roach            });
5610b5fd0a6SGreg Roach            $query->join('places AS individual_places', static function (JoinClause $join): void {
562dfb2cda2SGreg Roach                $join
563dfb2cda2SGreg Roach                    ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file')
564dfb2cda2SGreg Roach                    ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id');
565dfb2cda2SGreg Roach            });
566dfb2cda2SGreg Roach        }
567dfb2cda2SGreg Roach
568dfb2cda2SGreg Roach        if ($fam_plac) {
5690b5fd0a6SGreg Roach            $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void {
570dfb2cda2SGreg Roach                $join
571dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file')
572dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id');
573dfb2cda2SGreg Roach            });
5740b5fd0a6SGreg Roach            $query->join('places AS family_places', static function (JoinClause $join): void {
575dfb2cda2SGreg Roach                $join
576dfb2cda2SGreg Roach                    ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file')
577dfb2cda2SGreg Roach                    ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id');
578dfb2cda2SGreg Roach            });
579dfb2cda2SGreg Roach        }
580dfb2cda2SGreg Roach
581dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
58256946b7aSGreg Roach            $parts = explode(':', $field_name . ':::');
583dfb2cda2SGreg Roach            if ($parts[0] === 'NAME') {
584dfb2cda2SGreg Roach                // NAME:*
585dfb2cda2SGreg Roach                switch ($parts[1]) {
586dfb2cda2SGreg Roach                    case 'GIVN':
587dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
588dfb2cda2SGreg Roach                            case 'EXACT':
589dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', '=', $field_value);
590dfb2cda2SGreg Roach                                break;
591dfb2cda2SGreg Roach                            case 'BEGINS':
592dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', $field_value . '%');
593dfb2cda2SGreg Roach                                break;
594dfb2cda2SGreg Roach                            case 'CONTAINS':
595dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
596dfb2cda2SGreg Roach                                break;
597dfb2cda2SGreg Roach                            case 'SDX_STD':
598dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
599dfb2cda2SGreg Roach                                if ($sdx !== '') {
600dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx);
601dfb2cda2SGreg Roach                                } else {
602dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
603dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
604dfb2cda2SGreg Roach                                }
605dfb2cda2SGreg Roach                                break;
606dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
607dfb2cda2SGreg Roach                            case 'SDX_DM':
608dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
609dfb2cda2SGreg Roach                                if ($sdx !== '') {
610dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx);
611dfb2cda2SGreg Roach                                } else {
612dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
613dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
614dfb2cda2SGreg Roach                                }
615dfb2cda2SGreg Roach                                break;
616dfb2cda2SGreg Roach                        }
617dfb2cda2SGreg Roach                        break;
618dfb2cda2SGreg Roach                    case 'SURN':
619dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
620dfb2cda2SGreg Roach                            case 'EXACT':
621fc6d23eeSGreg Roach                                $query->where(function (Builder $query) use ($field_value): void {
622fc6d23eeSGreg Roach                                    $query
623fc6d23eeSGreg Roach                                        ->where('individual_name.n_surn', '=', $field_value)
624fc6d23eeSGreg Roach                                        ->orWhere('individual_name.n_surname', '=', $field_value);
625fc6d23eeSGreg Roach                                });
626dfb2cda2SGreg Roach                                break;
627dfb2cda2SGreg Roach                            case 'BEGINS':
628fc6d23eeSGreg Roach                                $query->where(function (Builder $query) use ($field_value): void {
629fc6d23eeSGreg Roach                                    $query
630fc6d23eeSGreg Roach                                        ->where('individual_name.n_surn', 'LIKE', $field_value . '%')
6319875d474SGreg Roach                                        ->orWhere('individual_name.n_surname', 'LIKE', $field_value . '%');
632fc6d23eeSGreg Roach                                });
633dfb2cda2SGreg Roach                                break;
634dfb2cda2SGreg Roach                            case 'CONTAINS':
635fc6d23eeSGreg Roach                                $query->where(function (Builder $query) use ($field_value): void {
636fc6d23eeSGreg Roach                                    $query
637fc6d23eeSGreg Roach                                        ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%')
6389875d474SGreg Roach                                        ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%');
639fc6d23eeSGreg Roach                                });
640dfb2cda2SGreg Roach                                break;
641dfb2cda2SGreg Roach                            case 'SDX_STD':
642dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
643dfb2cda2SGreg Roach                                if ($sdx !== '') {
644dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx);
645dfb2cda2SGreg Roach                                } else {
646dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
647fc6d23eeSGreg Roach                                    $query->where(function (Builder $query) use ($field_value): void {
648fc6d23eeSGreg Roach                                        $query
649fc6d23eeSGreg Roach                                            ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%')
6509875d474SGreg Roach                                            ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%');
651fc6d23eeSGreg Roach                                    });
652dfb2cda2SGreg Roach                                }
653dfb2cda2SGreg Roach                                break;
654dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
655dfb2cda2SGreg Roach                            case 'SDX_DM':
656dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
657dfb2cda2SGreg Roach                                if ($sdx !== '') {
658dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx);
659dfb2cda2SGreg Roach                                } else {
660dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
6619875d474SGreg Roach                                    $query->where(function (Builder $query) use ($field_value): void {
6629875d474SGreg Roach                                        $query
6639875d474SGreg Roach                                            ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%')
6649875d474SGreg Roach                                            ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%');
6659875d474SGreg Roach                                    });
666dfb2cda2SGreg Roach                                }
667dfb2cda2SGreg Roach                                break;
668dfb2cda2SGreg Roach                        }
669*b4adec9eSGreg Roach                        unset($fields[$field_name]);
670dfb2cda2SGreg Roach                        break;
671dfb2cda2SGreg Roach                    case 'NICK':
672dfb2cda2SGreg Roach                    case '_MARNM':
673dfb2cda2SGreg Roach                    case '_HEB':
674dfb2cda2SGreg Roach                    case '_AKA':
675*b4adec9eSGreg Roach                        $like = "%\n1 " . $parts[0] . "%\n2 " . $parts[1] . ' %' . preg_quote($field_value, '/') . '%';
676*b4adec9eSGreg Roach                        $query->where('individuals.i_gedcom', 'LIKE', $like);
677dfb2cda2SGreg Roach                        break;
678dfb2cda2SGreg Roach                }
679dfb2cda2SGreg Roach            } elseif ($parts[1] === 'DATE') {
680dfb2cda2SGreg Roach                // *:DATE
681dfb2cda2SGreg Roach                $date = new Date($field_value);
682dfb2cda2SGreg Roach                if ($date->isOK()) {
683dfb2cda2SGreg Roach                    $delta = 365 * ($modifiers[$field_name] ?? 0);
684dfb2cda2SGreg Roach                    $query
68556946b7aSGreg Roach                        ->where('date_' . $parts[0] . '.d_fact', '=', $parts[0])
68656946b7aSGreg Roach                        ->where('date_' . $parts[0] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
68756946b7aSGreg Roach                        ->where('date_' . $parts[0] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta);
688dfb2cda2SGreg Roach                }
689dfb2cda2SGreg Roach                unset($fields[$field_name]);
690dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') {
691dfb2cda2SGreg Roach                // FAMS:*:DATE
692dfb2cda2SGreg Roach                $date = new Date($field_value);
693dfb2cda2SGreg Roach                if ($date->isOK()) {
694dfb2cda2SGreg Roach                    $delta = 365 * $modifiers[$field_name];
695dfb2cda2SGreg Roach                    $query
69656946b7aSGreg Roach                        ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1])
69756946b7aSGreg Roach                        ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
69856946b7aSGreg Roach                        ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta);
699dfb2cda2SGreg Roach                }
700dfb2cda2SGreg Roach                unset($fields[$field_name]);
701dfb2cda2SGreg Roach            } elseif ($parts[1] === 'PLAC') {
702dfb2cda2SGreg Roach                // *:PLAC
703dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
704dfb2cda2SGreg Roach                $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%');
705dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
706dfb2cda2SGreg Roach                // FAMS:*:PLAC
707dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
708dfb2cda2SGreg Roach                $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%');
709dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') {
710dfb2cda2SGreg Roach                $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name';
711dfb2cda2SGreg Roach                // NAME:*
712dfb2cda2SGreg Roach                switch ($parts[3]) {
713dfb2cda2SGreg Roach                    case 'GIVN':
714dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
715dfb2cda2SGreg Roach                            case 'EXACT':
716dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', '=', $field_value);
717dfb2cda2SGreg Roach                                break;
718dfb2cda2SGreg Roach                            case 'BEGINS':
719dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', $field_value . '%');
720dfb2cda2SGreg Roach                                break;
721dfb2cda2SGreg Roach                            case 'CONTAINS':
722dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
723dfb2cda2SGreg Roach                                break;
724dfb2cda2SGreg Roach                            case 'SDX_STD':
725dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
726dfb2cda2SGreg Roach                                if ($sdx !== '') {
727dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx);
728dfb2cda2SGreg Roach                                } else {
729dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
730dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
731dfb2cda2SGreg Roach                                }
732dfb2cda2SGreg Roach                                break;
733dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
734dfb2cda2SGreg Roach                            case 'SDX_DM':
735dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
736dfb2cda2SGreg Roach                                if ($sdx !== '') {
737dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx);
738dfb2cda2SGreg Roach                                } else {
739dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
740dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
741dfb2cda2SGreg Roach                                }
742dfb2cda2SGreg Roach                                break;
743dfb2cda2SGreg Roach                        }
744dfb2cda2SGreg Roach                        break;
745dfb2cda2SGreg Roach                    case 'SURN':
746dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
747dfb2cda2SGreg Roach                            case 'EXACT':
748dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', '=', $field_value);
749dfb2cda2SGreg Roach                                break;
750dfb2cda2SGreg Roach                            case 'BEGINS':
751dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', $field_value . '%');
752dfb2cda2SGreg Roach                                break;
753dfb2cda2SGreg Roach                            case 'CONTAINS':
754dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
755dfb2cda2SGreg Roach                                break;
756dfb2cda2SGreg Roach                            case 'SDX_STD':
757dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
758dfb2cda2SGreg Roach                                if ($sdx !== '') {
759dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx);
760dfb2cda2SGreg Roach                                } else {
761dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
762dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
763dfb2cda2SGreg Roach                                }
764dfb2cda2SGreg Roach                                break;
765dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
766dfb2cda2SGreg Roach                            case 'SDX_DM':
767dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
768dfb2cda2SGreg Roach                                if ($sdx !== '') {
769dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx);
770dfb2cda2SGreg Roach                                } else {
771dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
772dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
773dfb2cda2SGreg Roach                                }
774dfb2cda2SGreg Roach                                break;
775dfb2cda2SGreg Roach                        }
776dfb2cda2SGreg Roach                        break;
777dfb2cda2SGreg Roach                }
778dfb2cda2SGreg Roach                unset($fields[$field_name]);
779dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS') {
780dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
781dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
7823c480c6bSGreg Roach                $query->where('spouse_families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%');
783dfb2cda2SGreg Roach            } elseif ($parts[1] === 'TYPE') {
784dfb2cda2SGreg Roach                // e.g. FACT:TYPE or EVEN:TYPE
785dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
786*b4adec9eSGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . "%\n2 TYPE %" . $field_value . '%');
787dfb2cda2SGreg Roach            } else {
788dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
789dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
790c5457947SGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%' . $parts[1] . '%' . $field_value . '%');
791dfb2cda2SGreg Roach            }
792dfb2cda2SGreg Roach        }
793*b4adec9eSGreg Roach
794dfb2cda2SGreg Roach        return $query
795dfb2cda2SGreg Roach            ->get()
79652a8ef61SGreg Roach            ->each($this->rowLimiter())
797d5ad3db0SGreg Roach            ->map($this->individualRowMapper())
798dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter())
7990b5fd0a6SGreg Roach            ->filter(static function (Individual $individual) use ($fields): bool {
800c5457947SGreg Roach                // Check for searches which were only partially matched by SQL
801dfb2cda2SGreg Roach                foreach ($fields as $field_name => $field_value) {
802c5457947SGreg Roach                    $parts = explode(':', $field_name . '::::');
803dfb2cda2SGreg Roach
804*b4adec9eSGreg Roach                    // NAME:*
805*b4adec9eSGreg Roach                    if ($parts[0] === 'NAME') {
806*b4adec9eSGreg Roach                        $regex = '/\n1 NAME.*(?:\n2.*)*\n2 ' . $parts[1] . ' .*' . preg_quote($field_value, '/') . '/i';
807*b4adec9eSGreg Roach
808*b4adec9eSGreg Roach                        if (preg_match($regex, $individual->gedcom())) {
809*b4adec9eSGreg Roach                            continue;
810*b4adec9eSGreg Roach                        }
811*b4adec9eSGreg Roach
812*b4adec9eSGreg Roach                        return false;
813*b4adec9eSGreg Roach                    }
814*b4adec9eSGreg Roach
815*b4adec9eSGreg Roach                    $regex = '/' . preg_quote($field_value, '/') . '/i';
816*b4adec9eSGreg Roach
817dfb2cda2SGreg Roach                    // *:PLAC
818c5457947SGreg Roach                    if ($parts[1] === 'PLAC') {
819a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
820392561bbSGreg Roach                            if (preg_match($regex, $fact->place()->gedcomName())) {
821c5457947SGreg Roach                                continue 2;
822dfb2cda2SGreg Roach                            }
823a41e65f0SGreg Roach                        }
824c5457947SGreg Roach                        return false;
825c5457947SGreg Roach                    }
826c5457947SGreg Roach
827dfb2cda2SGreg Roach                    // FAMS:*:PLAC
828c5457947SGreg Roach                    if ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
82939ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
830a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
831392561bbSGreg Roach                                if (preg_match($regex, $fact->place()->gedcomName())) {
832c5457947SGreg Roach                                    continue 2;
833a41e65f0SGreg Roach                                }
834dfb2cda2SGreg Roach                            }
835dfb2cda2SGreg Roach                        }
836c5457947SGreg Roach                        return false;
837c5457947SGreg Roach                    }
838c5457947SGreg Roach
839dfb2cda2SGreg Roach                    // e.g. searches for occupation, religion, note, etc.
840c5457947SGreg Roach                    if ($parts[0] === 'FAMS') {
84139ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
842a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
843a41e65f0SGreg Roach                                if (preg_match($regex, $fact->value())) {
844c5457947SGreg Roach                                    continue 3;
845a41e65f0SGreg Roach                                }
846dfb2cda2SGreg Roach                            }
847dfb2cda2SGreg Roach                        }
848c5457947SGreg Roach                        return false;
849c5457947SGreg Roach                    }
850c5457947SGreg Roach
851dfb2cda2SGreg Roach                    // e.g. FACT:TYPE or EVEN:TYPE
852c5457947SGreg Roach                    if ($parts[1] === 'TYPE' || $parts[1] === '_WT_USER') {
853a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
854c5457947SGreg Roach                            if (preg_match($regex, $fact->attribute($parts[1]))) {
855c5457947SGreg Roach                                continue 2;
856dfb2cda2SGreg Roach                            }
857dfb2cda2SGreg Roach                        }
858dfb2cda2SGreg Roach
859dfb2cda2SGreg Roach                        return false;
860dfb2cda2SGreg Roach                    }
861c5457947SGreg Roach                }
862dfb2cda2SGreg Roach
863dfb2cda2SGreg Roach                return true;
864dfb2cda2SGreg Roach            });
865dfb2cda2SGreg Roach    }
866dfb2cda2SGreg Roach
867dfb2cda2SGreg Roach    /**
868dfb2cda2SGreg Roach     * @param string $soundex
869dfb2cda2SGreg Roach     * @param string $lastname
870dfb2cda2SGreg Roach     * @param string $firstname
871dfb2cda2SGreg Roach     * @param string $place
872dfb2cda2SGreg Roach     * @param Tree[] $search_trees
873dfb2cda2SGreg Roach     *
874b5c8fd7eSGreg Roach     * @return Collection<Individual>
875dfb2cda2SGreg Roach     */
876dfb2cda2SGreg Roach    public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection
877dfb2cda2SGreg Roach    {
878dfb2cda2SGreg Roach        switch ($soundex) {
879dfb2cda2SGreg Roach            default:
880dfb2cda2SGreg Roach            case 'Russell':
881dfb2cda2SGreg Roach                $givn_sdx   = Soundex::russell($firstname);
882dfb2cda2SGreg Roach                $surn_sdx   = Soundex::russell($lastname);
883dfb2cda2SGreg Roach                $plac_sdx   = Soundex::russell($place);
884dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_std';
885dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_std';
886dfb2cda2SGreg Roach                $plac_field = 'p_std_soundex';
887dfb2cda2SGreg Roach                break;
888dfb2cda2SGreg Roach            case 'DaitchM':
889dfb2cda2SGreg Roach                $givn_sdx   = Soundex::daitchMokotoff($firstname);
890dfb2cda2SGreg Roach                $surn_sdx   = Soundex::daitchMokotoff($lastname);
891dfb2cda2SGreg Roach                $plac_sdx   = Soundex::daitchMokotoff($place);
892dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_dm';
893dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_dm';
894dfb2cda2SGreg Roach                $plac_field = 'p_dm_soundex';
895dfb2cda2SGreg Roach                break;
896dfb2cda2SGreg Roach        }
897dfb2cda2SGreg Roach
898dfb2cda2SGreg Roach        // Nothing to search for? Return nothing.
899dfb2cda2SGreg Roach        if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') {
9003fda39a7SGreg Roach            return new Collection();
901dfb2cda2SGreg Roach        }
902dfb2cda2SGreg Roach
903dfb2cda2SGreg Roach        $query = DB::table('individuals')
904dfb2cda2SGreg Roach            ->select(['individuals.*'])
905dfb2cda2SGreg Roach            ->distinct();
906dfb2cda2SGreg Roach
907dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $search_trees);
908dfb2cda2SGreg Roach
909dfb2cda2SGreg Roach        if ($plac_sdx !== '') {
9100b5fd0a6SGreg Roach            $query->join('placelinks', static function (JoinClause $join): void {
911dfb2cda2SGreg Roach                $join
912dfb2cda2SGreg Roach                    ->on('placelinks.pl_file', '=', 'individuals.i_file')
913dfb2cda2SGreg Roach                    ->on('placelinks.pl_gid', '=', 'individuals.i_id');
914dfb2cda2SGreg Roach            });
9150b5fd0a6SGreg Roach            $query->join('places', static function (JoinClause $join): void {
916dfb2cda2SGreg Roach                $join
917dfb2cda2SGreg Roach                    ->on('places.p_file', '=', 'placelinks.pl_file')
918dfb2cda2SGreg Roach                    ->on('places.p_id', '=', 'placelinks.pl_p_id');
919dfb2cda2SGreg Roach            });
920dfb2cda2SGreg Roach
921dfb2cda2SGreg Roach            $this->wherePhonetic($query, $plac_field, $plac_sdx);
922dfb2cda2SGreg Roach        }
923dfb2cda2SGreg Roach
924dfb2cda2SGreg Roach        if ($givn_sdx !== '' || $surn_sdx !== '') {
9250b5fd0a6SGreg Roach            $query->join('name', static function (JoinClause $join): void {
926dfb2cda2SGreg Roach                $join
927dfb2cda2SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
928dfb2cda2SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
929dfb2cda2SGreg Roach            });
930dfb2cda2SGreg Roach
931dfb2cda2SGreg Roach            $this->wherePhonetic($query, $givn_field, $givn_sdx);
932dfb2cda2SGreg Roach            $this->wherePhonetic($query, $surn_field, $surn_sdx);
933dfb2cda2SGreg Roach        }
934dfb2cda2SGreg Roach
935dfb2cda2SGreg Roach        return $query
936dfb2cda2SGreg Roach            ->get()
93752a8ef61SGreg Roach            ->each($this->rowLimiter())
938d5ad3db0SGreg Roach            ->map($this->individualRowMapper())
939dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter());
940dfb2cda2SGreg Roach    }
941dfb2cda2SGreg Roach
942dfb2cda2SGreg Roach    /**
94332cd2800SGreg Roach     * Paginate a search query.
94432cd2800SGreg Roach     *
94532cd2800SGreg Roach     * @param Builder $query      Searches the database for the desired records.
94632cd2800SGreg Roach     * @param Closure $row_mapper Converts a row from the query into a record.
947a7a24840SGreg Roach     * @param Closure $row_filter
94832cd2800SGreg Roach     * @param int     $offset     Skip this many rows.
94932cd2800SGreg Roach     * @param int     $limit      Take this many rows.
95032cd2800SGreg Roach     *
951b5c8fd7eSGreg Roach     * @return Collection<mixed>
95232cd2800SGreg Roach     */
953a7a24840SGreg Roach    private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection
95432cd2800SGreg Roach    {
95532cd2800SGreg Roach        $collection = new Collection();
95632cd2800SGreg Roach
95732cd2800SGreg Roach        foreach ($query->cursor() as $row) {
95832cd2800SGreg Roach            $record = $row_mapper($row);
959b68caec6SGreg Roach            // If the object has a method "canShow()", then use it to filter for privacy.
960a7a24840SGreg Roach            if ($row_filter($record)) {
96132cd2800SGreg Roach                if ($offset > 0) {
96232cd2800SGreg Roach                    $offset--;
96332cd2800SGreg Roach                } else {
96432cd2800SGreg Roach                    if ($limit > 0) {
96532cd2800SGreg Roach                        $collection->push($record);
96632cd2800SGreg Roach                    }
96732cd2800SGreg Roach
96832cd2800SGreg Roach                    $limit--;
96932cd2800SGreg Roach
97032cd2800SGreg Roach                    if ($limit === 0) {
97132cd2800SGreg Roach                        break;
97232cd2800SGreg Roach                    }
97332cd2800SGreg Roach                }
97432cd2800SGreg Roach            }
97532cd2800SGreg Roach        }
97632cd2800SGreg Roach
977e0458bdcSGreg Roach
97832cd2800SGreg Roach        return $collection;
97932cd2800SGreg Roach    }
980a7a24840SGreg Roach
981a7a24840SGreg Roach    /**
982a7a24840SGreg Roach     * Apply search filters to a SQL query column.  Apply collation rules to MySQL.
983a7a24840SGreg Roach     *
984a7a24840SGreg Roach     * @param Builder           $query
985a7a24840SGreg Roach     * @param Expression|string $field
986a7a24840SGreg Roach     * @param string[]          $search_terms
987a7a24840SGreg Roach     */
988a7a24840SGreg Roach    private function whereSearch(Builder $query, $field, array $search_terms): void
989a7a24840SGreg Roach    {
990a7a24840SGreg Roach        if ($field instanceof Expression) {
991a7a24840SGreg Roach            $field = $field->getValue();
992a7a24840SGreg Roach        }
993a7a24840SGreg Roach
994a7a24840SGreg Roach        foreach ($search_terms as $search_term) {
995b5961194SGreg Roach            $query->where(new Expression($field), 'LIKE', '%' . addcslashes($search_term, '\\%_') . '%');
996a7a24840SGreg Roach        }
997a7a24840SGreg Roach    }
998a7a24840SGreg Roach
999a7a24840SGreg Roach    /**
10002d686e68SGreg Roach     * Apply soundex search filters to a SQL query column.
10012d686e68SGreg Roach     *
10022d686e68SGreg Roach     * @param Builder           $query
10032d686e68SGreg Roach     * @param Expression|string $field
10042d686e68SGreg Roach     * @param string            $soundex
10052d686e68SGreg Roach     */
10062d686e68SGreg Roach    private function wherePhonetic(Builder $query, $field, string $soundex): void
10072d686e68SGreg Roach    {
10082d686e68SGreg Roach        if ($soundex !== '') {
10090b5fd0a6SGreg Roach            $query->where(static function (Builder $query) use ($soundex, $field): void {
10102d686e68SGreg Roach                foreach (explode(':', $soundex) as $sdx) {
10112d686e68SGreg Roach                    $query->orWhere($field, 'LIKE', '%' . $sdx . '%');
10122d686e68SGreg Roach                }
10132d686e68SGreg Roach            });
10142d686e68SGreg Roach        }
10152d686e68SGreg Roach    }
10162d686e68SGreg Roach
10172d686e68SGreg Roach    /**
1018a7a24840SGreg Roach     * @param Builder $query
1019a7a24840SGreg Roach     * @param string  $tree_id_field
1020a7a24840SGreg Roach     * @param Tree[]  $trees
1021a7a24840SGreg Roach     */
1022a7a24840SGreg Roach    private function whereTrees(Builder $query, string $tree_id_field, array $trees): void
1023a7a24840SGreg Roach    {
10240b5fd0a6SGreg Roach        $tree_ids = array_map(static function (Tree $tree): int {
1025a7a24840SGreg Roach            return $tree->id();
1026a7a24840SGreg Roach        }, $trees);
1027a7a24840SGreg Roach
1028a7a24840SGreg Roach        $query->whereIn($tree_id_field, $tree_ids);
1029a7a24840SGreg Roach    }
1030a7a24840SGreg Roach
1031d5ad3db0SGreg Roach    /**
1032d5ad3db0SGreg Roach     * Find the media object that uses a particular media file.
1033d5ad3db0SGreg Roach     *
1034d5ad3db0SGreg Roach     * @param string $file
1035d5ad3db0SGreg Roach     *
1036d5ad3db0SGreg Roach     * @return Media[]
1037d5ad3db0SGreg Roach     */
1038d5ad3db0SGreg Roach    public function findMediaObjectsForMediaFile(string $file): array
1039d5ad3db0SGreg Roach    {
1040d5ad3db0SGreg Roach        return DB::table('media')
1041d5ad3db0SGreg Roach            ->join('media_file', static function (JoinClause $join): void {
1042d5ad3db0SGreg Roach                $join
1043d5ad3db0SGreg Roach                    ->on('media_file.m_file', '=', 'media.m_file')
1044d5ad3db0SGreg Roach                    ->on('media_file.m_id', '=', 'media.m_id');
1045d5ad3db0SGreg Roach            })
1046d5ad3db0SGreg Roach            ->join('gedcom_setting', 'media.m_file', '=', 'gedcom_setting.gedcom_id')
1047d5ad3db0SGreg Roach            ->where(new Expression('setting_value || multimedia_file_refn'), '=', $file)
1048d5ad3db0SGreg Roach            ->select(['media.*'])
1049d5ad3db0SGreg Roach            ->distinct()
1050d5ad3db0SGreg Roach            ->get()
1051d5ad3db0SGreg Roach            ->map($this->mediaRowMapper())
1052d5ad3db0SGreg Roach            ->all();
1053d5ad3db0SGreg Roach    }
1054d5ad3db0SGreg Roach
1055a7a24840SGreg Roach    /**
1056a7a24840SGreg Roach     * A closure to filter records by privacy-filtered GEDCOM data.
1057a7a24840SGreg Roach     *
10585d4b7ec2SGreg Roach     * @param array<string> $search_terms
1059a7a24840SGreg Roach     *
1060a7a24840SGreg Roach     * @return Closure
1061a7a24840SGreg Roach     */
1062a7a24840SGreg Roach    private function rawGedcomFilter(array $search_terms): Closure
1063a7a24840SGreg Roach    {
10646c2179e2SGreg Roach        return static function (GedcomRecord $record) use ($search_terms): bool {
1065a7a24840SGreg Roach            // Ignore non-genealogy fields
1066faa5e163SGreg Roach            $gedcom = preg_replace('/\n\d (?:_UID|_WT_USER) .*/', '', $record->gedcom());
1067a7a24840SGreg Roach
1068a7a24840SGreg Roach            // Ignore matches in links
1069a7a24840SGreg Roach            $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom);
1070a7a24840SGreg Roach
1071a7a24840SGreg Roach            // Re-apply the filtering
1072a7a24840SGreg Roach            foreach ($search_terms as $search_term) {
1073a7a24840SGreg Roach                if (mb_stripos($gedcom, $search_term) === false) {
1074a7a24840SGreg Roach                    return false;
1075a7a24840SGreg Roach                }
1076a7a24840SGreg Roach            }
1077a7a24840SGreg Roach
1078a7a24840SGreg Roach            return true;
1079a7a24840SGreg Roach        };
1080a7a24840SGreg Roach    }
108152a8ef61SGreg Roach
108252a8ef61SGreg Roach    /**
108352a8ef61SGreg Roach     * Searching for short or common text can give more results than the system can process.
108452a8ef61SGreg Roach     *
108552a8ef61SGreg Roach     * @param int $limit
108652a8ef61SGreg Roach     *
108752a8ef61SGreg Roach     * @return Closure
108852a8ef61SGreg Roach     */
108952a8ef61SGreg Roach    private function rowLimiter(int $limit = 1000): Closure
109052a8ef61SGreg Roach    {
10916c2179e2SGreg Roach        return static function () use ($limit): void {
109252a8ef61SGreg Roach            static $n = 0;
109352a8ef61SGreg Roach
109452a8ef61SGreg Roach            if (++$n > $limit) {
109552a8ef61SGreg Roach                $message = I18N::translate('The search returned too many results.');
109652a8ef61SGreg Roach
1097d501c45dSGreg Roach                throw new HttpServiceUnavailableException($message);
109852a8ef61SGreg Roach            }
109952a8ef61SGreg Roach        };
110052a8ef61SGreg Roach    }
1101d5ad3db0SGreg Roach
1102d5ad3db0SGreg Roach    /**
1103d5ad3db0SGreg Roach     * Convert a row from any tree in the families table into a family object.
1104d5ad3db0SGreg Roach     *
1105d5ad3db0SGreg Roach     * @return Closure
1106d5ad3db0SGreg Roach     */
1107d5ad3db0SGreg Roach    private function familyRowMapper(): Closure
1108d5ad3db0SGreg Roach    {
1109d5ad3db0SGreg Roach        return function (stdClass $row): Family {
1110d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->f_file);
1111d5ad3db0SGreg Roach
1112a091ac74SGreg Roach            return Factory::family()->mapper($tree)($row);
1113d5ad3db0SGreg Roach        };
1114d5ad3db0SGreg Roach    }
1115d5ad3db0SGreg Roach
1116d5ad3db0SGreg Roach    /**
1117d5ad3db0SGreg Roach     * Convert a row from any tree in the individuals table into an individual object.
1118d5ad3db0SGreg Roach     *
1119d5ad3db0SGreg Roach     * @return Closure
1120d5ad3db0SGreg Roach     */
1121d5ad3db0SGreg Roach    private function individualRowMapper(): Closure
1122d5ad3db0SGreg Roach    {
1123d5ad3db0SGreg Roach        return function (stdClass $row): Individual {
1124d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->i_file);
1125d5ad3db0SGreg Roach
1126a091ac74SGreg Roach            return Factory::individual()->mapper($tree)($row);
1127d5ad3db0SGreg Roach        };
1128d5ad3db0SGreg Roach    }
1129d5ad3db0SGreg Roach
1130d5ad3db0SGreg Roach    /**
1131d5ad3db0SGreg Roach     * Convert a row from any tree in the media table into an media object.
1132d5ad3db0SGreg Roach     *
1133d5ad3db0SGreg Roach     * @return Closure
1134d5ad3db0SGreg Roach     */
1135d5ad3db0SGreg Roach    private function mediaRowMapper(): Closure
1136d5ad3db0SGreg Roach    {
1137d5ad3db0SGreg Roach        return function (stdClass $row): Media {
1138d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->m_file);
1139d5ad3db0SGreg Roach
1140a091ac74SGreg Roach            return Factory::media()->mapper($tree)($row);
1141d5ad3db0SGreg Roach        };
1142d5ad3db0SGreg Roach    }
1143d5ad3db0SGreg Roach
1144d5ad3db0SGreg Roach    /**
1145d5ad3db0SGreg Roach     * Convert a row from any tree in the other table into a note object.
1146d5ad3db0SGreg Roach     *
1147d5ad3db0SGreg Roach     * @return Closure
1148d5ad3db0SGreg Roach     */
1149d5ad3db0SGreg Roach    private function noteRowMapper(): Closure
1150d5ad3db0SGreg Roach    {
1151d5ad3db0SGreg Roach        return function (stdClass $row): Note {
1152d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->o_file);
1153d5ad3db0SGreg Roach
1154a091ac74SGreg Roach            return Factory::note()->mapper($tree)($row);
1155d5ad3db0SGreg Roach        };
1156d5ad3db0SGreg Roach    }
1157d5ad3db0SGreg Roach
1158d5ad3db0SGreg Roach    /**
1159d5ad3db0SGreg Roach     * Convert a row from any tree in the other table into a repository object.
1160d5ad3db0SGreg Roach     *
1161d5ad3db0SGreg Roach     * @return Closure
1162d5ad3db0SGreg Roach     */
1163d5ad3db0SGreg Roach    private function repositoryRowMapper(): Closure
1164d5ad3db0SGreg Roach    {
1165d5ad3db0SGreg Roach        return function (stdClass $row): Repository {
1166d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->o_file);
1167d5ad3db0SGreg Roach
1168a091ac74SGreg Roach            return Factory::repository()->mapper($tree)($row);
1169d5ad3db0SGreg Roach        };
1170d5ad3db0SGreg Roach    }
1171d5ad3db0SGreg Roach
1172d5ad3db0SGreg Roach    /**
1173d5ad3db0SGreg Roach     * Convert a row from any tree in the sources table into a source object.
1174d5ad3db0SGreg Roach     *
1175d5ad3db0SGreg Roach     * @return Closure
1176d5ad3db0SGreg Roach     */
1177d5ad3db0SGreg Roach    private function sourceRowMapper(): Closure
1178d5ad3db0SGreg Roach    {
1179d5ad3db0SGreg Roach        return function (stdClass $row): Source {
1180d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->s_file);
1181d5ad3db0SGreg Roach
1182a091ac74SGreg Roach            return Factory::source()->mapper($tree)($row);
1183d5ad3db0SGreg Roach        };
1184d5ad3db0SGreg Roach    }
1185d5ad3db0SGreg Roach
1186d5ad3db0SGreg Roach    /**
1187d5ad3db0SGreg Roach     * Convert a row from any tree in the other table into a submitter object.
1188d5ad3db0SGreg Roach     *
1189d5ad3db0SGreg Roach     * @return Closure
1190d5ad3db0SGreg Roach     */
1191d5ad3db0SGreg Roach    private function submitterRowMapper(): Closure
1192d5ad3db0SGreg Roach    {
11933959eeb6SGreg Roach        return function (stdClass $row): Submitter {
1194d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $row->o_file);
1195d5ad3db0SGreg Roach
1196a091ac74SGreg Roach            return Factory::submitter()->mapper($tree)($row);
1197d5ad3db0SGreg Roach        };
1198d5ad3db0SGreg Roach    }
119932cd2800SGreg Roach}
1200