18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 226ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 24d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 25a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 26a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 27b1b85189SGreg Roachuse Fisharebest\Webtrees\Html; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note; 320e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository; 333df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source; 350e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 36fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 37a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 38886b77daSGreg Roachuse Illuminate\Support\Collection; 396ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 406ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 418c2e8227SGreg Roach 423df1e584SGreg Roachuse function redirect; 433df1e584SGreg Roachuse function view; 443df1e584SGreg Roach 458c2e8227SGreg Roach/** 468c2e8227SGreg Roach * Class SiteMapModule 478c2e8227SGreg Roach */ 4837eb8894SGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface 49c1010edaSGreg Roach{ 5049a243cbSGreg Roach use ModuleConfigTrait; 5149a243cbSGreg Roach 5216d6367aSGreg Roach private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits. 5316d6367aSGreg Roach private const CACHE_LIFE = 1209600; // Two weeks 548c2e8227SGreg Roach 553df1e584SGreg Roach /** @var TreeService */ 563df1e584SGreg Roach private $tree_service; 573df1e584SGreg Roach 583df1e584SGreg Roach /** 593df1e584SGreg Roach * TreesMenuModule constructor. 603df1e584SGreg Roach * 613df1e584SGreg Roach * @param TreeService $tree_service 623df1e584SGreg Roach */ 633df1e584SGreg Roach public function __construct(TreeService $tree_service) 643df1e584SGreg Roach { 653df1e584SGreg Roach $this->tree_service = $tree_service; 663df1e584SGreg Roach } 673df1e584SGreg Roach 68a5f7ed67SGreg Roach /** 69a5f7ed67SGreg Roach * A sentence describing what this module does. 70a5f7ed67SGreg Roach * 71a5f7ed67SGreg Roach * @return string 72a5f7ed67SGreg Roach */ 7349a243cbSGreg Roach public function description(): string 74c1010edaSGreg Roach { 75bbb76c12SGreg Roach /* I18N: Description of the “Sitemaps” module */ 76bbb76c12SGreg Roach return I18N::translate('Generate sitemap files for search engines.'); 778c2e8227SGreg Roach } 788c2e8227SGreg Roach 7976692c8bSGreg Roach /** 80abafa13cSGreg Roach * Should this module be enabled when it is first installed? 81abafa13cSGreg Roach * 82abafa13cSGreg Roach * @return bool 83abafa13cSGreg Roach */ 84abafa13cSGreg Roach public function isEnabledByDefault(): bool 85abafa13cSGreg Roach { 86abafa13cSGreg Roach return false; 87abafa13cSGreg Roach } 88abafa13cSGreg Roach 89abafa13cSGreg Roach /** 9057ab2231SGreg Roach * @param ServerRequestInterface $request 9157ab2231SGreg Roach * 926ccdf4f0SGreg Roach * @return ResponseInterface 938c2e8227SGreg Roach */ 9457ab2231SGreg Roach public function getAdminAction(ServerRequestInterface $request): ResponseInterface 95c1010edaSGreg Roach { 96a5f7ed67SGreg Roach $this->layout = 'layouts/administration'; 97a5f7ed67SGreg Roach 98c1010edaSGreg Roach $sitemap_url = route('module', [ 9926684e68SGreg Roach 'module' => $this->name(), 100c1010edaSGreg Roach 'action' => 'Index', 101c1010edaSGreg Roach ]); 102a5f7ed67SGreg Roach 103*ce42304aSGreg Roach // This list comes from https://en.wikipedia.org/wiki/Sitemaps 104a5f7ed67SGreg Roach $submit_urls = [ 105a5f7ed67SGreg Roach 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 106a5f7ed67SGreg Roach 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 107a5f7ed67SGreg Roach ]; 108a5f7ed67SGreg Roach 109291c1b19SGreg Roach return $this->viewResponse('modules/sitemap/config', [ 1103df1e584SGreg Roach 'all_trees' => $this->tree_service->all(), 111a5f7ed67SGreg Roach 'sitemap_url' => $sitemap_url, 112a5f7ed67SGreg Roach 'submit_urls' => $submit_urls, 11349a243cbSGreg Roach 'title' => $this->title(), 114a5f7ed67SGreg Roach ]); 1158c2e8227SGreg Roach } 1168c2e8227SGreg Roach 1178c2e8227SGreg Roach /** 1186ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 119a5f7ed67SGreg Roach * 1206ccdf4f0SGreg Roach * @return string 1218c2e8227SGreg Roach */ 1226ccdf4f0SGreg Roach public function title(): string 1236ccdf4f0SGreg Roach { 1246ccdf4f0SGreg Roach /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */ 1256ccdf4f0SGreg Roach return I18N::translate('Sitemaps'); 1266ccdf4f0SGreg Roach } 1276ccdf4f0SGreg Roach 1286ccdf4f0SGreg Roach /** 1296ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1306ccdf4f0SGreg Roach * 1316ccdf4f0SGreg Roach * @return ResponseInterface 1326ccdf4f0SGreg Roach */ 1336ccdf4f0SGreg Roach public function postAdminAction(ServerRequestInterface $request): ResponseInterface 134c1010edaSGreg Roach { 135b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 136b6b9dcc9SGreg Roach 1373df1e584SGreg Roach foreach ($this->tree_service->all() as $tree) { 138b6b9dcc9SGreg Roach $include_in_sitemap = (bool) ($params['sitemap' . $tree->id()] ?? false); 139a5f7ed67SGreg Roach $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 1408c2e8227SGreg Roach } 141a5f7ed67SGreg Roach 14249a243cbSGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 143a5f7ed67SGreg Roach 1446ccdf4f0SGreg Roach return redirect($this->getConfigLink()); 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 1478c2e8227SGreg Roach /** 14857ab2231SGreg Roach * @param ServerRequestInterface $request 14957ab2231SGreg Roach * 1506ccdf4f0SGreg Roach * @return ResponseInterface 1518c2e8227SGreg Roach */ 15257ab2231SGreg Roach public function getIndexAction(ServerRequestInterface $request): ResponseInterface 153c1010edaSGreg Roach { 154a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap.timestamp'); 155a5f7ed67SGreg Roach 1564459dc9aSGreg Roach if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) { 157a5f7ed67SGreg Roach $content = $this->getPreference('sitemap.xml'); 1588c2e8227SGreg Roach } else { 159fa17fb66SGreg Roach $count_individuals = DB::table('individuals') 1607f5c2944SGreg Roach ->groupBy(['i_file']) 161a69f5655SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'i_file']) 162fa17fb66SGreg Roach ->pluck('total', 'i_file'); 163a5f7ed67SGreg Roach 164fa17fb66SGreg Roach $count_media = DB::table('media') 1657f5c2944SGreg Roach ->groupBy(['m_file']) 166a69f5655SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'm_file']) 167fa17fb66SGreg Roach ->pluck('total', 'm_file'); 168a5f7ed67SGreg Roach 169fa17fb66SGreg Roach $count_notes = DB::table('other') 170fa17fb66SGreg Roach ->where('o_type', '=', 'NOTE') 1717f5c2944SGreg Roach ->groupBy(['o_file']) 172a69f5655SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'o_file']) 173fa17fb66SGreg Roach ->pluck('total', 'o_file'); 174a5f7ed67SGreg Roach 175fa17fb66SGreg Roach $count_repositories = DB::table('other') 176fa17fb66SGreg Roach ->where('o_type', '=', 'REPO') 1777f5c2944SGreg Roach ->groupBy(['o_file']) 178a69f5655SGreg Roach ->select([new Expression('COUNT(*) AS total'), 'o_file']) 179fa17fb66SGreg Roach ->pluck('total', 'o_file'); 180a5f7ed67SGreg Roach 181fa17fb66SGreg Roach $count_sources = DB::table('sources') 1827f5c2944SGreg Roach ->groupBy(['s_file']) 183a69f5655SGreg Roach ->select([new Expression('COUNT(*) AS total'), 's_file']) 184fa17fb66SGreg Roach ->pluck('total', 's_file'); 185a5f7ed67SGreg Roach 186a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-index.xml', [ 1873df1e584SGreg Roach 'all_trees' => $this->tree_service->all(), 188a5f7ed67SGreg Roach 'count_individuals' => $count_individuals, 189a5f7ed67SGreg Roach 'count_media' => $count_media, 190a5f7ed67SGreg Roach 'count_notes' => $count_notes, 191a5f7ed67SGreg Roach 'count_repositories' => $count_repositories, 192a5f7ed67SGreg Roach 'count_sources' => $count_sources, 193a5f7ed67SGreg Roach 'last_mod' => date('Y-m-d'), 194a5f7ed67SGreg Roach 'records_per_volume' => self::RECORDS_PER_VOLUME, 195a5f7ed67SGreg Roach ]); 196a5f7ed67SGreg Roach 197a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 198a5f7ed67SGreg Roach } 199a5f7ed67SGreg Roach 2006ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 201a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 202a5f7ed67SGreg Roach ]); 203a5f7ed67SGreg Roach } 204a5f7ed67SGreg Roach 205a5f7ed67SGreg Roach /** 2066ccdf4f0SGreg Roach * @param ServerRequestInterface $request 207a5f7ed67SGreg Roach * 2086ccdf4f0SGreg Roach * @return ResponseInterface 209a5f7ed67SGreg Roach */ 2106ccdf4f0SGreg Roach public function getFileAction(ServerRequestInterface $request): ResponseInterface 211c1010edaSGreg Roach { 212b6b9dcc9SGreg Roach $file = $request->getQueryParams()['file']; 213a5f7ed67SGreg Roach 214a5f7ed67SGreg Roach if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) { 215d501c45dSGreg Roach throw new HttpNotFoundException('Bad sitemap file'); 216a5f7ed67SGreg Roach } 217a5f7ed67SGreg Roach 218a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp'); 2194459dc9aSGreg Roach $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix(); 220a5f7ed67SGreg Roach 221ad98d39dSGreg Roach if ($timestamp > $expiry_time) { 222a5f7ed67SGreg Roach $content = $this->getPreference('sitemap-' . $file . '.xml'); 223a5f7ed67SGreg Roach } else { 224d5ad3db0SGreg Roach $tree = $this->tree_service->find((int) $match[1]); 225a5f7ed67SGreg Roach 226bdb3725aSGreg Roach $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]); 227a5f7ed67SGreg Roach 228a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]); 229a5f7ed67SGreg Roach 230a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 231a5f7ed67SGreg Roach } 232a5f7ed67SGreg Roach 2336ccdf4f0SGreg Roach return response($content, StatusCodeInterface::STATUS_OK, [ 234a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 235a5f7ed67SGreg Roach ]); 236a5f7ed67SGreg Roach } 237a5f7ed67SGreg Roach 238a5f7ed67SGreg Roach /** 239a5f7ed67SGreg Roach * @param Tree $tree 240a5f7ed67SGreg Roach * @param string $type 241a5f7ed67SGreg Roach * @param int $limit 242a5f7ed67SGreg Roach * @param int $offset 243a5f7ed67SGreg Roach * 244b5c8fd7eSGreg Roach * @return Collection<GedcomRecord> 245a5f7ed67SGreg Roach */ 246886b77daSGreg Roach private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection 247c1010edaSGreg Roach { 248a5f7ed67SGreg Roach switch ($type) { 2498c2e8227SGreg Roach case 'i': 250a5f7ed67SGreg Roach $records = $this->sitemapIndividuals($tree, $limit, $offset); 251a5f7ed67SGreg Roach break; 252a5f7ed67SGreg Roach 253a5f7ed67SGreg Roach case 'm': 254a5f7ed67SGreg Roach $records = $this->sitemapMedia($tree, $limit, $offset); 255a5f7ed67SGreg Roach break; 256a5f7ed67SGreg Roach 257a5f7ed67SGreg Roach case 'n': 258a5f7ed67SGreg Roach $records = $this->sitemapNotes($tree, $limit, $offset); 259a5f7ed67SGreg Roach break; 260a5f7ed67SGreg Roach 261a5f7ed67SGreg Roach case 'r': 262a5f7ed67SGreg Roach $records = $this->sitemapRepositories($tree, $limit, $offset); 263a5f7ed67SGreg Roach break; 264a5f7ed67SGreg Roach 265a5f7ed67SGreg Roach case 's': 266a5f7ed67SGreg Roach $records = $this->sitemapSources($tree, $limit, $offset); 267a5f7ed67SGreg Roach break; 268a5f7ed67SGreg Roach 269a5f7ed67SGreg Roach default: 270d501c45dSGreg Roach throw new HttpNotFoundException('Invalid record type: ' . $type); 271a5f7ed67SGreg Roach } 272a5f7ed67SGreg Roach 273a5f7ed67SGreg Roach // Skip private records. 2744146fabcSGreg Roach $records = $records->filter(GedcomRecord::accessFilter()); 275a5f7ed67SGreg Roach 276a5f7ed67SGreg Roach return $records; 277a5f7ed67SGreg Roach } 278a5f7ed67SGreg Roach 279a5f7ed67SGreg Roach /** 280a5f7ed67SGreg Roach * @param Tree $tree 281a5f7ed67SGreg Roach * @param int $limit 282a5f7ed67SGreg Roach * @param int $offset 283a5f7ed67SGreg Roach * 284b5c8fd7eSGreg Roach * @return Collection<Individual> 285a5f7ed67SGreg Roach */ 286886b77daSGreg Roach private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection 287c1010edaSGreg Roach { 288886b77daSGreg Roach return DB::table('individuals') 289fa17fb66SGreg Roach ->where('i_file', '=', $tree->id()) 290fa17fb66SGreg Roach ->orderBy('i_id') 291fa17fb66SGreg Roach ->skip($offset) 292fa17fb66SGreg Roach ->take($limit) 293886b77daSGreg Roach ->get() 294d5ad3db0SGreg Roach ->map(Individual::rowMapper($tree)); 2958c2e8227SGreg Roach } 296a5f7ed67SGreg Roach 297a5f7ed67SGreg Roach /** 298a5f7ed67SGreg Roach * @param Tree $tree 299a5f7ed67SGreg Roach * @param int $limit 300a5f7ed67SGreg Roach * @param int $offset 301a5f7ed67SGreg Roach * 302b5c8fd7eSGreg Roach * @return Collection<Media> 303a5f7ed67SGreg Roach */ 304886b77daSGreg Roach private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection 305c1010edaSGreg Roach { 306886b77daSGreg Roach return DB::table('media') 307fa17fb66SGreg Roach ->where('m_file', '=', $tree->id()) 308fa17fb66SGreg Roach ->orderBy('m_id') 309fa17fb66SGreg Roach ->skip($offset) 310fa17fb66SGreg Roach ->take($limit) 311886b77daSGreg Roach ->get() 312d5ad3db0SGreg Roach ->map(Media::rowMapper($tree)); 3138c2e8227SGreg Roach } 3148c2e8227SGreg Roach 3158c2e8227SGreg Roach /** 316a5f7ed67SGreg Roach * @param Tree $tree 317a5f7ed67SGreg Roach * @param int $limit 318a5f7ed67SGreg Roach * @param int $offset 319a5f7ed67SGreg Roach * 320b5c8fd7eSGreg Roach * @return Collection<Note> 3218c2e8227SGreg Roach */ 322886b77daSGreg Roach private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection 323c1010edaSGreg Roach { 324886b77daSGreg Roach return DB::table('other') 325fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 326fa17fb66SGreg Roach ->where('o_type', '=', 'NOTE') 327fa17fb66SGreg Roach ->orderBy('o_id') 328fa17fb66SGreg Roach ->skip($offset) 329fa17fb66SGreg Roach ->take($limit) 330886b77daSGreg Roach ->get() 331d5ad3db0SGreg Roach ->map(Note::rowMapper($tree)); 3328c2e8227SGreg Roach } 3338c2e8227SGreg Roach 334a5f7ed67SGreg Roach /** 335a5f7ed67SGreg Roach * @param Tree $tree 336a5f7ed67SGreg Roach * @param int $limit 337a5f7ed67SGreg Roach * @param int $offset 338a5f7ed67SGreg Roach * 339b5c8fd7eSGreg Roach * @return Collection<Repository> 340a5f7ed67SGreg Roach */ 341886b77daSGreg Roach private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection 342c1010edaSGreg Roach { 343886b77daSGreg Roach return DB::table('other') 344fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 345fa17fb66SGreg Roach ->where('o_type', '=', 'REPO') 346fa17fb66SGreg Roach ->orderBy('o_id') 347fa17fb66SGreg Roach ->skip($offset) 348fa17fb66SGreg Roach ->take($limit) 349886b77daSGreg Roach ->get() 350d5ad3db0SGreg Roach ->map(Repository::rowMapper($tree)); 351a5f7ed67SGreg Roach } 352a5f7ed67SGreg Roach 353a5f7ed67SGreg Roach /** 354a5f7ed67SGreg Roach * @param Tree $tree 355a5f7ed67SGreg Roach * @param int $limit 356a5f7ed67SGreg Roach * @param int $offset 357a5f7ed67SGreg Roach * 358b5c8fd7eSGreg Roach * @return Collection<Source> 359a5f7ed67SGreg Roach */ 360886b77daSGreg Roach private function sitemapSources(Tree $tree, int $limit, int $offset): Collection 361c1010edaSGreg Roach { 362886b77daSGreg Roach return DB::table('sources') 363fa17fb66SGreg Roach ->where('s_file', '=', $tree->id()) 364fa17fb66SGreg Roach ->orderBy('s_id') 365fa17fb66SGreg Roach ->skip($offset) 366fa17fb66SGreg Roach ->take($limit) 367886b77daSGreg Roach ->get() 368d5ad3db0SGreg Roach ->map(Source::rowMapper($tree)); 3698c2e8227SGreg Roach } 3708c2e8227SGreg Roach} 371