xref: /webtrees/app/Services/SearchService.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
132cd2800SGreg Roach<?php
23976b470SGreg Roach
332cd2800SGreg Roach/**
432cd2800SGreg Roach * webtrees: online genealogy
532cd2800SGreg Roach * Copyright (C) 2019 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 */
17*fcfa147eSGreg Roach
1832cd2800SGreg Roachdeclare(strict_types=1);
1932cd2800SGreg Roach
2032cd2800SGreg Roachnamespace Fisharebest\Webtrees\Services;
2132cd2800SGreg Roach
2232cd2800SGreg Roachuse Closure;
23dfb2cda2SGreg Roachuse Fisharebest\Webtrees\Date;
2452a8ef61SGreg Roachuse Fisharebest\Webtrees\Exceptions\InternalServerErrorException;
2532cd2800SGreg Roachuse Fisharebest\Webtrees\Family;
26a7a24840SGreg Roachuse Fisharebest\Webtrees\Gedcom;
2732cd2800SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2852a8ef61SGreg Roachuse Fisharebest\Webtrees\I18N;
2932cd2800SGreg Roachuse Fisharebest\Webtrees\Individual;
3032cd2800SGreg Roachuse Fisharebest\Webtrees\Media;
3132cd2800SGreg Roachuse Fisharebest\Webtrees\Note;
32b68caec6SGreg Roachuse Fisharebest\Webtrees\Place;
3332cd2800SGreg Roachuse Fisharebest\Webtrees\Repository;
342d686e68SGreg Roachuse Fisharebest\Webtrees\Soundex;
3532cd2800SGreg Roachuse Fisharebest\Webtrees\Source;
3632cd2800SGreg Roachuse Fisharebest\Webtrees\Tree;
3732cd2800SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3832cd2800SGreg Roachuse Illuminate\Database\Query\Builder;
39a7a24840SGreg Roachuse Illuminate\Database\Query\Expression;
4032cd2800SGreg Roachuse Illuminate\Database\Query\JoinClause;
4132cd2800SGreg Roachuse Illuminate\Support\Collection;
4232cd2800SGreg Roachuse stdClass;
433976b470SGreg Roach
44a7a24840SGreg Roachuse function mb_stripos;
4532cd2800SGreg Roach
4632cd2800SGreg Roach/**
4732cd2800SGreg Roach * Search trees for genealogy records.
4832cd2800SGreg Roach */
4932cd2800SGreg Roachclass SearchService
5032cd2800SGreg Roach{
51a7a24840SGreg Roach    /**
52a7a24840SGreg Roach     * @param Tree[]   $trees
53a7a24840SGreg Roach     * @param string[] $search
54a7a24840SGreg Roach     *
5554c7f8dfSGreg Roach     * @return Collection
56a7a24840SGreg Roach     */
57a7a24840SGreg Roach    public function searchFamilies(array $trees, array $search): Collection
58a7a24840SGreg Roach    {
59a7a24840SGreg Roach        $query = DB::table('families');
60a7a24840SGreg Roach
61a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
62a7a24840SGreg Roach        $this->whereSearch($query, 'f_gedcom', $search);
63a7a24840SGreg Roach
64a7a24840SGreg Roach        return $query
65a7a24840SGreg Roach            ->get()
6652a8ef61SGreg Roach            ->each($this->rowLimiter())
67a7a24840SGreg Roach            ->map(Family::rowMapper())
68a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
697f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
70a7a24840SGreg Roach    }
71a7a24840SGreg Roach
7232cd2800SGreg Roach    /**
7332cd2800SGreg Roach     * Search for families by name.
7432cd2800SGreg Roach     *
75a7a24840SGreg Roach     * @param Tree[]   $trees
76a7a24840SGreg Roach     * @param string[] $search
7732cd2800SGreg Roach     * @param int      $offset
7832cd2800SGreg Roach     * @param int      $limit
7932cd2800SGreg Roach     *
8054c7f8dfSGreg Roach     * @return Collection
8132cd2800SGreg Roach     */
82a7a24840SGreg Roach    public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
8332cd2800SGreg Roach    {
8432cd2800SGreg Roach        $query = DB::table('families')
85ac499332SGreg Roach            ->leftJoin('name AS husb_name', static function (JoinClause $join): void {
8632cd2800SGreg Roach                $join
8732cd2800SGreg Roach                    ->on('husb_name.n_file', '=', 'families.f_file')
88ac499332SGreg Roach                    ->on('husb_name.n_id', '=', 'families.f_husb')
89ac499332SGreg Roach                    ->where('husb_name.n_type', '<>', '_MARNM');
9032cd2800SGreg Roach            })
91ac499332SGreg Roach            ->leftJoin('name AS wife_name', static function (JoinClause $join): void {
9232cd2800SGreg Roach                $join
9332cd2800SGreg Roach                    ->on('wife_name.n_file', '=', 'families.f_file')
94ac499332SGreg Roach                    ->on('wife_name.n_id', '=', 'families.f_wife')
95ac499332SGreg Roach                    ->where('wife_name.n_type', '<>', '_MARNM');
96ac499332SGreg Roach            });
97a7a24840SGreg Roach
98a7a24840SGreg Roach        $prefix = DB::connection()->getTablePrefix();
99a69f5655SGreg Roach        $field  = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')");
100a7a24840SGreg Roach
101a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
102a7a24840SGreg Roach        $this->whereSearch($query, $field, $search);
103a7a24840SGreg Roach
104a7a24840SGreg Roach        $query
10532cd2800SGreg Roach            ->orderBy('husb_name.n_sort')
10632cd2800SGreg Roach            ->orderBy('wife_name.n_sort')
107c0804649SGreg Roach            ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort'])
10832cd2800SGreg Roach            ->distinct();
10932cd2800SGreg Roach
110a7a24840SGreg Roach        return $this->paginateQuery($query, Family::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
111a7a24840SGreg Roach    }
112a7a24840SGreg Roach
113a7a24840SGreg Roach    /**
114a7a24840SGreg Roach     * @param Tree[]   $trees
115a7a24840SGreg Roach     * @param string[] $search
116a7a24840SGreg Roach     *
11754c7f8dfSGreg Roach     * @return Collection
118a7a24840SGreg Roach     */
119a7a24840SGreg Roach    public function searchIndividuals(array $trees, array $search): Collection
120a7a24840SGreg Roach    {
121a7a24840SGreg Roach        $query = DB::table('individuals');
122a7a24840SGreg Roach
123a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
124a7a24840SGreg Roach        $this->whereSearch($query, 'i_gedcom', $search);
125a7a24840SGreg Roach
126a7a24840SGreg Roach        return $query
127a7a24840SGreg Roach            ->get()
12852a8ef61SGreg Roach            ->each($this->rowLimiter())
129a7a24840SGreg Roach            ->map(Individual::rowMapper())
130a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
1317f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
13232cd2800SGreg Roach    }
13332cd2800SGreg Roach
13432cd2800SGreg Roach    /**
13532cd2800SGreg Roach     * Search for individuals by name.
13632cd2800SGreg Roach     *
137a7a24840SGreg Roach     * @param Tree[]   $trees
138a7a24840SGreg Roach     * @param string[] $search
13932cd2800SGreg Roach     * @param int      $offset
14032cd2800SGreg Roach     * @param int      $limit
14132cd2800SGreg Roach     *
14254c7f8dfSGreg Roach     * @return Collection
14332cd2800SGreg Roach     */
144a7a24840SGreg Roach    public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
14532cd2800SGreg Roach    {
14632cd2800SGreg Roach        $query = DB::table('individuals')
1470b5fd0a6SGreg Roach            ->join('name', static function (JoinClause $join): void {
14832cd2800SGreg Roach                $join
14932cd2800SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
150a7a24840SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
15132cd2800SGreg Roach            })
152e84cf2deSGreg Roach            ->orderBy('n_sort')
153e0458bdcSGreg Roach            ->select(['individuals.*', 'n_num']);
15432cd2800SGreg Roach
155a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
156a7a24840SGreg Roach        $this->whereSearch($query, 'n_full', $search);
157a7a24840SGreg Roach
158a7a24840SGreg Roach        return $this->paginateQuery($query, Individual::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
15932cd2800SGreg Roach    }
16032cd2800SGreg Roach
16132cd2800SGreg Roach    /**
16232cd2800SGreg Roach     * Search for media objects.
16332cd2800SGreg Roach     *
164a7a24840SGreg Roach     * @param Tree[]   $trees
165a7a24840SGreg Roach     * @param string[] $search
16632cd2800SGreg Roach     * @param int      $offset
16732cd2800SGreg Roach     * @param int      $limit
16832cd2800SGreg Roach     *
16954c7f8dfSGreg Roach     * @return Collection
17032cd2800SGreg Roach     */
171a7a24840SGreg Roach    public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
17232cd2800SGreg Roach    {
173a7a24840SGreg Roach        $query = DB::table('media');
17432cd2800SGreg Roach
175a7a24840SGreg Roach        $this->whereTrees($query, 'media.m_file', $trees);
176a7a24840SGreg Roach        $this->whereSearch($query, 'm_gedcom', $search);
177a7a24840SGreg Roach
178a7a24840SGreg Roach        return $this->paginateQuery($query, Media::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
17932cd2800SGreg Roach    }
18032cd2800SGreg Roach
18132cd2800SGreg Roach    /**
18232cd2800SGreg Roach     * Search for notes.
18332cd2800SGreg Roach     *
184a7a24840SGreg Roach     * @param Tree[]   $trees
185a7a24840SGreg Roach     * @param string[] $search
18632cd2800SGreg Roach     * @param int      $offset
18732cd2800SGreg Roach     * @param int      $limit
18832cd2800SGreg Roach     *
18954c7f8dfSGreg Roach     * @return Collection
19032cd2800SGreg Roach     */
191a7a24840SGreg Roach    public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
19232cd2800SGreg Roach    {
19332cd2800SGreg Roach        $query = DB::table('other')
194a7a24840SGreg Roach            ->where('o_type', '=', 'NOTE');
19532cd2800SGreg Roach
196a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
197a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
198a7a24840SGreg Roach
199a7a24840SGreg Roach        return $this->paginateQuery($query, Note::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
20032cd2800SGreg Roach    }
20132cd2800SGreg Roach
20232cd2800SGreg Roach    /**
20332cd2800SGreg Roach     * Search for repositories.
20432cd2800SGreg Roach     *
205a7a24840SGreg Roach     * @param Tree[]   $trees
206a7a24840SGreg Roach     * @param string[] $search
20732cd2800SGreg Roach     * @param int      $offset
20832cd2800SGreg Roach     * @param int      $limit
20932cd2800SGreg Roach     *
21054c7f8dfSGreg Roach     * @return Collection
21132cd2800SGreg Roach     */
212a7a24840SGreg Roach    public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
21332cd2800SGreg Roach    {
21432cd2800SGreg Roach        $query = DB::table('other')
215a7a24840SGreg Roach            ->where('o_type', '=', 'REPO');
21632cd2800SGreg Roach
217a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
218a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
219a7a24840SGreg Roach
220a7a24840SGreg Roach        return $this->paginateQuery($query, Repository::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
22132cd2800SGreg Roach    }
22232cd2800SGreg Roach
22332cd2800SGreg Roach    /**
224a7a24840SGreg Roach     * Search for sources.
22532cd2800SGreg Roach     *
226a7a24840SGreg Roach     * @param Tree[]   $trees
227a7a24840SGreg Roach     * @param string[] $search
22832cd2800SGreg Roach     * @param int      $offset
22932cd2800SGreg Roach     * @param int      $limit
23032cd2800SGreg Roach     *
23154c7f8dfSGreg Roach     * @return Collection
23232cd2800SGreg Roach     */
233a7a24840SGreg Roach    public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
234a7a24840SGreg Roach    {
235a7a24840SGreg Roach        $query = DB::table('sources');
236a7a24840SGreg Roach
237a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
238a7a24840SGreg Roach        $this->whereSearch($query, 's_gedcom', $search);
239a7a24840SGreg Roach
240a7a24840SGreg Roach        return $this->paginateQuery($query, Source::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
241a7a24840SGreg Roach    }
242a7a24840SGreg Roach
243a7a24840SGreg Roach    /**
244a7a24840SGreg Roach     * Search for sources by name.
245a7a24840SGreg Roach     *
246a7a24840SGreg Roach     * @param Tree[]   $trees
247a7a24840SGreg Roach     * @param string[] $search
248a7a24840SGreg Roach     * @param int      $offset
249a7a24840SGreg Roach     * @param int      $limit
250a7a24840SGreg Roach     *
25154c7f8dfSGreg Roach     * @return Collection
252a7a24840SGreg Roach     */
253a7a24840SGreg Roach    public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
25432cd2800SGreg Roach    {
25532cd2800SGreg Roach        $query = DB::table('sources')
256c0804649SGreg Roach            ->orderBy('s_name');
25732cd2800SGreg Roach
258a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
259a7a24840SGreg Roach        $this->whereSearch($query, 's_name', $search);
260a7a24840SGreg Roach
261a7a24840SGreg Roach        return $this->paginateQuery($query, Source::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
26232cd2800SGreg Roach    }
26332cd2800SGreg Roach
26432cd2800SGreg Roach    /**
26532cd2800SGreg Roach     * Search for submitters.
26632cd2800SGreg Roach     *
267a7a24840SGreg Roach     * @param Tree[]   $trees
268a7a24840SGreg Roach     * @param string[] $search
26932cd2800SGreg Roach     * @param int      $offset
27032cd2800SGreg Roach     * @param int      $limit
27132cd2800SGreg Roach     *
27254c7f8dfSGreg Roach     * @return Collection
27332cd2800SGreg Roach     */
274a7a24840SGreg Roach    public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
27532cd2800SGreg Roach    {
27632cd2800SGreg Roach        $query = DB::table('other')
277a7a24840SGreg Roach            ->where('o_type', '=', 'SUBM');
27832cd2800SGreg Roach
279a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
280a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
281a7a24840SGreg Roach
282a7a24840SGreg Roach        return $this->paginateQuery($query, GedcomRecord::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
28332cd2800SGreg Roach    }
28432cd2800SGreg Roach
28532cd2800SGreg Roach    /**
286b68caec6SGreg Roach     * Search for places.
287b68caec6SGreg Roach     *
288b68caec6SGreg Roach     * @param Tree   $tree
289b68caec6SGreg Roach     * @param string $search
290b68caec6SGreg Roach     * @param int    $offset
291b68caec6SGreg Roach     * @param int    $limit
292b68caec6SGreg Roach     *
29354c7f8dfSGreg Roach     * @return Collection
294b68caec6SGreg Roach     */
295b68caec6SGreg Roach    public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
296b68caec6SGreg Roach    {
297b68caec6SGreg Roach        $query = DB::table('places AS p0')
298b68caec6SGreg Roach            ->where('p0.p_file', '=', $tree->id())
299b68caec6SGreg Roach            ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id')
300b68caec6SGreg Roach            ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id')
301b68caec6SGreg Roach            ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id')
302b68caec6SGreg Roach            ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id')
303b68caec6SGreg Roach            ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id')
304b68caec6SGreg Roach            ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id')
305b68caec6SGreg Roach            ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id')
306b68caec6SGreg Roach            ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id')
307b68caec6SGreg Roach            ->orderBy('p0.p_place')
308b68caec6SGreg Roach            ->orderBy('p1.p_place')
309b68caec6SGreg Roach            ->orderBy('p2.p_place')
310b68caec6SGreg Roach            ->orderBy('p3.p_place')
311b68caec6SGreg Roach            ->orderBy('p4.p_place')
312b68caec6SGreg Roach            ->orderBy('p5.p_place')
313b68caec6SGreg Roach            ->orderBy('p6.p_place')
314b68caec6SGreg Roach            ->orderBy('p7.p_place')
315b68caec6SGreg Roach            ->orderBy('p8.p_place')
316b68caec6SGreg Roach            ->select([
317b68caec6SGreg Roach                'p0.p_place AS place0',
318b68caec6SGreg Roach                'p1.p_place AS place1',
319b68caec6SGreg Roach                'p2.p_place AS place2',
320b68caec6SGreg Roach                'p3.p_place AS place3',
321b68caec6SGreg Roach                'p4.p_place AS place4',
322b68caec6SGreg Roach                'p5.p_place AS place5',
323b68caec6SGreg Roach                'p6.p_place AS place6',
324b68caec6SGreg Roach                'p7.p_place AS place7',
325b68caec6SGreg Roach                'p8.p_place AS place8',
326b68caec6SGreg Roach            ]);
327b68caec6SGreg Roach
328b68caec6SGreg Roach        // Filter each level of the hierarchy.
329b68caec6SGreg Roach        foreach (explode(',', $search, 9) as $level => $string) {
330b68caec6SGreg Roach            $query->whereContains('p' . $level . '.p_place', $string);
331b68caec6SGreg Roach        }
332b68caec6SGreg Roach
3336c2179e2SGreg Roach        $row_mapper = static function (stdClass $row) use ($tree): Place {
334b68caec6SGreg Roach            $place = implode(', ', array_filter((array) $row));
335b68caec6SGreg Roach
336b68caec6SGreg Roach            return new Place($place, $tree);
337b68caec6SGreg Roach        };
338b68caec6SGreg Roach
3396c2179e2SGreg Roach        $filter = static function (): bool {
340a7a24840SGreg Roach            return true;
341a7a24840SGreg Roach        };
342b68caec6SGreg Roach
343a7a24840SGreg Roach        return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit);
344a7a24840SGreg Roach    }
345b68caec6SGreg Roach
346b68caec6SGreg Roach    /**
347a5dbc5b2SGreg Roach     * @param Tree[]   $trees
348dfb2cda2SGreg Roach     * @param string[] $fields
349dfb2cda2SGreg Roach     * @param string[] $modifiers
350dfb2cda2SGreg Roach     *
35154c7f8dfSGreg Roach     * @return Collection
352dfb2cda2SGreg Roach     */
353dfb2cda2SGreg Roach    public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection
354dfb2cda2SGreg Roach    {
355dfb2cda2SGreg Roach        $fields = array_filter($fields);
356dfb2cda2SGreg Roach
357dfb2cda2SGreg Roach        $query = DB::table('individuals')
358dfb2cda2SGreg Roach            ->select(['individuals.*'])
359dfb2cda2SGreg Roach            ->distinct();
360dfb2cda2SGreg Roach
361dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
362dfb2cda2SGreg Roach
363dfb2cda2SGreg Roach        // Join the following tables
364dfb2cda2SGreg Roach        $father_name   = false;
365dfb2cda2SGreg Roach        $mother_name   = false;
366dfb2cda2SGreg Roach        $spouse_family = false;
367dfb2cda2SGreg Roach        $indi_name     = false;
368dfb2cda2SGreg Roach        $indi_date     = false;
369dfb2cda2SGreg Roach        $fam_date      = false;
370dfb2cda2SGreg Roach        $indi_plac     = false;
371dfb2cda2SGreg Roach        $fam_plac      = false;
372dfb2cda2SGreg Roach
373dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
374dfb2cda2SGreg Roach            if ($field_value !== '') {
375dfb2cda2SGreg Roach                if (substr($field_name, 0, 14) === 'FAMC:HUSB:NAME') {
376dfb2cda2SGreg Roach                    $father_name = true;
377dfb2cda2SGreg Roach                } elseif (substr($field_name, 0, 14) === 'FAMC:WIFE:NAME') {
378dfb2cda2SGreg Roach                    $mother_name = true;
379dfb2cda2SGreg Roach                } elseif (substr($field_name, 0, 4) === 'NAME') {
380dfb2cda2SGreg Roach                    $indi_name = true;
381dfb2cda2SGreg Roach                } elseif (strpos($field_name, ':DATE') !== false) {
382dfb2cda2SGreg Roach                    if (substr($field_name, 0, 4) === 'FAMS') {
383dfb2cda2SGreg Roach                        $fam_date      = true;
384dfb2cda2SGreg Roach                        $spouse_family = true;
385dfb2cda2SGreg Roach                    } else {
386dfb2cda2SGreg Roach                        $indi_date = true;
387dfb2cda2SGreg Roach                    }
388dfb2cda2SGreg Roach                } elseif (strpos($field_name, ':PLAC') !== false) {
389dfb2cda2SGreg Roach                    if (substr($field_name, 0, 4) === 'FAMS') {
390dfb2cda2SGreg Roach                        $fam_plac      = true;
391dfb2cda2SGreg Roach                        $spouse_family = true;
392dfb2cda2SGreg Roach                    } else {
393dfb2cda2SGreg Roach                        $indi_plac = true;
394dfb2cda2SGreg Roach                    }
395dfb2cda2SGreg Roach                } elseif ($field_name === 'FAMS:NOTE') {
396dfb2cda2SGreg Roach                    $spouse_family = true;
397dfb2cda2SGreg Roach                }
398dfb2cda2SGreg Roach            }
399dfb2cda2SGreg Roach        }
400dfb2cda2SGreg Roach
401dfb2cda2SGreg Roach        if ($father_name || $mother_name) {
4020b5fd0a6SGreg Roach            $query->join('link AS l1', static function (JoinClause $join): void {
403dfb2cda2SGreg Roach                $join
404dfb2cda2SGreg Roach                    ->on('l1.l_file', '=', 'individuals.i_file')
405dfb2cda2SGreg Roach                    ->on('l1.l_from', '=', 'individuals.i_id')
406dfb2cda2SGreg Roach                    ->where('l1.l_type', '=', 'FAMC');
407dfb2cda2SGreg Roach            });
408dfb2cda2SGreg Roach
409dfb2cda2SGreg Roach            if ($father_name) {
4100b5fd0a6SGreg Roach                $query->join('link AS l2', static function (JoinClause $join): void {
411dfb2cda2SGreg Roach                    $join
412dfb2cda2SGreg Roach                        ->on('l2.l_file', '=', 'l1.l_file')
413dfb2cda2SGreg Roach                        ->on('l2.l_from', '=', 'l1.l_to')
414dfb2cda2SGreg Roach                        ->where('l2.l_type', '=', 'HUSB');
415dfb2cda2SGreg Roach                });
4160b5fd0a6SGreg Roach                $query->join('name AS father_name', static function (JoinClause $join): void {
417dfb2cda2SGreg Roach                    $join
418dfb2cda2SGreg Roach                        ->on('father_name.n_file', '=', 'l2.l_file')
419dfb2cda2SGreg Roach                        ->on('father_name.n_id', '=', 'l2.l_to');
420dfb2cda2SGreg Roach                });
421dfb2cda2SGreg Roach            }
422dfb2cda2SGreg Roach
423dfb2cda2SGreg Roach            if ($mother_name) {
4240b5fd0a6SGreg Roach                $query->join('link AS l3', static function (JoinClause $join): void {
425dfb2cda2SGreg Roach                    $join
426dfb2cda2SGreg Roach                        ->on('l3.l_file', '=', 'l1.l_file')
427dfb2cda2SGreg Roach                        ->on('l3.l_from', '=', 'l1.l_to')
428dfb2cda2SGreg Roach                        ->where('l3.l_type', '=', 'WIFE');
429dfb2cda2SGreg Roach                });
4300b5fd0a6SGreg Roach                $query->join('name AS mother_name', static function (JoinClause $join): void {
431dfb2cda2SGreg Roach                    $join
432dfb2cda2SGreg Roach                        ->on('mother_name.n_file', '=', 'l3.l_file')
433dfb2cda2SGreg Roach                        ->on('mother_name.n_id', '=', 'l3.l_to');
434dfb2cda2SGreg Roach                });
435dfb2cda2SGreg Roach            }
436dfb2cda2SGreg Roach        }
437dfb2cda2SGreg Roach
438dfb2cda2SGreg Roach        if ($spouse_family) {
4390b5fd0a6SGreg Roach            $query->join('link AS l4', static function (JoinClause $join): void {
440dfb2cda2SGreg Roach                $join
441dfb2cda2SGreg Roach                    ->on('l4.l_file', '=', 'individuals.i_file')
442dfb2cda2SGreg Roach                    ->on('l4.l_from', '=', 'individuals.i_id')
443dfb2cda2SGreg Roach                    ->where('l4.l_type', '=', 'FAMS');
444dfb2cda2SGreg Roach            });
4450b5fd0a6SGreg Roach            $query->join('families AS spouse_families', static function (JoinClause $join): void {
446dfb2cda2SGreg Roach                $join
447dfb2cda2SGreg Roach                    ->on('spouse_families.f_file', '=', 'l4.l_file')
448dfb2cda2SGreg Roach                    ->on('spouse_families.f_id', '=', 'l4.l_to');
449dfb2cda2SGreg Roach            });
450dfb2cda2SGreg Roach        }
451dfb2cda2SGreg Roach
452dfb2cda2SGreg Roach        if ($indi_name) {
4530b5fd0a6SGreg Roach            $query->join('name AS individual_name', static function (JoinClause $join): void {
454dfb2cda2SGreg Roach                $join
455dfb2cda2SGreg Roach                    ->on('individual_name.n_file', '=', 'individuals.i_file')
456dfb2cda2SGreg Roach                    ->on('individual_name.n_id', '=', 'individuals.i_id');
457dfb2cda2SGreg Roach            });
458dfb2cda2SGreg Roach        }
459dfb2cda2SGreg Roach
460dfb2cda2SGreg Roach        if ($indi_date) {
4610b5fd0a6SGreg Roach            $query->join('dates AS individual_dates', static function (JoinClause $join): void {
462dfb2cda2SGreg Roach                $join
463dfb2cda2SGreg Roach                    ->on('individual_dates.d_file', '=', 'individuals.i_file')
464dfb2cda2SGreg Roach                    ->on('individual_dates.d_gid', '=', 'individuals.i_id');
465dfb2cda2SGreg Roach            });
466dfb2cda2SGreg Roach        }
467dfb2cda2SGreg Roach
468dfb2cda2SGreg Roach        if ($fam_date) {
4690b5fd0a6SGreg Roach            $query->join('dates AS family_dates', static function (JoinClause $join): void {
470dfb2cda2SGreg Roach                $join
471dfb2cda2SGreg Roach                    ->on('family_dates.d_file', '=', 'spouse_families.f_file')
472dfb2cda2SGreg Roach                    ->on('family_dates.d_gid', '=', 'spouse_families.f_id');
473dfb2cda2SGreg Roach            });
474dfb2cda2SGreg Roach        }
475dfb2cda2SGreg Roach
476dfb2cda2SGreg Roach        if ($indi_plac) {
4770b5fd0a6SGreg Roach            $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void {
478dfb2cda2SGreg Roach                $join
479dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_file', '=', 'individuals.i_file')
480dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id');
481dfb2cda2SGreg Roach            });
4820b5fd0a6SGreg Roach            $query->join('places AS individual_places', static function (JoinClause $join): void {
483dfb2cda2SGreg Roach                $join
484dfb2cda2SGreg Roach                    ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file')
485dfb2cda2SGreg Roach                    ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id');
486dfb2cda2SGreg Roach            });
487dfb2cda2SGreg Roach        }
488dfb2cda2SGreg Roach
489dfb2cda2SGreg Roach        if ($fam_plac) {
4900b5fd0a6SGreg Roach            $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void {
491dfb2cda2SGreg Roach                $join
492dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file')
493dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id');
494dfb2cda2SGreg Roach            });
4950b5fd0a6SGreg Roach            $query->join('places AS family_places', static function (JoinClause $join): void {
496dfb2cda2SGreg Roach                $join
497dfb2cda2SGreg Roach                    ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file')
498dfb2cda2SGreg Roach                    ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id');
499dfb2cda2SGreg Roach            });
500dfb2cda2SGreg Roach        }
501dfb2cda2SGreg Roach
502dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
5033cfcc809SGreg Roach            $parts = explode(':', $field_name . '::::');
504dfb2cda2SGreg Roach            if ($parts[0] === 'NAME') {
505dfb2cda2SGreg Roach                // NAME:*
506dfb2cda2SGreg Roach                switch ($parts[1]) {
507dfb2cda2SGreg Roach                    case 'GIVN':
508dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
509dfb2cda2SGreg Roach                            case 'EXACT':
510dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', '=', $field_value);
511dfb2cda2SGreg Roach                                break;
512dfb2cda2SGreg Roach                            case 'BEGINS':
513dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', $field_value . '%');
514dfb2cda2SGreg Roach                                break;
515dfb2cda2SGreg Roach                            case 'CONTAINS':
516dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
517dfb2cda2SGreg Roach                                break;
518dfb2cda2SGreg Roach                            case 'SDX_STD':
519dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
520dfb2cda2SGreg Roach                                if ($sdx !== '') {
521dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx);
522dfb2cda2SGreg Roach                                } else {
523dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
524dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
525dfb2cda2SGreg Roach                                }
526dfb2cda2SGreg Roach                                break;
527dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
528dfb2cda2SGreg Roach                            case 'SDX_DM':
529dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
530dfb2cda2SGreg Roach                                if ($sdx !== '') {
531dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx);
532dfb2cda2SGreg Roach                                } else {
533dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
534dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
535dfb2cda2SGreg Roach                                }
536dfb2cda2SGreg Roach                                break;
537dfb2cda2SGreg Roach                        }
538dfb2cda2SGreg Roach                        break;
539dfb2cda2SGreg Roach                    case 'SURN':
540dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
541dfb2cda2SGreg Roach                            case 'EXACT':
542dfb2cda2SGreg Roach                                $query->where('individual_name.n_surn', '=', $field_value);
543dfb2cda2SGreg Roach                                break;
544dfb2cda2SGreg Roach                            case 'BEGINS':
545dfb2cda2SGreg Roach                                $query->where('individual_name.n_surn', 'LIKE', $field_value . '%');
546dfb2cda2SGreg Roach                                break;
547dfb2cda2SGreg Roach                            case 'CONTAINS':
548dfb2cda2SGreg Roach                                $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%');
549dfb2cda2SGreg Roach                                break;
550dfb2cda2SGreg Roach                            case 'SDX_STD':
551dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
552dfb2cda2SGreg Roach                                if ($sdx !== '') {
553dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx);
554dfb2cda2SGreg Roach                                } else {
555dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
556dfb2cda2SGreg Roach                                    $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%');
557dfb2cda2SGreg Roach                                }
558dfb2cda2SGreg Roach                                break;
559dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
560dfb2cda2SGreg Roach                            case 'SDX_DM':
561dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
562dfb2cda2SGreg Roach                                if ($sdx !== '') {
563dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx);
564dfb2cda2SGreg Roach                                } else {
565dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
566dfb2cda2SGreg Roach                                    $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%');
567dfb2cda2SGreg Roach                                }
568dfb2cda2SGreg Roach                                break;
569dfb2cda2SGreg Roach                        }
570dfb2cda2SGreg Roach                        break;
571dfb2cda2SGreg Roach                    case 'NICK':
572dfb2cda2SGreg Roach                    case '_MARNM':
573dfb2cda2SGreg Roach                    case '_HEB':
574dfb2cda2SGreg Roach                    case '_AKA':
575dfb2cda2SGreg Roach                        $query
576dfb2cda2SGreg Roach                            ->where('individual_name', '=', $parts[1])
577dfb2cda2SGreg Roach                            ->where('individual_name', 'LIKE', '%' . $field_value . '%');
578dfb2cda2SGreg Roach                        break;
579dfb2cda2SGreg Roach                }
580dfb2cda2SGreg Roach                unset($fields[$field_name]);
581dfb2cda2SGreg Roach            } elseif ($parts[1] === 'DATE') {
582dfb2cda2SGreg Roach                // *:DATE
583dfb2cda2SGreg Roach                $date = new Date($field_value);
584dfb2cda2SGreg Roach                if ($date->isOK()) {
585dfb2cda2SGreg Roach                    $delta = 365 * ($modifiers[$field_name] ?? 0);
586dfb2cda2SGreg Roach                    $query
587dfb2cda2SGreg Roach                        ->where('individual_dates.d_fact', '=', $parts[0])
588dfb2cda2SGreg Roach                        ->where('individual_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
589dfb2cda2SGreg Roach                        ->where('individual_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta);
590dfb2cda2SGreg Roach                }
591dfb2cda2SGreg Roach                unset($fields[$field_name]);
592dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') {
593dfb2cda2SGreg Roach                // FAMS:*:DATE
594dfb2cda2SGreg Roach                $date = new Date($field_value);
595dfb2cda2SGreg Roach                if ($date->isOK()) {
596dfb2cda2SGreg Roach                    $delta = 365 * $modifiers[$field_name];
597dfb2cda2SGreg Roach                    $query
598dfb2cda2SGreg Roach                        ->where('family_dates.d_fact', '=', $parts[1])
599dfb2cda2SGreg Roach                        ->where('family_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
600dfb2cda2SGreg Roach                        ->where('family_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta);
601dfb2cda2SGreg Roach                }
602dfb2cda2SGreg Roach                unset($fields[$field_name]);
603dfb2cda2SGreg Roach            } elseif ($parts[1] === 'PLAC') {
604dfb2cda2SGreg Roach                // *:PLAC
605dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
606dfb2cda2SGreg Roach                $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%');
607dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
608dfb2cda2SGreg Roach                // FAMS:*:PLAC
609dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
610dfb2cda2SGreg Roach                $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%');
611dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') {
612dfb2cda2SGreg Roach                $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name';
613dfb2cda2SGreg Roach                // NAME:*
614dfb2cda2SGreg Roach                switch ($parts[3]) {
615dfb2cda2SGreg Roach                    case 'GIVN':
616dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
617dfb2cda2SGreg Roach                            case 'EXACT':
618dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', '=', $field_value);
619dfb2cda2SGreg Roach                                break;
620dfb2cda2SGreg Roach                            case 'BEGINS':
621dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', $field_value . '%');
622dfb2cda2SGreg Roach                                break;
623dfb2cda2SGreg Roach                            case 'CONTAINS':
624dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
625dfb2cda2SGreg Roach                                break;
626dfb2cda2SGreg Roach                            case 'SDX_STD':
627dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
628dfb2cda2SGreg Roach                                if ($sdx !== '') {
629dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx);
630dfb2cda2SGreg Roach                                } else {
631dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
632dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
633dfb2cda2SGreg Roach                                }
634dfb2cda2SGreg Roach                                break;
635dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
636dfb2cda2SGreg Roach                            case 'SDX_DM':
637dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
638dfb2cda2SGreg Roach                                if ($sdx !== '') {
639dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx);
640dfb2cda2SGreg Roach                                } else {
641dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
642dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
643dfb2cda2SGreg Roach                                }
644dfb2cda2SGreg Roach                                break;
645dfb2cda2SGreg Roach                        }
646dfb2cda2SGreg Roach                        break;
647dfb2cda2SGreg Roach                    case 'SURN':
648dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
649dfb2cda2SGreg Roach                            case 'EXACT':
650dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', '=', $field_value);
651dfb2cda2SGreg Roach                                break;
652dfb2cda2SGreg Roach                            case 'BEGINS':
653dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', $field_value . '%');
654dfb2cda2SGreg Roach                                break;
655dfb2cda2SGreg Roach                            case 'CONTAINS':
656dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
657dfb2cda2SGreg Roach                                break;
658dfb2cda2SGreg Roach                            case 'SDX_STD':
659dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
660dfb2cda2SGreg Roach                                if ($sdx !== '') {
661dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx);
662dfb2cda2SGreg Roach                                } else {
663dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
664dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
665dfb2cda2SGreg Roach                                }
666dfb2cda2SGreg Roach                                break;
667dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
668dfb2cda2SGreg Roach                            case 'SDX_DM':
669dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
670dfb2cda2SGreg Roach                                if ($sdx !== '') {
671dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx);
672dfb2cda2SGreg Roach                                } else {
673dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
674dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
675dfb2cda2SGreg Roach                                }
676dfb2cda2SGreg Roach                                break;
677dfb2cda2SGreg Roach                        }
678dfb2cda2SGreg Roach                        break;
679dfb2cda2SGreg Roach                }
680dfb2cda2SGreg Roach                unset($fields[$field_name]);
681dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS') {
682dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
683dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
684dfb2cda2SGreg Roach                $query->where('families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%');
685dfb2cda2SGreg Roach            } elseif ($parts[1] === 'TYPE') {
686dfb2cda2SGreg Roach                // e.g. FACT:TYPE or EVEN:TYPE
687dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
688dfb2cda2SGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%\n2 TYPE %' . $field_value . '%');
689dfb2cda2SGreg Roach            } else {
690dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
691dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
692c5457947SGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%' . $parts[1] . '%' . $field_value . '%');
693dfb2cda2SGreg Roach            }
694dfb2cda2SGreg Roach        }
695dfb2cda2SGreg Roach        return $query
696dfb2cda2SGreg Roach            ->get()
69752a8ef61SGreg Roach            ->each($this->rowLimiter())
698dfb2cda2SGreg Roach            ->map(Individual::rowMapper())
699dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter())
7000b5fd0a6SGreg Roach            ->filter(static function (Individual $individual) use ($fields): bool {
701c5457947SGreg Roach                // Check for searches which were only partially matched by SQL
702dfb2cda2SGreg Roach                foreach ($fields as $field_name => $field_value) {
703a41e65f0SGreg Roach                    $regex = '/' . preg_quote($field_value, '/') . '/i';
704dfb2cda2SGreg Roach
705c5457947SGreg Roach                    $parts = explode(':', $field_name . '::::');
706dfb2cda2SGreg Roach
707dfb2cda2SGreg Roach                    // *:PLAC
708c5457947SGreg Roach                    if ($parts[1] === 'PLAC') {
709a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
710392561bbSGreg Roach                            if (preg_match($regex, $fact->place()->gedcomName())) {
711c5457947SGreg Roach                                continue 2;
712dfb2cda2SGreg Roach                            }
713a41e65f0SGreg Roach                        }
714c5457947SGreg Roach                        return false;
715c5457947SGreg Roach                    }
716c5457947SGreg Roach
717dfb2cda2SGreg Roach                    // FAMS:*:PLAC
718c5457947SGreg Roach                    if ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
71939ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
720a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
721392561bbSGreg Roach                                if (preg_match($regex, $fact->place()->gedcomName())) {
722c5457947SGreg Roach                                    continue 2;
723a41e65f0SGreg Roach                                }
724dfb2cda2SGreg Roach                            }
725dfb2cda2SGreg Roach                        }
726c5457947SGreg Roach                        return false;
727c5457947SGreg Roach                    }
728c5457947SGreg Roach
729dfb2cda2SGreg Roach                    // e.g. searches for occupation, religion, note, etc.
730c5457947SGreg Roach                    if ($parts[0] === 'FAMS') {
73139ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
732a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
733a41e65f0SGreg Roach                                if (preg_match($regex, $fact->value())) {
734c5457947SGreg Roach                                    continue 3;
735a41e65f0SGreg Roach                                }
736dfb2cda2SGreg Roach                            }
737dfb2cda2SGreg Roach                        }
738c5457947SGreg Roach                        return false;
739c5457947SGreg Roach                    }
740c5457947SGreg Roach
741dfb2cda2SGreg Roach                    // e.g. FACT:TYPE or EVEN:TYPE
742c5457947SGreg Roach                    if ($parts[1] === 'TYPE' || $parts[1] === '_WT_USER') {
743a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
744c5457947SGreg Roach                            if (preg_match($regex, $fact->attribute($parts[1]))) {
745c5457947SGreg Roach                                continue 2;
746dfb2cda2SGreg Roach                            }
747dfb2cda2SGreg Roach                        }
748dfb2cda2SGreg Roach
749dfb2cda2SGreg Roach                        return false;
750dfb2cda2SGreg Roach                    }
751c5457947SGreg Roach                }
752dfb2cda2SGreg Roach
753dfb2cda2SGreg Roach                return true;
754dfb2cda2SGreg Roach            });
755dfb2cda2SGreg Roach    }
756dfb2cda2SGreg Roach
757dfb2cda2SGreg Roach    /**
758dfb2cda2SGreg Roach     * @param string $soundex
759dfb2cda2SGreg Roach     * @param string $lastname
760dfb2cda2SGreg Roach     * @param string $firstname
761dfb2cda2SGreg Roach     * @param string $place
762dfb2cda2SGreg Roach     * @param Tree[] $search_trees
763dfb2cda2SGreg Roach     *
76454c7f8dfSGreg Roach     * @return Collection
765dfb2cda2SGreg Roach     */
766dfb2cda2SGreg Roach    public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection
767dfb2cda2SGreg Roach    {
768dfb2cda2SGreg Roach        switch ($soundex) {
769dfb2cda2SGreg Roach            default:
770dfb2cda2SGreg Roach            case 'Russell':
771dfb2cda2SGreg Roach                $givn_sdx   = Soundex::russell($firstname);
772dfb2cda2SGreg Roach                $surn_sdx   = Soundex::russell($lastname);
773dfb2cda2SGreg Roach                $plac_sdx   = Soundex::russell($place);
774dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_std';
775dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_std';
776dfb2cda2SGreg Roach                $plac_field = 'p_std_soundex';
777dfb2cda2SGreg Roach                break;
778dfb2cda2SGreg Roach            case 'DaitchM':
779dfb2cda2SGreg Roach                $givn_sdx   = Soundex::daitchMokotoff($firstname);
780dfb2cda2SGreg Roach                $surn_sdx   = Soundex::daitchMokotoff($lastname);
781dfb2cda2SGreg Roach                $plac_sdx   = Soundex::daitchMokotoff($place);
782dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_dm';
783dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_dm';
784dfb2cda2SGreg Roach                $plac_field = 'p_dm_soundex';
785dfb2cda2SGreg Roach                break;
786dfb2cda2SGreg Roach        }
787dfb2cda2SGreg Roach
788dfb2cda2SGreg Roach        // Nothing to search for? Return nothing.
789dfb2cda2SGreg Roach        if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') {
7903fda39a7SGreg Roach            return new Collection();
791dfb2cda2SGreg Roach        }
792dfb2cda2SGreg Roach
793dfb2cda2SGreg Roach        $query = DB::table('individuals')
794dfb2cda2SGreg Roach            ->select(['individuals.*'])
795dfb2cda2SGreg Roach            ->distinct();
796dfb2cda2SGreg Roach
797dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $search_trees);
798dfb2cda2SGreg Roach
799dfb2cda2SGreg Roach        if ($plac_sdx !== '') {
8000b5fd0a6SGreg Roach            $query->join('placelinks', static function (JoinClause $join): void {
801dfb2cda2SGreg Roach                $join
802dfb2cda2SGreg Roach                    ->on('placelinks.pl_file', '=', 'individuals.i_file')
803dfb2cda2SGreg Roach                    ->on('placelinks.pl_gid', '=', 'individuals.i_id');
804dfb2cda2SGreg Roach            });
8050b5fd0a6SGreg Roach            $query->join('places', static function (JoinClause $join): void {
806dfb2cda2SGreg Roach                $join
807dfb2cda2SGreg Roach                    ->on('places.p_file', '=', 'placelinks.pl_file')
808dfb2cda2SGreg Roach                    ->on('places.p_id', '=', 'placelinks.pl_p_id');
809dfb2cda2SGreg Roach            });
810dfb2cda2SGreg Roach
811dfb2cda2SGreg Roach            $this->wherePhonetic($query, $plac_field, $plac_sdx);
812dfb2cda2SGreg Roach        }
813dfb2cda2SGreg Roach
814dfb2cda2SGreg Roach        if ($givn_sdx !== '' || $surn_sdx !== '') {
8150b5fd0a6SGreg Roach            $query->join('name', static function (JoinClause $join): void {
816dfb2cda2SGreg Roach                $join
817dfb2cda2SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
818dfb2cda2SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
819dfb2cda2SGreg Roach            });
820dfb2cda2SGreg Roach
821dfb2cda2SGreg Roach            $this->wherePhonetic($query, $givn_field, $givn_sdx);
822dfb2cda2SGreg Roach            $this->wherePhonetic($query, $surn_field, $surn_sdx);
823dfb2cda2SGreg Roach        }
824dfb2cda2SGreg Roach
825dfb2cda2SGreg Roach        return $query
826dfb2cda2SGreg Roach            ->get()
82752a8ef61SGreg Roach            ->each($this->rowLimiter())
828dfb2cda2SGreg Roach            ->map(Individual::rowMapper())
829dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter());
830dfb2cda2SGreg Roach    }
831dfb2cda2SGreg Roach
832dfb2cda2SGreg Roach    /**
83332cd2800SGreg Roach     * Paginate a search query.
83432cd2800SGreg Roach     *
83532cd2800SGreg Roach     * @param Builder $query      Searches the database for the desired records.
83632cd2800SGreg Roach     * @param Closure $row_mapper Converts a row from the query into a record.
837a7a24840SGreg Roach     * @param Closure $row_filter
83832cd2800SGreg Roach     * @param int     $offset     Skip this many rows.
83932cd2800SGreg Roach     * @param int     $limit      Take this many rows.
84032cd2800SGreg Roach     *
84132cd2800SGreg Roach     * @return Collection
84232cd2800SGreg Roach     */
843a7a24840SGreg Roach    private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection
84432cd2800SGreg Roach    {
84532cd2800SGreg Roach        $collection = new Collection();
84632cd2800SGreg Roach
84732cd2800SGreg Roach        foreach ($query->cursor() as $row) {
84832cd2800SGreg Roach            $record = $row_mapper($row);
849b68caec6SGreg Roach            // If the object has a method "canShow()", then use it to filter for privacy.
850a7a24840SGreg Roach            if ($row_filter($record)) {
85132cd2800SGreg Roach                if ($offset > 0) {
85232cd2800SGreg Roach                    $offset--;
85332cd2800SGreg Roach                } else {
85432cd2800SGreg Roach                    if ($limit > 0) {
85532cd2800SGreg Roach                        $collection->push($record);
85632cd2800SGreg Roach                    }
85732cd2800SGreg Roach
85832cd2800SGreg Roach                    $limit--;
85932cd2800SGreg Roach
86032cd2800SGreg Roach                    if ($limit === 0) {
86132cd2800SGreg Roach                        break;
86232cd2800SGreg Roach                    }
86332cd2800SGreg Roach                }
86432cd2800SGreg Roach            }
86532cd2800SGreg Roach        }
86632cd2800SGreg Roach
867e0458bdcSGreg Roach
86832cd2800SGreg Roach        return $collection;
86932cd2800SGreg Roach    }
870a7a24840SGreg Roach
871a7a24840SGreg Roach    /**
872a7a24840SGreg Roach     * Apply search filters to a SQL query column.  Apply collation rules to MySQL.
873a7a24840SGreg Roach     *
874a7a24840SGreg Roach     * @param Builder           $query
875a7a24840SGreg Roach     * @param Expression|string $field
876a7a24840SGreg Roach     * @param string[]          $search_terms
877a7a24840SGreg Roach     */
878a7a24840SGreg Roach    private function whereSearch(Builder $query, $field, array $search_terms): void
879a7a24840SGreg Roach    {
880a7a24840SGreg Roach        if ($field instanceof Expression) {
881a7a24840SGreg Roach            $field = $field->getValue();
882a7a24840SGreg Roach        }
883a7a24840SGreg Roach
884a7a24840SGreg Roach        foreach ($search_terms as $search_term) {
885a69f5655SGreg Roach            $query->whereContains(new Expression($field), $search_term);
886a7a24840SGreg Roach        }
887a7a24840SGreg Roach    }
888a7a24840SGreg Roach
889a7a24840SGreg Roach    /**
8902d686e68SGreg Roach     * Apply soundex search filters to a SQL query column.
8912d686e68SGreg Roach     *
8922d686e68SGreg Roach     * @param Builder           $query
8932d686e68SGreg Roach     * @param Expression|string $field
8942d686e68SGreg Roach     * @param string            $soundex
8952d686e68SGreg Roach     */
8962d686e68SGreg Roach    private function wherePhonetic(Builder $query, $field, string $soundex): void
8972d686e68SGreg Roach    {
8982d686e68SGreg Roach        if ($soundex !== '') {
8990b5fd0a6SGreg Roach            $query->where(static function (Builder $query) use ($soundex, $field): void {
9002d686e68SGreg Roach                foreach (explode(':', $soundex) as $sdx) {
9012d686e68SGreg Roach                    $query->orWhere($field, 'LIKE', '%' . $sdx . '%');
9022d686e68SGreg Roach                }
9032d686e68SGreg Roach            });
9042d686e68SGreg Roach        }
9052d686e68SGreg Roach    }
9062d686e68SGreg Roach
9072d686e68SGreg Roach    /**
908a7a24840SGreg Roach     * @param Builder $query
909a7a24840SGreg Roach     * @param string  $tree_id_field
910a7a24840SGreg Roach     * @param Tree[]  $trees
911a7a24840SGreg Roach     */
912a7a24840SGreg Roach    private function whereTrees(Builder $query, string $tree_id_field, array $trees): void
913a7a24840SGreg Roach    {
9140b5fd0a6SGreg Roach        $tree_ids = array_map(static function (Tree $tree): int {
915a7a24840SGreg Roach            return $tree->id();
916a7a24840SGreg Roach        }, $trees);
917a7a24840SGreg Roach
918a7a24840SGreg Roach        $query->whereIn($tree_id_field, $tree_ids);
919a7a24840SGreg Roach    }
920a7a24840SGreg Roach
921a7a24840SGreg Roach    /**
922a7a24840SGreg Roach     * A closure to filter records by privacy-filtered GEDCOM data.
923a7a24840SGreg Roach     *
924a7a24840SGreg Roach     * @param array $search_terms
925a7a24840SGreg Roach     *
926a7a24840SGreg Roach     * @return Closure
927a7a24840SGreg Roach     */
928a7a24840SGreg Roach    private function rawGedcomFilter(array $search_terms): Closure
929a7a24840SGreg Roach    {
9306c2179e2SGreg Roach        return static function (GedcomRecord $record) use ($search_terms): bool {
931a7a24840SGreg Roach            // Ignore non-genealogy fields
932a7a24840SGreg Roach            $gedcom = preg_replace('/\n\d (?:_UID) .*/', '', $record->gedcom());
933a7a24840SGreg Roach
934a7a24840SGreg Roach            // Ignore matches in links
935a7a24840SGreg Roach            $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom);
936a7a24840SGreg Roach
937a7a24840SGreg Roach            // Re-apply the filtering
938a7a24840SGreg Roach            foreach ($search_terms as $search_term) {
939a7a24840SGreg Roach                if (mb_stripos($gedcom, $search_term) === false) {
940a7a24840SGreg Roach                    return false;
941a7a24840SGreg Roach                }
942a7a24840SGreg Roach            }
943a7a24840SGreg Roach
944a7a24840SGreg Roach            return true;
945a7a24840SGreg Roach        };
946a7a24840SGreg Roach    }
94752a8ef61SGreg Roach
94852a8ef61SGreg Roach    /**
94952a8ef61SGreg Roach     * Searching for short or common text can give more results than the system can process.
95052a8ef61SGreg Roach     *
95152a8ef61SGreg Roach     * @param int $limit
95252a8ef61SGreg Roach     *
95352a8ef61SGreg Roach     * @return Closure
95452a8ef61SGreg Roach     */
95552a8ef61SGreg Roach    private function rowLimiter(int $limit = 1000): Closure
95652a8ef61SGreg Roach    {
9576c2179e2SGreg Roach        return static function () use ($limit): void {
95852a8ef61SGreg Roach            static $n = 0;
95952a8ef61SGreg Roach
96052a8ef61SGreg Roach            if (++$n > $limit) {
96152a8ef61SGreg Roach                $message = I18N::translate('The search returned too many results.');
96252a8ef61SGreg Roach
96352a8ef61SGreg Roach                throw new InternalServerErrorException($message);
96452a8ef61SGreg Roach            }
96552a8ef61SGreg Roach        };
96652a8ef61SGreg Roach    }
96732cd2800SGreg Roach}
968