132cd2800SGreg Roach<?php 23976b470SGreg Roach 332cd2800SGreg Roach/** 432cd2800SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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; 2432cd2800SGreg Roachuse Fisharebest\Webtrees\Family; 25a7a24840SGreg Roachuse Fisharebest\Webtrees\Gedcom; 2632cd2800SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 2781b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpServiceUnavailableException; 2852a8ef61SGreg Roachuse Fisharebest\Webtrees\I18N; 2932cd2800SGreg Roachuse Fisharebest\Webtrees\Individual; 3081b729d3SGreg Roachuse Fisharebest\Webtrees\Location; 3132cd2800SGreg Roachuse Fisharebest\Webtrees\Media; 3232cd2800SGreg Roachuse Fisharebest\Webtrees\Note; 33b68caec6SGreg Roachuse Fisharebest\Webtrees\Place; 3481b729d3SGreg Roachuse Fisharebest\Webtrees\Registry; 3532cd2800SGreg Roachuse Fisharebest\Webtrees\Repository; 36*701f5d18SGreg Roachuse Fisharebest\Webtrees\SharedNote; 372d686e68SGreg Roachuse Fisharebest\Webtrees\Soundex; 3832cd2800SGreg Roachuse Fisharebest\Webtrees\Source; 39d4ac8227SGreg Roachuse Fisharebest\Webtrees\Submission; 40b5c8fd7eSGreg Roachuse Fisharebest\Webtrees\Submitter; 4132cd2800SGreg Roachuse Fisharebest\Webtrees\Tree; 4232cd2800SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 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 * SearchService constructor. 75d5ad3db0SGreg Roach * 76d5ad3db0SGreg Roach * @param TreeService $tree_service 77d5ad3db0SGreg Roach */ 783959eeb6SGreg Roach public function __construct( 793959eeb6SGreg Roach TreeService $tree_service 803959eeb6SGreg Roach ) { 81d5ad3db0SGreg Roach $this->tree_service = $tree_service; 82d5ad3db0SGreg Roach } 83d5ad3db0SGreg Roach 84a7a24840SGreg Roach /** 8509482a55SGreg Roach * @param array<Tree> $trees 8609482a55SGreg Roach * @param array<string> $search 87a7a24840SGreg Roach * 8836779af1SGreg Roach * @return Collection<int,Family> 89a7a24840SGreg Roach */ 90a7a24840SGreg Roach public function searchFamilies(array $trees, array $search): Collection 91a7a24840SGreg Roach { 92a7a24840SGreg Roach $query = DB::table('families'); 93a7a24840SGreg Roach 94a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 95a7a24840SGreg Roach $this->whereSearch($query, 'f_gedcom', $search); 96a7a24840SGreg Roach 97a7a24840SGreg Roach return $query 98a7a24840SGreg Roach ->get() 9952a8ef61SGreg Roach ->each($this->rowLimiter()) 100d5ad3db0SGreg Roach ->map($this->familyRowMapper()) 101a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 1027f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 103a7a24840SGreg Roach } 104a7a24840SGreg Roach 10532cd2800SGreg Roach /** 10632cd2800SGreg Roach * Search for families by name. 10732cd2800SGreg Roach * 10809482a55SGreg Roach * @param array<Tree> $trees 10909482a55SGreg Roach * @param array<string> $search 11032cd2800SGreg Roach * @param int $offset 11132cd2800SGreg Roach * @param int $limit 11232cd2800SGreg Roach * 11336779af1SGreg Roach * @return Collection<int,Family> 11432cd2800SGreg Roach */ 115a7a24840SGreg Roach public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 11632cd2800SGreg Roach { 11732cd2800SGreg Roach $query = DB::table('families') 118ac499332SGreg Roach ->leftJoin('name AS husb_name', static function (JoinClause $join): void { 11932cd2800SGreg Roach $join 12032cd2800SGreg Roach ->on('husb_name.n_file', '=', 'families.f_file') 121ac499332SGreg Roach ->on('husb_name.n_id', '=', 'families.f_husb') 122ac499332SGreg Roach ->where('husb_name.n_type', '<>', '_MARNM'); 12332cd2800SGreg Roach }) 124ac499332SGreg Roach ->leftJoin('name AS wife_name', static function (JoinClause $join): void { 12532cd2800SGreg Roach $join 12632cd2800SGreg Roach ->on('wife_name.n_file', '=', 'families.f_file') 127ac499332SGreg Roach ->on('wife_name.n_id', '=', 'families.f_wife') 128ac499332SGreg Roach ->where('wife_name.n_type', '<>', '_MARNM'); 129ac499332SGreg Roach }); 130a7a24840SGreg Roach 131a7a24840SGreg Roach $prefix = DB::connection()->getTablePrefix(); 132a69f5655SGreg Roach $field = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')"); 133a7a24840SGreg Roach 134a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 135a7a24840SGreg Roach $this->whereSearch($query, $field, $search); 136a7a24840SGreg Roach 137a7a24840SGreg Roach $query 13832cd2800SGreg Roach ->orderBy('husb_name.n_sort') 13932cd2800SGreg Roach ->orderBy('wife_name.n_sort') 1408aaf5f58SGreg Roach ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort']); 14132cd2800SGreg Roach 142d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 143a7a24840SGreg Roach } 144a7a24840SGreg Roach 145a7a24840SGreg Roach /** 146dbe53437SGreg Roach * @param Place $place 147dbe53437SGreg Roach * 14836779af1SGreg Roach * @return Collection<int,Family> 149dbe53437SGreg Roach */ 150dbe53437SGreg Roach public function searchFamiliesInPlace(Place $place): Collection 151dbe53437SGreg Roach { 152dbe53437SGreg Roach return DB::table('families') 153dbe53437SGreg Roach ->join('placelinks', static function (JoinClause $query) { 154dbe53437SGreg Roach $query 155dbe53437SGreg Roach ->on('families.f_file', '=', 'placelinks.pl_file') 156dbe53437SGreg Roach ->on('families.f_id', '=', 'placelinks.pl_gid'); 157dbe53437SGreg Roach }) 158dbe53437SGreg Roach ->where('f_file', '=', $place->tree()->id()) 159dbe53437SGreg Roach ->where('pl_p_id', '=', $place->id()) 160dbe53437SGreg Roach ->select(['families.*']) 161dbe53437SGreg Roach ->get() 162dbe53437SGreg Roach ->each($this->rowLimiter()) 163dbe53437SGreg Roach ->map($this->familyRowMapper()) 164dbe53437SGreg Roach ->filter(GedcomRecord::accessFilter()); 165dbe53437SGreg Roach } 166dbe53437SGreg Roach 167dbe53437SGreg Roach /** 16809482a55SGreg Roach * @param array<Tree> $trees 16909482a55SGreg Roach * @param array<string> $search 170a7a24840SGreg Roach * 17136779af1SGreg Roach * @return Collection<int,Individual> 172a7a24840SGreg Roach */ 173a7a24840SGreg Roach public function searchIndividuals(array $trees, array $search): Collection 174a7a24840SGreg Roach { 175a7a24840SGreg Roach $query = DB::table('individuals'); 176a7a24840SGreg Roach 177a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 178a7a24840SGreg Roach $this->whereSearch($query, 'i_gedcom', $search); 179a7a24840SGreg Roach 180a7a24840SGreg Roach return $query 181a7a24840SGreg Roach ->get() 18252a8ef61SGreg Roach ->each($this->rowLimiter()) 183d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 184a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 1857f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 18632cd2800SGreg Roach } 18732cd2800SGreg Roach 18832cd2800SGreg Roach /** 18932cd2800SGreg Roach * Search for individuals by name. 19032cd2800SGreg Roach * 19109482a55SGreg Roach * @param array<Tree> $trees 19209482a55SGreg Roach * @param array<string> $search 19332cd2800SGreg Roach * @param int $offset 19432cd2800SGreg Roach * @param int $limit 19532cd2800SGreg Roach * 19636779af1SGreg Roach * @return Collection<int,Individual> 19732cd2800SGreg Roach */ 198a7a24840SGreg Roach public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 19932cd2800SGreg Roach { 20032cd2800SGreg Roach $query = DB::table('individuals') 2010b5fd0a6SGreg Roach ->join('name', static function (JoinClause $join): void { 20232cd2800SGreg Roach $join 20332cd2800SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 204a7a24840SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 20532cd2800SGreg Roach }) 206e84cf2deSGreg Roach ->orderBy('n_sort') 207685de081SGreg Roach ->select(['individuals.*', 'n_sort']); 20832cd2800SGreg Roach 209a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 210a7a24840SGreg Roach $this->whereSearch($query, 'n_full', $search); 211a7a24840SGreg Roach 212d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 21332cd2800SGreg Roach } 21432cd2800SGreg Roach 21532cd2800SGreg Roach /** 216dbe53437SGreg Roach * @param Place $place 217dbe53437SGreg Roach * 21836779af1SGreg Roach * @return Collection<int,Individual> 219dbe53437SGreg Roach */ 220dbe53437SGreg Roach public function searchIndividualsInPlace(Place $place): Collection 221dbe53437SGreg Roach { 222dbe53437SGreg Roach return DB::table('individuals') 223dbe53437SGreg Roach ->join('placelinks', static function (JoinClause $join) { 224dbe53437SGreg Roach $join 225dbe53437SGreg Roach ->on('i_file', '=', 'pl_file') 226dbe53437SGreg Roach ->on('i_id', '=', 'pl_gid'); 227dbe53437SGreg Roach }) 228dbe53437SGreg Roach ->where('i_file', '=', $place->tree()->id()) 229dbe53437SGreg Roach ->where('pl_p_id', '=', $place->id()) 230dbe53437SGreg Roach ->select(['individuals.*']) 231dbe53437SGreg Roach ->get() 232dbe53437SGreg Roach ->each($this->rowLimiter()) 233dbe53437SGreg Roach ->map($this->individualRowMapper()) 234dbe53437SGreg Roach ->filter(GedcomRecord::accessFilter()); 235dbe53437SGreg Roach } 236dbe53437SGreg Roach 237dbe53437SGreg Roach /** 238d4ac8227SGreg Roach * Search for submissions. 239d4ac8227SGreg Roach * 24009482a55SGreg Roach * @param array<Tree> $trees 24109482a55SGreg Roach * @param array<string> $search 242d4ac8227SGreg Roach * @param int $offset 243d4ac8227SGreg Roach * @param int $limit 244d4ac8227SGreg Roach * 24536779af1SGreg Roach * @return Collection<int,Location> 246d4ac8227SGreg Roach */ 247d4ac8227SGreg Roach public function searchLocations(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 248d4ac8227SGreg Roach { 249d4ac8227SGreg Roach $query = DB::table('other') 250058ba724SGreg Roach ->where('o_type', '=', Location::RECORD_TYPE); 251d4ac8227SGreg Roach 252d4ac8227SGreg Roach $this->whereTrees($query, 'o_file', $trees); 253d4ac8227SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 254d4ac8227SGreg Roach 255d4ac8227SGreg Roach return $this->paginateQuery($query, $this->locationRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 256d4ac8227SGreg Roach } 257d4ac8227SGreg Roach 258d4ac8227SGreg Roach /** 25932cd2800SGreg Roach * Search for media objects. 26032cd2800SGreg Roach * 26109482a55SGreg Roach * @param array<Tree> $trees 26209482a55SGreg Roach * @param array<string> $search 26332cd2800SGreg Roach * @param int $offset 26432cd2800SGreg Roach * @param int $limit 26532cd2800SGreg Roach * 26636779af1SGreg Roach * @return Collection<int,Media> 26732cd2800SGreg Roach */ 268a7a24840SGreg Roach public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 26932cd2800SGreg Roach { 270a7a24840SGreg Roach $query = DB::table('media'); 27132cd2800SGreg Roach 272a7a24840SGreg Roach $this->whereTrees($query, 'media.m_file', $trees); 273a7a24840SGreg Roach $this->whereSearch($query, 'm_gedcom', $search); 274a7a24840SGreg Roach 275d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 27632cd2800SGreg Roach } 27732cd2800SGreg Roach 27832cd2800SGreg Roach /** 27932cd2800SGreg Roach * Search for notes. 28032cd2800SGreg Roach * 28109482a55SGreg Roach * @param array<Tree> $trees 28209482a55SGreg Roach * @param array<string> $search 28332cd2800SGreg Roach * @param int $offset 28432cd2800SGreg Roach * @param int $limit 28532cd2800SGreg Roach * 28636779af1SGreg Roach * @return Collection<int,Note> 28732cd2800SGreg Roach */ 288a7a24840SGreg Roach public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 28932cd2800SGreg Roach { 29032cd2800SGreg Roach $query = DB::table('other') 291058ba724SGreg Roach ->where('o_type', '=', Note::RECORD_TYPE); 29232cd2800SGreg Roach 293a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 294a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 295a7a24840SGreg Roach 296d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 29732cd2800SGreg Roach } 29832cd2800SGreg Roach 29932cd2800SGreg Roach /** 300*701f5d18SGreg Roach * Search for notes. 301*701f5d18SGreg Roach * 302*701f5d18SGreg Roach * @param array<Tree> $trees 303*701f5d18SGreg Roach * @param array<string> $search 304*701f5d18SGreg Roach * @param int $offset 305*701f5d18SGreg Roach * @param int $limit 306*701f5d18SGreg Roach * 307*701f5d18SGreg Roach * @return Collection<int,SharedNote> 308*701f5d18SGreg Roach */ 309*701f5d18SGreg Roach public function searchSharedNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 310*701f5d18SGreg Roach { 311*701f5d18SGreg Roach $query = DB::table('other') 312*701f5d18SGreg Roach ->where('o_type', '=', SharedNote::RECORD_TYPE); 313*701f5d18SGreg Roach 314*701f5d18SGreg Roach $this->whereTrees($query, 'o_file', $trees); 315*701f5d18SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 316*701f5d18SGreg Roach 317*701f5d18SGreg Roach return $this->paginateQuery($query, $this->sharedNoteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 318*701f5d18SGreg Roach } 319*701f5d18SGreg Roach 320*701f5d18SGreg Roach /** 32132cd2800SGreg Roach * Search for repositories. 32232cd2800SGreg Roach * 32309482a55SGreg Roach * @param array<Tree> $trees 32409482a55SGreg Roach * @param array<string> $search 32532cd2800SGreg Roach * @param int $offset 32632cd2800SGreg Roach * @param int $limit 32732cd2800SGreg Roach * 32836779af1SGreg Roach * @return Collection<int,Repository> 32932cd2800SGreg Roach */ 330a7a24840SGreg Roach public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 33132cd2800SGreg Roach { 33232cd2800SGreg Roach $query = DB::table('other') 333058ba724SGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE); 33432cd2800SGreg Roach 335a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 336a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 337a7a24840SGreg Roach 338d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 33932cd2800SGreg Roach } 34032cd2800SGreg Roach 34132cd2800SGreg Roach /** 342a7a24840SGreg Roach * Search for sources. 34332cd2800SGreg Roach * 34409482a55SGreg Roach * @param array<Tree> $trees 34509482a55SGreg Roach * @param array<string> $search 34632cd2800SGreg Roach * @param int $offset 34732cd2800SGreg Roach * @param int $limit 34832cd2800SGreg Roach * 34936779af1SGreg Roach * @return Collection<int,Source> 35032cd2800SGreg Roach */ 351a7a24840SGreg Roach public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 352a7a24840SGreg Roach { 353a7a24840SGreg Roach $query = DB::table('sources'); 354a7a24840SGreg Roach 355a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 356a7a24840SGreg Roach $this->whereSearch($query, 's_gedcom', $search); 357a7a24840SGreg Roach 358d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 359a7a24840SGreg Roach } 360a7a24840SGreg Roach 361a7a24840SGreg Roach /** 362a7a24840SGreg Roach * Search for sources by name. 363a7a24840SGreg Roach * 36409482a55SGreg Roach * @param array<Tree> $trees 36509482a55SGreg Roach * @param array<string> $search 366a7a24840SGreg Roach * @param int $offset 367a7a24840SGreg Roach * @param int $limit 368a7a24840SGreg Roach * 36936779af1SGreg Roach * @return Collection<int,Source> 370a7a24840SGreg Roach */ 371a7a24840SGreg Roach public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 37232cd2800SGreg Roach { 37332cd2800SGreg Roach $query = DB::table('sources') 374c0804649SGreg Roach ->orderBy('s_name'); 37532cd2800SGreg Roach 376a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 377a7a24840SGreg Roach $this->whereSearch($query, 's_name', $search); 378a7a24840SGreg Roach 379d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 38032cd2800SGreg Roach } 38132cd2800SGreg Roach 38232cd2800SGreg Roach /** 38363763244SGreg Roach * Search for sources. 38463763244SGreg Roach * 38509482a55SGreg Roach * @param array<Tree> $trees 38609482a55SGreg Roach * @param array<string> $search 38763763244SGreg Roach * @param int $offset 38863763244SGreg Roach * @param int $limit 38963763244SGreg Roach * 39036779af1SGreg Roach * @return Collection<int,string> 39163763244SGreg Roach */ 39263763244SGreg Roach public function searchSurnames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 39363763244SGreg Roach { 39463763244SGreg Roach $query = DB::table('name'); 39563763244SGreg Roach 39663763244SGreg Roach $this->whereTrees($query, 'n_file', $trees); 39763763244SGreg Roach $this->whereSearch($query, 'n_surname', $search); 39863763244SGreg Roach 39963763244SGreg Roach return $query 40063763244SGreg Roach ->groupBy(['n_surname']) 40163763244SGreg Roach ->orderBy('n_surname') 40263763244SGreg Roach ->skip($offset) 40363763244SGreg Roach ->take($limit) 40463763244SGreg Roach ->pluck('n_surname'); 40563763244SGreg Roach } 40663763244SGreg Roach 40763763244SGreg Roach /** 408d4ac8227SGreg Roach * Search for submissions. 409d4ac8227SGreg Roach * 41009482a55SGreg Roach * @param array<Tree> $trees 41109482a55SGreg Roach * @param array<string> $search 412d4ac8227SGreg Roach * @param int $offset 413d4ac8227SGreg Roach * @param int $limit 414d4ac8227SGreg Roach * 41536779af1SGreg Roach * @return Collection<int,Submission> 416d4ac8227SGreg Roach */ 417d4ac8227SGreg Roach public function searchSubmissions(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 418d4ac8227SGreg Roach { 419d4ac8227SGreg Roach $query = DB::table('other') 420058ba724SGreg Roach ->where('o_type', '=', Submission::RECORD_TYPE); 421d4ac8227SGreg Roach 422d4ac8227SGreg Roach $this->whereTrees($query, 'o_file', $trees); 423d4ac8227SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 424d4ac8227SGreg Roach 425d4ac8227SGreg Roach return $this->paginateQuery($query, $this->submissionRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 426d4ac8227SGreg Roach } 427d4ac8227SGreg Roach 428d4ac8227SGreg Roach /** 42932cd2800SGreg Roach * Search for submitters. 43032cd2800SGreg Roach * 43109482a55SGreg Roach * @param array<Tree> $trees 43209482a55SGreg Roach * @param array<string> $search 43332cd2800SGreg Roach * @param int $offset 43432cd2800SGreg Roach * @param int $limit 43532cd2800SGreg Roach * 43636779af1SGreg Roach * @return Collection<int,Submitter> 43732cd2800SGreg Roach */ 438a7a24840SGreg Roach public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 43932cd2800SGreg Roach { 44032cd2800SGreg Roach $query = DB::table('other') 441058ba724SGreg Roach ->where('o_type', '=', Submitter::RECORD_TYPE); 44232cd2800SGreg Roach 443a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 444a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 445a7a24840SGreg Roach 446d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 44732cd2800SGreg Roach } 44832cd2800SGreg Roach 44932cd2800SGreg Roach /** 450b68caec6SGreg Roach * Search for places. 451b68caec6SGreg Roach * 452b68caec6SGreg Roach * @param Tree $tree 453b68caec6SGreg Roach * @param string $search 454b68caec6SGreg Roach * @param int $offset 455b68caec6SGreg Roach * @param int $limit 456b68caec6SGreg Roach * 45736779af1SGreg Roach * @return Collection<int,Place> 458b68caec6SGreg Roach */ 459b68caec6SGreg Roach public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 460b68caec6SGreg Roach { 461b68caec6SGreg Roach $query = DB::table('places AS p0') 462b68caec6SGreg Roach ->where('p0.p_file', '=', $tree->id()) 463b68caec6SGreg Roach ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id') 464b68caec6SGreg Roach ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id') 465b68caec6SGreg Roach ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id') 466b68caec6SGreg Roach ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id') 467b68caec6SGreg Roach ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id') 468b68caec6SGreg Roach ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id') 469b68caec6SGreg Roach ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id') 470b68caec6SGreg Roach ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id') 471b68caec6SGreg Roach ->orderBy('p0.p_place') 472b68caec6SGreg Roach ->orderBy('p1.p_place') 473b68caec6SGreg Roach ->orderBy('p2.p_place') 474b68caec6SGreg Roach ->orderBy('p3.p_place') 475b68caec6SGreg Roach ->orderBy('p4.p_place') 476b68caec6SGreg Roach ->orderBy('p5.p_place') 477b68caec6SGreg Roach ->orderBy('p6.p_place') 478b68caec6SGreg Roach ->orderBy('p7.p_place') 479b68caec6SGreg Roach ->orderBy('p8.p_place') 480b68caec6SGreg Roach ->select([ 481b68caec6SGreg Roach 'p0.p_place AS place0', 482b68caec6SGreg Roach 'p1.p_place AS place1', 483b68caec6SGreg Roach 'p2.p_place AS place2', 484b68caec6SGreg Roach 'p3.p_place AS place3', 485b68caec6SGreg Roach 'p4.p_place AS place4', 486b68caec6SGreg Roach 'p5.p_place AS place5', 487b68caec6SGreg Roach 'p6.p_place AS place6', 488b68caec6SGreg Roach 'p7.p_place AS place7', 489b68caec6SGreg Roach 'p8.p_place AS place8', 490b68caec6SGreg Roach ]); 491b68caec6SGreg Roach 492b68caec6SGreg Roach // Filter each level of the hierarchy. 493b68caec6SGreg Roach foreach (explode(',', $search, 9) as $level => $string) { 494b5961194SGreg Roach $query->where('p' . $level . '.p_place', 'LIKE', '%' . addcslashes($string, '\\%_') . '%'); 495b68caec6SGreg Roach } 496b68caec6SGreg Roach 497f70bcff5SGreg Roach $row_mapper = static function (object $row) use ($tree): Place { 498b68caec6SGreg Roach $place = implode(', ', array_filter((array) $row)); 499b68caec6SGreg Roach 500b68caec6SGreg Roach return new Place($place, $tree); 501b68caec6SGreg Roach }; 502b68caec6SGreg Roach 5036c2179e2SGreg Roach $filter = static function (): bool { 504a7a24840SGreg Roach return true; 505a7a24840SGreg Roach }; 506b68caec6SGreg Roach 507a7a24840SGreg Roach return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit); 508a7a24840SGreg Roach } 509b68caec6SGreg Roach 510b68caec6SGreg Roach /** 51109482a55SGreg Roach * @param array<Tree> $trees 512d8809d62SGreg Roach * @param array<string,string> $fields 513d8809d62SGreg Roach * @param array<string,string> $modifiers 514dfb2cda2SGreg Roach * 51536779af1SGreg Roach * @return Collection<int,Individual> 516dfb2cda2SGreg Roach */ 517dfb2cda2SGreg Roach public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection 518dfb2cda2SGreg Roach { 519dfb2cda2SGreg Roach $fields = array_filter($fields); 520dfb2cda2SGreg Roach 521dfb2cda2SGreg Roach $query = DB::table('individuals') 522dfb2cda2SGreg Roach ->select(['individuals.*']) 523dfb2cda2SGreg Roach ->distinct(); 524dfb2cda2SGreg Roach 525dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $trees); 526dfb2cda2SGreg Roach 527dfb2cda2SGreg Roach // Join the following tables 528dfb2cda2SGreg Roach $father_name = false; 529dfb2cda2SGreg Roach $mother_name = false; 530dfb2cda2SGreg Roach $spouse_family = false; 531dfb2cda2SGreg Roach $indi_name = false; 53256946b7aSGreg Roach $indi_dates = []; 53356946b7aSGreg Roach $fam_dates = []; 534dfb2cda2SGreg Roach $indi_plac = false; 535dfb2cda2SGreg Roach $fam_plac = false; 536dfb2cda2SGreg Roach 537dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 538dfb2cda2SGreg Roach if ($field_value !== '') { 5392f86083cSGreg Roach if (str_starts_with($field_name, 'FATHER:NAME')) { 540dfb2cda2SGreg Roach $father_name = true; 5412f86083cSGreg Roach } elseif (str_starts_with($field_name, 'MOTHER:NAME')) { 542dfb2cda2SGreg Roach $mother_name = true; 5432f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:NAME:GIVN')) { 54456946b7aSGreg Roach $indi_name = true; 5452f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:NAME:SURN')) { 5462f86083cSGreg Roach $indi_name = true; 5472f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:')) { 548dfb2cda2SGreg Roach $spouse_family = true; 5492f86083cSGreg Roach if (str_ends_with($field_name, ':DATE')) { 5502f86083cSGreg Roach $fam_dates[] = explode(':', $field_name)[1]; 5512f86083cSGreg Roach } elseif (str_ends_with($field_name, ':PLAC')) { 55256946b7aSGreg Roach $fam_plac = true; 55356946b7aSGreg Roach } 5542f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:')) { 5552f86083cSGreg Roach if (str_ends_with($field_name, ':DATE')) { 5562f86083cSGreg Roach $indi_dates[] = explode(':', $field_name)[1]; 5572f86083cSGreg Roach } elseif (str_ends_with($field_name, ':PLAC')) { 558dfb2cda2SGreg Roach $indi_plac = true; 559dfb2cda2SGreg Roach } 560dfb2cda2SGreg Roach } 561dfb2cda2SGreg Roach } 562dfb2cda2SGreg Roach } 563dfb2cda2SGreg Roach 564dfb2cda2SGreg Roach if ($father_name || $mother_name) { 5650b5fd0a6SGreg Roach $query->join('link AS l1', static function (JoinClause $join): void { 566dfb2cda2SGreg Roach $join 567dfb2cda2SGreg Roach ->on('l1.l_file', '=', 'individuals.i_file') 568dfb2cda2SGreg Roach ->on('l1.l_from', '=', 'individuals.i_id') 569dfb2cda2SGreg Roach ->where('l1.l_type', '=', 'FAMC'); 570dfb2cda2SGreg Roach }); 571dfb2cda2SGreg Roach 572dfb2cda2SGreg Roach if ($father_name) { 5730b5fd0a6SGreg Roach $query->join('link AS l2', static function (JoinClause $join): void { 574dfb2cda2SGreg Roach $join 575dfb2cda2SGreg Roach ->on('l2.l_file', '=', 'l1.l_file') 576dfb2cda2SGreg Roach ->on('l2.l_from', '=', 'l1.l_to') 577dfb2cda2SGreg Roach ->where('l2.l_type', '=', 'HUSB'); 578dfb2cda2SGreg Roach }); 5790b5fd0a6SGreg Roach $query->join('name AS father_name', static function (JoinClause $join): void { 580dfb2cda2SGreg Roach $join 581dfb2cda2SGreg Roach ->on('father_name.n_file', '=', 'l2.l_file') 582dfb2cda2SGreg Roach ->on('father_name.n_id', '=', 'l2.l_to'); 583dfb2cda2SGreg Roach }); 584dfb2cda2SGreg Roach } 585dfb2cda2SGreg Roach 586dfb2cda2SGreg Roach if ($mother_name) { 5870b5fd0a6SGreg Roach $query->join('link AS l3', static function (JoinClause $join): void { 588dfb2cda2SGreg Roach $join 589dfb2cda2SGreg Roach ->on('l3.l_file', '=', 'l1.l_file') 590dfb2cda2SGreg Roach ->on('l3.l_from', '=', 'l1.l_to') 591dfb2cda2SGreg Roach ->where('l3.l_type', '=', 'WIFE'); 592dfb2cda2SGreg Roach }); 5930b5fd0a6SGreg Roach $query->join('name AS mother_name', static function (JoinClause $join): void { 594dfb2cda2SGreg Roach $join 595dfb2cda2SGreg Roach ->on('mother_name.n_file', '=', 'l3.l_file') 596dfb2cda2SGreg Roach ->on('mother_name.n_id', '=', 'l3.l_to'); 597dfb2cda2SGreg Roach }); 598dfb2cda2SGreg Roach } 599dfb2cda2SGreg Roach } 600dfb2cda2SGreg Roach 601dfb2cda2SGreg Roach if ($spouse_family) { 6020b5fd0a6SGreg Roach $query->join('link AS l4', static function (JoinClause $join): void { 603dfb2cda2SGreg Roach $join 604dfb2cda2SGreg Roach ->on('l4.l_file', '=', 'individuals.i_file') 605dfb2cda2SGreg Roach ->on('l4.l_from', '=', 'individuals.i_id') 606dfb2cda2SGreg Roach ->where('l4.l_type', '=', 'FAMS'); 607dfb2cda2SGreg Roach }); 6080b5fd0a6SGreg Roach $query->join('families AS spouse_families', static function (JoinClause $join): void { 609dfb2cda2SGreg Roach $join 610dfb2cda2SGreg Roach ->on('spouse_families.f_file', '=', 'l4.l_file') 611dfb2cda2SGreg Roach ->on('spouse_families.f_id', '=', 'l4.l_to'); 612dfb2cda2SGreg Roach }); 613dfb2cda2SGreg Roach } 614dfb2cda2SGreg Roach 615dfb2cda2SGreg Roach if ($indi_name) { 6160b5fd0a6SGreg Roach $query->join('name AS individual_name', static function (JoinClause $join): void { 617dfb2cda2SGreg Roach $join 618dfb2cda2SGreg Roach ->on('individual_name.n_file', '=', 'individuals.i_file') 619dfb2cda2SGreg Roach ->on('individual_name.n_id', '=', 'individuals.i_id'); 620dfb2cda2SGreg Roach }); 621dfb2cda2SGreg Roach } 622dfb2cda2SGreg Roach 62356946b7aSGreg Roach foreach (array_unique($indi_dates) as $indi_date) { 62456946b7aSGreg Roach $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void { 625dfb2cda2SGreg Roach $join 62656946b7aSGreg Roach ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file') 62756946b7aSGreg Roach ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id'); 628dfb2cda2SGreg Roach }); 629dfb2cda2SGreg Roach } 630dfb2cda2SGreg Roach 63156946b7aSGreg Roach foreach (array_unique($fam_dates) as $fam_date) { 63256946b7aSGreg Roach $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void { 633dfb2cda2SGreg Roach $join 634b06d667bSGreg Roach ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.f_file') 635b06d667bSGreg Roach ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.f_id'); 636dfb2cda2SGreg Roach }); 637dfb2cda2SGreg Roach } 638dfb2cda2SGreg Roach 639dfb2cda2SGreg Roach if ($indi_plac) { 6400b5fd0a6SGreg Roach $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { 641dfb2cda2SGreg Roach $join 642dfb2cda2SGreg Roach ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') 643dfb2cda2SGreg Roach ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); 644dfb2cda2SGreg Roach }); 6450b5fd0a6SGreg Roach $query->join('places AS individual_places', static function (JoinClause $join): void { 646dfb2cda2SGreg Roach $join 647dfb2cda2SGreg Roach ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') 648dfb2cda2SGreg Roach ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); 649dfb2cda2SGreg Roach }); 650dfb2cda2SGreg Roach } 651dfb2cda2SGreg Roach 652dfb2cda2SGreg Roach if ($fam_plac) { 6530b5fd0a6SGreg Roach $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void { 654dfb2cda2SGreg Roach $join 655dfb2cda2SGreg Roach ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file') 656dfb2cda2SGreg Roach ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id'); 657dfb2cda2SGreg Roach }); 6580b5fd0a6SGreg Roach $query->join('places AS family_places', static function (JoinClause $join): void { 659dfb2cda2SGreg Roach $join 660dfb2cda2SGreg Roach ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file') 661dfb2cda2SGreg Roach ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id'); 662dfb2cda2SGreg Roach }); 663dfb2cda2SGreg Roach } 664dfb2cda2SGreg Roach 665dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 66656946b7aSGreg Roach $parts = explode(':', $field_name . ':::'); 6672f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:NAME:')) { 6682f86083cSGreg Roach switch ($field_name) { 6692f86083cSGreg Roach case 'INDI:NAME:GIVN': 670dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 671dfb2cda2SGreg Roach case 'EXACT': 672dfb2cda2SGreg Roach $query->where('individual_name.n_givn', '=', $field_value); 673dfb2cda2SGreg Roach break; 674dfb2cda2SGreg Roach case 'BEGINS': 675dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', $field_value . '%'); 676dfb2cda2SGreg Roach break; 677dfb2cda2SGreg Roach case 'CONTAINS': 678dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 679dfb2cda2SGreg Roach break; 680dfb2cda2SGreg Roach case 'SDX_STD': 681dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 682dfb2cda2SGreg Roach if ($sdx !== '') { 683dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx); 684dfb2cda2SGreg Roach } else { 685dfb2cda2SGreg Roach // No phonetic content? Use a substring match 686dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 687dfb2cda2SGreg Roach } 688dfb2cda2SGreg Roach break; 689dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 690dfb2cda2SGreg Roach case 'SDX_DM': 691dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 692dfb2cda2SGreg Roach if ($sdx !== '') { 693dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx); 694dfb2cda2SGreg Roach } else { 695dfb2cda2SGreg Roach // No phonetic content? Use a substring match 696dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 697dfb2cda2SGreg Roach } 698dfb2cda2SGreg Roach break; 699dfb2cda2SGreg Roach } 700788f881dSGreg Roach unset($fields[$field_name]); 701dfb2cda2SGreg Roach break; 7022f86083cSGreg Roach case 'INDI:NAME:SURN': 703dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 704dfb2cda2SGreg Roach case 'EXACT': 705fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 706fc6d23eeSGreg Roach $query 707fc6d23eeSGreg Roach ->where('individual_name.n_surn', '=', $field_value) 708fc6d23eeSGreg Roach ->orWhere('individual_name.n_surname', '=', $field_value); 709fc6d23eeSGreg Roach }); 710dfb2cda2SGreg Roach break; 711dfb2cda2SGreg Roach case 'BEGINS': 712fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 713fc6d23eeSGreg Roach $query 714fc6d23eeSGreg Roach ->where('individual_name.n_surn', 'LIKE', $field_value . '%') 7159875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', $field_value . '%'); 716fc6d23eeSGreg Roach }); 717dfb2cda2SGreg Roach break; 718dfb2cda2SGreg Roach case 'CONTAINS': 719fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 720fc6d23eeSGreg Roach $query 721fc6d23eeSGreg Roach ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%') 7229875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%'); 723fc6d23eeSGreg Roach }); 724dfb2cda2SGreg Roach break; 725dfb2cda2SGreg Roach case 'SDX_STD': 726dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 727dfb2cda2SGreg Roach if ($sdx !== '') { 728dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); 729dfb2cda2SGreg Roach } else { 730dfb2cda2SGreg Roach // No phonetic content? Use a substring match 731fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 732fc6d23eeSGreg Roach $query 733fc6d23eeSGreg Roach ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%') 7349875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%'); 735fc6d23eeSGreg Roach }); 736dfb2cda2SGreg Roach } 737dfb2cda2SGreg Roach break; 738dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 739dfb2cda2SGreg Roach case 'SDX_DM': 740dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 741dfb2cda2SGreg Roach if ($sdx !== '') { 742dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); 743dfb2cda2SGreg Roach } else { 744dfb2cda2SGreg Roach // No phonetic content? Use a substring match 7459875d474SGreg Roach $query->where(function (Builder $query) use ($field_value): void { 7469875d474SGreg Roach $query 7479875d474SGreg Roach ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%') 7489875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%'); 7499875d474SGreg Roach }); 750dfb2cda2SGreg Roach } 751dfb2cda2SGreg Roach break; 752dfb2cda2SGreg Roach } 753b4adec9eSGreg Roach unset($fields[$field_name]); 754dfb2cda2SGreg Roach break; 7552f86083cSGreg Roach case 'INDI:NAME:NICK': 7562f86083cSGreg Roach case 'INDI:NAME:_MARNM': 7572f86083cSGreg Roach case 'INDI:NAME:_HEB': 7582f86083cSGreg Roach case 'INDI:NAME:_AKA': 7592f86083cSGreg Roach $like = "%\n1 NAME%\n2 " . $parts[2] . ' %' . preg_quote($field_value, '/') . '%'; 760b4adec9eSGreg Roach $query->where('individuals.i_gedcom', 'LIKE', $like); 761dfb2cda2SGreg Roach break; 762dfb2cda2SGreg Roach } 7632f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':DATE')) { 764dfb2cda2SGreg Roach $date = new Date($field_value); 765dfb2cda2SGreg Roach if ($date->isOK()) { 766d8809d62SGreg Roach $delta = 365 * (int) ($modifiers[$field_name] ?? 0); 767dfb2cda2SGreg Roach $query 7682f86083cSGreg Roach ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) 7692f86083cSGreg Roach ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 7702f86083cSGreg Roach ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); 771dfb2cda2SGreg Roach } 772dfb2cda2SGreg Roach unset($fields[$field_name]); 7732f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':DATE')) { 774dfb2cda2SGreg Roach $date = new Date($field_value); 775dfb2cda2SGreg Roach if ($date->isOK()) { 776d8809d62SGreg Roach $delta = 365 * (int) ($modifiers[$field_name] ?? 0); 777dfb2cda2SGreg Roach $query 77856946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) 77956946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 78056946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); 781dfb2cda2SGreg Roach } 782dfb2cda2SGreg Roach unset($fields[$field_name]); 7832f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { 784dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 785dfb2cda2SGreg Roach $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%'); 7862f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { 787dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 788dfb2cda2SGreg Roach $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%'); 7892f86083cSGreg Roach } elseif (str_starts_with($field_name, 'MOTHER:NAME:') || str_starts_with($field_name, 'FATHER:NAME:')) { 7902f86083cSGreg Roach $table = str_starts_with($field_name, 'FATHER:NAME:') ? 'father_name' : 'mother_name'; 7912f86083cSGreg Roach switch ($parts[2]) { 792dfb2cda2SGreg Roach case 'GIVN': 793dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 794dfb2cda2SGreg Roach case 'EXACT': 795dfb2cda2SGreg Roach $query->where($table . '.n_givn', '=', $field_value); 796dfb2cda2SGreg Roach break; 797dfb2cda2SGreg Roach case 'BEGINS': 798dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', $field_value . '%'); 799dfb2cda2SGreg Roach break; 800dfb2cda2SGreg Roach case 'CONTAINS': 801dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 802dfb2cda2SGreg Roach break; 803dfb2cda2SGreg Roach case 'SDX_STD': 804dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 805dfb2cda2SGreg Roach if ($sdx !== '') { 806dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); 807dfb2cda2SGreg Roach } else { 808dfb2cda2SGreg Roach // No phonetic content? Use a substring match 809dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 810dfb2cda2SGreg Roach } 811dfb2cda2SGreg Roach break; 812dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 813dfb2cda2SGreg Roach case 'SDX_DM': 814dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 815dfb2cda2SGreg Roach if ($sdx !== '') { 816dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); 817dfb2cda2SGreg Roach } else { 818dfb2cda2SGreg Roach // No phonetic content? Use a substring match 819dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 820dfb2cda2SGreg Roach } 821dfb2cda2SGreg Roach break; 822dfb2cda2SGreg Roach } 823dfb2cda2SGreg Roach break; 824dfb2cda2SGreg Roach case 'SURN': 825dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 826dfb2cda2SGreg Roach case 'EXACT': 827dfb2cda2SGreg Roach $query->where($table . '.n_surn', '=', $field_value); 828dfb2cda2SGreg Roach break; 829dfb2cda2SGreg Roach case 'BEGINS': 830dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', $field_value . '%'); 831dfb2cda2SGreg Roach break; 832dfb2cda2SGreg Roach case 'CONTAINS': 833dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 834dfb2cda2SGreg Roach break; 835dfb2cda2SGreg Roach case 'SDX_STD': 836dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 837dfb2cda2SGreg Roach if ($sdx !== '') { 838dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); 839dfb2cda2SGreg Roach } else { 840dfb2cda2SGreg Roach // No phonetic content? Use a substring match 841dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 842dfb2cda2SGreg Roach } 843dfb2cda2SGreg Roach break; 844dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 845dfb2cda2SGreg Roach case 'SDX_DM': 846dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 847dfb2cda2SGreg Roach if ($sdx !== '') { 848dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); 849dfb2cda2SGreg Roach } else { 850dfb2cda2SGreg Roach // No phonetic content? Use a substring match 851dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 852dfb2cda2SGreg Roach } 853dfb2cda2SGreg Roach break; 854dfb2cda2SGreg Roach } 855dfb2cda2SGreg Roach break; 856dfb2cda2SGreg Roach } 857dfb2cda2SGreg Roach unset($fields[$field_name]); 8582f86083cSGreg Roach } elseif (str_starts_with($field_name, 'FAM:')) { 859dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 860dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 8613c480c6bSGreg Roach $query->where('spouse_families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%'); 8622f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':TYPE')) { 863dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 8642f86083cSGreg Roach $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[1] . "%\n2 TYPE %" . $field_value . '%'); 8652f86083cSGreg Roach } elseif (str_starts_with($field_name, 'INDI:')) { 866dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 867dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 8682f86083cSGreg Roach $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[1] . '%' . $parts[2] . '%' . $field_value . '%'); 869dfb2cda2SGreg Roach } 870dfb2cda2SGreg Roach } 871b4adec9eSGreg Roach 872dfb2cda2SGreg Roach return $query 873dfb2cda2SGreg Roach ->get() 87452a8ef61SGreg Roach ->each($this->rowLimiter()) 875d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 876dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()) 8770b5fd0a6SGreg Roach ->filter(static function (Individual $individual) use ($fields): bool { 878c5457947SGreg Roach // Check for searches which were only partially matched by SQL 879dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 880c5457947SGreg Roach $parts = explode(':', $field_name . '::::'); 881dfb2cda2SGreg Roach 8822f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:NAME:') && $field_name !== 'INDI:NAME:GIVN' && $field_name !== 'INDI:NAME:SURN') { 8832f86083cSGreg Roach $regex = '/\n1 NAME.*(?:\n2.*)*\n2 ' . $parts[2] . ' .*' . preg_quote($field_value, '/') . '/i'; 884b4adec9eSGreg Roach 885b4adec9eSGreg Roach if (preg_match($regex, $individual->gedcom())) { 886b4adec9eSGreg Roach continue; 887b4adec9eSGreg Roach } 888b4adec9eSGreg Roach 889b4adec9eSGreg Roach return false; 890b4adec9eSGreg Roach } 891b4adec9eSGreg Roach 892b4adec9eSGreg Roach $regex = '/' . preg_quote($field_value, '/') . '/i'; 893b4adec9eSGreg Roach 8942f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { 8952f86083cSGreg Roach foreach ($individual->facts([$parts[1]]) as $fact) { 896392561bbSGreg Roach if (preg_match($regex, $fact->place()->gedcomName())) { 897c5457947SGreg Roach continue 2; 898dfb2cda2SGreg Roach } 899a41e65f0SGreg Roach } 900c5457947SGreg Roach return false; 901c5457947SGreg Roach } 902c5457947SGreg Roach 9032f86083cSGreg Roach if (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { 90439ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 905a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 906392561bbSGreg Roach if (preg_match($regex, $fact->place()->gedcomName())) { 9073b3576aeSGreg Roach continue 3; 908a41e65f0SGreg Roach } 909dfb2cda2SGreg Roach } 910dfb2cda2SGreg Roach } 911c5457947SGreg Roach return false; 912c5457947SGreg Roach } 913c5457947SGreg Roach 9142f86083cSGreg Roach if ($field_name === 'INDI:FACT:TYPE' || $field_name === 'INDI:EVEN:TYPE' || $field_name === 'INDI:CHAN:_WT_USER') { 9152f86083cSGreg Roach foreach ($individual->facts([$parts[1]]) as $fact) { 9162f86083cSGreg Roach if (preg_match($regex, $fact->attribute($parts[2]))) { 9172f86083cSGreg Roach continue 2; 9182f86083cSGreg Roach } 9192f86083cSGreg Roach } 9202f86083cSGreg Roach 9212f86083cSGreg Roach return false; 9222f86083cSGreg Roach } 9232f86083cSGreg Roach 9242f86083cSGreg Roach if (str_starts_with($field_name, 'INDI:')) { 9252f86083cSGreg Roach foreach ($individual->facts([$parts[1]]) as $fact) { 9262f86083cSGreg Roach if (preg_match($regex, $fact->value())) { 9272f86083cSGreg Roach continue 2; 9282f86083cSGreg Roach } 9292f86083cSGreg Roach } 9302f86083cSGreg Roach 9312f86083cSGreg Roach return false; 9322f86083cSGreg Roach } 9332f86083cSGreg Roach 9342f86083cSGreg Roach if (str_starts_with($field_name, 'FAM:')) { 93539ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 936a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 937a41e65f0SGreg Roach if (preg_match($regex, $fact->value())) { 938c5457947SGreg Roach continue 3; 939a41e65f0SGreg Roach } 940dfb2cda2SGreg Roach } 941dfb2cda2SGreg Roach } 942c5457947SGreg Roach return false; 943c5457947SGreg Roach } 944c5457947SGreg Roach } 945dfb2cda2SGreg Roach 946dfb2cda2SGreg Roach return true; 947dfb2cda2SGreg Roach }); 948dfb2cda2SGreg Roach } 949dfb2cda2SGreg Roach 950dfb2cda2SGreg Roach /** 951dfb2cda2SGreg Roach * @param string $soundex 952dfb2cda2SGreg Roach * @param string $lastname 953dfb2cda2SGreg Roach * @param string $firstname 954dfb2cda2SGreg Roach * @param string $place 95509482a55SGreg Roach * @param array<Tree> $search_trees 956dfb2cda2SGreg Roach * 95736779af1SGreg Roach * @return Collection<int,Individual> 958dfb2cda2SGreg Roach */ 959dfb2cda2SGreg Roach public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection 960dfb2cda2SGreg Roach { 961dfb2cda2SGreg Roach switch ($soundex) { 962dfb2cda2SGreg Roach default: 963dfb2cda2SGreg Roach case 'Russell': 964dfb2cda2SGreg Roach $givn_sdx = Soundex::russell($firstname); 965dfb2cda2SGreg Roach $surn_sdx = Soundex::russell($lastname); 966dfb2cda2SGreg Roach $plac_sdx = Soundex::russell($place); 967dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_std'; 968dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_std'; 969dfb2cda2SGreg Roach $plac_field = 'p_std_soundex'; 970dfb2cda2SGreg Roach break; 971dfb2cda2SGreg Roach case 'DaitchM': 972dfb2cda2SGreg Roach $givn_sdx = Soundex::daitchMokotoff($firstname); 973dfb2cda2SGreg Roach $surn_sdx = Soundex::daitchMokotoff($lastname); 974dfb2cda2SGreg Roach $plac_sdx = Soundex::daitchMokotoff($place); 975dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_dm'; 976dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_dm'; 977dfb2cda2SGreg Roach $plac_field = 'p_dm_soundex'; 978dfb2cda2SGreg Roach break; 979dfb2cda2SGreg Roach } 980dfb2cda2SGreg Roach 981dfb2cda2SGreg Roach // Nothing to search for? Return nothing. 982dfb2cda2SGreg Roach if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') { 9833fda39a7SGreg Roach return new Collection(); 984dfb2cda2SGreg Roach } 985dfb2cda2SGreg Roach 986dfb2cda2SGreg Roach $query = DB::table('individuals') 987dfb2cda2SGreg Roach ->select(['individuals.*']) 988dfb2cda2SGreg Roach ->distinct(); 989dfb2cda2SGreg Roach 990dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $search_trees); 991dfb2cda2SGreg Roach 992dfb2cda2SGreg Roach if ($plac_sdx !== '') { 9930b5fd0a6SGreg Roach $query->join('placelinks', static function (JoinClause $join): void { 994dfb2cda2SGreg Roach $join 995dfb2cda2SGreg Roach ->on('placelinks.pl_file', '=', 'individuals.i_file') 996dfb2cda2SGreg Roach ->on('placelinks.pl_gid', '=', 'individuals.i_id'); 997dfb2cda2SGreg Roach }); 9980b5fd0a6SGreg Roach $query->join('places', static function (JoinClause $join): void { 999dfb2cda2SGreg Roach $join 1000dfb2cda2SGreg Roach ->on('places.p_file', '=', 'placelinks.pl_file') 1001dfb2cda2SGreg Roach ->on('places.p_id', '=', 'placelinks.pl_p_id'); 1002dfb2cda2SGreg Roach }); 1003dfb2cda2SGreg Roach 1004dfb2cda2SGreg Roach $this->wherePhonetic($query, $plac_field, $plac_sdx); 1005dfb2cda2SGreg Roach } 1006dfb2cda2SGreg Roach 1007dfb2cda2SGreg Roach if ($givn_sdx !== '' || $surn_sdx !== '') { 10080b5fd0a6SGreg Roach $query->join('name', static function (JoinClause $join): void { 1009dfb2cda2SGreg Roach $join 1010dfb2cda2SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 1011dfb2cda2SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 1012dfb2cda2SGreg Roach }); 1013dfb2cda2SGreg Roach 1014dfb2cda2SGreg Roach $this->wherePhonetic($query, $givn_field, $givn_sdx); 1015dfb2cda2SGreg Roach $this->wherePhonetic($query, $surn_field, $surn_sdx); 1016dfb2cda2SGreg Roach } 1017dfb2cda2SGreg Roach 1018dfb2cda2SGreg Roach return $query 1019dfb2cda2SGreg Roach ->get() 102052a8ef61SGreg Roach ->each($this->rowLimiter()) 1021d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 1022dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()); 1023dfb2cda2SGreg Roach } 1024dfb2cda2SGreg Roach 1025dfb2cda2SGreg Roach /** 102632cd2800SGreg Roach * Paginate a search query. 102732cd2800SGreg Roach * 102832cd2800SGreg Roach * @param Builder $query Searches the database for the desired records. 102932cd2800SGreg Roach * @param Closure $row_mapper Converts a row from the query into a record. 1030a7a24840SGreg Roach * @param Closure $row_filter 103132cd2800SGreg Roach * @param int $offset Skip this many rows. 103232cd2800SGreg Roach * @param int $limit Take this many rows. 103332cd2800SGreg Roach * 103436779af1SGreg Roach * @return Collection<int,mixed> 103532cd2800SGreg Roach */ 1036a7a24840SGreg Roach private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection 103732cd2800SGreg Roach { 103832cd2800SGreg Roach $collection = new Collection(); 103932cd2800SGreg Roach 104032cd2800SGreg Roach foreach ($query->cursor() as $row) { 104132cd2800SGreg Roach $record = $row_mapper($row); 10428aaf5f58SGreg Roach // searchIndividualNames() and searchFamilyNames() can return duplicate rows, 10438aaf5f58SGreg Roach // where individuals have multiple names - and we need to sort results by name. 10448aaf5f58SGreg Roach if ($collection->containsStrict($record)) { 10458aaf5f58SGreg Roach continue; 10468aaf5f58SGreg Roach } 1047b68caec6SGreg Roach // If the object has a method "canShow()", then use it to filter for privacy. 1048a7a24840SGreg Roach if ($row_filter($record)) { 104932cd2800SGreg Roach if ($offset > 0) { 105032cd2800SGreg Roach $offset--; 105132cd2800SGreg Roach } else { 105232cd2800SGreg Roach if ($limit > 0) { 105332cd2800SGreg Roach $collection->push($record); 105432cd2800SGreg Roach } 105532cd2800SGreg Roach 105632cd2800SGreg Roach $limit--; 105732cd2800SGreg Roach 105832cd2800SGreg Roach if ($limit === 0) { 105932cd2800SGreg Roach break; 106032cd2800SGreg Roach } 106132cd2800SGreg Roach } 106232cd2800SGreg Roach } 106332cd2800SGreg Roach } 106432cd2800SGreg Roach 1065e0458bdcSGreg Roach 106632cd2800SGreg Roach return $collection; 106732cd2800SGreg Roach } 1068a7a24840SGreg Roach 1069a7a24840SGreg Roach /** 1070a7a24840SGreg Roach * Apply search filters to a SQL query column. Apply collation rules to MySQL. 1071a7a24840SGreg Roach * 1072a7a24840SGreg Roach * @param Builder $query 1073a7a24840SGreg Roach * @param Expression|string $field 107409482a55SGreg Roach * @param array<string> $search_terms 1075a7a24840SGreg Roach */ 1076a7a24840SGreg Roach private function whereSearch(Builder $query, $field, array $search_terms): void 1077a7a24840SGreg Roach { 1078a7a24840SGreg Roach if ($field instanceof Expression) { 1079a7a24840SGreg Roach $field = $field->getValue(); 1080a7a24840SGreg Roach } 1081a7a24840SGreg Roach 1082a7a24840SGreg Roach foreach ($search_terms as $search_term) { 1083b5961194SGreg Roach $query->where(new Expression($field), 'LIKE', '%' . addcslashes($search_term, '\\%_') . '%'); 1084a7a24840SGreg Roach } 1085a7a24840SGreg Roach } 1086a7a24840SGreg Roach 1087a7a24840SGreg Roach /** 10882d686e68SGreg Roach * Apply soundex search filters to a SQL query column. 10892d686e68SGreg Roach * 10902d686e68SGreg Roach * @param Builder $query 10912d686e68SGreg Roach * @param Expression|string $field 10922d686e68SGreg Roach * @param string $soundex 10932d686e68SGreg Roach */ 10942d686e68SGreg Roach private function wherePhonetic(Builder $query, $field, string $soundex): void 10952d686e68SGreg Roach { 10962d686e68SGreg Roach if ($soundex !== '') { 10970b5fd0a6SGreg Roach $query->where(static function (Builder $query) use ($soundex, $field): void { 10982d686e68SGreg Roach foreach (explode(':', $soundex) as $sdx) { 10992d686e68SGreg Roach $query->orWhere($field, 'LIKE', '%' . $sdx . '%'); 11002d686e68SGreg Roach } 11012d686e68SGreg Roach }); 11022d686e68SGreg Roach } 11032d686e68SGreg Roach } 11042d686e68SGreg Roach 11052d686e68SGreg Roach /** 1106a7a24840SGreg Roach * @param Builder $query 1107a7a24840SGreg Roach * @param string $tree_id_field 110809482a55SGreg Roach * @param array<Tree> $trees 1109a7a24840SGreg Roach */ 1110a7a24840SGreg Roach private function whereTrees(Builder $query, string $tree_id_field, array $trees): void 1111a7a24840SGreg Roach { 11120b5fd0a6SGreg Roach $tree_ids = array_map(static function (Tree $tree): int { 1113a7a24840SGreg Roach return $tree->id(); 1114a7a24840SGreg Roach }, $trees); 1115a7a24840SGreg Roach 1116a7a24840SGreg Roach $query->whereIn($tree_id_field, $tree_ids); 1117a7a24840SGreg Roach } 1118a7a24840SGreg Roach 1119d5ad3db0SGreg Roach /** 1120d5ad3db0SGreg Roach * Find the media object that uses a particular media file. 1121d5ad3db0SGreg Roach * 1122d5ad3db0SGreg Roach * @param string $file 1123d5ad3db0SGreg Roach * 112409482a55SGreg Roach * @return array<Media> 1125d5ad3db0SGreg Roach */ 1126d5ad3db0SGreg Roach public function findMediaObjectsForMediaFile(string $file): array 1127d5ad3db0SGreg Roach { 1128d5ad3db0SGreg Roach return DB::table('media') 1129d5ad3db0SGreg Roach ->join('media_file', static function (JoinClause $join): void { 1130d5ad3db0SGreg Roach $join 1131d5ad3db0SGreg Roach ->on('media_file.m_file', '=', 'media.m_file') 1132d5ad3db0SGreg Roach ->on('media_file.m_id', '=', 'media.m_id'); 1133d5ad3db0SGreg Roach }) 1134d5ad3db0SGreg Roach ->join('gedcom_setting', 'media.m_file', '=', 'gedcom_setting.gedcom_id') 1135d5ad3db0SGreg Roach ->where(new Expression('setting_value || multimedia_file_refn'), '=', $file) 1136d5ad3db0SGreg Roach ->select(['media.*']) 1137d5ad3db0SGreg Roach ->distinct() 1138d5ad3db0SGreg Roach ->get() 1139d5ad3db0SGreg Roach ->map($this->mediaRowMapper()) 1140d5ad3db0SGreg Roach ->all(); 1141d5ad3db0SGreg Roach } 1142d5ad3db0SGreg Roach 1143a7a24840SGreg Roach /** 1144a7a24840SGreg Roach * A closure to filter records by privacy-filtered GEDCOM data. 1145a7a24840SGreg Roach * 11465d4b7ec2SGreg Roach * @param array<string> $search_terms 1147a7a24840SGreg Roach * 1148a7a24840SGreg Roach * @return Closure 1149a7a24840SGreg Roach */ 1150a7a24840SGreg Roach private function rawGedcomFilter(array $search_terms): Closure 1151a7a24840SGreg Roach { 11526c2179e2SGreg Roach return static function (GedcomRecord $record) use ($search_terms): bool { 1153a7a24840SGreg Roach // Ignore non-genealogy fields 1154faa5e163SGreg Roach $gedcom = preg_replace('/\n\d (?:_UID|_WT_USER) .*/', '', $record->gedcom()); 1155a7a24840SGreg Roach 1156a7a24840SGreg Roach // Ignore matches in links 1157a7a24840SGreg Roach $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom); 1158a7a24840SGreg Roach 1159a7a24840SGreg Roach // Re-apply the filtering 1160a7a24840SGreg Roach foreach ($search_terms as $search_term) { 1161a7a24840SGreg Roach if (mb_stripos($gedcom, $search_term) === false) { 1162a7a24840SGreg Roach return false; 1163a7a24840SGreg Roach } 1164a7a24840SGreg Roach } 1165a7a24840SGreg Roach 1166a7a24840SGreg Roach return true; 1167a7a24840SGreg Roach }; 1168a7a24840SGreg Roach } 116952a8ef61SGreg Roach 117052a8ef61SGreg Roach /** 117152a8ef61SGreg Roach * Searching for short or common text can give more results than the system can process. 117252a8ef61SGreg Roach * 117352a8ef61SGreg Roach * @param int $limit 117452a8ef61SGreg Roach * 117552a8ef61SGreg Roach * @return Closure 117652a8ef61SGreg Roach */ 1177645e3037SGreg Roach private function rowLimiter(int $limit = self::MAX_SEARCH_RESULTS): Closure 117852a8ef61SGreg Roach { 11796c2179e2SGreg Roach return static function () use ($limit): void { 118052a8ef61SGreg Roach static $n = 0; 118152a8ef61SGreg Roach 118252a8ef61SGreg Roach if (++$n > $limit) { 118352a8ef61SGreg Roach $message = I18N::translate('The search returned too many results.'); 118452a8ef61SGreg Roach 1185d501c45dSGreg Roach throw new HttpServiceUnavailableException($message); 118652a8ef61SGreg Roach } 118752a8ef61SGreg Roach }; 118852a8ef61SGreg Roach } 1189d5ad3db0SGreg Roach 1190d5ad3db0SGreg Roach /** 1191d5ad3db0SGreg Roach * Convert a row from any tree in the families table into a family object. 1192d5ad3db0SGreg Roach * 1193d5ad3db0SGreg Roach * @return Closure 1194d5ad3db0SGreg Roach */ 1195d5ad3db0SGreg Roach private function familyRowMapper(): Closure 1196d5ad3db0SGreg Roach { 1197f70bcff5SGreg Roach return function (object $row): Family { 1198d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->f_file); 1199d5ad3db0SGreg Roach 12006b9cb339SGreg Roach return Registry::familyFactory()->mapper($tree)($row); 1201d5ad3db0SGreg Roach }; 1202d5ad3db0SGreg Roach } 1203d5ad3db0SGreg Roach 1204d5ad3db0SGreg Roach /** 1205d5ad3db0SGreg Roach * Convert a row from any tree in the individuals table into an individual object. 1206d5ad3db0SGreg Roach * 1207d5ad3db0SGreg Roach * @return Closure 1208d5ad3db0SGreg Roach */ 1209d5ad3db0SGreg Roach private function individualRowMapper(): Closure 1210d5ad3db0SGreg Roach { 1211f70bcff5SGreg Roach return function (object $row): Individual { 1212d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->i_file); 1213d5ad3db0SGreg Roach 12146b9cb339SGreg Roach return Registry::individualFactory()->mapper($tree)($row); 1215d5ad3db0SGreg Roach }; 1216d5ad3db0SGreg Roach } 1217d5ad3db0SGreg Roach 1218d5ad3db0SGreg Roach /** 1219d4ac8227SGreg Roach * Convert a row from any tree in the media table into a location object. 1220d4ac8227SGreg Roach * 1221d4ac8227SGreg Roach * @return Closure 1222d4ac8227SGreg Roach */ 1223d4ac8227SGreg Roach private function locationRowMapper(): Closure 1224d4ac8227SGreg Roach { 1225f70bcff5SGreg Roach return function (object $row): Location { 12269d28ed8bSGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1227d4ac8227SGreg Roach 1228d4ac8227SGreg Roach return Registry::locationFactory()->mapper($tree)($row); 1229d4ac8227SGreg Roach }; 1230d4ac8227SGreg Roach } 1231d4ac8227SGreg Roach 1232d4ac8227SGreg Roach /** 1233d5ad3db0SGreg Roach * Convert a row from any tree in the media table into an media object. 1234d5ad3db0SGreg Roach * 1235d5ad3db0SGreg Roach * @return Closure 1236d5ad3db0SGreg Roach */ 1237d5ad3db0SGreg Roach private function mediaRowMapper(): Closure 1238d5ad3db0SGreg Roach { 1239f70bcff5SGreg Roach return function (object $row): Media { 1240d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->m_file); 1241d5ad3db0SGreg Roach 12426b9cb339SGreg Roach return Registry::mediaFactory()->mapper($tree)($row); 1243d5ad3db0SGreg Roach }; 1244d5ad3db0SGreg Roach } 1245d5ad3db0SGreg Roach 1246d5ad3db0SGreg Roach /** 1247d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a note object. 1248d5ad3db0SGreg Roach * 1249d5ad3db0SGreg Roach * @return Closure 1250d5ad3db0SGreg Roach */ 1251d5ad3db0SGreg Roach private function noteRowMapper(): Closure 1252d5ad3db0SGreg Roach { 1253f70bcff5SGreg Roach return function (object $row): Note { 1254d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1255d5ad3db0SGreg Roach 12566b9cb339SGreg Roach return Registry::noteFactory()->mapper($tree)($row); 1257d5ad3db0SGreg Roach }; 1258d5ad3db0SGreg Roach } 1259d5ad3db0SGreg Roach 1260d5ad3db0SGreg Roach /** 1261d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a repository object. 1262d5ad3db0SGreg Roach * 1263d5ad3db0SGreg Roach * @return Closure 1264d5ad3db0SGreg Roach */ 1265d5ad3db0SGreg Roach private function repositoryRowMapper(): Closure 1266d5ad3db0SGreg Roach { 1267f70bcff5SGreg Roach return function (object $row): Repository { 1268d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1269d5ad3db0SGreg Roach 12706b9cb339SGreg Roach return Registry::repositoryFactory()->mapper($tree)($row); 1271d5ad3db0SGreg Roach }; 1272d5ad3db0SGreg Roach } 1273d5ad3db0SGreg Roach 1274d5ad3db0SGreg Roach /** 1275*701f5d18SGreg Roach * Convert a row from any tree in the other table into a note object. 1276*701f5d18SGreg Roach * 1277*701f5d18SGreg Roach * @return Closure 1278*701f5d18SGreg Roach */ 1279*701f5d18SGreg Roach private function sharedNoteRowMapper(): Closure 1280*701f5d18SGreg Roach { 1281*701f5d18SGreg Roach return function (object $row): Note { 1282*701f5d18SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1283*701f5d18SGreg Roach 1284*701f5d18SGreg Roach return Registry::sharedNoteFactory()->mapper($tree)($row); 1285*701f5d18SGreg Roach }; 1286*701f5d18SGreg Roach } 1287*701f5d18SGreg Roach 1288*701f5d18SGreg Roach /** 1289d5ad3db0SGreg Roach * Convert a row from any tree in the sources table into a source object. 1290d5ad3db0SGreg Roach * 1291d5ad3db0SGreg Roach * @return Closure 1292d5ad3db0SGreg Roach */ 1293d5ad3db0SGreg Roach private function sourceRowMapper(): Closure 1294d5ad3db0SGreg Roach { 1295f70bcff5SGreg Roach return function (object $row): Source { 1296d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->s_file); 1297d5ad3db0SGreg Roach 12986b9cb339SGreg Roach return Registry::sourceFactory()->mapper($tree)($row); 1299d5ad3db0SGreg Roach }; 1300d5ad3db0SGreg Roach } 1301d5ad3db0SGreg Roach 1302d5ad3db0SGreg Roach /** 1303d4ac8227SGreg Roach * Convert a row from any tree in the other table into a submission object. 1304d4ac8227SGreg Roach * 1305d4ac8227SGreg Roach * @return Closure 1306d4ac8227SGreg Roach */ 1307d4ac8227SGreg Roach private function submissionRowMapper(): Closure 1308d4ac8227SGreg Roach { 1309f70bcff5SGreg Roach return function (object $row): Submission { 1310d4ac8227SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1311d4ac8227SGreg Roach 1312d4ac8227SGreg Roach return Registry::submissionFactory()->mapper($tree)($row); 1313d4ac8227SGreg Roach }; 1314d4ac8227SGreg Roach } 1315d4ac8227SGreg Roach 1316d4ac8227SGreg Roach /** 1317d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a submitter object. 1318d5ad3db0SGreg Roach * 1319d5ad3db0SGreg Roach * @return Closure 1320d5ad3db0SGreg Roach */ 1321d5ad3db0SGreg Roach private function submitterRowMapper(): Closure 1322d5ad3db0SGreg Roach { 1323f70bcff5SGreg Roach return function (object $row): Submitter { 1324d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1325d5ad3db0SGreg Roach 13266b9cb339SGreg Roach return Registry::submitterFactory()->mapper($tree)($row); 1327d5ad3db0SGreg Roach }; 1328d5ad3db0SGreg Roach } 132932cd2800SGreg Roach} 1330