132cd2800SGreg Roach<?php 23976b470SGreg Roach 332cd2800SGreg Roach/** 432cd2800SGreg Roach * webtrees: online genealogy 5b5961194SGreg Roach * Copyright (C) 2020 webtrees development team 632cd2800SGreg Roach * This program is free software: you can redistribute it and/or modify 732cd2800SGreg Roach * it under the terms of the GNU General Public License as published by 832cd2800SGreg Roach * the Free Software Foundation, either version 3 of the License, or 932cd2800SGreg Roach * (at your option) any later version. 1032cd2800SGreg Roach * This program is distributed in the hope that it will be useful, 1132cd2800SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1232cd2800SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1332cd2800SGreg Roach * GNU General Public License for more details. 1432cd2800SGreg Roach * You should have received a copy of the GNU General Public License 1532cd2800SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1632cd2800SGreg Roach */ 17fcfa147eSGreg Roach 1832cd2800SGreg Roachdeclare(strict_types=1); 1932cd2800SGreg Roach 2032cd2800SGreg Roachnamespace Fisharebest\Webtrees\Services; 2132cd2800SGreg Roach 2232cd2800SGreg Roachuse Closure; 23dfb2cda2SGreg Roachuse Fisharebest\Webtrees\Date; 24d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpServiceUnavailableException; 2532cd2800SGreg Roachuse Fisharebest\Webtrees\Family; 26a7a24840SGreg Roachuse Fisharebest\Webtrees\Gedcom; 2732cd2800SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 2852a8ef61SGreg Roachuse Fisharebest\Webtrees\I18N; 2932cd2800SGreg Roachuse Fisharebest\Webtrees\Individual; 3032cd2800SGreg Roachuse Fisharebest\Webtrees\Media; 3132cd2800SGreg Roachuse Fisharebest\Webtrees\Note; 32b68caec6SGreg Roachuse Fisharebest\Webtrees\Place; 3332cd2800SGreg Roachuse Fisharebest\Webtrees\Repository; 342d686e68SGreg Roachuse Fisharebest\Webtrees\Soundex; 3532cd2800SGreg Roachuse Fisharebest\Webtrees\Source; 36b5c8fd7eSGreg Roachuse Fisharebest\Webtrees\Submitter; 3732cd2800SGreg Roachuse Fisharebest\Webtrees\Tree; 3832cd2800SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 3932cd2800SGreg Roachuse Illuminate\Database\Query\Builder; 40a7a24840SGreg Roachuse Illuminate\Database\Query\Expression; 4132cd2800SGreg Roachuse Illuminate\Database\Query\JoinClause; 4232cd2800SGreg Roachuse Illuminate\Support\Collection; 4332cd2800SGreg Roachuse stdClass; 443976b470SGreg Roach 45b5961194SGreg Roachuse function addcslashes; 4656946b7aSGreg Roachuse function array_unique; 4756946b7aSGreg Roachuse function explode; 48a7a24840SGreg Roachuse function mb_stripos; 4932cd2800SGreg Roach 5032cd2800SGreg Roach/** 5132cd2800SGreg Roach * Search trees for genealogy records. 5232cd2800SGreg Roach */ 5332cd2800SGreg Roachclass SearchService 5432cd2800SGreg Roach{ 55d5ad3db0SGreg Roach /** @var TreeService */ 56d5ad3db0SGreg Roach private $tree_service; 57d5ad3db0SGreg Roach 58d5ad3db0SGreg Roach /** 59d5ad3db0SGreg Roach * SearchService constructor. 60d5ad3db0SGreg Roach * 61d5ad3db0SGreg Roach * @param TreeService $tree_service 62d5ad3db0SGreg Roach */ 633959eeb6SGreg Roach public function __construct( 643959eeb6SGreg Roach TreeService $tree_service 653959eeb6SGreg Roach ) { 66d5ad3db0SGreg Roach $this->tree_service = $tree_service; 67d5ad3db0SGreg Roach } 68d5ad3db0SGreg Roach 69a7a24840SGreg Roach /** 70a7a24840SGreg Roach * @param Tree[] $trees 71a7a24840SGreg Roach * @param string[] $search 72a7a24840SGreg Roach * 73b5c8fd7eSGreg Roach * @return Collection<Family> 74a7a24840SGreg Roach */ 75a7a24840SGreg Roach public function searchFamilies(array $trees, array $search): Collection 76a7a24840SGreg Roach { 77a7a24840SGreg Roach $query = DB::table('families'); 78a7a24840SGreg Roach 79a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 80a7a24840SGreg Roach $this->whereSearch($query, 'f_gedcom', $search); 81a7a24840SGreg Roach 82a7a24840SGreg Roach return $query 83a7a24840SGreg Roach ->get() 8452a8ef61SGreg Roach ->each($this->rowLimiter()) 85d5ad3db0SGreg Roach ->map($this->familyRowMapper()) 86a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 877f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 88a7a24840SGreg Roach } 89a7a24840SGreg Roach 9032cd2800SGreg Roach /** 9132cd2800SGreg Roach * Search for families by name. 9232cd2800SGreg Roach * 93a7a24840SGreg Roach * @param Tree[] $trees 94a7a24840SGreg Roach * @param string[] $search 9532cd2800SGreg Roach * @param int $offset 9632cd2800SGreg Roach * @param int $limit 9732cd2800SGreg Roach * 98b5c8fd7eSGreg Roach * @return Collection<Family> 9932cd2800SGreg Roach */ 100a7a24840SGreg Roach public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 10132cd2800SGreg Roach { 10232cd2800SGreg Roach $query = DB::table('families') 103ac499332SGreg Roach ->leftJoin('name AS husb_name', static function (JoinClause $join): void { 10432cd2800SGreg Roach $join 10532cd2800SGreg Roach ->on('husb_name.n_file', '=', 'families.f_file') 106ac499332SGreg Roach ->on('husb_name.n_id', '=', 'families.f_husb') 107ac499332SGreg Roach ->where('husb_name.n_type', '<>', '_MARNM'); 10832cd2800SGreg Roach }) 109ac499332SGreg Roach ->leftJoin('name AS wife_name', static function (JoinClause $join): void { 11032cd2800SGreg Roach $join 11132cd2800SGreg Roach ->on('wife_name.n_file', '=', 'families.f_file') 112ac499332SGreg Roach ->on('wife_name.n_id', '=', 'families.f_wife') 113ac499332SGreg Roach ->where('wife_name.n_type', '<>', '_MARNM'); 114ac499332SGreg Roach }); 115a7a24840SGreg Roach 116a7a24840SGreg Roach $prefix = DB::connection()->getTablePrefix(); 117a69f5655SGreg Roach $field = new Expression('COALESCE(' . $prefix . "husb_name.n_full, '') || COALESCE(" . $prefix . "wife_name.n_full, '')"); 118a7a24840SGreg Roach 119a7a24840SGreg Roach $this->whereTrees($query, 'f_file', $trees); 120a7a24840SGreg Roach $this->whereSearch($query, $field, $search); 121a7a24840SGreg Roach 122a7a24840SGreg Roach $query 12332cd2800SGreg Roach ->orderBy('husb_name.n_sort') 12432cd2800SGreg Roach ->orderBy('wife_name.n_sort') 125c0804649SGreg Roach ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort']) 12632cd2800SGreg Roach ->distinct(); 12732cd2800SGreg Roach 128d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->familyRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 129a7a24840SGreg Roach } 130a7a24840SGreg Roach 131a7a24840SGreg Roach /** 132dbe53437SGreg Roach * @param Place $place 133dbe53437SGreg Roach * 134dbe53437SGreg Roach * @return Collection<Family> 135dbe53437SGreg Roach */ 136dbe53437SGreg Roach public function searchFamiliesInPlace(Place $place): Collection 137dbe53437SGreg Roach { 138dbe53437SGreg Roach return DB::table('families') 139dbe53437SGreg Roach ->join('placelinks', static function (JoinClause $query) { 140dbe53437SGreg Roach $query 141dbe53437SGreg Roach ->on('families.f_file', '=', 'placelinks.pl_file') 142dbe53437SGreg Roach ->on('families.f_id', '=', 'placelinks.pl_gid'); 143dbe53437SGreg Roach }) 144dbe53437SGreg Roach ->where('f_file', '=', $place->tree()->id()) 145dbe53437SGreg Roach ->where('pl_p_id', '=', $place->id()) 146dbe53437SGreg Roach ->select(['families.*']) 147dbe53437SGreg Roach ->get() 148dbe53437SGreg Roach ->each($this->rowLimiter()) 149dbe53437SGreg Roach ->map($this->familyRowMapper()) 150dbe53437SGreg Roach ->filter(GedcomRecord::accessFilter()); 151dbe53437SGreg Roach } 152dbe53437SGreg Roach 153dbe53437SGreg Roach /** 154a7a24840SGreg Roach * @param Tree[] $trees 155a7a24840SGreg Roach * @param string[] $search 156a7a24840SGreg Roach * 157b5c8fd7eSGreg Roach * @return Collection<Individual> 158a7a24840SGreg Roach */ 159a7a24840SGreg Roach public function searchIndividuals(array $trees, array $search): Collection 160a7a24840SGreg Roach { 161a7a24840SGreg Roach $query = DB::table('individuals'); 162a7a24840SGreg Roach 163a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 164a7a24840SGreg Roach $this->whereSearch($query, 'i_gedcom', $search); 165a7a24840SGreg Roach 166a7a24840SGreg Roach return $query 167a7a24840SGreg Roach ->get() 16852a8ef61SGreg Roach ->each($this->rowLimiter()) 169d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 170a7a24840SGreg Roach ->filter(GedcomRecord::accessFilter()) 1717f5fa3c2SGreg Roach ->filter($this->rawGedcomFilter($search)); 17232cd2800SGreg Roach } 17332cd2800SGreg Roach 17432cd2800SGreg Roach /** 17532cd2800SGreg Roach * Search for individuals by name. 17632cd2800SGreg Roach * 177a7a24840SGreg Roach * @param Tree[] $trees 178a7a24840SGreg Roach * @param string[] $search 17932cd2800SGreg Roach * @param int $offset 18032cd2800SGreg Roach * @param int $limit 18132cd2800SGreg Roach * 182b5c8fd7eSGreg Roach * @return Collection<Individual> 18332cd2800SGreg Roach */ 184a7a24840SGreg Roach public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 18532cd2800SGreg Roach { 18632cd2800SGreg Roach $query = DB::table('individuals') 1870b5fd0a6SGreg Roach ->join('name', static function (JoinClause $join): void { 18832cd2800SGreg Roach $join 18932cd2800SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 190a7a24840SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 19132cd2800SGreg Roach }) 192e84cf2deSGreg Roach ->orderBy('n_sort') 193d78d61f7SGreg Roach ->distinct() 194685de081SGreg Roach ->select(['individuals.*', 'n_sort']); 19532cd2800SGreg Roach 196a7a24840SGreg Roach $this->whereTrees($query, 'i_file', $trees); 197a7a24840SGreg Roach $this->whereSearch($query, 'n_full', $search); 198a7a24840SGreg Roach 199d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->individualRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 20032cd2800SGreg Roach } 20132cd2800SGreg Roach 20232cd2800SGreg Roach /** 203dbe53437SGreg Roach * @param Place $place 204dbe53437SGreg Roach * 205dbe53437SGreg Roach * @return Collection<Individual> 206dbe53437SGreg Roach */ 207dbe53437SGreg Roach public function searchIndividualsInPlace(Place $place): Collection 208dbe53437SGreg Roach { 209dbe53437SGreg Roach return DB::table('individuals') 210dbe53437SGreg Roach ->join('placelinks', static function (JoinClause $join) { 211dbe53437SGreg Roach $join 212dbe53437SGreg Roach ->on('i_file', '=', 'pl_file') 213dbe53437SGreg Roach ->on('i_id', '=', 'pl_gid'); 214dbe53437SGreg Roach }) 215dbe53437SGreg Roach ->where('i_file', '=', $place->tree()->id()) 216dbe53437SGreg Roach ->where('pl_p_id', '=', $place->id()) 217dbe53437SGreg Roach ->select(['individuals.*']) 218dbe53437SGreg Roach ->get() 219dbe53437SGreg Roach ->each($this->rowLimiter()) 220dbe53437SGreg Roach ->map($this->individualRowMapper()) 221dbe53437SGreg Roach ->filter(GedcomRecord::accessFilter()); 222dbe53437SGreg Roach } 223dbe53437SGreg Roach 224dbe53437SGreg Roach /** 22532cd2800SGreg Roach * Search for media objects. 22632cd2800SGreg Roach * 227a7a24840SGreg Roach * @param Tree[] $trees 228a7a24840SGreg Roach * @param string[] $search 22932cd2800SGreg Roach * @param int $offset 23032cd2800SGreg Roach * @param int $limit 23132cd2800SGreg Roach * 232b5c8fd7eSGreg Roach * @return Collection<Media> 23332cd2800SGreg Roach */ 234a7a24840SGreg Roach public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 23532cd2800SGreg Roach { 236a7a24840SGreg Roach $query = DB::table('media'); 23732cd2800SGreg Roach 238a7a24840SGreg Roach $this->whereTrees($query, 'media.m_file', $trees); 239a7a24840SGreg Roach $this->whereSearch($query, 'm_gedcom', $search); 240a7a24840SGreg Roach 241d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->mediaRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 24232cd2800SGreg Roach } 24332cd2800SGreg Roach 24432cd2800SGreg Roach /** 24532cd2800SGreg Roach * Search for notes. 24632cd2800SGreg Roach * 247a7a24840SGreg Roach * @param Tree[] $trees 248a7a24840SGreg Roach * @param string[] $search 24932cd2800SGreg Roach * @param int $offset 25032cd2800SGreg Roach * @param int $limit 25132cd2800SGreg Roach * 252b5c8fd7eSGreg Roach * @return Collection<Note> 25332cd2800SGreg Roach */ 254a7a24840SGreg Roach public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 25532cd2800SGreg Roach { 25632cd2800SGreg Roach $query = DB::table('other') 257a7a24840SGreg Roach ->where('o_type', '=', 'NOTE'); 25832cd2800SGreg Roach 259a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 260a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 261a7a24840SGreg Roach 262d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->noteRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 26332cd2800SGreg Roach } 26432cd2800SGreg Roach 26532cd2800SGreg Roach /** 26632cd2800SGreg Roach * Search for repositories. 26732cd2800SGreg Roach * 268a7a24840SGreg Roach * @param Tree[] $trees 269a7a24840SGreg Roach * @param string[] $search 27032cd2800SGreg Roach * @param int $offset 27132cd2800SGreg Roach * @param int $limit 27232cd2800SGreg Roach * 273b5c8fd7eSGreg Roach * @return Collection<Repository> 27432cd2800SGreg Roach */ 275a7a24840SGreg Roach public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 27632cd2800SGreg Roach { 27732cd2800SGreg Roach $query = DB::table('other') 278a7a24840SGreg Roach ->where('o_type', '=', 'REPO'); 27932cd2800SGreg Roach 280a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 281a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 282a7a24840SGreg Roach 283d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->repositoryRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 28432cd2800SGreg Roach } 28532cd2800SGreg Roach 28632cd2800SGreg Roach /** 287a7a24840SGreg Roach * Search for sources. 28832cd2800SGreg Roach * 289a7a24840SGreg Roach * @param Tree[] $trees 290a7a24840SGreg Roach * @param string[] $search 29132cd2800SGreg Roach * @param int $offset 29232cd2800SGreg Roach * @param int $limit 29332cd2800SGreg Roach * 294b5c8fd7eSGreg Roach * @return Collection<Source> 29532cd2800SGreg Roach */ 296a7a24840SGreg Roach public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 297a7a24840SGreg Roach { 298a7a24840SGreg Roach $query = DB::table('sources'); 299a7a24840SGreg Roach 300a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 301a7a24840SGreg Roach $this->whereSearch($query, 's_gedcom', $search); 302a7a24840SGreg Roach 303d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 304a7a24840SGreg Roach } 305a7a24840SGreg Roach 306a7a24840SGreg Roach /** 307a7a24840SGreg Roach * Search for sources by name. 308a7a24840SGreg Roach * 309a7a24840SGreg Roach * @param Tree[] $trees 310a7a24840SGreg Roach * @param string[] $search 311a7a24840SGreg Roach * @param int $offset 312a7a24840SGreg Roach * @param int $limit 313a7a24840SGreg Roach * 314b5c8fd7eSGreg Roach * @return Collection<Source> 315a7a24840SGreg Roach */ 316a7a24840SGreg Roach public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 31732cd2800SGreg Roach { 31832cd2800SGreg Roach $query = DB::table('sources') 319c0804649SGreg Roach ->orderBy('s_name'); 32032cd2800SGreg Roach 321a7a24840SGreg Roach $this->whereTrees($query, 's_file', $trees); 322a7a24840SGreg Roach $this->whereSearch($query, 's_name', $search); 323a7a24840SGreg Roach 324d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->sourceRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 32532cd2800SGreg Roach } 32632cd2800SGreg Roach 32732cd2800SGreg Roach /** 32832cd2800SGreg Roach * Search for submitters. 32932cd2800SGreg Roach * 330a7a24840SGreg Roach * @param Tree[] $trees 331a7a24840SGreg Roach * @param string[] $search 33232cd2800SGreg Roach * @param int $offset 33332cd2800SGreg Roach * @param int $limit 33432cd2800SGreg Roach * 335b5c8fd7eSGreg Roach * @return Collection<Submitter> 33632cd2800SGreg Roach */ 337a7a24840SGreg Roach public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 33832cd2800SGreg Roach { 33932cd2800SGreg Roach $query = DB::table('other') 340a7a24840SGreg Roach ->where('o_type', '=', 'SUBM'); 34132cd2800SGreg Roach 342a7a24840SGreg Roach $this->whereTrees($query, 'o_file', $trees); 343a7a24840SGreg Roach $this->whereSearch($query, 'o_gedcom', $search); 344a7a24840SGreg Roach 345d5ad3db0SGreg Roach return $this->paginateQuery($query, $this->submitterRowMapper(), GedcomRecord::accessFilter(), $offset, $limit); 34632cd2800SGreg Roach } 34732cd2800SGreg Roach 34832cd2800SGreg Roach /** 349b68caec6SGreg Roach * Search for places. 350b68caec6SGreg Roach * 351b68caec6SGreg Roach * @param Tree $tree 352b68caec6SGreg Roach * @param string $search 353b68caec6SGreg Roach * @param int $offset 354b68caec6SGreg Roach * @param int $limit 355b68caec6SGreg Roach * 356b5c8fd7eSGreg Roach * @return Collection<Place> 357b68caec6SGreg Roach */ 358b68caec6SGreg Roach public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection 359b68caec6SGreg Roach { 360b68caec6SGreg Roach $query = DB::table('places AS p0') 361b68caec6SGreg Roach ->where('p0.p_file', '=', $tree->id()) 362b68caec6SGreg Roach ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id') 363b68caec6SGreg Roach ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id') 364b68caec6SGreg Roach ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id') 365b68caec6SGreg Roach ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id') 366b68caec6SGreg Roach ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id') 367b68caec6SGreg Roach ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id') 368b68caec6SGreg Roach ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id') 369b68caec6SGreg Roach ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id') 370b68caec6SGreg Roach ->orderBy('p0.p_place') 371b68caec6SGreg Roach ->orderBy('p1.p_place') 372b68caec6SGreg Roach ->orderBy('p2.p_place') 373b68caec6SGreg Roach ->orderBy('p3.p_place') 374b68caec6SGreg Roach ->orderBy('p4.p_place') 375b68caec6SGreg Roach ->orderBy('p5.p_place') 376b68caec6SGreg Roach ->orderBy('p6.p_place') 377b68caec6SGreg Roach ->orderBy('p7.p_place') 378b68caec6SGreg Roach ->orderBy('p8.p_place') 379b68caec6SGreg Roach ->select([ 380b68caec6SGreg Roach 'p0.p_place AS place0', 381b68caec6SGreg Roach 'p1.p_place AS place1', 382b68caec6SGreg Roach 'p2.p_place AS place2', 383b68caec6SGreg Roach 'p3.p_place AS place3', 384b68caec6SGreg Roach 'p4.p_place AS place4', 385b68caec6SGreg Roach 'p5.p_place AS place5', 386b68caec6SGreg Roach 'p6.p_place AS place6', 387b68caec6SGreg Roach 'p7.p_place AS place7', 388b68caec6SGreg Roach 'p8.p_place AS place8', 389b68caec6SGreg Roach ]); 390b68caec6SGreg Roach 391b68caec6SGreg Roach // Filter each level of the hierarchy. 392b68caec6SGreg Roach foreach (explode(',', $search, 9) as $level => $string) { 393b5961194SGreg Roach $query->where('p' . $level . '.p_place', 'LIKE', '%' . addcslashes($string, '\\%_') . '%'); 394b68caec6SGreg Roach } 395b68caec6SGreg Roach 3966c2179e2SGreg Roach $row_mapper = static function (stdClass $row) use ($tree): Place { 397b68caec6SGreg Roach $place = implode(', ', array_filter((array) $row)); 398b68caec6SGreg Roach 399b68caec6SGreg Roach return new Place($place, $tree); 400b68caec6SGreg Roach }; 401b68caec6SGreg Roach 4026c2179e2SGreg Roach $filter = static function (): bool { 403a7a24840SGreg Roach return true; 404a7a24840SGreg Roach }; 405b68caec6SGreg Roach 406a7a24840SGreg Roach return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit); 407a7a24840SGreg Roach } 408b68caec6SGreg Roach 409b68caec6SGreg Roach /** 410a5dbc5b2SGreg Roach * @param Tree[] $trees 411dfb2cda2SGreg Roach * @param string[] $fields 412dfb2cda2SGreg Roach * @param string[] $modifiers 413dfb2cda2SGreg Roach * 414b5c8fd7eSGreg Roach * @return Collection<Individual> 415dfb2cda2SGreg Roach */ 416dfb2cda2SGreg Roach public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection 417dfb2cda2SGreg Roach { 418dfb2cda2SGreg Roach $fields = array_filter($fields); 419dfb2cda2SGreg Roach 420dfb2cda2SGreg Roach $query = DB::table('individuals') 421dfb2cda2SGreg Roach ->select(['individuals.*']) 422dfb2cda2SGreg Roach ->distinct(); 423dfb2cda2SGreg Roach 424dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $trees); 425dfb2cda2SGreg Roach 426dfb2cda2SGreg Roach // Join the following tables 427dfb2cda2SGreg Roach $father_name = false; 428dfb2cda2SGreg Roach $mother_name = false; 429dfb2cda2SGreg Roach $spouse_family = false; 430dfb2cda2SGreg Roach $indi_name = false; 43156946b7aSGreg Roach $indi_dates = []; 43256946b7aSGreg Roach $fam_dates = []; 433dfb2cda2SGreg Roach $indi_plac = false; 434dfb2cda2SGreg Roach $fam_plac = false; 435dfb2cda2SGreg Roach 436dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 437dfb2cda2SGreg Roach if ($field_value !== '') { 43856946b7aSGreg Roach // Fields can have up to 4 parts, but we only need the first 3 to identify 43956946b7aSGreg Roach // which tables to select 44056946b7aSGreg Roach $field_parts = explode(':', $field_name . '::'); 44156946b7aSGreg Roach 44256946b7aSGreg Roach if ($field_parts[0] === 'FAMC') { 44356946b7aSGreg Roach // Parent name - FAMC:[HUSB|WIFE]:NAME:[GIVN|SURN] 44456946b7aSGreg Roach if ($field_parts[1] === 'HUSB') { 445dfb2cda2SGreg Roach $father_name = true; 446*31dd1910SGreg Roach } else { 447dfb2cda2SGreg Roach $mother_name = true; 448dfb2cda2SGreg Roach } 44956946b7aSGreg Roach } elseif ($field_parts[0] === 'NAME') { 45056946b7aSGreg Roach // Individual name - NAME:[GIVN|SURN] 45156946b7aSGreg Roach $indi_name = true; 45256946b7aSGreg Roach } elseif ($field_parts[0] === 'FAMS') { 45356946b7aSGreg Roach // Family facts - FAMS:NOTE or FAMS:[FACT]:[DATE|PLAC] 454dfb2cda2SGreg Roach $spouse_family = true; 45556946b7aSGreg Roach if ($field_parts[2] === 'DATE') { 45656946b7aSGreg Roach $fam_dates[] = $field_parts[1]; 45756946b7aSGreg Roach } elseif ($field_parts[2] === 'PLAC') { 45856946b7aSGreg Roach $fam_plac = true; 45956946b7aSGreg Roach } 460dfb2cda2SGreg Roach } else { 46156946b7aSGreg Roach // Individual facts - [FACT] or [FACT]:[DATE|PLAC] 46256946b7aSGreg Roach if ($field_parts[1] === 'DATE') { 46356946b7aSGreg Roach $indi_dates[] = $field_parts[0]; 46456946b7aSGreg Roach } elseif ($field_parts[1] === 'PLAC') { 465dfb2cda2SGreg Roach $indi_plac = true; 466dfb2cda2SGreg Roach } 467dfb2cda2SGreg Roach } 468dfb2cda2SGreg Roach } 469dfb2cda2SGreg Roach } 470dfb2cda2SGreg Roach 471dfb2cda2SGreg Roach if ($father_name || $mother_name) { 4720b5fd0a6SGreg Roach $query->join('link AS l1', static function (JoinClause $join): void { 473dfb2cda2SGreg Roach $join 474dfb2cda2SGreg Roach ->on('l1.l_file', '=', 'individuals.i_file') 475dfb2cda2SGreg Roach ->on('l1.l_from', '=', 'individuals.i_id') 476dfb2cda2SGreg Roach ->where('l1.l_type', '=', 'FAMC'); 477dfb2cda2SGreg Roach }); 478dfb2cda2SGreg Roach 479dfb2cda2SGreg Roach if ($father_name) { 4800b5fd0a6SGreg Roach $query->join('link AS l2', static function (JoinClause $join): void { 481dfb2cda2SGreg Roach $join 482dfb2cda2SGreg Roach ->on('l2.l_file', '=', 'l1.l_file') 483dfb2cda2SGreg Roach ->on('l2.l_from', '=', 'l1.l_to') 484dfb2cda2SGreg Roach ->where('l2.l_type', '=', 'HUSB'); 485dfb2cda2SGreg Roach }); 4860b5fd0a6SGreg Roach $query->join('name AS father_name', static function (JoinClause $join): void { 487dfb2cda2SGreg Roach $join 488dfb2cda2SGreg Roach ->on('father_name.n_file', '=', 'l2.l_file') 489dfb2cda2SGreg Roach ->on('father_name.n_id', '=', 'l2.l_to'); 490dfb2cda2SGreg Roach }); 491dfb2cda2SGreg Roach } 492dfb2cda2SGreg Roach 493dfb2cda2SGreg Roach if ($mother_name) { 4940b5fd0a6SGreg Roach $query->join('link AS l3', static function (JoinClause $join): void { 495dfb2cda2SGreg Roach $join 496dfb2cda2SGreg Roach ->on('l3.l_file', '=', 'l1.l_file') 497dfb2cda2SGreg Roach ->on('l3.l_from', '=', 'l1.l_to') 498dfb2cda2SGreg Roach ->where('l3.l_type', '=', 'WIFE'); 499dfb2cda2SGreg Roach }); 5000b5fd0a6SGreg Roach $query->join('name AS mother_name', static function (JoinClause $join): void { 501dfb2cda2SGreg Roach $join 502dfb2cda2SGreg Roach ->on('mother_name.n_file', '=', 'l3.l_file') 503dfb2cda2SGreg Roach ->on('mother_name.n_id', '=', 'l3.l_to'); 504dfb2cda2SGreg Roach }); 505dfb2cda2SGreg Roach } 506dfb2cda2SGreg Roach } 507dfb2cda2SGreg Roach 508dfb2cda2SGreg Roach if ($spouse_family) { 5090b5fd0a6SGreg Roach $query->join('link AS l4', static function (JoinClause $join): void { 510dfb2cda2SGreg Roach $join 511dfb2cda2SGreg Roach ->on('l4.l_file', '=', 'individuals.i_file') 512dfb2cda2SGreg Roach ->on('l4.l_from', '=', 'individuals.i_id') 513dfb2cda2SGreg Roach ->where('l4.l_type', '=', 'FAMS'); 514dfb2cda2SGreg Roach }); 5150b5fd0a6SGreg Roach $query->join('families AS spouse_families', static function (JoinClause $join): void { 516dfb2cda2SGreg Roach $join 517dfb2cda2SGreg Roach ->on('spouse_families.f_file', '=', 'l4.l_file') 518dfb2cda2SGreg Roach ->on('spouse_families.f_id', '=', 'l4.l_to'); 519dfb2cda2SGreg Roach }); 520dfb2cda2SGreg Roach } 521dfb2cda2SGreg Roach 522dfb2cda2SGreg Roach if ($indi_name) { 5230b5fd0a6SGreg Roach $query->join('name AS individual_name', static function (JoinClause $join): void { 524dfb2cda2SGreg Roach $join 525dfb2cda2SGreg Roach ->on('individual_name.n_file', '=', 'individuals.i_file') 526dfb2cda2SGreg Roach ->on('individual_name.n_id', '=', 'individuals.i_id'); 527dfb2cda2SGreg Roach }); 528dfb2cda2SGreg Roach } 529dfb2cda2SGreg Roach 53056946b7aSGreg Roach foreach (array_unique($indi_dates) as $indi_date) { 53156946b7aSGreg Roach $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void { 532dfb2cda2SGreg Roach $join 53356946b7aSGreg Roach ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file') 53456946b7aSGreg Roach ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id'); 535dfb2cda2SGreg Roach }); 536dfb2cda2SGreg Roach } 537dfb2cda2SGreg Roach 53856946b7aSGreg Roach foreach (array_unique($fam_dates) as $fam_date) { 53956946b7aSGreg Roach $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void { 540dfb2cda2SGreg Roach $join 54156946b7aSGreg Roach ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.i_file') 54256946b7aSGreg Roach ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.i_id'); 543dfb2cda2SGreg Roach }); 544dfb2cda2SGreg Roach } 545dfb2cda2SGreg Roach 546dfb2cda2SGreg Roach if ($indi_plac) { 5470b5fd0a6SGreg Roach $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { 548dfb2cda2SGreg Roach $join 549dfb2cda2SGreg Roach ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') 550dfb2cda2SGreg Roach ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); 551dfb2cda2SGreg Roach }); 5520b5fd0a6SGreg Roach $query->join('places AS individual_places', static function (JoinClause $join): void { 553dfb2cda2SGreg Roach $join 554dfb2cda2SGreg Roach ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') 555dfb2cda2SGreg Roach ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); 556dfb2cda2SGreg Roach }); 557dfb2cda2SGreg Roach } 558dfb2cda2SGreg Roach 559dfb2cda2SGreg Roach if ($fam_plac) { 5600b5fd0a6SGreg Roach $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void { 561dfb2cda2SGreg Roach $join 562dfb2cda2SGreg Roach ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file') 563dfb2cda2SGreg Roach ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id'); 564dfb2cda2SGreg Roach }); 5650b5fd0a6SGreg Roach $query->join('places AS family_places', static function (JoinClause $join): void { 566dfb2cda2SGreg Roach $join 567dfb2cda2SGreg Roach ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file') 568dfb2cda2SGreg Roach ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id'); 569dfb2cda2SGreg Roach }); 570dfb2cda2SGreg Roach } 571dfb2cda2SGreg Roach 572dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 57356946b7aSGreg Roach $parts = explode(':', $field_name . ':::'); 574dfb2cda2SGreg Roach if ($parts[0] === 'NAME') { 575dfb2cda2SGreg Roach // NAME:* 576dfb2cda2SGreg Roach switch ($parts[1]) { 577dfb2cda2SGreg Roach case 'GIVN': 578dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 579dfb2cda2SGreg Roach case 'EXACT': 580dfb2cda2SGreg Roach $query->where('individual_name.n_givn', '=', $field_value); 581dfb2cda2SGreg Roach break; 582dfb2cda2SGreg Roach case 'BEGINS': 583dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', $field_value . '%'); 584dfb2cda2SGreg Roach break; 585dfb2cda2SGreg Roach case 'CONTAINS': 586dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 587dfb2cda2SGreg Roach break; 588dfb2cda2SGreg Roach case 'SDX_STD': 589dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 590dfb2cda2SGreg Roach if ($sdx !== '') { 591dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx); 592dfb2cda2SGreg Roach } else { 593dfb2cda2SGreg Roach // No phonetic content? Use a substring match 594dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 595dfb2cda2SGreg Roach } 596dfb2cda2SGreg Roach break; 597dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 598dfb2cda2SGreg Roach case 'SDX_DM': 599dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 600dfb2cda2SGreg Roach if ($sdx !== '') { 601dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx); 602dfb2cda2SGreg Roach } else { 603dfb2cda2SGreg Roach // No phonetic content? Use a substring match 604dfb2cda2SGreg Roach $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); 605dfb2cda2SGreg Roach } 606dfb2cda2SGreg Roach break; 607dfb2cda2SGreg Roach } 608dfb2cda2SGreg Roach break; 609dfb2cda2SGreg Roach case 'SURN': 610dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 611dfb2cda2SGreg Roach case 'EXACT': 612fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 613fc6d23eeSGreg Roach $query 614fc6d23eeSGreg Roach ->where('individual_name.n_surn', '=', $field_value) 615fc6d23eeSGreg Roach ->orWhere('individual_name.n_surname', '=', $field_value); 616fc6d23eeSGreg Roach }); 617dfb2cda2SGreg Roach break; 618dfb2cda2SGreg Roach case 'BEGINS': 619fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 620fc6d23eeSGreg Roach $query 621fc6d23eeSGreg Roach ->where('individual_name.n_surn', 'LIKE', $field_value . '%') 6229875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', $field_value . '%'); 623fc6d23eeSGreg Roach }); 624dfb2cda2SGreg Roach break; 625dfb2cda2SGreg Roach case 'CONTAINS': 626fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 627fc6d23eeSGreg Roach $query 628fc6d23eeSGreg Roach ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%') 6299875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%'); 630fc6d23eeSGreg Roach }); 631dfb2cda2SGreg Roach break; 632dfb2cda2SGreg Roach case 'SDX_STD': 633dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 634dfb2cda2SGreg Roach if ($sdx !== '') { 635dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); 636dfb2cda2SGreg Roach } else { 637dfb2cda2SGreg Roach // No phonetic content? Use a substring match 638fc6d23eeSGreg Roach $query->where(function (Builder $query) use ($field_value): void { 639fc6d23eeSGreg Roach $query 640fc6d23eeSGreg Roach ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%') 6419875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%'); 642fc6d23eeSGreg Roach }); 643dfb2cda2SGreg Roach } 644dfb2cda2SGreg Roach break; 645dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 646dfb2cda2SGreg Roach case 'SDX_DM': 647dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 648dfb2cda2SGreg Roach if ($sdx !== '') { 649dfb2cda2SGreg Roach $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); 650dfb2cda2SGreg Roach } else { 651dfb2cda2SGreg Roach // No phonetic content? Use a substring match 6529875d474SGreg Roach $query->where(function (Builder $query) use ($field_value): void { 6539875d474SGreg Roach $query 6549875d474SGreg Roach ->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%') 6559875d474SGreg Roach ->orWhere('individual_name.n_surname', 'LIKE', '%' . $field_value . '%'); 6569875d474SGreg Roach }); 657dfb2cda2SGreg Roach } 658dfb2cda2SGreg Roach break; 659dfb2cda2SGreg Roach } 660dfb2cda2SGreg Roach break; 661dfb2cda2SGreg Roach case 'NICK': 662dfb2cda2SGreg Roach case '_MARNM': 663dfb2cda2SGreg Roach case '_HEB': 664dfb2cda2SGreg Roach case '_AKA': 665dfb2cda2SGreg Roach $query 666dfb2cda2SGreg Roach ->where('individual_name', '=', $parts[1]) 667dfb2cda2SGreg Roach ->where('individual_name', 'LIKE', '%' . $field_value . '%'); 668dfb2cda2SGreg Roach break; 669dfb2cda2SGreg Roach } 670dfb2cda2SGreg Roach unset($fields[$field_name]); 671dfb2cda2SGreg Roach } elseif ($parts[1] === 'DATE') { 672dfb2cda2SGreg Roach // *:DATE 673dfb2cda2SGreg Roach $date = new Date($field_value); 674dfb2cda2SGreg Roach if ($date->isOK()) { 675dfb2cda2SGreg Roach $delta = 365 * ($modifiers[$field_name] ?? 0); 676dfb2cda2SGreg Roach $query 67756946b7aSGreg Roach ->where('date_' . $parts[0] . '.d_fact', '=', $parts[0]) 67856946b7aSGreg Roach ->where('date_' . $parts[0] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 67956946b7aSGreg Roach ->where('date_' . $parts[0] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); 680dfb2cda2SGreg Roach } 681dfb2cda2SGreg Roach unset($fields[$field_name]); 682dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') { 683dfb2cda2SGreg Roach // FAMS:*:DATE 684dfb2cda2SGreg Roach $date = new Date($field_value); 685dfb2cda2SGreg Roach if ($date->isOK()) { 686dfb2cda2SGreg Roach $delta = 365 * $modifiers[$field_name]; 687dfb2cda2SGreg Roach $query 68856946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) 68956946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) 69056946b7aSGreg Roach ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); 691dfb2cda2SGreg Roach } 692dfb2cda2SGreg Roach unset($fields[$field_name]); 693dfb2cda2SGreg Roach } elseif ($parts[1] === 'PLAC') { 694dfb2cda2SGreg Roach // *:PLAC 695dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 696dfb2cda2SGreg Roach $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%'); 697dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') { 698dfb2cda2SGreg Roach // FAMS:*:PLAC 699dfb2cda2SGreg Roach // SQL can only link a place to a person/family, not to an event. 700dfb2cda2SGreg Roach $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%'); 701dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') { 702dfb2cda2SGreg Roach $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name'; 703dfb2cda2SGreg Roach // NAME:* 704dfb2cda2SGreg Roach switch ($parts[3]) { 705dfb2cda2SGreg Roach case 'GIVN': 706dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 707dfb2cda2SGreg Roach case 'EXACT': 708dfb2cda2SGreg Roach $query->where($table . '.n_givn', '=', $field_value); 709dfb2cda2SGreg Roach break; 710dfb2cda2SGreg Roach case 'BEGINS': 711dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', $field_value . '%'); 712dfb2cda2SGreg Roach break; 713dfb2cda2SGreg Roach case 'CONTAINS': 714dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 715dfb2cda2SGreg Roach break; 716dfb2cda2SGreg Roach case 'SDX_STD': 717dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 718dfb2cda2SGreg Roach if ($sdx !== '') { 719dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); 720dfb2cda2SGreg Roach } else { 721dfb2cda2SGreg Roach // No phonetic content? Use a substring match 722dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 723dfb2cda2SGreg Roach } 724dfb2cda2SGreg Roach break; 725dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 726dfb2cda2SGreg Roach case 'SDX_DM': 727dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 728dfb2cda2SGreg Roach if ($sdx !== '') { 729dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); 730dfb2cda2SGreg Roach } else { 731dfb2cda2SGreg Roach // No phonetic content? Use a substring match 732dfb2cda2SGreg Roach $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); 733dfb2cda2SGreg Roach } 734dfb2cda2SGreg Roach break; 735dfb2cda2SGreg Roach } 736dfb2cda2SGreg Roach break; 737dfb2cda2SGreg Roach case 'SURN': 738dfb2cda2SGreg Roach switch ($modifiers[$field_name]) { 739dfb2cda2SGreg Roach case 'EXACT': 740dfb2cda2SGreg Roach $query->where($table . '.n_surn', '=', $field_value); 741dfb2cda2SGreg Roach break; 742dfb2cda2SGreg Roach case 'BEGINS': 743dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', $field_value . '%'); 744dfb2cda2SGreg Roach break; 745dfb2cda2SGreg Roach case 'CONTAINS': 746dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 747dfb2cda2SGreg Roach break; 748dfb2cda2SGreg Roach case 'SDX_STD': 749dfb2cda2SGreg Roach $sdx = Soundex::russell($field_value); 750dfb2cda2SGreg Roach if ($sdx !== '') { 751dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); 752dfb2cda2SGreg Roach } else { 753dfb2cda2SGreg Roach // No phonetic content? Use a substring match 754dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 755dfb2cda2SGreg Roach } 756dfb2cda2SGreg Roach break; 757dfb2cda2SGreg Roach case 'SDX': // SDX uses DM by default. 758dfb2cda2SGreg Roach case 'SDX_DM': 759dfb2cda2SGreg Roach $sdx = Soundex::daitchMokotoff($field_value); 760dfb2cda2SGreg Roach if ($sdx !== '') { 761dfb2cda2SGreg Roach $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); 762dfb2cda2SGreg Roach } else { 763dfb2cda2SGreg Roach // No phonetic content? Use a substring match 764dfb2cda2SGreg Roach $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); 765dfb2cda2SGreg Roach } 766dfb2cda2SGreg Roach break; 767dfb2cda2SGreg Roach } 768dfb2cda2SGreg Roach break; 769dfb2cda2SGreg Roach } 770dfb2cda2SGreg Roach unset($fields[$field_name]); 771dfb2cda2SGreg Roach } elseif ($parts[0] === 'FAMS') { 772dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 773dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 7743c480c6bSGreg Roach $query->where('spouse_families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%'); 775dfb2cda2SGreg Roach } elseif ($parts[1] === 'TYPE') { 776dfb2cda2SGreg Roach // e.g. FACT:TYPE or EVEN:TYPE 777dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 778dfb2cda2SGreg Roach $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%\n2 TYPE %' . $field_value . '%'); 779dfb2cda2SGreg Roach } else { 780dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 781dfb2cda2SGreg Roach // Initial matching only. Need PHP to apply filter. 782c5457947SGreg Roach $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%' . $parts[1] . '%' . $field_value . '%'); 783dfb2cda2SGreg Roach } 784dfb2cda2SGreg Roach } 785dfb2cda2SGreg Roach return $query 786dfb2cda2SGreg Roach ->get() 78752a8ef61SGreg Roach ->each($this->rowLimiter()) 788d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 789dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()) 7900b5fd0a6SGreg Roach ->filter(static function (Individual $individual) use ($fields): bool { 791c5457947SGreg Roach // Check for searches which were only partially matched by SQL 792dfb2cda2SGreg Roach foreach ($fields as $field_name => $field_value) { 793a41e65f0SGreg Roach $regex = '/' . preg_quote($field_value, '/') . '/i'; 794dfb2cda2SGreg Roach 795c5457947SGreg Roach $parts = explode(':', $field_name . '::::'); 796dfb2cda2SGreg Roach 797dfb2cda2SGreg Roach // *:PLAC 798c5457947SGreg Roach if ($parts[1] === 'PLAC') { 799a41e65f0SGreg Roach foreach ($individual->facts([$parts[0]]) as $fact) { 800392561bbSGreg Roach if (preg_match($regex, $fact->place()->gedcomName())) { 801c5457947SGreg Roach continue 2; 802dfb2cda2SGreg Roach } 803a41e65f0SGreg Roach } 804c5457947SGreg Roach return false; 805c5457947SGreg Roach } 806c5457947SGreg Roach 807dfb2cda2SGreg Roach // FAMS:*:PLAC 808c5457947SGreg Roach if ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') { 80939ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 810a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 811392561bbSGreg Roach if (preg_match($regex, $fact->place()->gedcomName())) { 812c5457947SGreg Roach continue 2; 813a41e65f0SGreg Roach } 814dfb2cda2SGreg Roach } 815dfb2cda2SGreg Roach } 816c5457947SGreg Roach return false; 817c5457947SGreg Roach } 818c5457947SGreg Roach 819dfb2cda2SGreg Roach // e.g. searches for occupation, religion, note, etc. 820c5457947SGreg Roach if ($parts[0] === 'FAMS') { 82139ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 822a41e65f0SGreg Roach foreach ($family->facts([$parts[1]]) as $fact) { 823a41e65f0SGreg Roach if (preg_match($regex, $fact->value())) { 824c5457947SGreg Roach continue 3; 825a41e65f0SGreg Roach } 826dfb2cda2SGreg Roach } 827dfb2cda2SGreg Roach } 828c5457947SGreg Roach return false; 829c5457947SGreg Roach } 830c5457947SGreg Roach 831dfb2cda2SGreg Roach // e.g. FACT:TYPE or EVEN:TYPE 832c5457947SGreg Roach if ($parts[1] === 'TYPE' || $parts[1] === '_WT_USER') { 833a41e65f0SGreg Roach foreach ($individual->facts([$parts[0]]) as $fact) { 834c5457947SGreg Roach if (preg_match($regex, $fact->attribute($parts[1]))) { 835c5457947SGreg Roach continue 2; 836dfb2cda2SGreg Roach } 837dfb2cda2SGreg Roach } 838dfb2cda2SGreg Roach 839dfb2cda2SGreg Roach return false; 840dfb2cda2SGreg Roach } 841c5457947SGreg Roach } 842dfb2cda2SGreg Roach 843dfb2cda2SGreg Roach return true; 844dfb2cda2SGreg Roach }); 845dfb2cda2SGreg Roach } 846dfb2cda2SGreg Roach 847dfb2cda2SGreg Roach /** 848dfb2cda2SGreg Roach * @param string $soundex 849dfb2cda2SGreg Roach * @param string $lastname 850dfb2cda2SGreg Roach * @param string $firstname 851dfb2cda2SGreg Roach * @param string $place 852dfb2cda2SGreg Roach * @param Tree[] $search_trees 853dfb2cda2SGreg Roach * 854b5c8fd7eSGreg Roach * @return Collection<Individual> 855dfb2cda2SGreg Roach */ 856dfb2cda2SGreg Roach public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection 857dfb2cda2SGreg Roach { 858dfb2cda2SGreg Roach switch ($soundex) { 859dfb2cda2SGreg Roach default: 860dfb2cda2SGreg Roach case 'Russell': 861dfb2cda2SGreg Roach $givn_sdx = Soundex::russell($firstname); 862dfb2cda2SGreg Roach $surn_sdx = Soundex::russell($lastname); 863dfb2cda2SGreg Roach $plac_sdx = Soundex::russell($place); 864dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_std'; 865dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_std'; 866dfb2cda2SGreg Roach $plac_field = 'p_std_soundex'; 867dfb2cda2SGreg Roach break; 868dfb2cda2SGreg Roach case 'DaitchM': 869dfb2cda2SGreg Roach $givn_sdx = Soundex::daitchMokotoff($firstname); 870dfb2cda2SGreg Roach $surn_sdx = Soundex::daitchMokotoff($lastname); 871dfb2cda2SGreg Roach $plac_sdx = Soundex::daitchMokotoff($place); 872dfb2cda2SGreg Roach $givn_field = 'n_soundex_givn_dm'; 873dfb2cda2SGreg Roach $surn_field = 'n_soundex_surn_dm'; 874dfb2cda2SGreg Roach $plac_field = 'p_dm_soundex'; 875dfb2cda2SGreg Roach break; 876dfb2cda2SGreg Roach } 877dfb2cda2SGreg Roach 878dfb2cda2SGreg Roach // Nothing to search for? Return nothing. 879dfb2cda2SGreg Roach if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') { 8803fda39a7SGreg Roach return new Collection(); 881dfb2cda2SGreg Roach } 882dfb2cda2SGreg Roach 883dfb2cda2SGreg Roach $query = DB::table('individuals') 884dfb2cda2SGreg Roach ->select(['individuals.*']) 885dfb2cda2SGreg Roach ->distinct(); 886dfb2cda2SGreg Roach 887dfb2cda2SGreg Roach $this->whereTrees($query, 'i_file', $search_trees); 888dfb2cda2SGreg Roach 889dfb2cda2SGreg Roach if ($plac_sdx !== '') { 8900b5fd0a6SGreg Roach $query->join('placelinks', static function (JoinClause $join): void { 891dfb2cda2SGreg Roach $join 892dfb2cda2SGreg Roach ->on('placelinks.pl_file', '=', 'individuals.i_file') 893dfb2cda2SGreg Roach ->on('placelinks.pl_gid', '=', 'individuals.i_id'); 894dfb2cda2SGreg Roach }); 8950b5fd0a6SGreg Roach $query->join('places', static function (JoinClause $join): void { 896dfb2cda2SGreg Roach $join 897dfb2cda2SGreg Roach ->on('places.p_file', '=', 'placelinks.pl_file') 898dfb2cda2SGreg Roach ->on('places.p_id', '=', 'placelinks.pl_p_id'); 899dfb2cda2SGreg Roach }); 900dfb2cda2SGreg Roach 901dfb2cda2SGreg Roach $this->wherePhonetic($query, $plac_field, $plac_sdx); 902dfb2cda2SGreg Roach } 903dfb2cda2SGreg Roach 904dfb2cda2SGreg Roach if ($givn_sdx !== '' || $surn_sdx !== '') { 9050b5fd0a6SGreg Roach $query->join('name', static function (JoinClause $join): void { 906dfb2cda2SGreg Roach $join 907dfb2cda2SGreg Roach ->on('name.n_file', '=', 'individuals.i_file') 908dfb2cda2SGreg Roach ->on('name.n_id', '=', 'individuals.i_id'); 909dfb2cda2SGreg Roach }); 910dfb2cda2SGreg Roach 911dfb2cda2SGreg Roach $this->wherePhonetic($query, $givn_field, $givn_sdx); 912dfb2cda2SGreg Roach $this->wherePhonetic($query, $surn_field, $surn_sdx); 913dfb2cda2SGreg Roach } 914dfb2cda2SGreg Roach 915dfb2cda2SGreg Roach return $query 916dfb2cda2SGreg Roach ->get() 91752a8ef61SGreg Roach ->each($this->rowLimiter()) 918d5ad3db0SGreg Roach ->map($this->individualRowMapper()) 919dfb2cda2SGreg Roach ->filter(GedcomRecord::accessFilter()); 920dfb2cda2SGreg Roach } 921dfb2cda2SGreg Roach 922dfb2cda2SGreg Roach /** 92332cd2800SGreg Roach * Paginate a search query. 92432cd2800SGreg Roach * 92532cd2800SGreg Roach * @param Builder $query Searches the database for the desired records. 92632cd2800SGreg Roach * @param Closure $row_mapper Converts a row from the query into a record. 927a7a24840SGreg Roach * @param Closure $row_filter 92832cd2800SGreg Roach * @param int $offset Skip this many rows. 92932cd2800SGreg Roach * @param int $limit Take this many rows. 93032cd2800SGreg Roach * 931b5c8fd7eSGreg Roach * @return Collection<mixed> 93232cd2800SGreg Roach */ 933a7a24840SGreg Roach private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection 93432cd2800SGreg Roach { 93532cd2800SGreg Roach $collection = new Collection(); 93632cd2800SGreg Roach 93732cd2800SGreg Roach foreach ($query->cursor() as $row) { 93832cd2800SGreg Roach $record = $row_mapper($row); 939b68caec6SGreg Roach // If the object has a method "canShow()", then use it to filter for privacy. 940a7a24840SGreg Roach if ($row_filter($record)) { 94132cd2800SGreg Roach if ($offset > 0) { 94232cd2800SGreg Roach $offset--; 94332cd2800SGreg Roach } else { 94432cd2800SGreg Roach if ($limit > 0) { 94532cd2800SGreg Roach $collection->push($record); 94632cd2800SGreg Roach } 94732cd2800SGreg Roach 94832cd2800SGreg Roach $limit--; 94932cd2800SGreg Roach 95032cd2800SGreg Roach if ($limit === 0) { 95132cd2800SGreg Roach break; 95232cd2800SGreg Roach } 95332cd2800SGreg Roach } 95432cd2800SGreg Roach } 95532cd2800SGreg Roach } 95632cd2800SGreg Roach 957e0458bdcSGreg Roach 95832cd2800SGreg Roach return $collection; 95932cd2800SGreg Roach } 960a7a24840SGreg Roach 961a7a24840SGreg Roach /** 962a7a24840SGreg Roach * Apply search filters to a SQL query column. Apply collation rules to MySQL. 963a7a24840SGreg Roach * 964a7a24840SGreg Roach * @param Builder $query 965a7a24840SGreg Roach * @param Expression|string $field 966a7a24840SGreg Roach * @param string[] $search_terms 967a7a24840SGreg Roach */ 968a7a24840SGreg Roach private function whereSearch(Builder $query, $field, array $search_terms): void 969a7a24840SGreg Roach { 970a7a24840SGreg Roach if ($field instanceof Expression) { 971a7a24840SGreg Roach $field = $field->getValue(); 972a7a24840SGreg Roach } 973a7a24840SGreg Roach 974a7a24840SGreg Roach foreach ($search_terms as $search_term) { 975b5961194SGreg Roach $query->where(new Expression($field), 'LIKE', '%' . addcslashes($search_term, '\\%_') . '%'); 976a7a24840SGreg Roach } 977a7a24840SGreg Roach } 978a7a24840SGreg Roach 979a7a24840SGreg Roach /** 9802d686e68SGreg Roach * Apply soundex search filters to a SQL query column. 9812d686e68SGreg Roach * 9822d686e68SGreg Roach * @param Builder $query 9832d686e68SGreg Roach * @param Expression|string $field 9842d686e68SGreg Roach * @param string $soundex 9852d686e68SGreg Roach */ 9862d686e68SGreg Roach private function wherePhonetic(Builder $query, $field, string $soundex): void 9872d686e68SGreg Roach { 9882d686e68SGreg Roach if ($soundex !== '') { 9890b5fd0a6SGreg Roach $query->where(static function (Builder $query) use ($soundex, $field): void { 9902d686e68SGreg Roach foreach (explode(':', $soundex) as $sdx) { 9912d686e68SGreg Roach $query->orWhere($field, 'LIKE', '%' . $sdx . '%'); 9922d686e68SGreg Roach } 9932d686e68SGreg Roach }); 9942d686e68SGreg Roach } 9952d686e68SGreg Roach } 9962d686e68SGreg Roach 9972d686e68SGreg Roach /** 998a7a24840SGreg Roach * @param Builder $query 999a7a24840SGreg Roach * @param string $tree_id_field 1000a7a24840SGreg Roach * @param Tree[] $trees 1001a7a24840SGreg Roach */ 1002a7a24840SGreg Roach private function whereTrees(Builder $query, string $tree_id_field, array $trees): void 1003a7a24840SGreg Roach { 10040b5fd0a6SGreg Roach $tree_ids = array_map(static function (Tree $tree): int { 1005a7a24840SGreg Roach return $tree->id(); 1006a7a24840SGreg Roach }, $trees); 1007a7a24840SGreg Roach 1008a7a24840SGreg Roach $query->whereIn($tree_id_field, $tree_ids); 1009a7a24840SGreg Roach } 1010a7a24840SGreg Roach 1011d5ad3db0SGreg Roach /** 1012d5ad3db0SGreg Roach * Find the media object that uses a particular media file. 1013d5ad3db0SGreg Roach * 1014d5ad3db0SGreg Roach * @param string $file 1015d5ad3db0SGreg Roach * 1016d5ad3db0SGreg Roach * @return Media[] 1017d5ad3db0SGreg Roach */ 1018d5ad3db0SGreg Roach public function findMediaObjectsForMediaFile(string $file): array 1019d5ad3db0SGreg Roach { 1020d5ad3db0SGreg Roach return DB::table('media') 1021d5ad3db0SGreg Roach ->join('media_file', static function (JoinClause $join): void { 1022d5ad3db0SGreg Roach $join 1023d5ad3db0SGreg Roach ->on('media_file.m_file', '=', 'media.m_file') 1024d5ad3db0SGreg Roach ->on('media_file.m_id', '=', 'media.m_id'); 1025d5ad3db0SGreg Roach }) 1026d5ad3db0SGreg Roach ->join('gedcom_setting', 'media.m_file', '=', 'gedcom_setting.gedcom_id') 1027d5ad3db0SGreg Roach ->where(new Expression('setting_value || multimedia_file_refn'), '=', $file) 1028d5ad3db0SGreg Roach ->select(['media.*']) 1029d5ad3db0SGreg Roach ->distinct() 1030d5ad3db0SGreg Roach ->get() 1031d5ad3db0SGreg Roach ->map($this->mediaRowMapper()) 1032d5ad3db0SGreg Roach ->all(); 1033d5ad3db0SGreg Roach } 1034d5ad3db0SGreg Roach 1035a7a24840SGreg Roach /** 1036a7a24840SGreg Roach * A closure to filter records by privacy-filtered GEDCOM data. 1037a7a24840SGreg Roach * 1038a7a24840SGreg Roach * @param array $search_terms 1039a7a24840SGreg Roach * 1040a7a24840SGreg Roach * @return Closure 1041a7a24840SGreg Roach */ 1042a7a24840SGreg Roach private function rawGedcomFilter(array $search_terms): Closure 1043a7a24840SGreg Roach { 10446c2179e2SGreg Roach return static function (GedcomRecord $record) use ($search_terms): bool { 1045a7a24840SGreg Roach // Ignore non-genealogy fields 1046faa5e163SGreg Roach $gedcom = preg_replace('/\n\d (?:_UID|_WT_USER) .*/', '', $record->gedcom()); 1047a7a24840SGreg Roach 1048a7a24840SGreg Roach // Ignore matches in links 1049a7a24840SGreg Roach $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom); 1050a7a24840SGreg Roach 1051a7a24840SGreg Roach // Re-apply the filtering 1052a7a24840SGreg Roach foreach ($search_terms as $search_term) { 1053a7a24840SGreg Roach if (mb_stripos($gedcom, $search_term) === false) { 1054a7a24840SGreg Roach return false; 1055a7a24840SGreg Roach } 1056a7a24840SGreg Roach } 1057a7a24840SGreg Roach 1058a7a24840SGreg Roach return true; 1059a7a24840SGreg Roach }; 1060a7a24840SGreg Roach } 106152a8ef61SGreg Roach 106252a8ef61SGreg Roach /** 106352a8ef61SGreg Roach * Searching for short or common text can give more results than the system can process. 106452a8ef61SGreg Roach * 106552a8ef61SGreg Roach * @param int $limit 106652a8ef61SGreg Roach * 106752a8ef61SGreg Roach * @return Closure 106852a8ef61SGreg Roach */ 106952a8ef61SGreg Roach private function rowLimiter(int $limit = 1000): Closure 107052a8ef61SGreg Roach { 10716c2179e2SGreg Roach return static function () use ($limit): void { 107252a8ef61SGreg Roach static $n = 0; 107352a8ef61SGreg Roach 107452a8ef61SGreg Roach if (++$n > $limit) { 107552a8ef61SGreg Roach $message = I18N::translate('The search returned too many results.'); 107652a8ef61SGreg Roach 1077d501c45dSGreg Roach throw new HttpServiceUnavailableException($message); 107852a8ef61SGreg Roach } 107952a8ef61SGreg Roach }; 108052a8ef61SGreg Roach } 1081d5ad3db0SGreg Roach 1082d5ad3db0SGreg Roach /** 1083d5ad3db0SGreg Roach * Convert a row from any tree in the families table into a family object. 1084d5ad3db0SGreg Roach * 1085d5ad3db0SGreg Roach * @return Closure 1086d5ad3db0SGreg Roach */ 1087d5ad3db0SGreg Roach private function familyRowMapper(): Closure 1088d5ad3db0SGreg Roach { 1089d5ad3db0SGreg Roach return function (stdClass $row): Family { 1090d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->f_file); 1091d5ad3db0SGreg Roach 10923959eeb6SGreg Roach return Family::rowMapper($tree)($row); 1093d5ad3db0SGreg Roach }; 1094d5ad3db0SGreg Roach } 1095d5ad3db0SGreg Roach 1096d5ad3db0SGreg Roach /** 1097d5ad3db0SGreg Roach * Convert a row from any tree in the individuals table into an individual object. 1098d5ad3db0SGreg Roach * 1099d5ad3db0SGreg Roach * @return Closure 1100d5ad3db0SGreg Roach */ 1101d5ad3db0SGreg Roach private function individualRowMapper(): Closure 1102d5ad3db0SGreg Roach { 1103d5ad3db0SGreg Roach return function (stdClass $row): Individual { 1104d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->i_file); 1105d5ad3db0SGreg Roach 11063959eeb6SGreg Roach return Individual::rowMapper($tree)($row); 1107d5ad3db0SGreg Roach }; 1108d5ad3db0SGreg Roach } 1109d5ad3db0SGreg Roach 1110d5ad3db0SGreg Roach /** 1111d5ad3db0SGreg Roach * Convert a row from any tree in the media table into an media object. 1112d5ad3db0SGreg Roach * 1113d5ad3db0SGreg Roach * @return Closure 1114d5ad3db0SGreg Roach */ 1115d5ad3db0SGreg Roach private function mediaRowMapper(): Closure 1116d5ad3db0SGreg Roach { 1117d5ad3db0SGreg Roach return function (stdClass $row): Media { 1118d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->m_file); 1119d5ad3db0SGreg Roach 11203959eeb6SGreg Roach return Media::rowMapper($tree)($row); 1121d5ad3db0SGreg Roach }; 1122d5ad3db0SGreg Roach } 1123d5ad3db0SGreg Roach 1124d5ad3db0SGreg Roach /** 1125d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a note object. 1126d5ad3db0SGreg Roach * 1127d5ad3db0SGreg Roach * @return Closure 1128d5ad3db0SGreg Roach */ 1129d5ad3db0SGreg Roach private function noteRowMapper(): Closure 1130d5ad3db0SGreg Roach { 1131d5ad3db0SGreg Roach return function (stdClass $row): Note { 1132d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1133d5ad3db0SGreg Roach 11343959eeb6SGreg Roach return Note::rowMapper($tree)($row); 1135d5ad3db0SGreg Roach }; 1136d5ad3db0SGreg Roach } 1137d5ad3db0SGreg Roach 1138d5ad3db0SGreg Roach /** 1139d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a repository object. 1140d5ad3db0SGreg Roach * 1141d5ad3db0SGreg Roach * @return Closure 1142d5ad3db0SGreg Roach */ 1143d5ad3db0SGreg Roach private function repositoryRowMapper(): Closure 1144d5ad3db0SGreg Roach { 1145d5ad3db0SGreg Roach return function (stdClass $row): Repository { 1146d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1147d5ad3db0SGreg Roach 11483959eeb6SGreg Roach return Repository::rowMapper($tree)($row); 1149d5ad3db0SGreg Roach }; 1150d5ad3db0SGreg Roach } 1151d5ad3db0SGreg Roach 1152d5ad3db0SGreg Roach /** 1153d5ad3db0SGreg Roach * Convert a row from any tree in the sources table into a source object. 1154d5ad3db0SGreg Roach * 1155d5ad3db0SGreg Roach * @return Closure 1156d5ad3db0SGreg Roach */ 1157d5ad3db0SGreg Roach private function sourceRowMapper(): Closure 1158d5ad3db0SGreg Roach { 1159d5ad3db0SGreg Roach return function (stdClass $row): Source { 1160d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->s_file); 1161d5ad3db0SGreg Roach 11623959eeb6SGreg Roach return Source::rowMapper($tree)($row); 1163d5ad3db0SGreg Roach }; 1164d5ad3db0SGreg Roach } 1165d5ad3db0SGreg Roach 1166d5ad3db0SGreg Roach /** 1167d5ad3db0SGreg Roach * Convert a row from any tree in the other table into a submitter object. 1168d5ad3db0SGreg Roach * 1169d5ad3db0SGreg Roach * @return Closure 1170d5ad3db0SGreg Roach */ 1171d5ad3db0SGreg Roach private function submitterRowMapper(): Closure 1172d5ad3db0SGreg Roach { 11733959eeb6SGreg Roach return function (stdClass $row): Submitter { 1174d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $row->o_file); 1175d5ad3db0SGreg Roach 11763959eeb6SGreg Roach return Submitter::rowMapper($tree)($row); 1177d5ad3db0SGreg Roach }; 1178d5ad3db0SGreg Roach } 117932cd2800SGreg Roach} 1180