18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 20a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 21a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 22b1b85189SGreg Roachuse Fisharebest\Webtrees\Html; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source; 290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 30*fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 31a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 32a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Request; 33a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Response; 34a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 358c2e8227SGreg Roach 368c2e8227SGreg Roach/** 378c2e8227SGreg Roach * Class SiteMapModule 388c2e8227SGreg Roach */ 39c1010edaSGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface 40c1010edaSGreg Roach{ 4116d6367aSGreg Roach private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits. 4216d6367aSGreg Roach private const CACHE_LIFE = 1209600; // Two weeks 438c2e8227SGreg Roach 44a5f7ed67SGreg Roach /** 45a5f7ed67SGreg Roach * How should this module be labelled on tabs, menus, etc.? 46a5f7ed67SGreg Roach * 47a5f7ed67SGreg Roach * @return string 48a5f7ed67SGreg Roach */ 498f53f488SRico Sonntag public function getTitle(): string 50c1010edaSGreg Roach { 51bbb76c12SGreg Roach /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */ 52bbb76c12SGreg Roach return I18N::translate('Sitemaps'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 55a5f7ed67SGreg Roach /** 56a5f7ed67SGreg Roach * A sentence describing what this module does. 57a5f7ed67SGreg Roach * 58a5f7ed67SGreg Roach * @return string 59a5f7ed67SGreg Roach */ 608f53f488SRico Sonntag public function getDescription(): string 61c1010edaSGreg Roach { 62bbb76c12SGreg Roach /* I18N: Description of the “Sitemaps” module */ 63bbb76c12SGreg Roach return I18N::translate('Generate sitemap files for search engines.'); 648c2e8227SGreg Roach } 658c2e8227SGreg Roach 6676692c8bSGreg Roach /** 67a5f7ed67SGreg Roach * The URL to a page where the user can modify the configuration of this module. 6876692c8bSGreg Roach * 69a5f7ed67SGreg Roach * @return string 7076692c8bSGreg Roach */ 718f53f488SRico Sonntag public function getConfigLink(): string 72c1010edaSGreg Roach { 73c1010edaSGreg Roach return route('module', [ 74c1010edaSGreg Roach 'module' => $this->getName(), 75c1010edaSGreg Roach 'action' => 'Admin', 76c1010edaSGreg Roach ]); 778c2e8227SGreg Roach } 788c2e8227SGreg Roach 798c2e8227SGreg Roach /** 80a5f7ed67SGreg Roach * @return Response 818c2e8227SGreg Roach */ 8236e59714SGreg Roach public function getAdminAction(): Response 83c1010edaSGreg Roach { 84a5f7ed67SGreg Roach $this->layout = 'layouts/administration'; 85a5f7ed67SGreg Roach 86c1010edaSGreg Roach $sitemap_url = route('module', [ 87c1010edaSGreg Roach 'module' => 'sitemap', 88c1010edaSGreg Roach 'action' => 'Index', 89c1010edaSGreg Roach ]); 90a5f7ed67SGreg Roach 91a5f7ed67SGreg Roach // This list comes from http://en.wikipedia.org/wiki/Sitemaps 92a5f7ed67SGreg Roach $submit_urls = [ 93a5f7ed67SGreg Roach 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 94a5f7ed67SGreg Roach 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 95a5f7ed67SGreg Roach ]; 96a5f7ed67SGreg Roach 97291c1b19SGreg Roach return $this->viewResponse('modules/sitemap/config', [ 98a5f7ed67SGreg Roach 'all_trees' => Tree::getAll(), 99a5f7ed67SGreg Roach 'sitemap_url' => $sitemap_url, 100a5f7ed67SGreg Roach 'submit_urls' => $submit_urls, 101a5f7ed67SGreg Roach 'title' => $this->getTitle(), 102a5f7ed67SGreg Roach ]); 1038c2e8227SGreg Roach } 1048c2e8227SGreg Roach 1058c2e8227SGreg Roach /** 106a5f7ed67SGreg Roach * @param Request $request 107a5f7ed67SGreg Roach * 108a5f7ed67SGreg Roach * @return RedirectResponse 1098c2e8227SGreg Roach */ 110c1010edaSGreg Roach public function postAdminAction(Request $request): RedirectResponse 111c1010edaSGreg Roach { 1128c2e8227SGreg Roach foreach (Tree::getAll() as $tree) { 11372cf66d4SGreg Roach $include_in_sitemap = (bool) $request->get('sitemap' . $tree->id()); 114a5f7ed67SGreg Roach $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 1158c2e8227SGreg Roach } 116a5f7ed67SGreg Roach 117291c1b19SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success'); 118a5f7ed67SGreg Roach 119a5f7ed67SGreg Roach return new RedirectResponse($this->getConfigLink()); 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 1228c2e8227SGreg Roach /** 123a5f7ed67SGreg Roach * @return Response 1248c2e8227SGreg Roach */ 12536e59714SGreg Roach public function getIndexAction(): Response 126c1010edaSGreg Roach { 127a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap.timestamp'); 128a5f7ed67SGreg Roach 129a5f7ed67SGreg Roach if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) { 130a5f7ed67SGreg Roach $content = $this->getPreference('sitemap.xml'); 1318c2e8227SGreg Roach } else { 132*fa17fb66SGreg Roach $count_individuals = DB::table('individuals') 133*fa17fb66SGreg Roach ->groupBy('i_file') 134*fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'i_file']) 135*fa17fb66SGreg Roach ->pluck('total', 'i_file'); 136a5f7ed67SGreg Roach 137*fa17fb66SGreg Roach $count_media = DB::table('media') 138*fa17fb66SGreg Roach ->groupBy('m_file') 139*fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'm_file']) 140*fa17fb66SGreg Roach ->pluck('total', 'm_file'); 141a5f7ed67SGreg Roach 142*fa17fb66SGreg Roach $count_notes = DB::table('other') 143*fa17fb66SGreg Roach ->where('o_type', '=', 'NOTE') 144*fa17fb66SGreg Roach ->groupBy('o_file') 145*fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 146*fa17fb66SGreg Roach ->pluck('total', 'o_file'); 147a5f7ed67SGreg Roach 148*fa17fb66SGreg Roach $count_repositories = DB::table('other') 149*fa17fb66SGreg Roach ->where('o_type', '=', 'REPO') 150*fa17fb66SGreg Roach ->groupBy('o_file') 151*fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 152*fa17fb66SGreg Roach ->pluck('total', 'o_file'); 153a5f7ed67SGreg Roach 154*fa17fb66SGreg Roach $count_sources = DB::table('sources') 155*fa17fb66SGreg Roach ->groupBy('s_file') 156*fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 's_file']) 157*fa17fb66SGreg Roach ->pluck('total', 's_file'); 158a5f7ed67SGreg Roach 159a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-index.xml', [ 160a5f7ed67SGreg Roach 'all_trees' => Tree::getAll(), 161a5f7ed67SGreg Roach 'count_individuals' => $count_individuals, 162a5f7ed67SGreg Roach 'count_media' => $count_media, 163a5f7ed67SGreg Roach 'count_notes' => $count_notes, 164a5f7ed67SGreg Roach 'count_repositories' => $count_repositories, 165a5f7ed67SGreg Roach 'count_sources' => $count_sources, 166a5f7ed67SGreg Roach 'last_mod' => date('Y-m-d'), 167a5f7ed67SGreg Roach 'records_per_volume' => self::RECORDS_PER_VOLUME, 168a5f7ed67SGreg Roach ]); 169a5f7ed67SGreg Roach 170a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 171a5f7ed67SGreg Roach } 172a5f7ed67SGreg Roach 173a5f7ed67SGreg Roach return new Response($content, Response::HTTP_OK, [ 174a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 175a5f7ed67SGreg Roach ]); 176a5f7ed67SGreg Roach } 177a5f7ed67SGreg Roach 178a5f7ed67SGreg Roach /** 179a5f7ed67SGreg Roach * @param Request $request 180a5f7ed67SGreg Roach * 181a5f7ed67SGreg Roach * @return Response 182a5f7ed67SGreg Roach */ 183c1010edaSGreg Roach public function getFileAction(Request $request): Response 184c1010edaSGreg Roach { 185a5f7ed67SGreg Roach $file = $request->get('file', ''); 186a5f7ed67SGreg Roach 187a5f7ed67SGreg Roach if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) { 188a5f7ed67SGreg Roach throw new NotFoundHttpException('Bad sitemap file'); 189a5f7ed67SGreg Roach } 190a5f7ed67SGreg Roach 191a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp'); 192a5f7ed67SGreg Roach 193a5f7ed67SGreg Roach if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) { 194a5f7ed67SGreg Roach $content = $this->getPreference('sitemap-' . $file . '.xml'); 195a5f7ed67SGreg Roach } else { 196a5f7ed67SGreg Roach $tree = Tree::findById((int) $match[1]); 197a5f7ed67SGreg Roach 198a5f7ed67SGreg Roach if ($tree === null) { 199a5f7ed67SGreg Roach throw new NotFoundHttpException('No such tree'); 200a5f7ed67SGreg Roach } 201a5f7ed67SGreg Roach 202bdb3725aSGreg Roach $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]); 203a5f7ed67SGreg Roach 204a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]); 205a5f7ed67SGreg Roach 206a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 207a5f7ed67SGreg Roach } 208a5f7ed67SGreg Roach 209a5f7ed67SGreg Roach return new Response($content, Response::HTTP_OK, [ 210a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 211a5f7ed67SGreg Roach ]); 212a5f7ed67SGreg Roach } 213a5f7ed67SGreg Roach 214a5f7ed67SGreg Roach /** 215a5f7ed67SGreg Roach * @param Tree $tree 216a5f7ed67SGreg Roach * @param string $type 217a5f7ed67SGreg Roach * @param int $limit 218a5f7ed67SGreg Roach * @param int $offset 219a5f7ed67SGreg Roach * 220a5f7ed67SGreg Roach * @return array 221a5f7ed67SGreg Roach */ 222c1010edaSGreg Roach private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): array 223c1010edaSGreg Roach { 224a5f7ed67SGreg Roach switch ($type) { 2258c2e8227SGreg Roach case 'i': 226a5f7ed67SGreg Roach $records = $this->sitemapIndividuals($tree, $limit, $offset); 227a5f7ed67SGreg Roach break; 228a5f7ed67SGreg Roach 229a5f7ed67SGreg Roach case 'm': 230a5f7ed67SGreg Roach $records = $this->sitemapMedia($tree, $limit, $offset); 231a5f7ed67SGreg Roach break; 232a5f7ed67SGreg Roach 233a5f7ed67SGreg Roach case 'n': 234a5f7ed67SGreg Roach $records = $this->sitemapNotes($tree, $limit, $offset); 235a5f7ed67SGreg Roach break; 236a5f7ed67SGreg Roach 237a5f7ed67SGreg Roach case 'r': 238a5f7ed67SGreg Roach $records = $this->sitemapRepositories($tree, $limit, $offset); 239a5f7ed67SGreg Roach break; 240a5f7ed67SGreg Roach 241a5f7ed67SGreg Roach case 's': 242a5f7ed67SGreg Roach $records = $this->sitemapSources($tree, $limit, $offset); 243a5f7ed67SGreg Roach break; 244a5f7ed67SGreg Roach 245a5f7ed67SGreg Roach default: 246a5f7ed67SGreg Roach throw new NotFoundHttpException('Invalid record type: ' . $type); 247a5f7ed67SGreg Roach } 248a5f7ed67SGreg Roach 249a5f7ed67SGreg Roach // Skip records that no longer exist. 250a5f7ed67SGreg Roach $records = array_filter($records); 251a5f7ed67SGreg Roach 252a5f7ed67SGreg Roach // Skip private records. 253492c7072SGreg Roach $records = array_filter($records, function (GedcomRecord $record): bool { 254a5f7ed67SGreg Roach return $record->canShow(); 255a5f7ed67SGreg Roach }); 256a5f7ed67SGreg Roach 257a5f7ed67SGreg Roach return $records; 258a5f7ed67SGreg Roach } 259a5f7ed67SGreg Roach 260a5f7ed67SGreg Roach /** 261a5f7ed67SGreg Roach * @param Tree $tree 262a5f7ed67SGreg Roach * @param int $limit 263a5f7ed67SGreg Roach * @param int $offset 264a5f7ed67SGreg Roach * 265a5f7ed67SGreg Roach * @return array 266a5f7ed67SGreg Roach */ 267c1010edaSGreg Roach private function sitemapIndividuals(Tree $tree, int $limit, int $offset): array 268c1010edaSGreg Roach { 269*fa17fb66SGreg Roach $rows = DB::table('individuals') 270*fa17fb66SGreg Roach ->where('i_file', '=', $tree->id()) 271*fa17fb66SGreg Roach ->orderBy('i_id') 272*fa17fb66SGreg Roach ->skip($offset) 273*fa17fb66SGreg Roach ->take($limit) 274*fa17fb66SGreg Roach ->get(); 275a5f7ed67SGreg Roach 276a5f7ed67SGreg Roach $records = []; 277a5f7ed67SGreg Roach 2788c2e8227SGreg Roach foreach ($rows as $row) { 27924ec66ceSGreg Roach $records[] = Individual::getInstance($row->xref, $tree, $row->gedcom); 2808c2e8227SGreg Roach } 281a5f7ed67SGreg Roach 282a5f7ed67SGreg Roach return $records; 2838c2e8227SGreg Roach } 284a5f7ed67SGreg Roach 285a5f7ed67SGreg Roach /** 286a5f7ed67SGreg Roach * @param Tree $tree 287a5f7ed67SGreg Roach * @param int $limit 288a5f7ed67SGreg Roach * @param int $offset 289a5f7ed67SGreg Roach * 290a5f7ed67SGreg Roach * @return array 291a5f7ed67SGreg Roach */ 292c1010edaSGreg Roach private function sitemapMedia(Tree $tree, int $limit, int $offset): array 293c1010edaSGreg Roach { 294*fa17fb66SGreg Roach $rows = DB::table('media') 295*fa17fb66SGreg Roach ->where('m_file', '=', $tree->id()) 296*fa17fb66SGreg Roach ->orderBy('m_id') 297*fa17fb66SGreg Roach ->skip($offset) 298*fa17fb66SGreg Roach ->take($limit) 299*fa17fb66SGreg Roach ->get(); 300a5f7ed67SGreg Roach 301a5f7ed67SGreg Roach $records = []; 302a5f7ed67SGreg Roach 3038c2e8227SGreg Roach foreach ($rows as $row) { 30424ec66ceSGreg Roach $records[] = Media::getInstance($row->xref, $tree, $row->gedcom); 3058c2e8227SGreg Roach } 306a5f7ed67SGreg Roach 307a5f7ed67SGreg Roach return $records; 3088c2e8227SGreg Roach } 3098c2e8227SGreg Roach 3108c2e8227SGreg Roach /** 311a5f7ed67SGreg Roach * @param Tree $tree 312a5f7ed67SGreg Roach * @param int $limit 313a5f7ed67SGreg Roach * @param int $offset 314a5f7ed67SGreg Roach * 315a5f7ed67SGreg Roach * @return array 3168c2e8227SGreg Roach */ 317c1010edaSGreg Roach private function sitemapNotes(Tree $tree, int $limit, int $offset): array 318c1010edaSGreg Roach { 319*fa17fb66SGreg Roach $rows = DB::table('other') 320*fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 321*fa17fb66SGreg Roach ->where('o_type', '=', 'NOTE') 322*fa17fb66SGreg Roach ->orderBy('o_id') 323*fa17fb66SGreg Roach ->skip($offset) 324*fa17fb66SGreg Roach ->take($limit) 325*fa17fb66SGreg Roach ->get(); 3268c2e8227SGreg Roach 327a5f7ed67SGreg Roach $records = []; 328a5f7ed67SGreg Roach 329a5f7ed67SGreg Roach foreach ($rows as $row) { 330a5f7ed67SGreg Roach $records[] = Note::getInstance($row->xref, $tree, $row->gedcom); 3318c2e8227SGreg Roach } 3328c2e8227SGreg Roach 333a5f7ed67SGreg Roach return $records; 3348c2e8227SGreg Roach } 3358c2e8227SGreg Roach 336a5f7ed67SGreg Roach /** 337a5f7ed67SGreg Roach * @param Tree $tree 338a5f7ed67SGreg Roach * @param int $limit 339a5f7ed67SGreg Roach * @param int $offset 340a5f7ed67SGreg Roach * 341a5f7ed67SGreg Roach * @return array 342a5f7ed67SGreg Roach */ 343c1010edaSGreg Roach private function sitemapRepositories(Tree $tree, int $limit, int $offset): array 344c1010edaSGreg Roach { 345*fa17fb66SGreg Roach $rows = DB::table('other') 346*fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 347*fa17fb66SGreg Roach ->where('o_type', '=', 'REPO') 348*fa17fb66SGreg Roach ->orderBy('o_id') 349*fa17fb66SGreg Roach ->skip($offset) 350*fa17fb66SGreg Roach ->take($limit) 351*fa17fb66SGreg Roach ->get(); 352a5f7ed67SGreg Roach 353a5f7ed67SGreg Roach $records = []; 354a5f7ed67SGreg Roach 355a5f7ed67SGreg Roach foreach ($rows as $row) { 356a5f7ed67SGreg Roach $records[] = Repository::getInstance($row->xref, $tree, $row->gedcom); 357a5f7ed67SGreg Roach } 358a5f7ed67SGreg Roach 359a5f7ed67SGreg Roach return $records; 360a5f7ed67SGreg Roach } 361a5f7ed67SGreg Roach 362a5f7ed67SGreg Roach /** 363a5f7ed67SGreg Roach * @param Tree $tree 364a5f7ed67SGreg Roach * @param int $limit 365a5f7ed67SGreg Roach * @param int $offset 366a5f7ed67SGreg Roach * 367a5f7ed67SGreg Roach * @return array 368a5f7ed67SGreg Roach */ 369c1010edaSGreg Roach private function sitemapSources(Tree $tree, int $limit, int $offset): array 370c1010edaSGreg Roach { 371*fa17fb66SGreg Roach $rows = DB::table('sources') 372*fa17fb66SGreg Roach ->where('s_file', '=', $tree->id()) 373*fa17fb66SGreg Roach ->orderBy('s_id') 374*fa17fb66SGreg Roach ->skip($offset) 375*fa17fb66SGreg Roach ->take($limit) 376*fa17fb66SGreg Roach ->get(); 377a5f7ed67SGreg Roach 378a5f7ed67SGreg Roach $records = []; 379a5f7ed67SGreg Roach 380a5f7ed67SGreg Roach foreach ($rows as $row) { 381a5f7ed67SGreg Roach $records[] = Source::getInstance($row->xref, $tree, $row->gedcom); 382a5f7ed67SGreg Roach } 383a5f7ed67SGreg Roach 384a5f7ed67SGreg Roach return $records; 3858c2e8227SGreg Roach } 3868c2e8227SGreg Roach} 387