18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg 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/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 2216a40a66SGreg Roachuse Aura\Router\Route; 2316a40a66SGreg Roachuse Aura\Router\RouterContainer; 246ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 25659aa375SGreg Roachuse Fisharebest\Webtrees\Auth; 26c5a1ffe6SGreg Roachuse Fisharebest\Webtrees\Family; 27a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 28a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 29b1b85189SGreg Roachuse Fisharebest\Webtrees\Html; 3081b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 320e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note; 3581b729d3SGreg Roachuse Fisharebest\Webtrees\Registry; 360e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository; 373df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 380e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source; 39c5a1ffe6SGreg Roachuse Fisharebest\Webtrees\Submitter; 400e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 41*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 42fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 43a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 44886b77daSGreg Roachuse Illuminate\Support\Collection; 456ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 466ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 4716a40a66SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 48d519210eSGreg Roach 4916a40a66SGreg Roachuse function app; 5016a40a66SGreg Roachuse function assert; 5116a40a66SGreg Roachuse function date; 523df1e584SGreg Roachuse function redirect; 5316a40a66SGreg Roachuse function response; 5416a40a66SGreg Roachuse function route; 553df1e584SGreg Roachuse function view; 563df1e584SGreg Roach 578c2e8227SGreg Roach/** 588c2e8227SGreg Roach * Class SiteMapModule 598c2e8227SGreg Roach */ 6016a40a66SGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface, RequestHandlerInterface 61c1010edaSGreg Roach{ 6249a243cbSGreg Roach use ModuleConfigTrait; 6349a243cbSGreg Roach 6416d6367aSGreg Roach private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits. 650daf451eSGreg Roach private const CACHE_LIFE = 209600; // Two weeks 668c2e8227SGreg Roach 67c5a1ffe6SGreg Roach private const PRIORITY = [ 68c5a1ffe6SGreg Roach Family::RECORD_TYPE => 0.7, 69c5a1ffe6SGreg Roach Individual::RECORD_TYPE => 0.9, 70c5a1ffe6SGreg Roach Media::RECORD_TYPE => 0.5, 71c5a1ffe6SGreg Roach Note::RECORD_TYPE => 0.3, 72c5a1ffe6SGreg Roach Repository::RECORD_TYPE => 0.5, 73c5a1ffe6SGreg Roach Source::RECORD_TYPE => 0.5, 74c5a1ffe6SGreg Roach Submitter::RECORD_TYPE => 0.3, 75c5a1ffe6SGreg Roach ]; 76c5a1ffe6SGreg Roach 7743f2f523SGreg Roach private TreeService $tree_service; 783df1e584SGreg Roach 793df1e584SGreg Roach /** 803df1e584SGreg Roach * TreesMenuModule constructor. 813df1e584SGreg Roach * 823df1e584SGreg Roach * @param TreeService $tree_service 833df1e584SGreg Roach */ 843df1e584SGreg Roach public function __construct(TreeService $tree_service) 853df1e584SGreg Roach { 863df1e584SGreg Roach $this->tree_service = $tree_service; 873df1e584SGreg Roach } 883df1e584SGreg Roach 89a5f7ed67SGreg Roach /** 9016a40a66SGreg Roach * Initialization. 9116a40a66SGreg Roach * 9216a40a66SGreg Roach * @return void 9316a40a66SGreg Roach */ 9416a40a66SGreg Roach public function boot(): void 9516a40a66SGreg Roach { 9616a40a66SGreg Roach $router_container = app(RouterContainer::class); 9716a40a66SGreg Roach assert($router_container instanceof RouterContainer); 9816a40a66SGreg Roach 9916a40a66SGreg Roach $router_container->getMap() 10014aabe72SGreg Roach ->get('sitemap-style', '/sitemap.xsl', $this); 10114aabe72SGreg Roach 10214aabe72SGreg Roach $router_container->getMap() 10316a40a66SGreg Roach ->get('sitemap-index', '/sitemap.xml', $this); 10416a40a66SGreg Roach 10516a40a66SGreg Roach $router_container->getMap() 106c5a1ffe6SGreg Roach ->get('sitemap-file', '/sitemap-{tree}-{type}-{page}.xml', $this); 10716a40a66SGreg Roach } 10816a40a66SGreg Roach 10916a40a66SGreg Roach /** 110a5f7ed67SGreg Roach * A sentence describing what this module does. 111a5f7ed67SGreg Roach * 112a5f7ed67SGreg Roach * @return string 113a5f7ed67SGreg Roach */ 11449a243cbSGreg Roach public function description(): string 115c1010edaSGreg Roach { 116bbb76c12SGreg Roach /* I18N: Description of the “Sitemaps” module */ 117bbb76c12SGreg Roach return I18N::translate('Generate sitemap files for search engines.'); 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 12076692c8bSGreg Roach /** 121abafa13cSGreg Roach * Should this module be enabled when it is first installed? 122abafa13cSGreg Roach * 123abafa13cSGreg Roach * @return bool 124abafa13cSGreg Roach */ 125abafa13cSGreg Roach public function isEnabledByDefault(): bool 126abafa13cSGreg Roach { 127abafa13cSGreg Roach return false; 128abafa13cSGreg Roach } 129abafa13cSGreg Roach 130abafa13cSGreg Roach /** 13157ab2231SGreg Roach * @param ServerRequestInterface $request 13257ab2231SGreg Roach * 1336ccdf4f0SGreg Roach * @return ResponseInterface 1348c2e8227SGreg Roach */ 135a09ea7ccSGreg Roach public function getAdminAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface 136c1010edaSGreg Roach { 137a5f7ed67SGreg Roach $this->layout = 'layouts/administration'; 138a5f7ed67SGreg Roach 13916a40a66SGreg Roach $sitemap_url = route('sitemap-index'); 140a5f7ed67SGreg Roach 141ce42304aSGreg Roach // This list comes from https://en.wikipedia.org/wiki/Sitemaps 142a5f7ed67SGreg Roach $submit_urls = [ 143a5f7ed67SGreg Roach 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 144a5f7ed67SGreg Roach 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 145a5f7ed67SGreg Roach ]; 146a5f7ed67SGreg Roach 147291c1b19SGreg Roach return $this->viewResponse('modules/sitemap/config', [ 1483df1e584SGreg Roach 'all_trees' => $this->tree_service->all(), 149a5f7ed67SGreg Roach 'sitemap_url' => $sitemap_url, 150a5f7ed67SGreg Roach 'submit_urls' => $submit_urls, 15149a243cbSGreg Roach 'title' => $this->title(), 152a5f7ed67SGreg Roach ]); 1538c2e8227SGreg Roach } 1548c2e8227SGreg Roach 1558c2e8227SGreg Roach /** 1566ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 157a5f7ed67SGreg Roach * 1586ccdf4f0SGreg Roach * @return string 1598c2e8227SGreg Roach */ 1606ccdf4f0SGreg Roach public function title(): string 1616ccdf4f0SGreg Roach { 162ad3143ccSGreg Roach /* I18N: Name of a module - see https://en.wikipedia.org/wiki/Sitemaps */ 1636ccdf4f0SGreg Roach return I18N::translate('Sitemaps'); 1646ccdf4f0SGreg Roach } 1656ccdf4f0SGreg Roach 1666ccdf4f0SGreg Roach /** 1676ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1686ccdf4f0SGreg Roach * 1696ccdf4f0SGreg Roach * @return ResponseInterface 1706ccdf4f0SGreg Roach */ 1716ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 172c1010edaSGreg Roach { 173b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 174b6b9dcc9SGreg Roach 1753df1e584SGreg Roach foreach ($this->tree_service->all() as $tree) { 176b6b9dcc9SGreg Roach $include_in_sitemap = (bool) ($params['sitemap' . $tree->id()] ?? false); 177a5f7ed67SGreg Roach $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 1788c2e8227SGreg Roach } 179a5f7ed67SGreg Roach 18049a243cbSGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 181a5f7ed67SGreg Roach 1826ccdf4f0SGreg Roach return redirect($this->getConfigLink()); 1838c2e8227SGreg Roach } 1848c2e8227SGreg Roach 1858c2e8227SGreg Roach /** 18657ab2231SGreg Roach * @param ServerRequestInterface $request 18757ab2231SGreg Roach * 1886ccdf4f0SGreg Roach * @return ResponseInterface 1898c2e8227SGreg Roach */ 19016a40a66SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 191c1010edaSGreg Roach { 192*b55cbc6bSGreg Roach $route = Validator::attributes($request)->route(); 193a5f7ed67SGreg Roach 19414aabe72SGreg Roach if ($route->name === 'sitemap-style') { 19514aabe72SGreg Roach $content = view('modules/sitemap/sitemap-xsl'); 19614aabe72SGreg Roach 19714aabe72SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 19814aabe72SGreg Roach 'Content-Type' => 'application/xml', 19914aabe72SGreg Roach ]); 20014aabe72SGreg Roach } 20114aabe72SGreg Roach 20216a40a66SGreg Roach if ($route->name === 'sitemap-index') { 20316a40a66SGreg Roach return $this->siteMapIndex($request); 20416a40a66SGreg Roach } 20516a40a66SGreg Roach 20616a40a66SGreg Roach return $this->siteMapFile($request); 20716a40a66SGreg Roach } 20816a40a66SGreg Roach 20916a40a66SGreg Roach /** 21016a40a66SGreg Roach * @param ServerRequestInterface $request 21116a40a66SGreg Roach * 21216a40a66SGreg Roach * @return ResponseInterface 21316a40a66SGreg Roach */ 214a09ea7ccSGreg Roach private function siteMapIndex(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface 21516a40a66SGreg Roach { 2166b9cb339SGreg Roach $content = Registry::cache()->file()->remember('sitemap.xml', function (): string { 217659aa375SGreg Roach // Which trees have sitemaps enabled? 218659aa375SGreg Roach $tree_ids = $this->tree_service->all()->filter(static function (Tree $tree): bool { 219659aa375SGreg Roach return $tree->getPreference('include_in_sitemap') === '1'; 220659aa375SGreg Roach })->map(static function (Tree $tree): int { 221659aa375SGreg Roach return $tree->id(); 222659aa375SGreg Roach }); 223659aa375SGreg Roach 224c5a1ffe6SGreg Roach $count_families = DB::table('families') 225c5a1ffe6SGreg Roach ->join('gedcom', 'f_file', '=', 'gedcom_id') 226c5a1ffe6SGreg Roach ->whereIn('gedcom_id', $tree_ids) 227c5a1ffe6SGreg Roach ->groupBy(['gedcom_id']) 228c5a1ffe6SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 229c5a1ffe6SGreg Roach ->pluck('total', 'gedcom_name'); 230c5a1ffe6SGreg Roach 231fa17fb66SGreg Roach $count_individuals = DB::table('individuals') 23216a40a66SGreg Roach ->join('gedcom', 'i_file', '=', 'gedcom_id') 23316a40a66SGreg Roach ->whereIn('gedcom_id', $tree_ids) 23416a40a66SGreg Roach ->groupBy(['gedcom_id']) 23516a40a66SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 23616a40a66SGreg Roach ->pluck('total', 'gedcom_name'); 237a5f7ed67SGreg Roach 238fa17fb66SGreg Roach $count_media = DB::table('media') 23916a40a66SGreg Roach ->join('gedcom', 'm_file', '=', 'gedcom_id') 24016a40a66SGreg Roach ->whereIn('gedcom_id', $tree_ids) 24116a40a66SGreg Roach ->groupBy(['gedcom_id']) 24216a40a66SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 24316a40a66SGreg Roach ->pluck('total', 'gedcom_name'); 244a5f7ed67SGreg Roach 245fa17fb66SGreg Roach $count_notes = DB::table('other') 24616a40a66SGreg Roach ->join('gedcom', 'o_file', '=', 'gedcom_id') 24716a40a66SGreg Roach ->whereIn('gedcom_id', $tree_ids) 248c5a1ffe6SGreg Roach ->where('o_type', '=', Note::RECORD_TYPE) 24916a40a66SGreg Roach ->groupBy(['gedcom_id']) 25016a40a66SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 25116a40a66SGreg Roach ->pluck('total', 'gedcom_name'); 252a5f7ed67SGreg Roach 253fa17fb66SGreg Roach $count_repositories = DB::table('other') 25416a40a66SGreg Roach ->join('gedcom', 'o_file', '=', 'gedcom_id') 25516a40a66SGreg Roach ->whereIn('gedcom_id', $tree_ids) 256c5a1ffe6SGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE) 25716a40a66SGreg Roach ->groupBy(['gedcom_id']) 25816a40a66SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 25916a40a66SGreg Roach ->pluck('total', 'gedcom_name'); 260a5f7ed67SGreg Roach 261fa17fb66SGreg Roach $count_sources = DB::table('sources') 26216a40a66SGreg Roach ->join('gedcom', 's_file', '=', 'gedcom_id') 26316a40a66SGreg Roach ->whereIn('gedcom_id', $tree_ids) 26416a40a66SGreg Roach ->groupBy(['gedcom_id']) 26516a40a66SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 26616a40a66SGreg Roach ->pluck('total', 'gedcom_name'); 267a5f7ed67SGreg Roach 268c5a1ffe6SGreg Roach $count_submitters = DB::table('other') 269c5a1ffe6SGreg Roach ->join('gedcom', 'o_file', '=', 'gedcom_id') 270c5a1ffe6SGreg Roach ->whereIn('gedcom_id', $tree_ids) 271c5a1ffe6SGreg Roach ->where('o_type', '=', Submitter::RECORD_TYPE) 272c5a1ffe6SGreg Roach ->groupBy(['gedcom_id']) 273c5a1ffe6SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'gedcom_name']) 274c5a1ffe6SGreg Roach ->pluck('total', 'gedcom_name'); 275c5a1ffe6SGreg Roach 27616a40a66SGreg Roach // Versions 2.0.1 and earlier of this module stored large amounts of data in the settings. 27716a40a66SGreg Roach DB::table('module_setting') 27816a40a66SGreg Roach ->where('module_name', '=', $this->name()) 27916a40a66SGreg Roach ->delete(); 28016a40a66SGreg Roach 28114aabe72SGreg Roach return view('modules/sitemap/sitemap-index-xml', [ 2823df1e584SGreg Roach 'all_trees' => $this->tree_service->all(), 283c5a1ffe6SGreg Roach 'count_families' => $count_families, 284a5f7ed67SGreg Roach 'count_individuals' => $count_individuals, 285a5f7ed67SGreg Roach 'count_media' => $count_media, 286a5f7ed67SGreg Roach 'count_notes' => $count_notes, 287a5f7ed67SGreg Roach 'count_repositories' => $count_repositories, 288a5f7ed67SGreg Roach 'count_sources' => $count_sources, 289c5a1ffe6SGreg Roach 'count_submitters' => $count_submitters, 290a5f7ed67SGreg Roach 'last_mod' => date('Y-m-d'), 291a5f7ed67SGreg Roach 'records_per_volume' => self::RECORDS_PER_VOLUME, 29214aabe72SGreg Roach 'sitemap_xsl' => route('sitemap-style'), 293a5f7ed67SGreg Roach ]); 29416a40a66SGreg Roach }, self::CACHE_LIFE); 295a5f7ed67SGreg Roach 2966ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 297a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 298a5f7ed67SGreg Roach ]); 299a5f7ed67SGreg Roach } 300a5f7ed67SGreg Roach 301a5f7ed67SGreg Roach /** 3026ccdf4f0SGreg Roach * @param ServerRequestInterface $request 303a5f7ed67SGreg Roach * 3046ccdf4f0SGreg Roach * @return ResponseInterface 305a5f7ed67SGreg Roach */ 30616a40a66SGreg Roach private function siteMapFile(ServerRequestInterface $request): ResponseInterface 307c1010edaSGreg Roach { 308*b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree('tree'); 309*b55cbc6bSGreg Roach $type = Validator::attributes($request)->string('type'); 310*b55cbc6bSGreg Roach $page = Validator::attributes($request)->integer('page'); 311a5f7ed67SGreg Roach 312659aa375SGreg Roach if ($tree->getPreference('include_in_sitemap') !== '1') { 313659aa375SGreg Roach throw new HttpNotFoundException(); 314659aa375SGreg Roach } 315659aa375SGreg Roach 316c5a1ffe6SGreg Roach $cache_key = 'sitemap/' . $tree->id() . '/' . $type . '/' . $page . '.xml'; 31716a40a66SGreg Roach 3186b9cb339SGreg Roach $content = Registry::cache()->file()->remember($cache_key, function () use ($tree, $type, $page): string { 319c5a1ffe6SGreg Roach $records = $this->sitemapRecords($tree, $type, self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $page); 32016a40a66SGreg Roach 32114aabe72SGreg Roach return view('modules/sitemap/sitemap-file-xml', [ 322c5a1ffe6SGreg Roach 'priority' => self::PRIORITY[$type], 3239e3c7a99SGreg Roach 'records' => $records, 32414aabe72SGreg Roach 'sitemap_xsl' => route('sitemap-style'), 3259e3c7a99SGreg Roach 'tree' => $tree, 3269e3c7a99SGreg Roach ]); 32716a40a66SGreg Roach }, self::CACHE_LIFE); 328a5f7ed67SGreg Roach 3296ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 330a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 331a5f7ed67SGreg Roach ]); 332a5f7ed67SGreg Roach } 333a5f7ed67SGreg Roach 334a5f7ed67SGreg Roach /** 335a5f7ed67SGreg Roach * @param Tree $tree 336a5f7ed67SGreg Roach * @param string $type 337a5f7ed67SGreg Roach * @param int $limit 338a5f7ed67SGreg Roach * @param int $offset 339a5f7ed67SGreg Roach * 34036779af1SGreg Roach * @return Collection<int,GedcomRecord> 341a5f7ed67SGreg Roach */ 342886b77daSGreg Roach private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection 343c1010edaSGreg Roach { 344a5f7ed67SGreg Roach switch ($type) { 345c5a1ffe6SGreg Roach case Family::RECORD_TYPE: 346c5a1ffe6SGreg Roach $records = $this->sitemapFamilies($tree, $limit, $offset); 347c5a1ffe6SGreg Roach break; 348c5a1ffe6SGreg Roach 34916a40a66SGreg Roach case Individual::RECORD_TYPE: 350a5f7ed67SGreg Roach $records = $this->sitemapIndividuals($tree, $limit, $offset); 351a5f7ed67SGreg Roach break; 352a5f7ed67SGreg Roach 35316a40a66SGreg Roach case Media::RECORD_TYPE: 354a5f7ed67SGreg Roach $records = $this->sitemapMedia($tree, $limit, $offset); 355a5f7ed67SGreg Roach break; 356a5f7ed67SGreg Roach 35716a40a66SGreg Roach case Note::RECORD_TYPE: 358a5f7ed67SGreg Roach $records = $this->sitemapNotes($tree, $limit, $offset); 359a5f7ed67SGreg Roach break; 360a5f7ed67SGreg Roach 36116a40a66SGreg Roach case Repository::RECORD_TYPE: 362a5f7ed67SGreg Roach $records = $this->sitemapRepositories($tree, $limit, $offset); 363a5f7ed67SGreg Roach break; 364a5f7ed67SGreg Roach 36516a40a66SGreg Roach case Source::RECORD_TYPE: 366a5f7ed67SGreg Roach $records = $this->sitemapSources($tree, $limit, $offset); 367a5f7ed67SGreg Roach break; 368a5f7ed67SGreg Roach 369c5a1ffe6SGreg Roach case Submitter::RECORD_TYPE: 370c5a1ffe6SGreg Roach $records = $this->sitemapSubmitters($tree, $limit, $offset); 371c5a1ffe6SGreg Roach break; 372c5a1ffe6SGreg Roach 373a5f7ed67SGreg Roach default: 374d501c45dSGreg Roach throw new HttpNotFoundException('Invalid record type: ' . $type); 375a5f7ed67SGreg Roach } 376a5f7ed67SGreg Roach 377a5f7ed67SGreg Roach // Skip private records. 378659aa375SGreg Roach $records = $records->filter(static function (GedcomRecord $record): bool { 379659aa375SGreg Roach return $record->canShow(Auth::PRIV_PRIVATE); 380659aa375SGreg Roach }); 381a5f7ed67SGreg Roach 382a5f7ed67SGreg Roach return $records; 383a5f7ed67SGreg Roach } 384a5f7ed67SGreg Roach 385a5f7ed67SGreg Roach /** 386a5f7ed67SGreg Roach * @param Tree $tree 387a5f7ed67SGreg Roach * @param int $limit 388a5f7ed67SGreg Roach * @param int $offset 389a5f7ed67SGreg Roach * 39036779af1SGreg Roach * @return Collection<int,Family> 391c5a1ffe6SGreg Roach */ 392c5a1ffe6SGreg Roach private function sitemapFamilies(Tree $tree, int $limit, int $offset): Collection 393c5a1ffe6SGreg Roach { 394c5a1ffe6SGreg Roach return DB::table('families') 395c5a1ffe6SGreg Roach ->where('f_file', '=', $tree->id()) 396c5a1ffe6SGreg Roach ->orderBy('f_id') 397c5a1ffe6SGreg Roach ->skip($offset) 398c5a1ffe6SGreg Roach ->take($limit) 399c5a1ffe6SGreg Roach ->get() 4006b9cb339SGreg Roach ->map(Registry::familyFactory()->mapper($tree)); 401c5a1ffe6SGreg Roach } 402c5a1ffe6SGreg Roach 403c5a1ffe6SGreg Roach /** 404c5a1ffe6SGreg Roach * @param Tree $tree 405c5a1ffe6SGreg Roach * @param int $limit 406c5a1ffe6SGreg Roach * @param int $offset 407c5a1ffe6SGreg Roach * 40836779af1SGreg Roach * @return Collection<int,Individual> 409a5f7ed67SGreg Roach */ 410886b77daSGreg Roach private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection 411c1010edaSGreg Roach { 412886b77daSGreg Roach return DB::table('individuals') 413fa17fb66SGreg Roach ->where('i_file', '=', $tree->id()) 414fa17fb66SGreg Roach ->orderBy('i_id') 415fa17fb66SGreg Roach ->skip($offset) 416fa17fb66SGreg Roach ->take($limit) 417886b77daSGreg Roach ->get() 4186b9cb339SGreg Roach ->map(Registry::individualFactory()->mapper($tree)); 4198c2e8227SGreg Roach } 420a5f7ed67SGreg Roach 421a5f7ed67SGreg Roach /** 422a5f7ed67SGreg Roach * @param Tree $tree 423a5f7ed67SGreg Roach * @param int $limit 424a5f7ed67SGreg Roach * @param int $offset 425a5f7ed67SGreg Roach * 42636779af1SGreg Roach * @return Collection<int,Media> 427a5f7ed67SGreg Roach */ 428886b77daSGreg Roach private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection 429c1010edaSGreg Roach { 430886b77daSGreg Roach return DB::table('media') 431fa17fb66SGreg Roach ->where('m_file', '=', $tree->id()) 432fa17fb66SGreg Roach ->orderBy('m_id') 433fa17fb66SGreg Roach ->skip($offset) 434fa17fb66SGreg Roach ->take($limit) 435886b77daSGreg Roach ->get() 4366b9cb339SGreg Roach ->map(Registry::mediaFactory()->mapper($tree)); 4378c2e8227SGreg Roach } 4388c2e8227SGreg Roach 4398c2e8227SGreg Roach /** 440a5f7ed67SGreg Roach * @param Tree $tree 441a5f7ed67SGreg Roach * @param int $limit 442a5f7ed67SGreg Roach * @param int $offset 443a5f7ed67SGreg Roach * 44436779af1SGreg Roach * @return Collection<int,Note> 4458c2e8227SGreg Roach */ 446886b77daSGreg Roach private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection 447c1010edaSGreg Roach { 448886b77daSGreg Roach return DB::table('other') 449fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 450c5a1ffe6SGreg Roach ->where('o_type', '=', Note::RECORD_TYPE) 451fa17fb66SGreg Roach ->orderBy('o_id') 452fa17fb66SGreg Roach ->skip($offset) 453fa17fb66SGreg Roach ->take($limit) 454886b77daSGreg Roach ->get() 4556b9cb339SGreg Roach ->map(Registry::noteFactory()->mapper($tree)); 4568c2e8227SGreg Roach } 4578c2e8227SGreg Roach 458a5f7ed67SGreg Roach /** 459a5f7ed67SGreg Roach * @param Tree $tree 460a5f7ed67SGreg Roach * @param int $limit 461a5f7ed67SGreg Roach * @param int $offset 462a5f7ed67SGreg Roach * 46336779af1SGreg Roach * @return Collection<int,Repository> 464a5f7ed67SGreg Roach */ 465886b77daSGreg Roach private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection 466c1010edaSGreg Roach { 467886b77daSGreg Roach return DB::table('other') 468fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 469c5a1ffe6SGreg Roach ->where('o_type', '=', Repository::RECORD_TYPE) 470fa17fb66SGreg Roach ->orderBy('o_id') 471fa17fb66SGreg Roach ->skip($offset) 472fa17fb66SGreg Roach ->take($limit) 473886b77daSGreg Roach ->get() 4746b9cb339SGreg Roach ->map(Registry::repositoryFactory()->mapper($tree)); 475a5f7ed67SGreg Roach } 476a5f7ed67SGreg Roach 477a5f7ed67SGreg Roach /** 478a5f7ed67SGreg Roach * @param Tree $tree 479a5f7ed67SGreg Roach * @param int $limit 480a5f7ed67SGreg Roach * @param int $offset 481a5f7ed67SGreg Roach * 48236779af1SGreg Roach * @return Collection<int,Source> 483a5f7ed67SGreg Roach */ 484886b77daSGreg Roach private function sitemapSources(Tree $tree, int $limit, int $offset): Collection 485c1010edaSGreg Roach { 486886b77daSGreg Roach return DB::table('sources') 487fa17fb66SGreg Roach ->where('s_file', '=', $tree->id()) 488fa17fb66SGreg Roach ->orderBy('s_id') 489fa17fb66SGreg Roach ->skip($offset) 490fa17fb66SGreg Roach ->take($limit) 491886b77daSGreg Roach ->get() 4926b9cb339SGreg Roach ->map(Registry::sourceFactory()->mapper($tree)); 4938c2e8227SGreg Roach } 494c5a1ffe6SGreg Roach 495c5a1ffe6SGreg Roach /** 496c5a1ffe6SGreg Roach * @param Tree $tree 497c5a1ffe6SGreg Roach * @param int $limit 498c5a1ffe6SGreg Roach * @param int $offset 499c5a1ffe6SGreg Roach * 50036779af1SGreg Roach * @return Collection<int,Submitter> 501c5a1ffe6SGreg Roach */ 502c5a1ffe6SGreg Roach private function sitemapSubmitters(Tree $tree, int $limit, int $offset): Collection 503c5a1ffe6SGreg Roach { 504c5a1ffe6SGreg Roach return DB::table('other') 505c5a1ffe6SGreg Roach ->where('o_file', '=', $tree->id()) 506c5a1ffe6SGreg Roach ->where('o_type', '=', Submitter::RECORD_TYPE) 507c5a1ffe6SGreg Roach ->orderBy('o_id') 508c5a1ffe6SGreg Roach ->skip($offset) 509c5a1ffe6SGreg Roach ->take($limit) 510c5a1ffe6SGreg Roach ->get() 5116b9cb339SGreg Roach ->map(Registry::submitterFactory()->mapper($tree)); 512c5a1ffe6SGreg Roach } 5138c2e8227SGreg Roach} 514