18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 19a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 20a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 21b1b85189SGreg Roachuse Fisharebest\Webtrees\Html; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree; 29a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 30a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Request; 31a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Response; 32a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 338c2e8227SGreg Roach 348c2e8227SGreg Roach/** 358c2e8227SGreg Roach * Class SiteMapModule 368c2e8227SGreg Roach */ 37e2a378d3SGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface { 388c2e8227SGreg Roach const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits. 398c2e8227SGreg Roach const CACHE_LIFE = 1209600; // Two weeks 408c2e8227SGreg Roach 41a5f7ed67SGreg Roach /** 42a5f7ed67SGreg Roach * How should this module be labelled on tabs, menus, etc.? 43a5f7ed67SGreg Roach * 44a5f7ed67SGreg Roach * @return string 45a5f7ed67SGreg Roach */ 468c2e8227SGreg Roach public function getTitle() { 47a5f7ed67SGreg Roach return /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */ 48a5f7ed67SGreg Roach I18N::translate('Sitemaps'); 498c2e8227SGreg Roach } 508c2e8227SGreg Roach 51a5f7ed67SGreg Roach /** 52a5f7ed67SGreg Roach * A sentence describing what this module does. 53a5f7ed67SGreg Roach * 54a5f7ed67SGreg Roach * @return string 55a5f7ed67SGreg Roach */ 568c2e8227SGreg Roach public function getDescription() { 57a5f7ed67SGreg Roach return /* I18N: Description of the “Sitemaps” module */ 58a5f7ed67SGreg Roach I18N::translate('Generate sitemap files for search engines.'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 6176692c8bSGreg Roach /** 62a5f7ed67SGreg Roach * The URL to a page where the user can modify the configuration of this module. 6376692c8bSGreg Roach * 64a5f7ed67SGreg Roach * @return string 6576692c8bSGreg Roach */ 66a5f7ed67SGreg Roach public function getConfigLink() { 67291c1b19SGreg Roach return route('module', ['module' => $this->getName(), 'action' => 'Admin']); 688c2e8227SGreg Roach } 698c2e8227SGreg Roach 708c2e8227SGreg Roach /** 71a5f7ed67SGreg Roach * @param Request $request 7276692c8bSGreg Roach * 73a5f7ed67SGreg Roach * @return Response 748c2e8227SGreg Roach */ 75a5f7ed67SGreg Roach public function getAdminAction(Request $request): Response { 76a5f7ed67SGreg Roach $this->layout = 'layouts/administration'; 77a5f7ed67SGreg Roach 78a5f7ed67SGreg Roach $sitemap_url = route('module', ['module' => 'sitemap', 'action' => 'Index']); 79a5f7ed67SGreg Roach 80a5f7ed67SGreg Roach // This list comes from http://en.wikipedia.org/wiki/Sitemaps 81a5f7ed67SGreg Roach $submit_urls = [ 82a5f7ed67SGreg Roach 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 83a5f7ed67SGreg Roach 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 84a5f7ed67SGreg Roach ]; 85a5f7ed67SGreg Roach 86291c1b19SGreg Roach return $this->viewResponse('modules/sitemap/config', [ 87a5f7ed67SGreg Roach 'all_trees' => Tree::getAll(), 88a5f7ed67SGreg Roach 'sitemap_url' => $sitemap_url, 89a5f7ed67SGreg Roach 'submit_urls' => $submit_urls, 90a5f7ed67SGreg Roach 'title' => $this->getTitle(), 91a5f7ed67SGreg Roach ]); 928c2e8227SGreg Roach } 938c2e8227SGreg Roach 948c2e8227SGreg Roach /** 95a5f7ed67SGreg Roach * @param Request $request 96a5f7ed67SGreg Roach * 97a5f7ed67SGreg Roach * @return RedirectResponse 988c2e8227SGreg Roach */ 99a5f7ed67SGreg Roach public function postAdminAction(Request $request): RedirectResponse { 1008c2e8227SGreg Roach foreach (Tree::getAll() as $tree) { 101a5f7ed67SGreg Roach $include_in_sitemap = (bool) $request->get('sitemap' . $tree->getTreeId()); 102a5f7ed67SGreg Roach $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 1038c2e8227SGreg Roach } 104a5f7ed67SGreg Roach 105291c1b19SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success'); 106a5f7ed67SGreg Roach 107a5f7ed67SGreg Roach return new RedirectResponse($this->getConfigLink()); 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 1108c2e8227SGreg Roach /** 111a5f7ed67SGreg Roach * @param Request $request 1128c2e8227SGreg Roach * 113a5f7ed67SGreg Roach * @return Response 1148c2e8227SGreg Roach */ 115a5f7ed67SGreg Roach public function getIndexAction(Request $request): Response { 116a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap.timestamp'); 117a5f7ed67SGreg Roach 118a5f7ed67SGreg Roach if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) { 119a5f7ed67SGreg Roach $content = $this->getPreference('sitemap.xml'); 1208c2e8227SGreg Roach } else { 121a5f7ed67SGreg Roach $count_individuals = Database::prepare( 122a5f7ed67SGreg Roach "SELECT i_file, COUNT(*) FROM `##individuals` GROUP BY i_file" 123a5f7ed67SGreg Roach )->execute()->fetchAssoc(); 124a5f7ed67SGreg Roach 125a5f7ed67SGreg Roach $count_media = Database::prepare( 126a5f7ed67SGreg Roach "SELECT m_file, COUNT(*) FROM `##media` GROUP BY m_file" 127a5f7ed67SGreg Roach )->execute()->fetchAssoc(); 128a5f7ed67SGreg Roach 129a5f7ed67SGreg Roach $count_notes = Database::prepare( 130a5f7ed67SGreg Roach "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='NOTE' GROUP BY o_file" 131a5f7ed67SGreg Roach )->execute()->fetchAssoc(); 132a5f7ed67SGreg Roach 133a5f7ed67SGreg Roach $count_repositories = Database::prepare( 134a5f7ed67SGreg Roach "SELECT o_file, COUNT(*) FROM `##other` WHERE o_type='REPO' GROUP BY o_file" 135a5f7ed67SGreg Roach )->execute()->fetchAssoc(); 136a5f7ed67SGreg Roach 137a5f7ed67SGreg Roach $count_sources = Database::prepare( 138a5f7ed67SGreg Roach "SELECT s_file, COUNT(*) FROM `##sources` GROUP BY s_file" 139a5f7ed67SGreg Roach )->execute()->fetchAssoc(); 140a5f7ed67SGreg Roach 141*a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-index.xml', [ 142a5f7ed67SGreg Roach 'all_trees' => Tree::getAll(), 143a5f7ed67SGreg Roach 'count_individuals' => $count_individuals, 144a5f7ed67SGreg Roach 'count_media' => $count_media, 145a5f7ed67SGreg Roach 'count_notes' => $count_notes, 146a5f7ed67SGreg Roach 'count_repositories' => $count_repositories, 147a5f7ed67SGreg Roach 'count_sources' => $count_sources, 148a5f7ed67SGreg Roach 'last_mod' => date('Y-m-d'), 149a5f7ed67SGreg Roach 'records_per_volume' => self::RECORDS_PER_VOLUME, 150a5f7ed67SGreg Roach ]); 151a5f7ed67SGreg Roach 152a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 153a5f7ed67SGreg Roach } 154a5f7ed67SGreg Roach 155a5f7ed67SGreg Roach return new Response($content, Response::HTTP_OK, [ 156a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 157a5f7ed67SGreg Roach ]); 158a5f7ed67SGreg Roach } 159a5f7ed67SGreg Roach 160a5f7ed67SGreg Roach /** 161a5f7ed67SGreg Roach * @param Request $request 162a5f7ed67SGreg Roach * 163a5f7ed67SGreg Roach * @return Response 164a5f7ed67SGreg Roach */ 165a5f7ed67SGreg Roach public function getFileAction(Request $request): Response { 166a5f7ed67SGreg Roach $file = $request->get('file', ''); 167a5f7ed67SGreg Roach 168a5f7ed67SGreg Roach if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) { 169a5f7ed67SGreg Roach throw new NotFoundHttpException('Bad sitemap file'); 170a5f7ed67SGreg Roach } 171a5f7ed67SGreg Roach 172a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp'); 173a5f7ed67SGreg Roach 174a5f7ed67SGreg Roach if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) { 175a5f7ed67SGreg Roach $content = $this->getPreference('sitemap-' . $file . '.xml'); 176a5f7ed67SGreg Roach } else { 177a5f7ed67SGreg Roach $tree = Tree::findById((int) $match[1]); 178a5f7ed67SGreg Roach 179a5f7ed67SGreg Roach if ($tree === null) { 180a5f7ed67SGreg Roach throw new NotFoundHttpException('No such tree'); 181a5f7ed67SGreg Roach } 182a5f7ed67SGreg Roach 183a5f7ed67SGreg Roach $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, 184a5f7ed67SGreg Roach self::RECORDS_PER_VOLUME * $match[3]); 185a5f7ed67SGreg Roach 186*a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]); 187a5f7ed67SGreg Roach 188a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 189a5f7ed67SGreg Roach } 190a5f7ed67SGreg Roach 191a5f7ed67SGreg Roach return new Response($content, Response::HTTP_OK, [ 192a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 193a5f7ed67SGreg Roach ]); 194a5f7ed67SGreg Roach } 195a5f7ed67SGreg Roach 196a5f7ed67SGreg Roach /** 197a5f7ed67SGreg Roach * @param Tree $tree 198a5f7ed67SGreg Roach * @param string $type 199a5f7ed67SGreg Roach * @param int $limit 200a5f7ed67SGreg Roach * @param int $offset 201a5f7ed67SGreg Roach * 202a5f7ed67SGreg Roach * @return array 203a5f7ed67SGreg Roach */ 204a5f7ed67SGreg Roach private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): array { 205a5f7ed67SGreg Roach switch ($type) { 2068c2e8227SGreg Roach case 'i': 207a5f7ed67SGreg Roach $records = $this->sitemapIndividuals($tree, $limit, $offset); 208a5f7ed67SGreg Roach break; 209a5f7ed67SGreg Roach 210a5f7ed67SGreg Roach case 'm': 211a5f7ed67SGreg Roach $records = $this->sitemapMedia($tree, $limit, $offset); 212a5f7ed67SGreg Roach break; 213a5f7ed67SGreg Roach 214a5f7ed67SGreg Roach case 'n': 215a5f7ed67SGreg Roach $records = $this->sitemapNotes($tree, $limit, $offset); 216a5f7ed67SGreg Roach break; 217a5f7ed67SGreg Roach 218a5f7ed67SGreg Roach case 'r': 219a5f7ed67SGreg Roach $records = $this->sitemapRepositories($tree, $limit, $offset); 220a5f7ed67SGreg Roach break; 221a5f7ed67SGreg Roach 222a5f7ed67SGreg Roach case 's': 223a5f7ed67SGreg Roach $records = $this->sitemapSources($tree, $limit, $offset); 224a5f7ed67SGreg Roach break; 225a5f7ed67SGreg Roach 226a5f7ed67SGreg Roach default: 227a5f7ed67SGreg Roach throw new NotFoundHttpException('Invalid record type: ' . $type); 228a5f7ed67SGreg Roach } 229a5f7ed67SGreg Roach 230a5f7ed67SGreg Roach // Skip records that no longer exist. 231a5f7ed67SGreg Roach $records = array_filter($records); 232a5f7ed67SGreg Roach 233a5f7ed67SGreg Roach // Skip private records. 234a5f7ed67SGreg Roach $records = array_filter($records, function (GedcomRecord $record) { 235a5f7ed67SGreg Roach return $record->canShow(); 236a5f7ed67SGreg Roach }); 237a5f7ed67SGreg Roach 238a5f7ed67SGreg Roach return $records; 239a5f7ed67SGreg Roach } 240a5f7ed67SGreg Roach 241a5f7ed67SGreg Roach /** 242a5f7ed67SGreg Roach * @param Tree $tree 243a5f7ed67SGreg Roach * @param int $limit 244a5f7ed67SGreg Roach * @param int $offset 245a5f7ed67SGreg Roach * 246a5f7ed67SGreg Roach * @return array 247a5f7ed67SGreg Roach */ 248a5f7ed67SGreg Roach private function sitemapIndividuals(Tree $tree, int $limit, int $offset): array { 2498c2e8227SGreg Roach $rows = Database::prepare( 25024ec66ceSGreg Roach "SELECT i_id AS xref, i_gedcom AS gedcom" . 2518c2e8227SGreg Roach " FROM `##individuals`" . 2528c2e8227SGreg Roach " WHERE i_file = :tree_id" . 2538c2e8227SGreg Roach " ORDER BY i_id" . 2548c2e8227SGreg Roach " LIMIT :limit OFFSET :offset" 25513abd6f3SGreg Roach )->execute([ 256a5f7ed67SGreg Roach 'tree_id' => $tree->getTreeId(), 257a5f7ed67SGreg Roach 'limit' => $limit, 258a5f7ed67SGreg Roach 'offset' => $offset, 25913abd6f3SGreg Roach ])->fetchAll(); 260a5f7ed67SGreg Roach 261a5f7ed67SGreg Roach $records = []; 262a5f7ed67SGreg Roach 2638c2e8227SGreg Roach foreach ($rows as $row) { 26424ec66ceSGreg Roach $records[] = Individual::getInstance($row->xref, $tree, $row->gedcom); 2658c2e8227SGreg Roach } 266a5f7ed67SGreg Roach 267a5f7ed67SGreg Roach return $records; 2688c2e8227SGreg Roach } 269a5f7ed67SGreg Roach 270a5f7ed67SGreg Roach /** 271a5f7ed67SGreg Roach * @param Tree $tree 272a5f7ed67SGreg Roach * @param int $limit 273a5f7ed67SGreg Roach * @param int $offset 274a5f7ed67SGreg Roach * 275a5f7ed67SGreg Roach * @return array 276a5f7ed67SGreg Roach */ 277a5f7ed67SGreg Roach private function sitemapMedia(Tree $tree, int $limit, int $offset): array { 2788c2e8227SGreg Roach $rows = Database::prepare( 27924ec66ceSGreg Roach "SELECT m_id AS xref, m_gedcom AS gedcom" . 2808c2e8227SGreg Roach " FROM `##media`" . 2818c2e8227SGreg Roach " WHERE m_file = :tree_id" . 2828c2e8227SGreg Roach " ORDER BY m_id" . 2838c2e8227SGreg Roach " LIMIT :limit OFFSET :offset" 28413abd6f3SGreg Roach )->execute([ 285a5f7ed67SGreg Roach 'tree_id' => $tree->getTreeId(), 286a5f7ed67SGreg Roach 'limit' => $limit, 287a5f7ed67SGreg Roach 'offset' => $offset, 28813abd6f3SGreg Roach ])->fetchAll(); 289a5f7ed67SGreg Roach 290a5f7ed67SGreg Roach $records = []; 291a5f7ed67SGreg Roach 2928c2e8227SGreg Roach foreach ($rows as $row) { 29324ec66ceSGreg Roach $records[] = Media::getInstance($row->xref, $tree, $row->gedcom); 2948c2e8227SGreg Roach } 295a5f7ed67SGreg Roach 296a5f7ed67SGreg Roach return $records; 2978c2e8227SGreg Roach } 2988c2e8227SGreg Roach 2998c2e8227SGreg Roach /** 300a5f7ed67SGreg Roach * @param Tree $tree 301a5f7ed67SGreg Roach * @param int $limit 302a5f7ed67SGreg Roach * @param int $offset 303a5f7ed67SGreg Roach * 304a5f7ed67SGreg Roach * @return array 3058c2e8227SGreg Roach */ 306a5f7ed67SGreg Roach private function sitemapNotes(Tree $tree, int $limit, int $offset): array { 307a5f7ed67SGreg Roach $rows = Database::prepare( 308a5f7ed67SGreg Roach "SELECT o_id AS xref, o_gedcom AS gedcom" . 309a5f7ed67SGreg Roach " FROM `##other`" . 310a5f7ed67SGreg Roach " WHERE o_file = :tree_id AND o_type = 'NOTE'" . 311a5f7ed67SGreg Roach " ORDER BY o_id" . 312a5f7ed67SGreg Roach " LIMIT :limit OFFSET :offset" 313a5f7ed67SGreg Roach )->execute([ 314a5f7ed67SGreg Roach 'tree_id' => $tree->getTreeId(), 315a5f7ed67SGreg Roach 'limit' => $limit, 316a5f7ed67SGreg Roach 'offset' => $offset, 317a5f7ed67SGreg Roach ])->fetchAll(); 3188c2e8227SGreg Roach 319a5f7ed67SGreg Roach $records = []; 320a5f7ed67SGreg Roach 321a5f7ed67SGreg Roach foreach ($rows as $row) { 322a5f7ed67SGreg Roach $records[] = Note::getInstance($row->xref, $tree, $row->gedcom); 3238c2e8227SGreg Roach } 3248c2e8227SGreg Roach 325a5f7ed67SGreg Roach return $records; 3268c2e8227SGreg Roach } 3278c2e8227SGreg Roach 328a5f7ed67SGreg Roach /** 329a5f7ed67SGreg Roach * @param Tree $tree 330a5f7ed67SGreg Roach * @param int $limit 331a5f7ed67SGreg Roach * @param int $offset 332a5f7ed67SGreg Roach * 333a5f7ed67SGreg Roach * @return array 334a5f7ed67SGreg Roach */ 335a5f7ed67SGreg Roach private function sitemapRepositories(Tree $tree, int $limit, int $offset): array { 336a5f7ed67SGreg Roach $rows = Database::prepare( 337a5f7ed67SGreg Roach "SELECT o_id AS xref, o_gedcom AS gedcom" . 338a5f7ed67SGreg Roach " FROM `##other`" . 339a5f7ed67SGreg Roach " WHERE o_file = :tree_id AND o_type = 'REPO'" . 340a5f7ed67SGreg Roach " ORDER BY o_id" . 341a5f7ed67SGreg Roach " LIMIT :limit OFFSET :offset" 342a5f7ed67SGreg Roach )->execute([ 343a5f7ed67SGreg Roach 'tree_id' => $tree->getTreeId(), 344a5f7ed67SGreg Roach 'limit' => $limit, 345a5f7ed67SGreg Roach 'offset' => $offset, 346a5f7ed67SGreg Roach ])->fetchAll(); 347a5f7ed67SGreg Roach 348a5f7ed67SGreg Roach $records = []; 349a5f7ed67SGreg Roach 350a5f7ed67SGreg Roach foreach ($rows as $row) { 351a5f7ed67SGreg Roach $records[] = Repository::getInstance($row->xref, $tree, $row->gedcom); 352a5f7ed67SGreg Roach } 353a5f7ed67SGreg Roach 354a5f7ed67SGreg Roach return $records; 355a5f7ed67SGreg Roach } 356a5f7ed67SGreg Roach 357a5f7ed67SGreg Roach /** 358a5f7ed67SGreg Roach * @param Tree $tree 359a5f7ed67SGreg Roach * @param int $limit 360a5f7ed67SGreg Roach * @param int $offset 361a5f7ed67SGreg Roach * 362a5f7ed67SGreg Roach * @return array 363a5f7ed67SGreg Roach */ 364a5f7ed67SGreg Roach private function sitemapSources(Tree $tree, int $limit, int $offset): array { 365a5f7ed67SGreg Roach $rows = Database::prepare( 366a5f7ed67SGreg Roach "SELECT s_id AS xref, s_gedcom AS gedcom" . 367a5f7ed67SGreg Roach " FROM `##sources`" . 368a5f7ed67SGreg Roach " WHERE s_file = :tree_id" . 369a5f7ed67SGreg Roach " ORDER BY s_id" . 370a5f7ed67SGreg Roach " LIMIT :limit OFFSET :offset" 371a5f7ed67SGreg Roach )->execute([ 372a5f7ed67SGreg Roach 'tree_id' => $tree->getTreeId(), 373a5f7ed67SGreg Roach 'limit' => $limit, 374a5f7ed67SGreg Roach 'offset' => $offset, 375a5f7ed67SGreg Roach ])->fetchAll(); 376a5f7ed67SGreg Roach 377a5f7ed67SGreg Roach $records = []; 378a5f7ed67SGreg Roach 379a5f7ed67SGreg Roach foreach ($rows as $row) { 380a5f7ed67SGreg Roach $records[] = Source::getInstance($row->xref, $tree, $row->gedcom); 381a5f7ed67SGreg Roach } 382a5f7ed67SGreg Roach 383a5f7ed67SGreg Roach return $records; 3848c2e8227SGreg Roach } 3858c2e8227SGreg Roach} 386