132cd2800SGreg Roach<?php 23976b470SGreg Roach 332cd2800SGreg Roach/** 432cd2800SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 246f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 2532cd2800SGreg Roachuse Fisharebest\Webtrees\Family; 26a7a24840SGreg Roachuse Fisharebest\Webtrees\Gedcom; 2732cd2800SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 2881b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpServiceUnavailableException; 2952a8ef61SGreg Roachuse Fisharebest\Webtrees\I18N; 3032cd2800SGreg Roachuse Fisharebest\Webtrees\Individual; 3181b729d3SGreg Roachuse Fisharebest\Webtrees\Location; 3232cd2800SGreg Roachuse Fisharebest\Webtrees\Media; 3332cd2800SGreg Roachuse Fisharebest\Webtrees\Note; 34b68caec6SGreg Roachuse Fisharebest\Webtrees\Place; 3581b729d3SGreg Roachuse Fisharebest\Webtrees\Registry; 3632cd2800SGreg Roachuse Fisharebest\Webtrees\Repository; 37701f5d18SGreg Roachuse Fisharebest\Webtrees\SharedNote; 382d686e68SGreg Roachuse Fisharebest\Webtrees\Soundex; 3932cd2800SGreg Roachuse Fisharebest\Webtrees\Source; 40d4ac8227SGreg Roachuse Fisharebest\Webtrees\Submission; 41b5c8fd7eSGreg Roachuse Fisharebest\Webtrees\Submitter; 4232cd2800SGreg Roachuse Fisharebest\Webtrees\Tree; 4332cd2800SGreg Roachuse Illuminate\Database\Query\Builder; 44a7a24840SGreg Roachuse Illuminate\Database\Query\Expression; 4532cd2800SGreg Roachuse Illuminate\Database\Query\JoinClause; 4632cd2800SGreg Roachuse Illuminate\Support\Collection; 473976b470SGreg Roach 48b5961194SGreg Roachuse function addcslashes; 49b4adec9eSGreg Roachuse function array_filter; 50b4adec9eSGreg Roachuse function array_map; 5156946b7aSGreg Roachuse function array_unique; 5256946b7aSGreg Roachuse function explode; 53b4adec9eSGreg Roachuse function implode; 54a7a24840SGreg Roachuse function mb_stripos; 55b4adec9eSGreg Roachuse function preg_match; 56b4adec9eSGreg Roachuse function preg_quote; 57b4adec9eSGreg Roachuse function preg_replace; 582f86083cSGreg Roachuse function str_ends_with; 592f86083cSGreg Roachuse function str_starts_with; 602f86083cSGreg Roach 61b4adec9eSGreg Roachuse const PHP_INT_MAX; 6232cd2800SGreg Roach 6332cd2800SGreg Roach/** 6432cd2800SGreg Roach * Search trees for genealogy records. 6532cd2800SGreg Roach */ 6632cd2800SGreg Roachclass SearchService 6732cd2800SGreg Roach{ 68645e3037SGreg Roach // Do not attempt to show search results larger than this/ 69645e3037SGreg Roach protected const MAX_SEARCH_RESULTS = 5000; 70645e3037SGreg Roach 71c4943cffSGreg Roach private TreeService $tree_service; 72d5ad3db0SGreg Roach 73d5ad3db0SGreg Roach /** 74d5ad3db0SGreg Roach * @param TreeService $tree_service 75d5ad3db0SGreg Roach */ 763959eeb6SGreg Roach public function __construct( 773959eeb6SGreg Roach TreeService $tree_service 783959eeb6SGreg Roach ) { 79d5ad3db0SGreg Roach $this->tree_service = $tree_service; 80d5ad3db0SGreg Roach } 81d5ad3db0SGreg Roach 82a7a24840SGreg Roach /** 8309482a55SGreg Roach * @param array<Tree> $trees 8409482a55SGreg Roach * @param array<string> $search 85a7a24840SGreg Roach * 8636779af1SGreg Roach * @return Collection<int,Family> 87a7a24840SGreg Roach */ 88a7a24840SGreg Roach public function searchFamilies(array $trees, array $search): Collection 89a7a24840SGreg Roach { 90a7a24840SGreg Roach $query = DB::table('families'); 91a7a24840SGreg Roach 92a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 93a7a24840SGreg Roach $this->whereSearch($query, 'f_gedcom', $search); 94a7a24840SGreg Roach 95a7a24840SGreg Roach return $query 96a7a24840SGreg Roach ->get() 9752a8ef61SGreg Roach ->each($this->rowLimiter()) 98d5ad3db0SGreg Roach ->map($this->familyRowMapper()) 99a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 1007f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 101a7a24840SGreg Roach } 102a7a24840SGreg Roach 10332cd2800SGreg Roach /** 10432cd2800SGreg Roach * Search for families by name. 10532cd2800SGreg Roach * 10609482a55SGreg Roach * @param array<Tree> $trees 10709482a55SGreg Roach * @param array<string> $search 10832cd2800SGreg Roach * @param int $offset 10932cd2800SGreg Roach * @param int $limit 11032cd2800SGreg Roach * 11136779af1SGreg Roach * @return Collection<int,Family> 11232cd2800SGreg Roach */ 113a7a24840SGreg Roach public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 11432cd2800SGreg Roach { 11532cd2800SGreg Roach $query = DB::table('families') 116ac499332SGreg Roach ->leftJoin('name AS husb_name', static function (JoinClause $join): void { 11732cd2800SGreg Roach $join 11832cd2800SGreg Roach ->on('husb_name.n_file', '=', 'families.f_file') 119ac499332SGreg Roach ->on('husb_name.n_id', '=', 'families.f_husb') 120ac499332SGreg Roach ->where('husb_name.n_type', '<>', '_MARNM'); 12132cd2800SGreg Roach }) 122ac499332SGreg Roach ->leftJoin('name AS wife_name', static function (JoinClause $join): void { 12332cd2800SGreg Roach $join 12432cd2800SGreg Roach ->on('wife_name.n_file', '=', 'families.f_file') 125ac499332SGreg Roach ->on('wife_name.n_id', '=', 'families.f_wife') 126ac499332SGreg Roach ->where('wife_name.n_type', '<>', '_MARNM'); 127ac499332SGreg Roach }); 128a7a24840SGreg Roach 129211018abSGreg Roach $prefix = DB::prefix(); 130a69f5655SGreg Roach $field = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')"); 131a7a24840SGreg Roach 132a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 133a7a24840SGreg Roach $this->whereSearch($query, $field, $search); 134a7a24840SGreg Roach 135a7a24840SGreg Roach $query 13632cd2800SGreg Roach ->orderBy('husb_name.n_sort') 13732cd2800SGreg Roach ->orderBy('wife_name.n_sort') 1388aaf5f58SGreg Roach ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort']); 13932cd2800SGreg Roach 140d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 141a7a24840SGreg Roach } 142a7a24840SGreg Roach 143a7a24840SGreg Roach /** 144dbe53437SGreg Roach * @param Place $place 145dbe53437SGreg Roach * 14636779af1SGreg Roach * @return Collection<int,Family> 147dbe53437SGreg Roach */ 148dbe53437SGreg Roach public function searchFamiliesInPlace(Place $place): Collection 149dbe53437SGreg Roach { 150dbe53437SGreg Roach return DB::table('families') 151dbe53437SGreg Roach ->join('placelinks', static function (JoinClause $query) { 152dbe53437SGreg Roach $query 153dbe53437SGreg Roach ->on('families.f_file', '=', 'placelinks.pl_file') 154dbe53437SGreg Roach ->on('families.f_id', '=', 'placelinks.pl_gid'); 155dbe53437SGreg Roach }) 156dbe53437SGreg Roach ->where('f_file', '=', $place->tree()->id()) 157dbe53437SGreg Roach ->where('pl_p_id', '=', $place->id()) 158dbe53437SGreg Roach ->select(['families.*']) 159dbe53437SGreg Roach ->get() 160dbe53437SGreg Roach ->each($this->rowLimiter()) 161dbe53437SGreg Roach ->map($this->familyRowMapper()) 162dbe53437SGreg Roach ->filter(GedcomRecord::accessFilter()); 163dbe53437SGreg Roach } 164dbe53437SGreg Roach 165dbe53437SGreg Roach /** 16609482a55SGreg Roach * @param array<Tree> $trees 16709482a55SGreg Roach * @param array<string> $search 168a7a24840SGreg Roach * 16936779af1SGreg Roach * @return Collection<int,Individual> 170a7a24840SGreg Roach */ 171a7a24840SGreg Roach public function searchIndividuals(array $trees, array $search): Collection 172a7a24840SGreg Roach { 173a7a24840SGreg Roach $query = DB::table('individuals'); 174a7a24840SGreg Roach 175a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 176a7a24840SGreg Roach $this->whereSearch($query, 'i_gedcom', $search); 177a7a24840SGreg Roach 178a7a24840SGreg Roach return $query 179a7a24840SGreg Roach ->get() 18052a8ef61SGreg Roach ->each($this->rowLimiter()) 181d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 182a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 1837f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 18432cd2800SGreg Roach } 18532cd2800SGreg Roach 18632cd2800SGreg Roach /** 18732cd2800SGreg Roach * Search for individuals by name. 18832cd2800SGreg Roach * 18909482a55SGreg Roach * @param array<Tree> $trees 19009482a55SGreg Roach * @param array<string> $search 19132cd2800SGreg Roach * @param int $offset 19232cd2800SGreg Roach * @param int $limit 19332cd2800SGreg Roach * 19436779af1SGreg Roach * @return Collection<int,Individual> 19532cd2800SGreg Roach */ 196a7a24840SGreg Roach public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 19732cd2800SGreg Roach { 19832cd2800SGreg Roach $query = DB::table('individuals') 1990b5fd0a6SGreg Roach ->join('name', static function (JoinClause $join): void { 20032cd2800SGreg Roach $join 20132cd2800SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 202a7a24840SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 20332cd2800SGreg Roach }) 204e84cf2deSGreg Roach ->orderBy('n_sort') 205685de081SGreg Roach ->select(['individuals.*', 'n_sort']); 20632cd2800SGreg Roach 207a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 208a7a24840SGreg Roach $this->whereSearch($query, 'n_full', $search); 209a7a24840SGreg Roach 210d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 21132cd2800SGreg Roach } 21232cd2800SGreg Roach 21332cd2800SGreg Roach /** 214dbe53437SGreg Roach * @param Place $place 215dbe53437SGreg Roach * 21636779af1SGreg Roach * @return Collection<int,Individual> 217dbe53437SGreg Roach */ 218dbe53437SGreg Roach public function searchIndividualsInPlace(Place $place): Collection 219dbe53437SGreg Roach { 220dbe53437SGreg Roach return DB::table('individuals') 221dbe53437SGreg Roach ->join('placelinks', static function (JoinClause $join) { 222dbe53437SGreg Roach $join 223dbe53437SGreg Roach ->on('i_file', '=', 'pl_file') 224dbe53437SGreg Roach ->on('i_id', '=', 'pl_gid'); 225dbe53437SGreg Roach }) 226dbe53437SGreg Roach ->where('i_file', '=', $place->tree()->id()) 227dbe53437SGreg Roach ->where('pl_p_id', '=', $place->id()) 228dbe53437SGreg Roach ->select(['individuals.*']) 229dbe53437SGreg Roach ->get() 230dbe53437SGreg Roach ->each($this->rowLimiter()) 231dbe53437SGreg Roach ->map($this->individualRowMapper()) 232dbe53437SGreg Roach ->filter(GedcomRecord::accessFilter()); 233dbe53437SGreg Roach } 234dbe53437SGreg Roach 235dbe53437SGreg Roach /** 236d4ac8227SGreg Roach * Search for submissions. 237d4ac8227SGreg Roach * 23809482a55SGreg Roach * @param array<Tree> $trees 23909482a55SGreg Roach * @param array<string> $search 240d4ac8227SGreg Roach * @param int $offset 241d4ac8227SGreg Roach * @param int $limit 242d4ac8227SGreg Roach * 24336779af1SGreg Roach * @return Collection<int,Location> 244d4ac8227SGreg Roach */ 245d4ac8227SGreg Roach public function searchLocations(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 246d4ac8227SGreg Roach { 247d4ac8227SGreg Roach $query = DB::table('other') 248058ba724SGreg Roach ->where('o_type', '=', Location::RECORD_TYPE); 249d4ac8227SGreg Roach 250d4ac8227SGreg Roach $this->whereTrees($query, 'o_file', $trees); 251d4ac8227SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 252d4ac8227SGreg Roach 253d4ac8227SGreg Roach return $this->paginateQuery($query, $this->locationRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 254d4ac8227SGreg Roach } 255d4ac8227SGreg Roach 256d4ac8227SGreg Roach /** 25732cd2800SGreg Roach * Search for media objects. 25832cd2800SGreg Roach * 25909482a55SGreg Roach * @param array<Tree> $trees 26009482a55SGreg Roach * @param array<string> $search 26132cd2800SGreg Roach * @param int $offset 26232cd2800SGreg Roach * @param int $limit 26332cd2800SGreg Roach * 26436779af1SGreg Roach * @return Collection<int,Media> 26532cd2800SGreg Roach */ 266a7a24840SGreg Roach public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 26732cd2800SGreg Roach { 268a7a24840SGreg Roach $query = DB::table('media'); 26932cd2800SGreg Roach 270a7a24840SGreg Roach $this->whereTrees($query, 'media.m_file', $trees); 271a7a24840SGreg Roach $this->whereSearch($query, 'm_gedcom', $search); 272a7a24840SGreg Roach 273d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 27432cd2800SGreg Roach } 27532cd2800SGreg Roach 27632cd2800SGreg Roach /** 27732cd2800SGreg Roach * Search for notes. 27832cd2800SGreg Roach * 27909482a55SGreg Roach * @param array<Tree> $trees 28009482a55SGreg Roach * @param array<string> $search 28132cd2800SGreg Roach * @param int $offset 28232cd2800SGreg Roach * @param int $limit 28332cd2800SGreg Roach * 28436779af1SGreg Roach * @return Collection<int,Note> 28532cd2800SGreg Roach */ 286a7a24840SGreg Roach public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 28732cd2800SGreg Roach { 28832cd2800SGreg Roach $query = DB::table('other') 289058ba724SGreg Roach ->where('o_type', '=', Note::RECORD_TYPE); 29032cd2800SGreg Roach 291a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 292a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 293a7a24840SGreg Roach 294d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 29532cd2800SGreg Roach } 29632cd2800SGreg Roach 29732cd2800SGreg Roach /** 298701f5d18SGreg Roach * Search for notes. 299701f5d18SGreg Roach * 300701f5d18SGreg Roach * @param array<Tree> $trees 301701f5d18SGreg Roach * @param array<string> $search 302701f5d18SGreg Roach * @param int $offset 303701f5d18SGreg Roach * @param int $limit 304701f5d18SGreg Roach * 305701f5d18SGreg Roach * @return Collection<int,SharedNote> 306701f5d18SGreg Roach */ 307701f5d18SGreg Roach public function searchSharedNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 308701f5d18SGreg Roach { 309701f5d18SGreg Roach $query = DB::table('other') 310701f5d18SGreg Roach ->where('o_type', '=', SharedNote::RECORD_TYPE); 311701f5d18SGreg Roach 312701f5d18SGreg Roach $this->whereTrees($query, 'o_file', $trees); 313701f5d18SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 314701f5d18SGreg Roach 315701f5d18SGreg Roach return $this->paginateQuery($query, $this->sharedNoteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 316701f5d18SGreg Roach } 317701f5d18SGreg Roach 318701f5d18SGreg Roach /** 31932cd2800SGreg Roach * Search for repositories. 32032cd2800SGreg Roach * 32109482a55SGreg Roach * @param array<Tree> $trees 32209482a55SGreg Roach * @param array<string> $search 32332cd2800SGreg Roach * @param int $offset 32432cd2800SGreg Roach * @param int $limit 32532cd2800SGreg Roach * 32636779af1SGreg Roach * @return Collection<int,Repository> 32732cd2800SGreg Roach */ 328a7a24840SGreg Roach public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 32932cd2800SGreg Roach { 33032cd2800SGreg Roach $query = DB::table('other') 331058ba724SGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE); 33232cd2800SGreg Roach 333a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 334a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 335a7a24840SGreg Roach 336d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 33732cd2800SGreg Roach } 33832cd2800SGreg Roach 33932cd2800SGreg Roach /** 340a7a24840SGreg Roach * Search for sources. 34132cd2800SGreg Roach * 34209482a55SGreg Roach * @param array<Tree> $trees 34309482a55SGreg Roach * @param array<string> $search 34432cd2800SGreg Roach * @param int $offset 34532cd2800SGreg Roach * @param int $limit 34632cd2800SGreg Roach * 34736779af1SGreg Roach * @return Collection<int,Source> 34832cd2800SGreg Roach */ 349a7a24840SGreg Roach public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 350a7a24840SGreg Roach { 351a7a24840SGreg Roach $query = DB::table('sources'); 352a7a24840SGreg Roach 353a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 354a7a24840SGreg Roach $this->whereSearch($query, 's_gedcom', $search); 355a7a24840SGreg Roach 356d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 357a7a24840SGreg Roach } 358a7a24840SGreg Roach 359a7a24840SGreg Roach /** 360a7a24840SGreg Roach * Search for sources by name. 361a7a24840SGreg Roach * 36209482a55SGreg Roach * @param array<Tree> $trees 36309482a55SGreg Roach * @param array<string> $search 364a7a24840SGreg Roach * @param int $offset 365a7a24840SGreg Roach * @param int $limit 366a7a24840SGreg Roach * 36736779af1SGreg Roach * @return Collection<int,Source> 368a7a24840SGreg Roach */ 369a7a24840SGreg Roach public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 37032cd2800SGreg Roach { 37132cd2800SGreg Roach $query = DB::table('sources') 372c0804649SGreg Roach ->orderBy('s_name'); 37332cd2800SGreg Roach 374a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 375a7a24840SGreg Roach $this->whereSearch($query, 's_name', $search); 376a7a24840SGreg Roach 377d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 37832cd2800SGreg Roach } 37932cd2800SGreg Roach 38032cd2800SGreg Roach /** 38163763244SGreg Roach * Search for sources. 38263763244SGreg Roach * 38309482a55SGreg Roach * @param array<Tree> $trees 38409482a55SGreg Roach * @param array<string> $search 38563763244SGreg Roach * @param int $offset 38663763244SGreg Roach * @param int $limit 38763763244SGreg Roach * 38836779af1SGreg Roach * @return Collection<int,string> 38963763244SGreg Roach */ 39063763244SGreg Roach public function searchSurnames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 39163763244SGreg Roach { 39263763244SGreg Roach $query = DB::table('name'); 39363763244SGreg Roach 39463763244SGreg Roach $this->whereTrees($query, 'n_file', $trees); 39563763244SGreg Roach $this->whereSearch($query, 'n_surname', $search); 39663763244SGreg Roach 39763763244SGreg Roach return $query 39863763244SGreg Roach ->groupBy(['n_surname']) 39963763244SGreg Roach ->orderBy('n_surname') 40063763244SGreg Roach ->skip($offset) 40163763244SGreg Roach ->take($limit) 40263763244SGreg Roach ->pluck('n_surname'); 40363763244SGreg Roach } 40463763244SGreg Roach 40563763244SGreg Roach /** 406d4ac8227SGreg Roach * Search for submissions. 407d4ac8227SGreg Roach * 40809482a55SGreg Roach * @param array<Tree> $trees 40909482a55SGreg Roach * @param array<string> $search 410d4ac8227SGreg Roach * @param int $offset 411d4ac8227SGreg Roach * @param int $limit 412d4ac8227SGreg Roach * 41336779af1SGreg Roach * @return Collection<int,Submission> 414d4ac8227SGreg Roach */ 415d4ac8227SGreg Roach public function searchSubmissions(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 416d4ac8227SGreg Roach { 417d4ac8227SGreg Roach $query = DB::table('other') 418058ba724SGreg Roach ->where('o_type', '=', Submission::RECORD_TYPE); 419d4ac8227SGreg Roach 420d4ac8227SGreg Roach $this->whereTrees($query, 'o_file', $trees); 421d4ac8227SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 422d4ac8227SGreg Roach 423d4ac8227SGreg Roach return $this->paginateQuery($query, $this->submissionRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 424d4ac8227SGreg Roach } 425d4ac8227SGreg Roach 426d4ac8227SGreg Roach /** 42732cd2800SGreg Roach * Search for submitters. 42832cd2800SGreg Roach * 42909482a55SGreg Roach * @param array<Tree> $trees 43009482a55SGreg Roach * @param array<string> $search 43132cd2800SGreg Roach * @param int $offset 43232cd2800SGreg Roach * @param int $limit 43332cd2800SGreg Roach * 43436779af1SGreg Roach * @return Collection<int,Submitter> 43532cd2800SGreg Roach */ 436a7a24840SGreg Roach public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 43732cd2800SGreg Roach { 43832cd2800SGreg Roach $query = DB::table('other') 439058ba724SGreg Roach ->where('o_type', '=', Submitter::RECORD_TYPE); 44032cd2800SGreg Roach 441a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 442a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 443a7a24840SGreg Roach 444d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 44532cd2800SGreg Roach } 44632cd2800SGreg Roach 44732cd2800SGreg Roach /** 448b68caec6SGreg Roach * Search for places. 449b68caec6SGreg Roach * 450b68caec6SGreg Roach * @param Tree $tree 451b68caec6SGreg Roach * @param string $search 452b68caec6SGreg Roach * @param int $offset 453b68caec6SGreg Roach * @param int $limit 454b68caec6SGreg Roach * 45536779af1SGreg Roach * @return Collection<int,Place> 456b68caec6SGreg Roach */ 457b68caec6SGreg Roach public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 458b68caec6SGreg Roach { 459b68caec6SGreg Roach $query = DB::table('places AS p0') 460b68caec6SGreg Roach ->where('p0.p_file', '=', $tree->id()) 461b68caec6SGreg Roach ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id') 462b68caec6SGreg Roach ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id') 463b68caec6SGreg Roach ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id') 464b68caec6SGreg Roach ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id') 465b68caec6SGreg Roach ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id') 466b68caec6SGreg Roach ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id') 467b68caec6SGreg Roach ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id') 468b68caec6SGreg Roach ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id') 469b68caec6SGreg Roach ->orderBy('p0.p_place') 470b68caec6SGreg Roach ->orderBy('p1.p_place') 471b68caec6SGreg Roach ->orderBy('p2.p_place') 472b68caec6SGreg Roach ->orderBy('p3.p_place') 473b68caec6SGreg Roach ->orderBy('p4.p_place') 474b68caec6SGreg Roach ->orderBy('p5.p_place') 475b68caec6SGreg Roach ->orderBy('p6.p_place') 476b68caec6SGreg Roach ->orderBy('p7.p_place') 477b68caec6SGreg Roach ->orderBy('p8.p_place') 478b68caec6SGreg Roach ->select([ 479b68caec6SGreg Roach 'p0.p_place AS place0', 480b68caec6SGreg Roach 'p1.p_place AS place1', 481b68caec6SGreg Roach 'p2.p_place AS place2', 482b68caec6SGreg Roach 'p3.p_place AS place3', 483b68caec6SGreg Roach 'p4.p_place AS place4', 484b68caec6SGreg Roach 'p5.p_place AS place5', 485b68caec6SGreg Roach 'p6.p_place AS place6', 486b68caec6SGreg Roach 'p7.p_place AS place7', 487b68caec6SGreg Roach 'p8.p_place AS place8', 488b68caec6SGreg Roach ]); 489b68caec6SGreg Roach 490b68caec6SGreg Roach // Filter each level of the hierarchy. 491b68caec6SGreg Roach foreach (explode(',', $search, 9) as $level => $string) { 49252550490SGreg Roach $query->where('p' . $level . '.p_place', DB::iLike(), '%' . addcslashes($string, '\\%_') . '%'); 493b68caec6SGreg Roach } 494b68caec6SGreg Roach 495f70bcff5SGreg Roach $row_mapper = static function (object $row) use ($tree): Place { 496b68caec6SGreg Roach $place = implode(', ', array_filter((array) $row)); 497b68caec6SGreg Roach 498b68caec6SGreg Roach return new Place($place, $tree); 499b68caec6SGreg Roach }; 500b68caec6SGreg Roach 501f25fc0f9SGreg Roach $filter = static fn (): bool => true; 502b68caec6SGreg Roach 503a7a24840SGreg Roach return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit); 504a7a24840SGreg Roach } 505b68caec6SGreg Roach 506b68caec6SGreg Roach /** 507ca1e6d47SGreg Roach * @param Tree $tree 508d8809d62SGreg Roach * @param array<string,string> $fields 509d8809d62SGreg Roach * @param array<string,string> $modifiers 510dfb2cda2SGreg Roach * 51136779af1SGreg Roach * @return Collection<int,Individual> 512dfb2cda2SGreg Roach */ 513ca1e6d47SGreg Roach public function searchIndividualsAdvanced(Tree $tree, array $fields, array $modifiers): Collection 514dfb2cda2SGreg Roach { 515ef475b14SGreg Roach $fields = array_filter($fields, static fn (string $x): bool => $x !== ''); 516dfb2cda2SGreg Roach 517dfb2cda2SGreg Roach $query = DB::table('individuals') 518ca1e6d47SGreg Roach ->where('i_file', '=', $tree->id()) 519dfb2cda2SGreg Roach ->select(['individuals.*']) 520dfb2cda2SGreg Roach ->distinct(); 521dfb2cda2SGreg Roach 522dfb2cda2SGreg Roach // Join the following tables 523dfb2cda2SGreg Roach $father_name = false; 524dfb2cda2SGreg Roach $mother_name = false; 525dfb2cda2SGreg Roach $spouse_family = false; 526dfb2cda2SGreg Roach $indi_name = false; 52756946b7aSGreg Roach $indi_dates = []; 52856946b7aSGreg Roach $fam_dates = []; 529dfb2cda2SGreg Roach $indi_plac = false; 530dfb2cda2SGreg Roach $fam_plac = false; 531dfb2cda2SGreg Roach 532dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 5332f86083cSGreg Roach if (str_starts_with($field_name, 'FATHER:NAME')) { 534dfb2cda2SGreg Roach $father_name = true; 5352f86083cSGreg Roach } elseif (str_starts_with($field_name, 'MOTHER:NAME')) { 536dfb2cda2SGreg Roach $mother_name = true; 5372f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:NAME:GIVN')) { 53856946b7aSGreg Roach $indi_name = true; 5392f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:NAME:SURN')) { 5402f86083cSGreg Roach $indi_name = true; 5412f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:')) { 542dfb2cda2SGreg Roach $spouse_family = true; 5432f86083cSGreg Roach if (str_ends_with($field_name, ':DATE')) { 5442f86083cSGreg Roach $fam_dates[] = explode(':', $field_name)[1]; 5452f86083cSGreg Roach } elseif (str_ends_with($field_name, ':PLAC')) { 54656946b7aSGreg Roach $fam_plac = true; 54756946b7aSGreg Roach } 5482f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:')) { 5492f86083cSGreg Roach if (str_ends_with($field_name, ':DATE')) { 5502f86083cSGreg Roach $indi_dates[] = explode(':', $field_name)[1]; 5512f86083cSGreg Roach } elseif (str_ends_with($field_name, ':PLAC')) { 552dfb2cda2SGreg Roach $indi_plac = true; 553dfb2cda2SGreg Roach } 554dfb2cda2SGreg Roach } 555dfb2cda2SGreg Roach } 556dfb2cda2SGreg Roach 557dfb2cda2SGreg Roach if ($father_name || $mother_name) { 5580b5fd0a6SGreg Roach $query->join('link AS l1', static function (JoinClause $join): void { 559dfb2cda2SGreg Roach $join 560dfb2cda2SGreg Roach ->on('l1.l_file', '=', 'individuals.i_file') 561dfb2cda2SGreg Roach ->on('l1.l_from', '=', 'individuals.i_id') 562dfb2cda2SGreg Roach ->where('l1.l_type', '=', 'FAMC'); 563dfb2cda2SGreg Roach }); 564dfb2cda2SGreg Roach 565dfb2cda2SGreg Roach if ($father_name) { 5660b5fd0a6SGreg Roach $query->join('link AS l2', static function (JoinClause $join): void { 567dfb2cda2SGreg Roach $join 568dfb2cda2SGreg Roach ->on('l2.l_file', '=', 'l1.l_file') 569dfb2cda2SGreg Roach ->on('l2.l_from', '=', 'l1.l_to') 570dfb2cda2SGreg Roach ->where('l2.l_type', '=', 'HUSB'); 571dfb2cda2SGreg Roach }); 5720b5fd0a6SGreg Roach $query->join('name AS father_name', static function (JoinClause $join): void { 573dfb2cda2SGreg Roach $join 574dfb2cda2SGreg Roach ->on('father_name.n_file', '=', 'l2.l_file') 575dfb2cda2SGreg Roach ->on('father_name.n_id', '=', 'l2.l_to'); 576dfb2cda2SGreg Roach }); 577dfb2cda2SGreg Roach } 578dfb2cda2SGreg Roach 579dfb2cda2SGreg Roach if ($mother_name) { 5800b5fd0a6SGreg Roach $query->join('link AS l3', static function (JoinClause $join): void { 581dfb2cda2SGreg Roach $join 582dfb2cda2SGreg Roach ->on('l3.l_file', '=', 'l1.l_file') 583dfb2cda2SGreg Roach ->on('l3.l_from', '=', 'l1.l_to') 584dfb2cda2SGreg Roach ->where('l3.l_type', '=', 'WIFE'); 585dfb2cda2SGreg Roach }); 5860b5fd0a6SGreg Roach $query->join('name AS mother_name', static function (JoinClause $join): void { 587dfb2cda2SGreg Roach $join 588dfb2cda2SGreg Roach ->on('mother_name.n_file', '=', 'l3.l_file') 589dfb2cda2SGreg Roach ->on('mother_name.n_id', '=', 'l3.l_to'); 590dfb2cda2SGreg Roach }); 591dfb2cda2SGreg Roach } 592dfb2cda2SGreg Roach } 593dfb2cda2SGreg Roach 594dfb2cda2SGreg Roach if ($spouse_family) { 5950b5fd0a6SGreg Roach $query->join('link AS l4', static function (JoinClause $join): void { 596dfb2cda2SGreg Roach $join 597dfb2cda2SGreg Roach ->on('l4.l_file', '=', 'individuals.i_file') 598dfb2cda2SGreg Roach ->on('l4.l_from', '=', 'individuals.i_id') 599dfb2cda2SGreg Roach ->where('l4.l_type', '=', 'FAMS'); 600dfb2cda2SGreg Roach }); 6010b5fd0a6SGreg Roach $query->join('families AS spouse_families', static function (JoinClause $join): void { 602dfb2cda2SGreg Roach $join 603dfb2cda2SGreg Roach ->on('spouse_families.f_file', '=', 'l4.l_file') 604dfb2cda2SGreg Roach ->on('spouse_families.f_id', '=', 'l4.l_to'); 605dfb2cda2SGreg Roach }); 606dfb2cda2SGreg Roach } 607dfb2cda2SGreg Roach 608dfb2cda2SGreg Roach if ($indi_name) { 6090b5fd0a6SGreg Roach $query->join('name AS individual_name', static function (JoinClause $join): void { 610dfb2cda2SGreg Roach $join 611dfb2cda2SGreg Roach ->on('individual_name.n_file', '=', 'individuals.i_file') 612dfb2cda2SGreg Roach ->on('individual_name.n_id', '=', 'individuals.i_id'); 613dfb2cda2SGreg Roach }); 614dfb2cda2SGreg Roach } 615dfb2cda2SGreg Roach 61656946b7aSGreg Roach foreach (array_unique($indi_dates) as $indi_date) { 61756946b7aSGreg Roach $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void { 618dfb2cda2SGreg Roach $join 61956946b7aSGreg Roach ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file') 62056946b7aSGreg Roach ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id'); 621dfb2cda2SGreg Roach }); 622dfb2cda2SGreg Roach } 623dfb2cda2SGreg Roach 62456946b7aSGreg Roach foreach (array_unique($fam_dates) as $fam_date) { 62556946b7aSGreg Roach $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void { 626dfb2cda2SGreg Roach $join 627b06d667bSGreg Roach ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.f_file') 628b06d667bSGreg Roach ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.f_id'); 629dfb2cda2SGreg Roach }); 630dfb2cda2SGreg Roach } 631dfb2cda2SGreg Roach 632dfb2cda2SGreg Roach if ($indi_plac) { 6330b5fd0a6SGreg Roach $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { 634dfb2cda2SGreg Roach $join 635dfb2cda2SGreg Roach ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') 636dfb2cda2SGreg Roach ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); 637dfb2cda2SGreg Roach }); 6380b5fd0a6SGreg Roach $query->join('places AS individual_places', static function (JoinClause $join): void { 639dfb2cda2SGreg Roach $join 640dfb2cda2SGreg Roach ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') 641dfb2cda2SGreg Roach ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); 642dfb2cda2SGreg Roach }); 643dfb2cda2SGreg Roach } 644dfb2cda2SGreg Roach 645dfb2cda2SGreg Roach if ($fam_plac) { 64639c9a984SGreg Roach $query->join('placelinks AS family_placelinks', static function (JoinClause $join): void { 647dfb2cda2SGreg Roach $join 64839c9a984SGreg Roach ->on('family_placelinks.pl_file', '=', 'spouse_families.f_file') 64939c9a984SGreg Roach ->on('family_placelinks.pl_gid', '=', 'spouse_families.f_id'); 650dfb2cda2SGreg Roach }); 6510b5fd0a6SGreg Roach $query->join('places AS family_places', static function (JoinClause $join): void { 652dfb2cda2SGreg Roach $join 65339c9a984SGreg Roach ->on('family_places.p_file', '=', 'family_placelinks.pl_file') 65439c9a984SGreg Roach ->on('family_places.p_id', '=', 'family_placelinks.pl_p_id'); 655dfb2cda2SGreg Roach }); 656dfb2cda2SGreg Roach } 657dfb2cda2SGreg Roach 658dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 65956946b7aSGreg Roach $parts = explode(':', $field_name . ':::'); 6602f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:NAME:')) { 6612f86083cSGreg Roach switch ($field_name) { 6622f86083cSGreg Roach case 'INDI:NAME:GIVN': 663dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 664dfb2cda2SGreg Roach case 'EXACT': 665dfb2cda2SGreg Roach $query->where('individual_name.n_givn', '=', $field_value); 666dfb2cda2SGreg Roach break; 667dfb2cda2SGreg Roach case 'BEGINS': 66852550490SGreg Roach $query->where('individual_name.n_givn', DB::iLike(), $field_value . '%'); 669dfb2cda2SGreg Roach break; 670dfb2cda2SGreg Roach case 'CONTAINS': 67152550490SGreg Roach $query->where('individual_name.n_givn', DB::iLike(), '%' . $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, 'individual_name.n_soundex_givn_std', $sdx); 677dfb2cda2SGreg Roach } else { 678dfb2cda2SGreg Roach // No phonetic content? Use a substring match 67952550490SGreg Roach $query->where('individual_name.n_givn', DB::iLike(), '%' . $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, 'individual_name.n_soundex_givn_dm', $sdx); 687dfb2cda2SGreg Roach } else { 688dfb2cda2SGreg Roach // No phonetic content? Use a substring match 68952550490SGreg Roach $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); 690dfb2cda2SGreg Roach } 691dfb2cda2SGreg Roach break; 692dfb2cda2SGreg Roach } 693788f881dSGreg Roach unset($fields[$field_name]); 694dfb2cda2SGreg Roach break; 6952f86083cSGreg Roach case 'INDI:NAME:SURN': 696dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 697dfb2cda2SGreg Roach case 'EXACT': 698fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 699fc6d23eeSGreg Roach $query 700fc6d23eeSGreg Roach ->where('individual_name.n_surn', '=', $field_value) 701fc6d23eeSGreg Roach ->orWhere('individual_name.n_surname', '=', $field_value); 702fc6d23eeSGreg Roach }); 703dfb2cda2SGreg Roach break; 704dfb2cda2SGreg Roach case 'BEGINS': 705fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 706fc6d23eeSGreg Roach $query 70752550490SGreg Roach ->where('individual_name.n_surn', DB::iLike(), $field_value . '%') 70852550490SGreg Roach ->orWhere('individual_name.n_surname', DB::iLike(), $field_value . '%'); 709fc6d23eeSGreg Roach }); 710dfb2cda2SGreg Roach break; 711dfb2cda2SGreg Roach case 'CONTAINS': 712fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 713fc6d23eeSGreg Roach $query 71452550490SGreg Roach ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') 71552550490SGreg Roach ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); 716fc6d23eeSGreg Roach }); 717dfb2cda2SGreg Roach break; 718dfb2cda2SGreg Roach case 'SDX_STD': 719dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 720dfb2cda2SGreg Roach if ($sdx !== '') { 721dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); 722dfb2cda2SGreg Roach } else { 723dfb2cda2SGreg Roach // No phonetic content? Use a substring match 724fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 725fc6d23eeSGreg Roach $query 72652550490SGreg Roach ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') 72752550490SGreg Roach ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); 728fc6d23eeSGreg Roach }); 729dfb2cda2SGreg Roach } 730dfb2cda2SGreg Roach break; 731dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 732dfb2cda2SGreg Roach case 'SDX_DM': 733dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 734dfb2cda2SGreg Roach if ($sdx !== '') { 735dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); 736dfb2cda2SGreg Roach } else { 737dfb2cda2SGreg Roach // No phonetic content? Use a substring match 7389875d474SGreg Roach $query->where(function (Builder $query) use ($field_value): void { 7399875d474SGreg Roach $query 74052550490SGreg Roach ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') 74152550490SGreg Roach ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); 7429875d474SGreg Roach }); 743dfb2cda2SGreg Roach } 744dfb2cda2SGreg Roach break; 745dfb2cda2SGreg Roach } 746b4adec9eSGreg Roach unset($fields[$field_name]); 747dfb2cda2SGreg Roach break; 7482f86083cSGreg Roach case 'INDI:NAME:NICK': 7492f86083cSGreg Roach case 'INDI:NAME:_MARNM': 7502f86083cSGreg Roach case 'INDI:NAME:_HEB': 7512f86083cSGreg Roach case 'INDI:NAME:_AKA': 7522f86083cSGreg Roach $like = "%\n1 NAME%\n2 " . $parts[2] . ' %' . preg_quote($field_value, '/') . '%'; 75352550490SGreg Roach $query->where('individuals.i_gedcom', DB::iLike(), $like); 754dfb2cda2SGreg Roach break; 755dfb2cda2SGreg Roach } 7562f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':DATE')) { 757dfb2cda2SGreg Roach $date = new Date($field_value); 758dfb2cda2SGreg Roach if ($date->isOK()) { 759d8809d62SGreg Roach $delta = 365 * (int) ($modifiers[$field_name] ?? 0); 760dfb2cda2SGreg Roach $query 7612f86083cSGreg Roach ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) 7622f86083cSGreg Roach ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 7632f86083cSGreg Roach ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); 764dfb2cda2SGreg Roach } 765dfb2cda2SGreg Roach unset($fields[$field_name]); 7662f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':DATE')) { 767dfb2cda2SGreg Roach $date = new Date($field_value); 768dfb2cda2SGreg Roach if ($date->isOK()) { 769d8809d62SGreg Roach $delta = 365 * (int) ($modifiers[$field_name] ?? 0); 770dfb2cda2SGreg Roach $query 77156946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) 77256946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 77356946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); 774dfb2cda2SGreg Roach } 775dfb2cda2SGreg Roach unset($fields[$field_name]); 7762f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { 777dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 778ca1e6d47SGreg Roach $query->where('individual_places.p_id', '=', $field_value); 7792f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { 780dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 781ca1e6d47SGreg Roach $query->where('family_places.p_id', '=', $field_value); 7822f86083cSGreg Roach } elseif (str_starts_with($field_name, 'MOTHER:NAME:') || str_starts_with($field_name, 'FATHER:NAME:')) { 7832f86083cSGreg Roach $table = str_starts_with($field_name, 'FATHER:NAME:') ? 'father_name' : 'mother_name'; 7842f86083cSGreg Roach switch ($parts[2]) { 785dfb2cda2SGreg Roach case 'GIVN': 786dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 787dfb2cda2SGreg Roach case 'EXACT': 788dfb2cda2SGreg Roach $query->where($table . '.n_givn', '=', $field_value); 789dfb2cda2SGreg Roach break; 790dfb2cda2SGreg Roach case 'BEGINS': 79152550490SGreg Roach $query->where($table . '.n_givn', DB::iLike(), $field_value . '%'); 792dfb2cda2SGreg Roach break; 793dfb2cda2SGreg Roach case 'CONTAINS': 79452550490SGreg Roach $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); 795dfb2cda2SGreg Roach break; 796dfb2cda2SGreg Roach case 'SDX_STD': 797dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 798dfb2cda2SGreg Roach if ($sdx !== '') { 799dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); 800dfb2cda2SGreg Roach } else { 801dfb2cda2SGreg Roach // No phonetic content? Use a substring match 80252550490SGreg Roach $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); 803dfb2cda2SGreg Roach } 804dfb2cda2SGreg Roach break; 805dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 806dfb2cda2SGreg Roach case 'SDX_DM': 807dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 808dfb2cda2SGreg Roach if ($sdx !== '') { 809dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); 810dfb2cda2SGreg Roach } else { 811dfb2cda2SGreg Roach // No phonetic content? Use a substring match 81252550490SGreg Roach $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); 813dfb2cda2SGreg Roach } 814dfb2cda2SGreg Roach break; 815dfb2cda2SGreg Roach } 816dfb2cda2SGreg Roach break; 817dfb2cda2SGreg Roach case 'SURN': 818dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 819dfb2cda2SGreg Roach case 'EXACT': 820dfb2cda2SGreg Roach $query->where($table . '.n_surn', '=', $field_value); 821dfb2cda2SGreg Roach break; 822dfb2cda2SGreg Roach case 'BEGINS': 82352550490SGreg Roach $query->where($table . '.n_surn', DB::iLike(), $field_value . '%'); 824dfb2cda2SGreg Roach break; 825dfb2cda2SGreg Roach case 'CONTAINS': 82652550490SGreg Roach $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); 827dfb2cda2SGreg Roach break; 828dfb2cda2SGreg Roach case 'SDX_STD': 829dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 830dfb2cda2SGreg Roach if ($sdx !== '') { 831dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); 832dfb2cda2SGreg Roach } else { 833dfb2cda2SGreg Roach // No phonetic content? Use a substring match 83452550490SGreg Roach $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); 835dfb2cda2SGreg Roach } 836dfb2cda2SGreg Roach break; 837dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 838dfb2cda2SGreg Roach case 'SDX_DM': 839dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 840dfb2cda2SGreg Roach if ($sdx !== '') { 841dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); 842dfb2cda2SGreg Roach } else { 843dfb2cda2SGreg Roach // No phonetic content? Use a substring match 84452550490SGreg Roach $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); 845dfb2cda2SGreg Roach } 846dfb2cda2SGreg Roach break; 847dfb2cda2SGreg Roach } 848dfb2cda2SGreg Roach break; 849dfb2cda2SGreg Roach } 850dfb2cda2SGreg Roach unset($fields[$field_name]); 8512f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:')) { 852dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 853dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 85452550490SGreg Roach $query->where('spouse_families.f_gedcom', DB::iLike(), "%\n1 " . $parts[1] . ' %' . $field_value . '%'); 8552f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':TYPE')) { 856dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 85752550490SGreg Roach $query->where('individuals.i_gedcom', DB::iLike(), "%\n1 " . $parts[1] . "%\n2 TYPE %" . $field_value . '%'); 8582f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:')) { 859dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 860dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 86152550490SGreg Roach $query->where('individuals.i_gedcom', DB::iLike(), "%\n1 " . $parts[1] . '%' . $parts[2] . '%' . $field_value . '%'); 862dfb2cda2SGreg Roach } 863dfb2cda2SGreg Roach } 864b4adec9eSGreg Roach 865dfb2cda2SGreg Roach return $query 866dfb2cda2SGreg Roach ->get() 86752a8ef61SGreg Roach ->each($this->rowLimiter()) 868d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 869dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()) 870ca1e6d47SGreg Roach ->filter(static function (Individual $individual) use ($fields, $tree): bool { 871c5457947SGreg Roach // Check for searches which were only partially matched by SQL 872dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 873c5457947SGreg Roach $parts = explode(':', $field_name . '::::'); 874dfb2cda2SGreg Roach 8752f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:NAME:') && $field_name !== 'INDI:NAME:GIVN' && $field_name !== 'INDI:NAME:SURN') { 8762f86083cSGreg Roach $regex = '/\n1 NAME.*(?:\n2.*)*\n2 ' . $parts[2] . ' .*' . preg_quote($field_value, '/') . '/i'; 877b4adec9eSGreg Roach 878ef475b14SGreg Roach if (preg_match($regex, $individual->gedcom()) === 1) { 879b4adec9eSGreg Roach continue; 880b4adec9eSGreg Roach } 881b4adec9eSGreg Roach 882b4adec9eSGreg Roach return false; 883b4adec9eSGreg Roach } 884b4adec9eSGreg Roach 8852f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { 886ca1e6d47SGreg Roach $place = Place::find((int) $field_value, $tree); 887ca1e6d47SGreg Roach 8882f86083cSGreg Roach foreach ($individual->facts([$parts[1]]) as $fact) { 889ca1e6d47SGreg Roach if ($fact->place()->gedcomName() === $place->gedcomName() || str_ends_with($fact->place()->gedcomName(), ', ' . $place->gedcomName())) { 890c5457947SGreg Roach continue 2; 891dfb2cda2SGreg Roach } 892a41e65f0SGreg Roach } 893c5457947SGreg Roach return false; 894c5457947SGreg Roach } 895c5457947SGreg Roach 8962f86083cSGreg Roach if (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { 897ca1e6d47SGreg Roach $place = Place::find((int) $field_value, $tree); 898ca1e6d47SGreg Roach 89939ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 900a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 901ca1e6d47SGreg Roach if ($fact->place()->gedcomName() === $place->gedcomName() || str_ends_with($fact->place()->gedcomName(), ', ' . $place->gedcomName())) { 9023b3576aeSGreg Roach continue 3; 903a41e65f0SGreg Roach } 904dfb2cda2SGreg Roach } 905dfb2cda2SGreg Roach } 906c5457947SGreg Roach return false; 907c5457947SGreg Roach } 908c5457947SGreg Roach 909ca1e6d47SGreg Roach $regex = '/' . preg_quote($field_value, '/') . '/i'; 910ca1e6d47SGreg Roach 9112f86083cSGreg Roach if ($field_name === 'INDI:FACT:TYPE' || $field_name === 'INDI:EVEN:TYPE' || $field_name === 'INDI:CHAN:_WT_USER') { 9122f86083cSGreg Roach foreach ($individual->facts([$parts[1]]) as $fact) { 913ef475b14SGreg Roach if (preg_match($regex, $fact->attribute($parts[2])) === 1) { 9142f86083cSGreg Roach continue 2; 9152f86083cSGreg Roach } 9162f86083cSGreg Roach } 9172f86083cSGreg Roach 9182f86083cSGreg Roach return false; 9192f86083cSGreg Roach } 9202f86083cSGreg Roach 9212f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:')) { 9222f86083cSGreg Roach foreach ($individual->facts([$parts[1]]) as $fact) { 923ef475b14SGreg Roach if (preg_match($regex, $fact->value()) === 1) { 9242f86083cSGreg Roach continue 2; 9252f86083cSGreg Roach } 9262f86083cSGreg Roach } 9272f86083cSGreg Roach 9282f86083cSGreg Roach return false; 9292f86083cSGreg Roach } 9302f86083cSGreg Roach 9312f86083cSGreg Roach if (str_starts_with($field_name, 'FAM:')) { 93239ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 933a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 934ef475b14SGreg Roach if (preg_match($regex, $fact->value()) === 1) { 935c5457947SGreg Roach continue 3; 936a41e65f0SGreg Roach } 937dfb2cda2SGreg Roach } 938dfb2cda2SGreg Roach } 939c5457947SGreg Roach return false; 940c5457947SGreg Roach } 941c5457947SGreg Roach } 942dfb2cda2SGreg Roach 943dfb2cda2SGreg Roach return true; 944dfb2cda2SGreg Roach }); 945dfb2cda2SGreg Roach } 946dfb2cda2SGreg Roach 947dfb2cda2SGreg Roach /** 948dfb2cda2SGreg Roach * @param string $soundex 949dfb2cda2SGreg Roach * @param string $lastname 950dfb2cda2SGreg Roach * @param string $firstname 951dfb2cda2SGreg Roach * @param string $place 95209482a55SGreg Roach * @param array<Tree> $search_trees 953dfb2cda2SGreg Roach * 95436779af1SGreg Roach * @return Collection<int,Individual> 955dfb2cda2SGreg Roach */ 956dfb2cda2SGreg Roach public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection 957dfb2cda2SGreg Roach { 958dfb2cda2SGreg Roach switch ($soundex) { 959dfb2cda2SGreg Roach default: 960dfb2cda2SGreg Roach case 'Russell': 961dfb2cda2SGreg Roach $givn_sdx = Soundex::russell($firstname); 962dfb2cda2SGreg Roach $surn_sdx = Soundex::russell($lastname); 963dfb2cda2SGreg Roach $plac_sdx = Soundex::russell($place); 964dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_std'; 965dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_std'; 966dfb2cda2SGreg Roach $plac_field = 'p_std_soundex'; 967dfb2cda2SGreg Roach break; 968dfb2cda2SGreg Roach case 'DaitchM': 969dfb2cda2SGreg Roach $givn_sdx = Soundex::daitchMokotoff($firstname); 970dfb2cda2SGreg Roach $surn_sdx = Soundex::daitchMokotoff($lastname); 971dfb2cda2SGreg Roach $plac_sdx = Soundex::daitchMokotoff($place); 972dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_dm'; 973dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_dm'; 974dfb2cda2SGreg Roach $plac_field = 'p_dm_soundex'; 975dfb2cda2SGreg Roach break; 976dfb2cda2SGreg Roach } 977dfb2cda2SGreg Roach 978dfb2cda2SGreg Roach // Nothing to search for? Return nothing. 979dfb2cda2SGreg Roach if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') { 9803fda39a7SGreg Roach return new Collection(); 981dfb2cda2SGreg Roach } 982dfb2cda2SGreg Roach 983dfb2cda2SGreg Roach $query = DB::table('individuals') 984dfb2cda2SGreg Roach ->select(['individuals.*']) 985dfb2cda2SGreg Roach ->distinct(); 986dfb2cda2SGreg Roach 987dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $search_trees); 988dfb2cda2SGreg Roach 989dfb2cda2SGreg Roach if ($plac_sdx !== '') { 9900b5fd0a6SGreg Roach $query->join('placelinks', static function (JoinClause $join): void { 991dfb2cda2SGreg Roach $join 992dfb2cda2SGreg Roach ->on('placelinks.pl_file', '=', 'individuals.i_file') 993dfb2cda2SGreg Roach ->on('placelinks.pl_gid', '=', 'individuals.i_id'); 994dfb2cda2SGreg Roach }); 9950b5fd0a6SGreg Roach $query->join('places', static function (JoinClause $join): void { 996dfb2cda2SGreg Roach $join 997dfb2cda2SGreg Roach ->on('places.p_file', '=', 'placelinks.pl_file') 998dfb2cda2SGreg Roach ->on('places.p_id', '=', 'placelinks.pl_p_id'); 999dfb2cda2SGreg Roach }); 1000dfb2cda2SGreg Roach 1001dfb2cda2SGreg Roach $this->wherePhonetic($query, $plac_field, $plac_sdx); 1002dfb2cda2SGreg Roach } 1003dfb2cda2SGreg Roach 1004dfb2cda2SGreg Roach if ($givn_sdx !== '' || $surn_sdx !== '') { 10050b5fd0a6SGreg Roach $query->join('name', static function (JoinClause $join): void { 1006dfb2cda2SGreg Roach $join 1007dfb2cda2SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 1008dfb2cda2SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 1009dfb2cda2SGreg Roach }); 1010dfb2cda2SGreg Roach 1011dfb2cda2SGreg Roach $this->wherePhonetic($query, $givn_field, $givn_sdx); 1012dfb2cda2SGreg Roach $this->wherePhonetic($query, $surn_field, $surn_sdx); 1013dfb2cda2SGreg Roach } 1014dfb2cda2SGreg Roach 1015dfb2cda2SGreg Roach return $query 1016dfb2cda2SGreg Roach ->get() 101752a8ef61SGreg Roach ->each($this->rowLimiter()) 1018d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 1019dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()); 1020dfb2cda2SGreg Roach } 1021dfb2cda2SGreg Roach 1022dfb2cda2SGreg Roach /** 102332cd2800SGreg Roach * Paginate a search query. 102432cd2800SGreg Roach * 102532cd2800SGreg Roach * @param Builder $query Searches the database for the desired records. 102632cd2800SGreg Roach * @param Closure $row_mapper Converts a row from the query into a record. 1027a7a24840SGreg Roach * @param Closure $row_filter 102832cd2800SGreg Roach * @param int $offset Skip this many rows. 102932cd2800SGreg Roach * @param int $limit Take this many rows. 103032cd2800SGreg Roach * 103136779af1SGreg Roach * @return Collection<int,mixed> 103232cd2800SGreg Roach */ 1033a7a24840SGreg Roach private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection 103432cd2800SGreg Roach { 103532cd2800SGreg Roach $collection = new Collection(); 103632cd2800SGreg Roach 103732cd2800SGreg Roach foreach ($query->cursor() as $row) { 103832cd2800SGreg Roach $record = $row_mapper($row); 10398aaf5f58SGreg Roach // searchIndividualNames() and searchFamilyNames() can return duplicate rows, 10408aaf5f58SGreg Roach // where individuals have multiple names - and we need to sort results by name. 10418aaf5f58SGreg Roach if ($collection->containsStrict($record)) { 10428aaf5f58SGreg Roach continue; 10438aaf5f58SGreg Roach } 1044b68caec6SGreg Roach // If the object has a method "canShow()", then use it to filter for privacy. 1045a7a24840SGreg Roach if ($row_filter($record)) { 104632cd2800SGreg Roach if ($offset > 0) { 104732cd2800SGreg Roach $offset--; 104832cd2800SGreg Roach } else { 104932cd2800SGreg Roach if ($limit > 0) { 105032cd2800SGreg Roach $collection->push($record); 105132cd2800SGreg Roach } 105232cd2800SGreg Roach 105332cd2800SGreg Roach $limit--; 105432cd2800SGreg Roach 105532cd2800SGreg Roach if ($limit === 0) { 105632cd2800SGreg Roach break; 105732cd2800SGreg Roach } 105832cd2800SGreg Roach } 105932cd2800SGreg Roach } 106032cd2800SGreg Roach } 106132cd2800SGreg Roach 106232cd2800SGreg Roach return $collection; 106332cd2800SGreg Roach } 1064a7a24840SGreg Roach 1065a7a24840SGreg Roach /** 1066a7a24840SGreg Roach * Apply search filters to a SQL query column. Apply collation rules to MySQL. 1067a7a24840SGreg Roach * 1068a7a24840SGreg Roach * @param Builder $query 106955c65200SGreg Roach * @param Expression|string $column 107009482a55SGreg Roach * @param array<string> $search_terms 1071a7a24840SGreg Roach */ 107255c65200SGreg Roach private function whereSearch(Builder $query, Expression|string $column, array $search_terms): void 1073a7a24840SGreg Roach { 1074a7a24840SGreg Roach foreach ($search_terms as $search_term) { 107552550490SGreg Roach $query->where($column, DB::iLike(), '%' . addcslashes($search_term, '\\%_') . '%'); 1076a7a24840SGreg Roach } 1077a7a24840SGreg Roach } 1078a7a24840SGreg Roach 1079a7a24840SGreg Roach /** 10802d686e68SGreg Roach * Apply soundex search filters to a SQL query column. 10812d686e68SGreg Roach * 10822d686e68SGreg Roach * @param Builder $query 10832d686e68SGreg Roach * @param Expression|string $field 10842d686e68SGreg Roach * @param string $soundex 10852d686e68SGreg Roach */ 10862d686e68SGreg Roach private function wherePhonetic(Builder $query, $field, string $soundex): void 10872d686e68SGreg Roach { 10882d686e68SGreg Roach if ($soundex !== '') { 1089ebb4f7d9SGreg Roach $query->where(function (Builder $query) use ($soundex, $field): void { 10902d686e68SGreg Roach foreach (explode(':', $soundex) as $sdx) { 109152550490SGreg Roach $query->orWhere($field, DB::iLike(), '%' . $sdx . '%'); 10922d686e68SGreg Roach } 10932d686e68SGreg Roach }); 10942d686e68SGreg Roach } 10952d686e68SGreg Roach } 10962d686e68SGreg Roach 10972d686e68SGreg Roach /** 1098a7a24840SGreg Roach * @param Builder $query 1099a7a24840SGreg Roach * @param string $tree_id_field 110009482a55SGreg Roach * @param array<Tree> $trees 1101a7a24840SGreg Roach */ 1102a7a24840SGreg Roach private function whereTrees(Builder $query, string $tree_id_field, array $trees): void 1103a7a24840SGreg Roach { 1104f25fc0f9SGreg Roach $tree_ids = array_map(static fn (Tree $tree): int => $tree->id(), $trees); 1105a7a24840SGreg Roach 1106a7a24840SGreg Roach $query->whereIn($tree_id_field, $tree_ids); 1107a7a24840SGreg Roach } 1108a7a24840SGreg Roach 1109d5ad3db0SGreg Roach /** 1110d5ad3db0SGreg Roach * Find the media object that uses a particular media file. 1111d5ad3db0SGreg Roach * 1112d5ad3db0SGreg Roach * @param string $file 1113d5ad3db0SGreg Roach * 111409482a55SGreg Roach * @return array<Media> 1115d5ad3db0SGreg Roach */ 1116d5ad3db0SGreg Roach public function findMediaObjectsForMediaFile(string $file): array 1117d5ad3db0SGreg Roach { 1118d5ad3db0SGreg Roach return DB::table('media') 1119d5ad3db0SGreg Roach ->join('media_file', static function (JoinClause $join): void { 1120d5ad3db0SGreg Roach $join 1121d5ad3db0SGreg Roach ->on('media_file.m_file', '=', 'media.m_file') 1122d5ad3db0SGreg Roach ->on('media_file.m_id', '=', 'media.m_id'); 1123d5ad3db0SGreg Roach }) 1124d5ad3db0SGreg Roach ->join('gedcom_setting', 'media.m_file', '=', 'gedcom_setting.gedcom_id') 1125d5ad3db0SGreg Roach ->where(new Expression('setting_value || multimedia_file_refn'), '=', $file) 1126d5ad3db0SGreg Roach ->select(['media.*']) 1127d5ad3db0SGreg Roach ->distinct() 1128d5ad3db0SGreg Roach ->get() 1129d5ad3db0SGreg Roach ->map($this->mediaRowMapper()) 1130d5ad3db0SGreg Roach ->all(); 1131d5ad3db0SGreg Roach } 1132d5ad3db0SGreg Roach 1133a7a24840SGreg Roach /** 1134a7a24840SGreg Roach * A closure to filter records by privacy-filtered GEDCOM data. 1135a7a24840SGreg Roach * 11365d4b7ec2SGreg Roach * @param array<string> $search_terms 1137a7a24840SGreg Roach * 1138c6921a17SGreg Roach * @return Closure(GedcomRecord):bool 1139a7a24840SGreg Roach */ 1140a7a24840SGreg Roach private function rawGedcomFilter(array $search_terms): Closure 1141a7a24840SGreg Roach { 11426c2179e2SGreg Roach return static function (GedcomRecord $record) use ($search_terms): bool { 1143a7a24840SGreg Roach // Ignore non-genealogy fields 1144faa5e163SGreg Roach $gedcom = preg_replace('/\n\d (?:_UID|_WT_USER) .*/', '', $record->gedcom()); 1145a7a24840SGreg Roach 1146a7a24840SGreg Roach // Ignore matches in links 1147a7a24840SGreg Roach $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom); 1148a7a24840SGreg Roach 1149a7a24840SGreg Roach // Re-apply the filtering 1150a7a24840SGreg Roach foreach ($search_terms as $search_term) { 1151a7a24840SGreg Roach if (mb_stripos($gedcom, $search_term) === false) { 1152a7a24840SGreg Roach return false; 1153a7a24840SGreg Roach } 1154a7a24840SGreg Roach } 1155a7a24840SGreg Roach 1156a7a24840SGreg Roach return true; 1157a7a24840SGreg Roach }; 1158a7a24840SGreg Roach } 115952a8ef61SGreg Roach 116052a8ef61SGreg Roach /** 116152a8ef61SGreg Roach * Searching for short or common text can give more results than the system can process. 116252a8ef61SGreg Roach * 116352a8ef61SGreg Roach * @param int $limit 116452a8ef61SGreg Roach * 1165c6921a17SGreg Roach * @return Closure():void 116652a8ef61SGreg Roach */ 1167645e3037SGreg Roach private function rowLimiter(int $limit = self::MAX_SEARCH_RESULTS): Closure 116852a8ef61SGreg Roach { 11696c2179e2SGreg Roach return static function () use ($limit): void { 117052a8ef61SGreg Roach static $n = 0; 117152a8ef61SGreg Roach 117252a8ef61SGreg Roach if (++$n > $limit) { 117352a8ef61SGreg Roach $message = I18N::translate('The search returned too many results.'); 117452a8ef61SGreg Roach 1175d501c45dSGreg Roach throw new HttpServiceUnavailableException($message); 117652a8ef61SGreg Roach } 117752a8ef61SGreg Roach }; 117852a8ef61SGreg Roach } 1179d5ad3db0SGreg Roach 1180d5ad3db0SGreg Roach /** 1181d5ad3db0SGreg Roach * Convert a row from any tree in the families table into a family object. 1182d5ad3db0SGreg Roach * 1183c6921a17SGreg Roach * @return Closure(object):Family 1184d5ad3db0SGreg Roach */ 1185d5ad3db0SGreg Roach private function familyRowMapper(): Closure 1186d5ad3db0SGreg Roach { 1187f70bcff5SGreg Roach return function (object $row): Family { 1188d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->f_file); 1189d5ad3db0SGreg Roach 11906b9cb339SGreg Roach return Registry::familyFactory()->mapper($tree)($row); 1191d5ad3db0SGreg Roach }; 1192d5ad3db0SGreg Roach } 1193d5ad3db0SGreg Roach 1194d5ad3db0SGreg Roach /** 1195d5ad3db0SGreg Roach * Convert a row from any tree in the individuals table into an individual object. 1196d5ad3db0SGreg Roach * 1197c6921a17SGreg Roach * @return Closure(object):Individual 1198d5ad3db0SGreg Roach */ 1199d5ad3db0SGreg Roach private function individualRowMapper(): Closure 1200d5ad3db0SGreg Roach { 1201f70bcff5SGreg Roach return function (object $row): Individual { 1202d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->i_file); 1203d5ad3db0SGreg Roach 12046b9cb339SGreg Roach return Registry::individualFactory()->mapper($tree)($row); 1205d5ad3db0SGreg Roach }; 1206d5ad3db0SGreg Roach } 1207d5ad3db0SGreg Roach 1208d5ad3db0SGreg Roach /** 1209d4ac8227SGreg Roach * Convert a row from any tree in the media table into a location object. 1210d4ac8227SGreg Roach * 1211c6921a17SGreg Roach * @return Closure(object):Location 1212d4ac8227SGreg Roach */ 1213d4ac8227SGreg Roach private function locationRowMapper(): Closure 1214d4ac8227SGreg Roach { 1215f70bcff5SGreg Roach return function (object $row): Location { 12169d28ed8bSGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1217d4ac8227SGreg Roach 1218d4ac8227SGreg Roach return Registry::locationFactory()->mapper($tree)($row); 1219d4ac8227SGreg Roach }; 1220d4ac8227SGreg Roach } 1221d4ac8227SGreg Roach 1222d4ac8227SGreg Roach /** 1223d5ad3db0SGreg Roach * Convert a row from any tree in the media table into an media object. 1224d5ad3db0SGreg Roach * 1225c6921a17SGreg Roach * @return Closure(object):Media 1226d5ad3db0SGreg Roach */ 1227d5ad3db0SGreg Roach private function mediaRowMapper(): Closure 1228d5ad3db0SGreg Roach { 1229f70bcff5SGreg Roach return function (object $row): Media { 1230d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->m_file); 1231d5ad3db0SGreg Roach 12326b9cb339SGreg Roach return Registry::mediaFactory()->mapper($tree)($row); 1233d5ad3db0SGreg Roach }; 1234d5ad3db0SGreg Roach } 1235d5ad3db0SGreg Roach 1236d5ad3db0SGreg Roach /** 1237d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a note object. 1238d5ad3db0SGreg Roach * 1239*73f6d2b5SGreg Roach * @return Closure(object):Note 1240d5ad3db0SGreg Roach */ 1241d5ad3db0SGreg Roach private function noteRowMapper(): Closure 1242d5ad3db0SGreg Roach { 1243f70bcff5SGreg Roach return function (object $row): Note { 1244d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1245d5ad3db0SGreg Roach 12466b9cb339SGreg Roach return Registry::noteFactory()->mapper($tree)($row); 1247d5ad3db0SGreg Roach }; 1248d5ad3db0SGreg Roach } 1249d5ad3db0SGreg Roach 1250d5ad3db0SGreg Roach /** 1251d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a repository object. 1252d5ad3db0SGreg Roach * 1253*73f6d2b5SGreg Roach * @return Closure(object):Repository 1254d5ad3db0SGreg Roach */ 1255d5ad3db0SGreg Roach private function repositoryRowMapper(): Closure 1256d5ad3db0SGreg Roach { 1257f70bcff5SGreg Roach return function (object $row): Repository { 1258d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1259d5ad3db0SGreg Roach 12606b9cb339SGreg Roach return Registry::repositoryFactory()->mapper($tree)($row); 1261d5ad3db0SGreg Roach }; 1262d5ad3db0SGreg Roach } 1263d5ad3db0SGreg Roach 1264d5ad3db0SGreg Roach /** 1265701f5d18SGreg Roach * Convert a row from any tree in the other table into a note object. 1266701f5d18SGreg Roach * 1267c6921a17SGreg Roach * @return Closure(object):SharedNote 1268701f5d18SGreg Roach */ 1269701f5d18SGreg Roach private function sharedNoteRowMapper(): Closure 1270701f5d18SGreg Roach { 1271701f5d18SGreg Roach return function (object $row): Note { 1272701f5d18SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1273701f5d18SGreg Roach 1274701f5d18SGreg Roach return Registry::sharedNoteFactory()->mapper($tree)($row); 1275701f5d18SGreg Roach }; 1276701f5d18SGreg Roach } 1277701f5d18SGreg Roach 1278701f5d18SGreg Roach /** 1279d5ad3db0SGreg Roach * Convert a row from any tree in the sources table into a source object. 1280d5ad3db0SGreg Roach * 1281*73f6d2b5SGreg Roach * @return Closure(object):Source 1282d5ad3db0SGreg Roach */ 1283d5ad3db0SGreg Roach private function sourceRowMapper(): Closure 1284d5ad3db0SGreg Roach { 1285f70bcff5SGreg Roach return function (object $row): Source { 1286d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->s_file); 1287d5ad3db0SGreg Roach 12886b9cb339SGreg Roach return Registry::sourceFactory()->mapper($tree)($row); 1289d5ad3db0SGreg Roach }; 1290d5ad3db0SGreg Roach } 1291d5ad3db0SGreg Roach 1292d5ad3db0SGreg Roach /** 1293d4ac8227SGreg Roach * Convert a row from any tree in the other table into a submission object. 1294d4ac8227SGreg Roach * 1295c6921a17SGreg Roach * @return Closure(object):Submission 1296d4ac8227SGreg Roach */ 1297d4ac8227SGreg Roach private function submissionRowMapper(): Closure 1298d4ac8227SGreg Roach { 1299f70bcff5SGreg Roach return function (object $row): Submission { 1300d4ac8227SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1301d4ac8227SGreg Roach 1302d4ac8227SGreg Roach return Registry::submissionFactory()->mapper($tree)($row); 1303d4ac8227SGreg Roach }; 1304d4ac8227SGreg Roach } 1305d4ac8227SGreg Roach 1306d4ac8227SGreg Roach /** 1307d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a submitter object. 1308d5ad3db0SGreg Roach * 1309c6921a17SGreg Roach * @return Closure(object):Submitter 1310d5ad3db0SGreg Roach */ 1311d5ad3db0SGreg Roach private function submitterRowMapper(): Closure 1312d5ad3db0SGreg Roach { 1313f70bcff5SGreg Roach return function (object $row): Submitter { 1314d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1315d5ad3db0SGreg Roach 13166b9cb339SGreg Roach return Registry::submitterFactory()->mapper($tree)($row); 1317d5ad3db0SGreg Roach }; 1318d5ad3db0SGreg Roach } 131932cd2800SGreg Roach} 1320