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 */ 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; 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; 36b5c8fd7eSGreg Roachuse Fisharebest\Webtrees\Submitter; 3732cd2800SGreg Roachuse Fisharebest\Webtrees\Tree; 3832cd2800SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 3932cd2800SGreg Roachuse Illuminate\Database\Query\Builder; 40a7a24840SGreg Roachuse Illuminate\Database\Query\Expression; 4132cd2800SGreg Roachuse Illuminate\Database\Query\JoinClause; 4232cd2800SGreg Roachuse Illuminate\Support\Collection; 4332cd2800SGreg Roachuse stdClass; 443976b470SGreg Roach 45a7a24840SGreg Roachuse function mb_stripos; 4632cd2800SGreg Roach 4732cd2800SGreg Roach/** 4832cd2800SGreg Roach * Search trees for genealogy records. 4932cd2800SGreg Roach */ 5032cd2800SGreg Roachclass SearchService 5132cd2800SGreg Roach{ 52d5ad3db0SGreg Roach /** @var TreeService */ 53d5ad3db0SGreg Roach private $tree_service; 54d5ad3db0SGreg Roach 55d5ad3db0SGreg Roach /** 56d5ad3db0SGreg Roach * SearchService constructor. 57d5ad3db0SGreg Roach * 58d5ad3db0SGreg Roach * @param TreeService $tree_service 59d5ad3db0SGreg Roach */ 60d5ad3db0SGreg Roach public function __construct(TreeService $tree_service) 61d5ad3db0SGreg Roach { 62d5ad3db0SGreg Roach $this->tree_service = $tree_service; 63d5ad3db0SGreg Roach } 64d5ad3db0SGreg Roach 65a7a24840SGreg Roach /** 66a7a24840SGreg Roach * @param Tree[] $trees 67a7a24840SGreg Roach * @param string[] $search 68a7a24840SGreg Roach * 69b5c8fd7eSGreg Roach * @return Collection<Family> 70a7a24840SGreg Roach */ 71a7a24840SGreg Roach public function searchFamilies(array $trees, array $search): Collection 72a7a24840SGreg Roach { 73a7a24840SGreg Roach $query = DB::table('families'); 74a7a24840SGreg Roach 75a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 76a7a24840SGreg Roach $this->whereSearch($query, 'f_gedcom', $search); 77a7a24840SGreg Roach 78a7a24840SGreg Roach return $query 79a7a24840SGreg Roach ->get() 8052a8ef61SGreg Roach ->each($this->rowLimiter()) 81d5ad3db0SGreg Roach ->map($this->familyRowMapper()) 82a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 837f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 84a7a24840SGreg Roach } 85a7a24840SGreg Roach 8632cd2800SGreg Roach /** 8732cd2800SGreg Roach * Search for families by name. 8832cd2800SGreg Roach * 89a7a24840SGreg Roach * @param Tree[] $trees 90a7a24840SGreg Roach * @param string[] $search 9132cd2800SGreg Roach * @param int $offset 9232cd2800SGreg Roach * @param int $limit 9332cd2800SGreg Roach * 94b5c8fd7eSGreg Roach * @return Collection<Family> 9532cd2800SGreg Roach */ 96a7a24840SGreg Roach public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 9732cd2800SGreg Roach { 9832cd2800SGreg Roach $query = DB::table('families') 99ac499332SGreg Roach ->leftJoin('name AS husb_name', static function (JoinClause $join): void { 10032cd2800SGreg Roach $join 10132cd2800SGreg Roach ->on('husb_name.n_file', '=', 'families.f_file') 102ac499332SGreg Roach ->on('husb_name.n_id', '=', 'families.f_husb') 103ac499332SGreg Roach ->where('husb_name.n_type', '<>', '_MARNM'); 10432cd2800SGreg Roach }) 105ac499332SGreg Roach ->leftJoin('name AS wife_name', static function (JoinClause $join): void { 10632cd2800SGreg Roach $join 10732cd2800SGreg Roach ->on('wife_name.n_file', '=', 'families.f_file') 108ac499332SGreg Roach ->on('wife_name.n_id', '=', 'families.f_wife') 109ac499332SGreg Roach ->where('wife_name.n_type', '<>', '_MARNM'); 110ac499332SGreg Roach }); 111a7a24840SGreg Roach 112a7a24840SGreg Roach $prefix = DB::connection()->getTablePrefix(); 113a69f5655SGreg Roach $field = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')"); 114a7a24840SGreg Roach 115a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 116a7a24840SGreg Roach $this->whereSearch($query, $field, $search); 117a7a24840SGreg Roach 118a7a24840SGreg Roach $query 11932cd2800SGreg Roach ->orderBy('husb_name.n_sort') 12032cd2800SGreg Roach ->orderBy('wife_name.n_sort') 121c0804649SGreg Roach ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort']) 12232cd2800SGreg Roach ->distinct(); 12332cd2800SGreg Roach 124d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 125a7a24840SGreg Roach } 126a7a24840SGreg Roach 127a7a24840SGreg Roach /** 128a7a24840SGreg Roach * @param Tree[] $trees 129a7a24840SGreg Roach * @param string[] $search 130a7a24840SGreg Roach * 131b5c8fd7eSGreg Roach * @return Collection<Individual> 132a7a24840SGreg Roach */ 133a7a24840SGreg Roach public function searchIndividuals(array $trees, array $search): Collection 134a7a24840SGreg Roach { 135a7a24840SGreg Roach $query = DB::table('individuals'); 136a7a24840SGreg Roach 137a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 138a7a24840SGreg Roach $this->whereSearch($query, 'i_gedcom', $search); 139a7a24840SGreg Roach 140a7a24840SGreg Roach return $query 141a7a24840SGreg Roach ->get() 14252a8ef61SGreg Roach ->each($this->rowLimiter()) 143d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 144a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 1457f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 14632cd2800SGreg Roach } 14732cd2800SGreg Roach 14832cd2800SGreg Roach /** 14932cd2800SGreg Roach * Search for individuals by name. 15032cd2800SGreg Roach * 151a7a24840SGreg Roach * @param Tree[] $trees 152a7a24840SGreg Roach * @param string[] $search 15332cd2800SGreg Roach * @param int $offset 15432cd2800SGreg Roach * @param int $limit 15532cd2800SGreg Roach * 156b5c8fd7eSGreg Roach * @return Collection<Individual> 15732cd2800SGreg Roach */ 158a7a24840SGreg Roach public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 15932cd2800SGreg Roach { 16032cd2800SGreg Roach $query = DB::table('individuals') 1610b5fd0a6SGreg Roach ->join('name', static function (JoinClause $join): void { 16232cd2800SGreg Roach $join 16332cd2800SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 164a7a24840SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 16532cd2800SGreg Roach }) 166e84cf2deSGreg Roach ->orderBy('n_sort') 167d78d61f7SGreg Roach ->distinct() 168685de081SGreg Roach ->select(['individuals.*', 'n_sort']); 16932cd2800SGreg Roach 170a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 171a7a24840SGreg Roach $this->whereSearch($query, 'n_full', $search); 172a7a24840SGreg Roach 173d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 17432cd2800SGreg Roach } 17532cd2800SGreg Roach 17632cd2800SGreg Roach /** 17732cd2800SGreg Roach * Search for media objects. 17832cd2800SGreg Roach * 179a7a24840SGreg Roach * @param Tree[] $trees 180a7a24840SGreg Roach * @param string[] $search 18132cd2800SGreg Roach * @param int $offset 18232cd2800SGreg Roach * @param int $limit 18332cd2800SGreg Roach * 184b5c8fd7eSGreg Roach * @return Collection<Media> 18532cd2800SGreg Roach */ 186a7a24840SGreg Roach public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 18732cd2800SGreg Roach { 188a7a24840SGreg Roach $query = DB::table('media'); 18932cd2800SGreg Roach 190a7a24840SGreg Roach $this->whereTrees($query, 'media.m_file', $trees); 191a7a24840SGreg Roach $this->whereSearch($query, 'm_gedcom', $search); 192a7a24840SGreg Roach 193d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 19432cd2800SGreg Roach } 19532cd2800SGreg Roach 19632cd2800SGreg Roach /** 19732cd2800SGreg Roach * Search for notes. 19832cd2800SGreg Roach * 199a7a24840SGreg Roach * @param Tree[] $trees 200a7a24840SGreg Roach * @param string[] $search 20132cd2800SGreg Roach * @param int $offset 20232cd2800SGreg Roach * @param int $limit 20332cd2800SGreg Roach * 204b5c8fd7eSGreg Roach * @return Collection<Note> 20532cd2800SGreg Roach */ 206a7a24840SGreg Roach public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 20732cd2800SGreg Roach { 20832cd2800SGreg Roach $query = DB::table('other') 209a7a24840SGreg Roach ->where('o_type', '=', 'NOTE'); 21032cd2800SGreg Roach 211a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 212a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 213a7a24840SGreg Roach 214d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 21532cd2800SGreg Roach } 21632cd2800SGreg Roach 21732cd2800SGreg Roach /** 21832cd2800SGreg Roach * Search for repositories. 21932cd2800SGreg Roach * 220a7a24840SGreg Roach * @param Tree[] $trees 221a7a24840SGreg Roach * @param string[] $search 22232cd2800SGreg Roach * @param int $offset 22332cd2800SGreg Roach * @param int $limit 22432cd2800SGreg Roach * 225b5c8fd7eSGreg Roach * @return Collection<Repository> 22632cd2800SGreg Roach */ 227a7a24840SGreg Roach public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 22832cd2800SGreg Roach { 22932cd2800SGreg Roach $query = DB::table('other') 230a7a24840SGreg Roach ->where('o_type', '=', 'REPO'); 23132cd2800SGreg Roach 232a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 233a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 234a7a24840SGreg Roach 235d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 23632cd2800SGreg Roach } 23732cd2800SGreg Roach 23832cd2800SGreg Roach /** 239a7a24840SGreg Roach * Search for sources. 24032cd2800SGreg Roach * 241a7a24840SGreg Roach * @param Tree[] $trees 242a7a24840SGreg Roach * @param string[] $search 24332cd2800SGreg Roach * @param int $offset 24432cd2800SGreg Roach * @param int $limit 24532cd2800SGreg Roach * 246b5c8fd7eSGreg Roach * @return Collection<Source> 24732cd2800SGreg Roach */ 248a7a24840SGreg Roach public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 249a7a24840SGreg Roach { 250a7a24840SGreg Roach $query = DB::table('sources'); 251a7a24840SGreg Roach 252a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 253a7a24840SGreg Roach $this->whereSearch($query, 's_gedcom', $search); 254a7a24840SGreg Roach 255d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 256a7a24840SGreg Roach } 257a7a24840SGreg Roach 258a7a24840SGreg Roach /** 259a7a24840SGreg Roach * Search for sources by name. 260a7a24840SGreg Roach * 261a7a24840SGreg Roach * @param Tree[] $trees 262a7a24840SGreg Roach * @param string[] $search 263a7a24840SGreg Roach * @param int $offset 264a7a24840SGreg Roach * @param int $limit 265a7a24840SGreg Roach * 266b5c8fd7eSGreg Roach * @return Collection<Source> 267a7a24840SGreg Roach */ 268a7a24840SGreg Roach public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 26932cd2800SGreg Roach { 27032cd2800SGreg Roach $query = DB::table('sources') 271c0804649SGreg Roach ->orderBy('s_name'); 27232cd2800SGreg Roach 273a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 274a7a24840SGreg Roach $this->whereSearch($query, 's_name', $search); 275a7a24840SGreg Roach 276d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 27732cd2800SGreg Roach } 27832cd2800SGreg Roach 27932cd2800SGreg Roach /** 28032cd2800SGreg Roach * Search for submitters. 28132cd2800SGreg Roach * 282a7a24840SGreg Roach * @param Tree[] $trees 283a7a24840SGreg Roach * @param string[] $search 28432cd2800SGreg Roach * @param int $offset 28532cd2800SGreg Roach * @param int $limit 28632cd2800SGreg Roach * 287b5c8fd7eSGreg Roach * @return Collection<Submitter> 28832cd2800SGreg Roach */ 289a7a24840SGreg Roach public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 29032cd2800SGreg Roach { 29132cd2800SGreg Roach $query = DB::table('other') 292a7a24840SGreg Roach ->where('o_type', '=', 'SUBM'); 29332cd2800SGreg Roach 294a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 295a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 296a7a24840SGreg Roach 297d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 29832cd2800SGreg Roach } 29932cd2800SGreg Roach 30032cd2800SGreg Roach /** 301b68caec6SGreg Roach * Search for places. 302b68caec6SGreg Roach * 303b68caec6SGreg Roach * @param Tree $tree 304b68caec6SGreg Roach * @param string $search 305b68caec6SGreg Roach * @param int $offset 306b68caec6SGreg Roach * @param int $limit 307b68caec6SGreg Roach * 308b5c8fd7eSGreg Roach * @return Collection<Place> 309b68caec6SGreg Roach */ 310b68caec6SGreg Roach public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 311b68caec6SGreg Roach { 312b68caec6SGreg Roach $query = DB::table('places AS p0') 313b68caec6SGreg Roach ->where('p0.p_file', '=', $tree->id()) 314b68caec6SGreg Roach ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id') 315b68caec6SGreg Roach ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id') 316b68caec6SGreg Roach ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id') 317b68caec6SGreg Roach ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id') 318b68caec6SGreg Roach ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id') 319b68caec6SGreg Roach ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id') 320b68caec6SGreg Roach ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id') 321b68caec6SGreg Roach ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id') 322b68caec6SGreg Roach ->orderBy('p0.p_place') 323b68caec6SGreg Roach ->orderBy('p1.p_place') 324b68caec6SGreg Roach ->orderBy('p2.p_place') 325b68caec6SGreg Roach ->orderBy('p3.p_place') 326b68caec6SGreg Roach ->orderBy('p4.p_place') 327b68caec6SGreg Roach ->orderBy('p5.p_place') 328b68caec6SGreg Roach ->orderBy('p6.p_place') 329b68caec6SGreg Roach ->orderBy('p7.p_place') 330b68caec6SGreg Roach ->orderBy('p8.p_place') 331b68caec6SGreg Roach ->select([ 332b68caec6SGreg Roach 'p0.p_place AS place0', 333b68caec6SGreg Roach 'p1.p_place AS place1', 334b68caec6SGreg Roach 'p2.p_place AS place2', 335b68caec6SGreg Roach 'p3.p_place AS place3', 336b68caec6SGreg Roach 'p4.p_place AS place4', 337b68caec6SGreg Roach 'p5.p_place AS place5', 338b68caec6SGreg Roach 'p6.p_place AS place6', 339b68caec6SGreg Roach 'p7.p_place AS place7', 340b68caec6SGreg Roach 'p8.p_place AS place8', 341b68caec6SGreg Roach ]); 342b68caec6SGreg Roach 343b68caec6SGreg Roach // Filter each level of the hierarchy. 344b68caec6SGreg Roach foreach (explode(',', $search, 9) as $level => $string) { 345b68caec6SGreg Roach $query->whereContains('p' . $level . '.p_place', $string); 346b68caec6SGreg Roach } 347b68caec6SGreg Roach 3486c2179e2SGreg Roach $row_mapper = static function (stdClass $row) use ($tree): Place { 349b68caec6SGreg Roach $place = implode(', ', array_filter((array) $row)); 350b68caec6SGreg Roach 351b68caec6SGreg Roach return new Place($place, $tree); 352b68caec6SGreg Roach }; 353b68caec6SGreg Roach 3546c2179e2SGreg Roach $filter = static function (): bool { 355a7a24840SGreg Roach return true; 356a7a24840SGreg Roach }; 357b68caec6SGreg Roach 358a7a24840SGreg Roach return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit); 359a7a24840SGreg Roach } 360b68caec6SGreg Roach 361b68caec6SGreg Roach /** 362a5dbc5b2SGreg Roach * @param Tree[] $trees 363dfb2cda2SGreg Roach * @param string[] $fields 364dfb2cda2SGreg Roach * @param string[] $modifiers 365dfb2cda2SGreg Roach * 366b5c8fd7eSGreg Roach * @return Collection<Individual> 367dfb2cda2SGreg Roach */ 368dfb2cda2SGreg Roach public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection 369dfb2cda2SGreg Roach { 370dfb2cda2SGreg Roach $fields = array_filter($fields); 371dfb2cda2SGreg Roach 372dfb2cda2SGreg Roach $query = DB::table('individuals') 373dfb2cda2SGreg Roach ->select(['individuals.*']) 374dfb2cda2SGreg Roach ->distinct(); 375dfb2cda2SGreg Roach 376dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $trees); 377dfb2cda2SGreg Roach 378dfb2cda2SGreg Roach // Join the following tables 379dfb2cda2SGreg Roach $father_name = false; 380dfb2cda2SGreg Roach $mother_name = false; 381dfb2cda2SGreg Roach $spouse_family = false; 382dfb2cda2SGreg Roach $indi_name = false; 383dfb2cda2SGreg Roach $indi_date = false; 384dfb2cda2SGreg Roach $fam_date = false; 385dfb2cda2SGreg Roach $indi_plac = false; 386dfb2cda2SGreg Roach $fam_plac = false; 387dfb2cda2SGreg Roach 388dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 389dfb2cda2SGreg Roach if ($field_value !== '') { 390dfb2cda2SGreg Roach if (substr($field_name, 0, 14) === 'FAMC:HUSB:NAME') { 391dfb2cda2SGreg Roach $father_name = true; 392dfb2cda2SGreg Roach } elseif (substr($field_name, 0, 14) === 'FAMC:WIFE:NAME') { 393dfb2cda2SGreg Roach $mother_name = true; 394dfb2cda2SGreg Roach } elseif (substr($field_name, 0, 4) === 'NAME') { 395dfb2cda2SGreg Roach $indi_name = true; 396dfb2cda2SGreg Roach } elseif (strpos($field_name, ':DATE') !== false) { 397dfb2cda2SGreg Roach if (substr($field_name, 0, 4) === 'FAMS') { 398dfb2cda2SGreg Roach $fam_date = true; 399dfb2cda2SGreg Roach $spouse_family = true; 400dfb2cda2SGreg Roach } else { 401dfb2cda2SGreg Roach $indi_date = true; 402dfb2cda2SGreg Roach } 403dfb2cda2SGreg Roach } elseif (strpos($field_name, ':PLAC') !== false) { 404dfb2cda2SGreg Roach if (substr($field_name, 0, 4) === 'FAMS') { 405dfb2cda2SGreg Roach $fam_plac = true; 406dfb2cda2SGreg Roach $spouse_family = true; 407dfb2cda2SGreg Roach } else { 408dfb2cda2SGreg Roach $indi_plac = true; 409dfb2cda2SGreg Roach } 410dfb2cda2SGreg Roach } elseif ($field_name === 'FAMS:NOTE') { 411dfb2cda2SGreg Roach $spouse_family = true; 412dfb2cda2SGreg Roach } 413dfb2cda2SGreg Roach } 414dfb2cda2SGreg Roach } 415dfb2cda2SGreg Roach 416dfb2cda2SGreg Roach if ($father_name || $mother_name) { 4170b5fd0a6SGreg Roach $query->join('link AS l1', static function (JoinClause $join): void { 418dfb2cda2SGreg Roach $join 419dfb2cda2SGreg Roach ->on('l1.l_file', '=', 'individuals.i_file') 420dfb2cda2SGreg Roach ->on('l1.l_from', '=', 'individuals.i_id') 421dfb2cda2SGreg Roach ->where('l1.l_type', '=', 'FAMC'); 422dfb2cda2SGreg Roach }); 423dfb2cda2SGreg Roach 424dfb2cda2SGreg Roach if ($father_name) { 4250b5fd0a6SGreg Roach $query->join('link AS l2', static function (JoinClause $join): void { 426dfb2cda2SGreg Roach $join 427dfb2cda2SGreg Roach ->on('l2.l_file', '=', 'l1.l_file') 428dfb2cda2SGreg Roach ->on('l2.l_from', '=', 'l1.l_to') 429dfb2cda2SGreg Roach ->where('l2.l_type', '=', 'HUSB'); 430dfb2cda2SGreg Roach }); 4310b5fd0a6SGreg Roach $query->join('name AS father_name', static function (JoinClause $join): void { 432dfb2cda2SGreg Roach $join 433dfb2cda2SGreg Roach ->on('father_name.n_file', '=', 'l2.l_file') 434dfb2cda2SGreg Roach ->on('father_name.n_id', '=', 'l2.l_to'); 435dfb2cda2SGreg Roach }); 436dfb2cda2SGreg Roach } 437dfb2cda2SGreg Roach 438dfb2cda2SGreg Roach if ($mother_name) { 4390b5fd0a6SGreg Roach $query->join('link AS l3', static function (JoinClause $join): void { 440dfb2cda2SGreg Roach $join 441dfb2cda2SGreg Roach ->on('l3.l_file', '=', 'l1.l_file') 442dfb2cda2SGreg Roach ->on('l3.l_from', '=', 'l1.l_to') 443dfb2cda2SGreg Roach ->where('l3.l_type', '=', 'WIFE'); 444dfb2cda2SGreg Roach }); 4450b5fd0a6SGreg Roach $query->join('name AS mother_name', static function (JoinClause $join): void { 446dfb2cda2SGreg Roach $join 447dfb2cda2SGreg Roach ->on('mother_name.n_file', '=', 'l3.l_file') 448dfb2cda2SGreg Roach ->on('mother_name.n_id', '=', 'l3.l_to'); 449dfb2cda2SGreg Roach }); 450dfb2cda2SGreg Roach } 451dfb2cda2SGreg Roach } 452dfb2cda2SGreg Roach 453dfb2cda2SGreg Roach if ($spouse_family) { 4540b5fd0a6SGreg Roach $query->join('link AS l4', static function (JoinClause $join): void { 455dfb2cda2SGreg Roach $join 456dfb2cda2SGreg Roach ->on('l4.l_file', '=', 'individuals.i_file') 457dfb2cda2SGreg Roach ->on('l4.l_from', '=', 'individuals.i_id') 458dfb2cda2SGreg Roach ->where('l4.l_type', '=', 'FAMS'); 459dfb2cda2SGreg Roach }); 4600b5fd0a6SGreg Roach $query->join('families AS spouse_families', static function (JoinClause $join): void { 461dfb2cda2SGreg Roach $join 462dfb2cda2SGreg Roach ->on('spouse_families.f_file', '=', 'l4.l_file') 463dfb2cda2SGreg Roach ->on('spouse_families.f_id', '=', 'l4.l_to'); 464dfb2cda2SGreg Roach }); 465dfb2cda2SGreg Roach } 466dfb2cda2SGreg Roach 467dfb2cda2SGreg Roach if ($indi_name) { 4680b5fd0a6SGreg Roach $query->join('name AS individual_name', static function (JoinClause $join): void { 469dfb2cda2SGreg Roach $join 470dfb2cda2SGreg Roach ->on('individual_name.n_file', '=', 'individuals.i_file') 471dfb2cda2SGreg Roach ->on('individual_name.n_id', '=', 'individuals.i_id'); 472dfb2cda2SGreg Roach }); 473dfb2cda2SGreg Roach } 474dfb2cda2SGreg Roach 475dfb2cda2SGreg Roach if ($indi_date) { 4760b5fd0a6SGreg Roach $query->join('dates AS individual_dates', static function (JoinClause $join): void { 477dfb2cda2SGreg Roach $join 478dfb2cda2SGreg Roach ->on('individual_dates.d_file', '=', 'individuals.i_file') 479dfb2cda2SGreg Roach ->on('individual_dates.d_gid', '=', 'individuals.i_id'); 480dfb2cda2SGreg Roach }); 481dfb2cda2SGreg Roach } 482dfb2cda2SGreg Roach 483dfb2cda2SGreg Roach if ($fam_date) { 4840b5fd0a6SGreg Roach $query->join('dates AS family_dates', static function (JoinClause $join): void { 485dfb2cda2SGreg Roach $join 486dfb2cda2SGreg Roach ->on('family_dates.d_file', '=', 'spouse_families.f_file') 487dfb2cda2SGreg Roach ->on('family_dates.d_gid', '=', 'spouse_families.f_id'); 488dfb2cda2SGreg Roach }); 489dfb2cda2SGreg Roach } 490dfb2cda2SGreg Roach 491dfb2cda2SGreg Roach if ($indi_plac) { 4920b5fd0a6SGreg Roach $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { 493dfb2cda2SGreg Roach $join 494dfb2cda2SGreg Roach ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') 495dfb2cda2SGreg Roach ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); 496dfb2cda2SGreg Roach }); 4970b5fd0a6SGreg Roach $query->join('places AS individual_places', static function (JoinClause $join): void { 498dfb2cda2SGreg Roach $join 499dfb2cda2SGreg Roach ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') 500dfb2cda2SGreg Roach ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); 501dfb2cda2SGreg Roach }); 502dfb2cda2SGreg Roach } 503dfb2cda2SGreg Roach 504dfb2cda2SGreg Roach if ($fam_plac) { 5050b5fd0a6SGreg Roach $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void { 506dfb2cda2SGreg Roach $join 507dfb2cda2SGreg Roach ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file') 508dfb2cda2SGreg Roach ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id'); 509dfb2cda2SGreg Roach }); 5100b5fd0a6SGreg Roach $query->join('places AS family_places', static function (JoinClause $join): void { 511dfb2cda2SGreg Roach $join 512dfb2cda2SGreg Roach ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file') 513dfb2cda2SGreg Roach ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id'); 514dfb2cda2SGreg Roach }); 515dfb2cda2SGreg Roach } 516dfb2cda2SGreg Roach 517dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 5183cfcc809SGreg Roach $parts = explode(':', $field_name . '::::'); 519dfb2cda2SGreg Roach if ($parts[0] === 'NAME') { 520dfb2cda2SGreg Roach // NAME:* 521dfb2cda2SGreg Roach switch ($parts[1]) { 522dfb2cda2SGreg Roach case 'GIVN': 523dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 524dfb2cda2SGreg Roach case 'EXACT': 525dfb2cda2SGreg Roach $query->where('individual_name.n_givn', '=', $field_value); 526dfb2cda2SGreg Roach break; 527dfb2cda2SGreg Roach case 'BEGINS': 528dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', $field_value . '%'); 529dfb2cda2SGreg Roach break; 530dfb2cda2SGreg Roach case 'CONTAINS': 531dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 532dfb2cda2SGreg Roach break; 533dfb2cda2SGreg Roach case 'SDX_STD': 534dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 535dfb2cda2SGreg Roach if ($sdx !== '') { 536dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx); 537dfb2cda2SGreg Roach } else { 538dfb2cda2SGreg Roach // No phonetic content? Use a substring match 539dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 540dfb2cda2SGreg Roach } 541dfb2cda2SGreg Roach break; 542dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 543dfb2cda2SGreg Roach case 'SDX_DM': 544dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 545dfb2cda2SGreg Roach if ($sdx !== '') { 546dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx); 547dfb2cda2SGreg Roach } else { 548dfb2cda2SGreg Roach // No phonetic content? Use a substring match 549dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 550dfb2cda2SGreg Roach } 551dfb2cda2SGreg Roach break; 552dfb2cda2SGreg Roach } 553dfb2cda2SGreg Roach break; 554dfb2cda2SGreg Roach case 'SURN': 555dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 556dfb2cda2SGreg Roach case 'EXACT': 557dfb2cda2SGreg Roach $query->where('individual_name.n_surn', '=', $field_value); 558dfb2cda2SGreg Roach break; 559dfb2cda2SGreg Roach case 'BEGINS': 560dfb2cda2SGreg Roach $query->where('individual_name.n_surn', 'LIKE', $field_value . '%'); 561dfb2cda2SGreg Roach break; 562dfb2cda2SGreg Roach case 'CONTAINS': 563dfb2cda2SGreg Roach $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%'); 564dfb2cda2SGreg Roach break; 565dfb2cda2SGreg Roach case 'SDX_STD': 566dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 567dfb2cda2SGreg Roach if ($sdx !== '') { 568dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); 569dfb2cda2SGreg Roach } else { 570dfb2cda2SGreg Roach // No phonetic content? Use a substring match 571dfb2cda2SGreg Roach $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%'); 572dfb2cda2SGreg Roach } 573dfb2cda2SGreg Roach break; 574dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 575dfb2cda2SGreg Roach case 'SDX_DM': 576dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 577dfb2cda2SGreg Roach if ($sdx !== '') { 578dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); 579dfb2cda2SGreg Roach } else { 580dfb2cda2SGreg Roach // No phonetic content? Use a substring match 581dfb2cda2SGreg Roach $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%'); 582dfb2cda2SGreg Roach } 583dfb2cda2SGreg Roach break; 584dfb2cda2SGreg Roach } 585dfb2cda2SGreg Roach break; 586dfb2cda2SGreg Roach case 'NICK': 587dfb2cda2SGreg Roach case '_MARNM': 588dfb2cda2SGreg Roach case '_HEB': 589dfb2cda2SGreg Roach case '_AKA': 590dfb2cda2SGreg Roach $query 591dfb2cda2SGreg Roach ->where('individual_name', '=', $parts[1]) 592dfb2cda2SGreg Roach ->where('individual_name', 'LIKE', '%' . $field_value . '%'); 593dfb2cda2SGreg Roach break; 594dfb2cda2SGreg Roach } 595dfb2cda2SGreg Roach unset($fields[$field_name]); 596dfb2cda2SGreg Roach } elseif ($parts[1] === 'DATE') { 597dfb2cda2SGreg Roach // *:DATE 598dfb2cda2SGreg Roach $date = new Date($field_value); 599dfb2cda2SGreg Roach if ($date->isOK()) { 600dfb2cda2SGreg Roach $delta = 365 * ($modifiers[$field_name] ?? 0); 601dfb2cda2SGreg Roach $query 602dfb2cda2SGreg Roach ->where('individual_dates.d_fact', '=', $parts[0]) 603dfb2cda2SGreg Roach ->where('individual_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 604dfb2cda2SGreg Roach ->where('individual_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta); 605dfb2cda2SGreg Roach } 606dfb2cda2SGreg Roach unset($fields[$field_name]); 607dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') { 608dfb2cda2SGreg Roach // FAMS:*:DATE 609dfb2cda2SGreg Roach $date = new Date($field_value); 610dfb2cda2SGreg Roach if ($date->isOK()) { 611dfb2cda2SGreg Roach $delta = 365 * $modifiers[$field_name]; 612dfb2cda2SGreg Roach $query 613dfb2cda2SGreg Roach ->where('family_dates.d_fact', '=', $parts[1]) 614dfb2cda2SGreg Roach ->where('family_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 615dfb2cda2SGreg Roach ->where('family_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta); 616dfb2cda2SGreg Roach } 617dfb2cda2SGreg Roach unset($fields[$field_name]); 618dfb2cda2SGreg Roach } elseif ($parts[1] === 'PLAC') { 619dfb2cda2SGreg Roach // *:PLAC 620dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 621dfb2cda2SGreg Roach $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%'); 622dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') { 623dfb2cda2SGreg Roach // FAMS:*:PLAC 624dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 625dfb2cda2SGreg Roach $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%'); 626dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') { 627dfb2cda2SGreg Roach $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name'; 628dfb2cda2SGreg Roach // NAME:* 629dfb2cda2SGreg Roach switch ($parts[3]) { 630dfb2cda2SGreg Roach case 'GIVN': 631dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 632dfb2cda2SGreg Roach case 'EXACT': 633dfb2cda2SGreg Roach $query->where($table . '.n_givn', '=', $field_value); 634dfb2cda2SGreg Roach break; 635dfb2cda2SGreg Roach case 'BEGINS': 636dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', $field_value . '%'); 637dfb2cda2SGreg Roach break; 638dfb2cda2SGreg Roach case 'CONTAINS': 639dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 640dfb2cda2SGreg Roach break; 641dfb2cda2SGreg Roach case 'SDX_STD': 642dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 643dfb2cda2SGreg Roach if ($sdx !== '') { 644dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); 645dfb2cda2SGreg Roach } else { 646dfb2cda2SGreg Roach // No phonetic content? Use a substring match 647dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 648dfb2cda2SGreg Roach } 649dfb2cda2SGreg Roach break; 650dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 651dfb2cda2SGreg Roach case 'SDX_DM': 652dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 653dfb2cda2SGreg Roach if ($sdx !== '') { 654dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); 655dfb2cda2SGreg Roach } else { 656dfb2cda2SGreg Roach // No phonetic content? Use a substring match 657dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 658dfb2cda2SGreg Roach } 659dfb2cda2SGreg Roach break; 660dfb2cda2SGreg Roach } 661dfb2cda2SGreg Roach break; 662dfb2cda2SGreg Roach case 'SURN': 663dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 664dfb2cda2SGreg Roach case 'EXACT': 665dfb2cda2SGreg Roach $query->where($table . '.n_surn', '=', $field_value); 666dfb2cda2SGreg Roach break; 667dfb2cda2SGreg Roach case 'BEGINS': 668dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', $field_value . '%'); 669dfb2cda2SGreg Roach break; 670dfb2cda2SGreg Roach case 'CONTAINS': 671dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 672dfb2cda2SGreg Roach break; 673dfb2cda2SGreg Roach case 'SDX_STD': 674dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 675dfb2cda2SGreg Roach if ($sdx !== '') { 676dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); 677dfb2cda2SGreg Roach } else { 678dfb2cda2SGreg Roach // No phonetic content? Use a substring match 679dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 680dfb2cda2SGreg Roach } 681dfb2cda2SGreg Roach break; 682dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 683dfb2cda2SGreg Roach case 'SDX_DM': 684dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 685dfb2cda2SGreg Roach if ($sdx !== '') { 686dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); 687dfb2cda2SGreg Roach } else { 688dfb2cda2SGreg Roach // No phonetic content? Use a substring match 689dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 690dfb2cda2SGreg Roach } 691dfb2cda2SGreg Roach break; 692dfb2cda2SGreg Roach } 693dfb2cda2SGreg Roach break; 694dfb2cda2SGreg Roach } 695dfb2cda2SGreg Roach unset($fields[$field_name]); 696dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMS') { 697dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 698dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 699*3c480c6bSGreg Roach $query->where('spouse_families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%'); 700dfb2cda2SGreg Roach } elseif ($parts[1] === 'TYPE') { 701dfb2cda2SGreg Roach // e.g. FACT:TYPE or EVEN:TYPE 702dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 703dfb2cda2SGreg Roach $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%\n2 TYPE %' . $field_value . '%'); 704dfb2cda2SGreg Roach } else { 705dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 706dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 707c5457947SGreg Roach $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%' . $parts[1] . '%' . $field_value . '%'); 708dfb2cda2SGreg Roach } 709dfb2cda2SGreg Roach } 710dfb2cda2SGreg Roach return $query 711dfb2cda2SGreg Roach ->get() 71252a8ef61SGreg Roach ->each($this->rowLimiter()) 713d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 714dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()) 7150b5fd0a6SGreg Roach ->filter(static function (Individual $individual) use ($fields): bool { 716c5457947SGreg Roach // Check for searches which were only partially matched by SQL 717dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 718a41e65f0SGreg Roach $regex = '/' . preg_quote($field_value, '/') . '/i'; 719dfb2cda2SGreg Roach 720c5457947SGreg Roach $parts = explode(':', $field_name . '::::'); 721dfb2cda2SGreg Roach 722dfb2cda2SGreg Roach // *:PLAC 723c5457947SGreg Roach if ($parts[1] === 'PLAC') { 724a41e65f0SGreg Roach foreach ($individual->facts([$parts[0]]) as $fact) { 725392561bbSGreg Roach if (preg_match($regex, $fact->place()->gedcomName())) { 726c5457947SGreg Roach continue 2; 727dfb2cda2SGreg Roach } 728a41e65f0SGreg Roach } 729c5457947SGreg Roach return false; 730c5457947SGreg Roach } 731c5457947SGreg Roach 732dfb2cda2SGreg Roach // FAMS:*:PLAC 733c5457947SGreg Roach if ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') { 73439ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 735a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 736392561bbSGreg Roach if (preg_match($regex, $fact->place()->gedcomName())) { 737c5457947SGreg Roach continue 2; 738a41e65f0SGreg Roach } 739dfb2cda2SGreg Roach } 740dfb2cda2SGreg Roach } 741c5457947SGreg Roach return false; 742c5457947SGreg Roach } 743c5457947SGreg Roach 744dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 745c5457947SGreg Roach if ($parts[0] === 'FAMS') { 74639ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 747a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 748a41e65f0SGreg Roach if (preg_match($regex, $fact->value())) { 749c5457947SGreg Roach continue 3; 750a41e65f0SGreg Roach } 751dfb2cda2SGreg Roach } 752dfb2cda2SGreg Roach } 753c5457947SGreg Roach return false; 754c5457947SGreg Roach } 755c5457947SGreg Roach 756dfb2cda2SGreg Roach // e.g. FACT:TYPE or EVEN:TYPE 757c5457947SGreg Roach if ($parts[1] === 'TYPE' || $parts[1] === '_WT_USER') { 758a41e65f0SGreg Roach foreach ($individual->facts([$parts[0]]) as $fact) { 759c5457947SGreg Roach if (preg_match($regex, $fact->attribute($parts[1]))) { 760c5457947SGreg Roach continue 2; 761dfb2cda2SGreg Roach } 762dfb2cda2SGreg Roach } 763dfb2cda2SGreg Roach 764dfb2cda2SGreg Roach return false; 765dfb2cda2SGreg Roach } 766c5457947SGreg Roach } 767dfb2cda2SGreg Roach 768dfb2cda2SGreg Roach return true; 769dfb2cda2SGreg Roach }); 770dfb2cda2SGreg Roach } 771dfb2cda2SGreg Roach 772dfb2cda2SGreg Roach /** 773dfb2cda2SGreg Roach * @param string $soundex 774dfb2cda2SGreg Roach * @param string $lastname 775dfb2cda2SGreg Roach * @param string $firstname 776dfb2cda2SGreg Roach * @param string $place 777dfb2cda2SGreg Roach * @param Tree[] $search_trees 778dfb2cda2SGreg Roach * 779b5c8fd7eSGreg Roach * @return Collection<Individual> 780dfb2cda2SGreg Roach */ 781dfb2cda2SGreg Roach public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection 782dfb2cda2SGreg Roach { 783dfb2cda2SGreg Roach switch ($soundex) { 784dfb2cda2SGreg Roach default: 785dfb2cda2SGreg Roach case 'Russell': 786dfb2cda2SGreg Roach $givn_sdx = Soundex::russell($firstname); 787dfb2cda2SGreg Roach $surn_sdx = Soundex::russell($lastname); 788dfb2cda2SGreg Roach $plac_sdx = Soundex::russell($place); 789dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_std'; 790dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_std'; 791dfb2cda2SGreg Roach $plac_field = 'p_std_soundex'; 792dfb2cda2SGreg Roach break; 793dfb2cda2SGreg Roach case 'DaitchM': 794dfb2cda2SGreg Roach $givn_sdx = Soundex::daitchMokotoff($firstname); 795dfb2cda2SGreg Roach $surn_sdx = Soundex::daitchMokotoff($lastname); 796dfb2cda2SGreg Roach $plac_sdx = Soundex::daitchMokotoff($place); 797dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_dm'; 798dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_dm'; 799dfb2cda2SGreg Roach $plac_field = 'p_dm_soundex'; 800dfb2cda2SGreg Roach break; 801dfb2cda2SGreg Roach } 802dfb2cda2SGreg Roach 803dfb2cda2SGreg Roach // Nothing to search for? Return nothing. 804dfb2cda2SGreg Roach if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') { 8053fda39a7SGreg Roach return new Collection(); 806dfb2cda2SGreg Roach } 807dfb2cda2SGreg Roach 808dfb2cda2SGreg Roach $query = DB::table('individuals') 809dfb2cda2SGreg Roach ->select(['individuals.*']) 810dfb2cda2SGreg Roach ->distinct(); 811dfb2cda2SGreg Roach 812dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $search_trees); 813dfb2cda2SGreg Roach 814dfb2cda2SGreg Roach if ($plac_sdx !== '') { 8150b5fd0a6SGreg Roach $query->join('placelinks', static function (JoinClause $join): void { 816dfb2cda2SGreg Roach $join 817dfb2cda2SGreg Roach ->on('placelinks.pl_file', '=', 'individuals.i_file') 818dfb2cda2SGreg Roach ->on('placelinks.pl_gid', '=', 'individuals.i_id'); 819dfb2cda2SGreg Roach }); 8200b5fd0a6SGreg Roach $query->join('places', static function (JoinClause $join): void { 821dfb2cda2SGreg Roach $join 822dfb2cda2SGreg Roach ->on('places.p_file', '=', 'placelinks.pl_file') 823dfb2cda2SGreg Roach ->on('places.p_id', '=', 'placelinks.pl_p_id'); 824dfb2cda2SGreg Roach }); 825dfb2cda2SGreg Roach 826dfb2cda2SGreg Roach $this->wherePhonetic($query, $plac_field, $plac_sdx); 827dfb2cda2SGreg Roach } 828dfb2cda2SGreg Roach 829dfb2cda2SGreg Roach if ($givn_sdx !== '' || $surn_sdx !== '') { 8300b5fd0a6SGreg Roach $query->join('name', static function (JoinClause $join): void { 831dfb2cda2SGreg Roach $join 832dfb2cda2SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 833dfb2cda2SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 834dfb2cda2SGreg Roach }); 835dfb2cda2SGreg Roach 836dfb2cda2SGreg Roach $this->wherePhonetic($query, $givn_field, $givn_sdx); 837dfb2cda2SGreg Roach $this->wherePhonetic($query, $surn_field, $surn_sdx); 838dfb2cda2SGreg Roach } 839dfb2cda2SGreg Roach 840dfb2cda2SGreg Roach return $query 841dfb2cda2SGreg Roach ->get() 84252a8ef61SGreg Roach ->each($this->rowLimiter()) 843d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 844dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()); 845dfb2cda2SGreg Roach } 846dfb2cda2SGreg Roach 847dfb2cda2SGreg Roach /** 84832cd2800SGreg Roach * Paginate a search query. 84932cd2800SGreg Roach * 85032cd2800SGreg Roach * @param Builder $query Searches the database for the desired records. 85132cd2800SGreg Roach * @param Closure $row_mapper Converts a row from the query into a record. 852a7a24840SGreg Roach * @param Closure $row_filter 85332cd2800SGreg Roach * @param int $offset Skip this many rows. 85432cd2800SGreg Roach * @param int $limit Take this many rows. 85532cd2800SGreg Roach * 856b5c8fd7eSGreg Roach * @return Collection<mixed> 85732cd2800SGreg Roach */ 858a7a24840SGreg Roach private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection 85932cd2800SGreg Roach { 86032cd2800SGreg Roach $collection = new Collection(); 86132cd2800SGreg Roach 86232cd2800SGreg Roach foreach ($query->cursor() as $row) { 86332cd2800SGreg Roach $record = $row_mapper($row); 864b68caec6SGreg Roach // If the object has a method "canShow()", then use it to filter for privacy. 865a7a24840SGreg Roach if ($row_filter($record)) { 86632cd2800SGreg Roach if ($offset > 0) { 86732cd2800SGreg Roach $offset--; 86832cd2800SGreg Roach } else { 86932cd2800SGreg Roach if ($limit > 0) { 87032cd2800SGreg Roach $collection->push($record); 87132cd2800SGreg Roach } 87232cd2800SGreg Roach 87332cd2800SGreg Roach $limit--; 87432cd2800SGreg Roach 87532cd2800SGreg Roach if ($limit === 0) { 87632cd2800SGreg Roach break; 87732cd2800SGreg Roach } 87832cd2800SGreg Roach } 87932cd2800SGreg Roach } 88032cd2800SGreg Roach } 88132cd2800SGreg Roach 882e0458bdcSGreg Roach 88332cd2800SGreg Roach return $collection; 88432cd2800SGreg Roach } 885a7a24840SGreg Roach 886a7a24840SGreg Roach /** 887a7a24840SGreg Roach * Apply search filters to a SQL query column. Apply collation rules to MySQL. 888a7a24840SGreg Roach * 889a7a24840SGreg Roach * @param Builder $query 890a7a24840SGreg Roach * @param Expression|string $field 891a7a24840SGreg Roach * @param string[] $search_terms 892a7a24840SGreg Roach */ 893a7a24840SGreg Roach private function whereSearch(Builder $query, $field, array $search_terms): void 894a7a24840SGreg Roach { 895a7a24840SGreg Roach if ($field instanceof Expression) { 896a7a24840SGreg Roach $field = $field->getValue(); 897a7a24840SGreg Roach } 898a7a24840SGreg Roach 899a7a24840SGreg Roach foreach ($search_terms as $search_term) { 900a69f5655SGreg Roach $query->whereContains(new Expression($field), $search_term); 901a7a24840SGreg Roach } 902a7a24840SGreg Roach } 903a7a24840SGreg Roach 904a7a24840SGreg Roach /** 9052d686e68SGreg Roach * Apply soundex search filters to a SQL query column. 9062d686e68SGreg Roach * 9072d686e68SGreg Roach * @param Builder $query 9082d686e68SGreg Roach * @param Expression|string $field 9092d686e68SGreg Roach * @param string $soundex 9102d686e68SGreg Roach */ 9112d686e68SGreg Roach private function wherePhonetic(Builder $query, $field, string $soundex): void 9122d686e68SGreg Roach { 9132d686e68SGreg Roach if ($soundex !== '') { 9140b5fd0a6SGreg Roach $query->where(static function (Builder $query) use ($soundex, $field): void { 9152d686e68SGreg Roach foreach (explode(':', $soundex) as $sdx) { 9162d686e68SGreg Roach $query->orWhere($field, 'LIKE', '%' . $sdx . '%'); 9172d686e68SGreg Roach } 9182d686e68SGreg Roach }); 9192d686e68SGreg Roach } 9202d686e68SGreg Roach } 9212d686e68SGreg Roach 9222d686e68SGreg Roach /** 923a7a24840SGreg Roach * @param Builder $query 924a7a24840SGreg Roach * @param string $tree_id_field 925a7a24840SGreg Roach * @param Tree[] $trees 926a7a24840SGreg Roach */ 927a7a24840SGreg Roach private function whereTrees(Builder $query, string $tree_id_field, array $trees): void 928a7a24840SGreg Roach { 9290b5fd0a6SGreg Roach $tree_ids = array_map(static function (Tree $tree): int { 930a7a24840SGreg Roach return $tree->id(); 931a7a24840SGreg Roach }, $trees); 932a7a24840SGreg Roach 933a7a24840SGreg Roach $query->whereIn($tree_id_field, $tree_ids); 934a7a24840SGreg Roach } 935a7a24840SGreg Roach 936d5ad3db0SGreg Roach 937d5ad3db0SGreg Roach /** 938d5ad3db0SGreg Roach * Find the media object that uses a particular media file. 939d5ad3db0SGreg Roach * 940d5ad3db0SGreg Roach * @param string $file 941d5ad3db0SGreg Roach * 942d5ad3db0SGreg Roach * @return Media[] 943d5ad3db0SGreg Roach */ 944d5ad3db0SGreg Roach public function findMediaObjectsForMediaFile(string $file): array 945d5ad3db0SGreg Roach { 946d5ad3db0SGreg Roach return DB::table('media') 947d5ad3db0SGreg Roach ->join('media_file', static function (JoinClause $join): void { 948d5ad3db0SGreg Roach $join 949d5ad3db0SGreg Roach ->on('media_file.m_file', '=', 'media.m_file') 950d5ad3db0SGreg Roach ->on('media_file.m_id', '=', 'media.m_id'); 951d5ad3db0SGreg Roach }) 952d5ad3db0SGreg Roach ->join('gedcom_setting', 'media.m_file', '=', 'gedcom_setting.gedcom_id') 953d5ad3db0SGreg Roach ->where(new Expression('setting_value || multimedia_file_refn'), '=', $file) 954d5ad3db0SGreg Roach ->select(['media.*']) 955d5ad3db0SGreg Roach ->distinct() 956d5ad3db0SGreg Roach ->get() 957d5ad3db0SGreg Roach ->map($this->mediaRowMapper()) 958d5ad3db0SGreg Roach ->all(); 959d5ad3db0SGreg Roach } 960d5ad3db0SGreg Roach 961a7a24840SGreg Roach /** 962a7a24840SGreg Roach * A closure to filter records by privacy-filtered GEDCOM data. 963a7a24840SGreg Roach * 964a7a24840SGreg Roach * @param array $search_terms 965a7a24840SGreg Roach * 966a7a24840SGreg Roach * @return Closure 967a7a24840SGreg Roach */ 968a7a24840SGreg Roach private function rawGedcomFilter(array $search_terms): Closure 969a7a24840SGreg Roach { 9706c2179e2SGreg Roach return static function (GedcomRecord $record) use ($search_terms): bool { 971a7a24840SGreg Roach // Ignore non-genealogy fields 972a7a24840SGreg Roach $gedcom = preg_replace('/\n\d (?:_UID) .*/', '', $record->gedcom()); 973a7a24840SGreg Roach 974a7a24840SGreg Roach // Ignore matches in links 975a7a24840SGreg Roach $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom); 976a7a24840SGreg Roach 977a7a24840SGreg Roach // Re-apply the filtering 978a7a24840SGreg Roach foreach ($search_terms as $search_term) { 979a7a24840SGreg Roach if (mb_stripos($gedcom, $search_term) === false) { 980a7a24840SGreg Roach return false; 981a7a24840SGreg Roach } 982a7a24840SGreg Roach } 983a7a24840SGreg Roach 984a7a24840SGreg Roach return true; 985a7a24840SGreg Roach }; 986a7a24840SGreg Roach } 98752a8ef61SGreg Roach 98852a8ef61SGreg Roach /** 98952a8ef61SGreg Roach * Searching for short or common text can give more results than the system can process. 99052a8ef61SGreg Roach * 99152a8ef61SGreg Roach * @param int $limit 99252a8ef61SGreg Roach * 99352a8ef61SGreg Roach * @return Closure 99452a8ef61SGreg Roach */ 99552a8ef61SGreg Roach private function rowLimiter(int $limit = 1000): Closure 99652a8ef61SGreg Roach { 9976c2179e2SGreg Roach return static function () use ($limit): void { 99852a8ef61SGreg Roach static $n = 0; 99952a8ef61SGreg Roach 100052a8ef61SGreg Roach if (++$n > $limit) { 100152a8ef61SGreg Roach $message = I18N::translate('The search returned too many results.'); 100252a8ef61SGreg Roach 1003d501c45dSGreg Roach throw new HttpServiceUnavailableException($message); 100452a8ef61SGreg Roach } 100552a8ef61SGreg Roach }; 100652a8ef61SGreg Roach } 1007d5ad3db0SGreg Roach 1008d5ad3db0SGreg Roach /** 1009d5ad3db0SGreg Roach * Convert a row from any tree in the families table into a family object. 1010d5ad3db0SGreg Roach * 1011d5ad3db0SGreg Roach * @return Closure 1012d5ad3db0SGreg Roach */ 1013d5ad3db0SGreg Roach private function familyRowMapper(): Closure 1014d5ad3db0SGreg Roach { 1015d5ad3db0SGreg Roach return function (stdClass $row): Family { 1016d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->f_file); 1017d5ad3db0SGreg Roach 1018d5ad3db0SGreg Roach return Family::getInstance($row->f_id, $tree, $row->f_gedcom); 1019d5ad3db0SGreg Roach }; 1020d5ad3db0SGreg Roach } 1021d5ad3db0SGreg Roach 1022d5ad3db0SGreg Roach /** 1023d5ad3db0SGreg Roach * Convert a row from any tree in the individuals table into an individual object. 1024d5ad3db0SGreg Roach * 1025d5ad3db0SGreg Roach * @return Closure 1026d5ad3db0SGreg Roach */ 1027d5ad3db0SGreg Roach private function individualRowMapper(): Closure 1028d5ad3db0SGreg Roach { 1029d5ad3db0SGreg Roach return function (stdClass $row): Individual { 1030d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->i_file); 1031d5ad3db0SGreg Roach 1032d5ad3db0SGreg Roach return Individual::getInstance($row->i_id, $tree, $row->i_gedcom); 1033d5ad3db0SGreg Roach }; 1034d5ad3db0SGreg Roach } 1035d5ad3db0SGreg Roach 1036d5ad3db0SGreg Roach /** 1037d5ad3db0SGreg Roach * Convert a row from any tree in the media table into an media object. 1038d5ad3db0SGreg Roach * 1039d5ad3db0SGreg Roach * @return Closure 1040d5ad3db0SGreg Roach */ 1041d5ad3db0SGreg Roach private function mediaRowMapper(): Closure 1042d5ad3db0SGreg Roach { 1043d5ad3db0SGreg Roach return function (stdClass $row): Media { 1044d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->m_file); 1045d5ad3db0SGreg Roach 1046d5ad3db0SGreg Roach return Media::getInstance($row->m_id, $tree, $row->m_gedcom); 1047d5ad3db0SGreg Roach }; 1048d5ad3db0SGreg Roach } 1049d5ad3db0SGreg Roach 1050d5ad3db0SGreg Roach /** 1051d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a note object. 1052d5ad3db0SGreg Roach * 1053d5ad3db0SGreg Roach * @return Closure 1054d5ad3db0SGreg Roach */ 1055d5ad3db0SGreg Roach private function noteRowMapper(): Closure 1056d5ad3db0SGreg Roach { 1057d5ad3db0SGreg Roach return function (stdClass $row): Note { 1058d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1059d5ad3db0SGreg Roach 1060d5ad3db0SGreg Roach return Note::getInstance($row->o_id, $tree, $row->o_gedcom); 1061d5ad3db0SGreg Roach }; 1062d5ad3db0SGreg Roach } 1063d5ad3db0SGreg Roach 1064d5ad3db0SGreg Roach /** 1065d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a repository object. 1066d5ad3db0SGreg Roach * 1067d5ad3db0SGreg Roach * @return Closure 1068d5ad3db0SGreg Roach */ 1069d5ad3db0SGreg Roach private function repositoryRowMapper(): Closure 1070d5ad3db0SGreg Roach { 1071d5ad3db0SGreg Roach return function (stdClass $row): Repository { 1072d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1073d5ad3db0SGreg Roach 1074d5ad3db0SGreg Roach return Repository::getInstance($row->o_id, $tree, $row->o_gedcom); 1075d5ad3db0SGreg Roach }; 1076d5ad3db0SGreg Roach } 1077d5ad3db0SGreg Roach 1078d5ad3db0SGreg Roach /** 1079d5ad3db0SGreg Roach * Convert a row from any tree in the sources table into a source object. 1080d5ad3db0SGreg Roach * 1081d5ad3db0SGreg Roach * @return Closure 1082d5ad3db0SGreg Roach */ 1083d5ad3db0SGreg Roach private function sourceRowMapper(): Closure 1084d5ad3db0SGreg Roach { 1085d5ad3db0SGreg Roach return function (stdClass $row): Source { 1086d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->s_file); 1087d5ad3db0SGreg Roach 1088d5ad3db0SGreg Roach return Source::getInstance($row->s_id, $tree, $row->s_gedcom); 1089d5ad3db0SGreg Roach }; 1090d5ad3db0SGreg Roach } 1091d5ad3db0SGreg Roach 1092d5ad3db0SGreg Roach /** 1093d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a submitter object. 1094d5ad3db0SGreg Roach * 1095d5ad3db0SGreg Roach * @return Closure 1096d5ad3db0SGreg Roach */ 1097d5ad3db0SGreg Roach private function submitterRowMapper(): Closure 1098d5ad3db0SGreg Roach { 1099d5ad3db0SGreg Roach return function (stdClass $row): GedcomRecord { 1100d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1101d5ad3db0SGreg Roach 1102d5ad3db0SGreg Roach return GedcomRecord::getInstance($row->o_id, $tree, $row->o_gedcom); 1103d5ad3db0SGreg Roach }; 1104d5ad3db0SGreg Roach } 110532cd2800SGreg Roach} 1106