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; 30fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 31886b77daSGreg Roachuse Illuminate\Support\Collection; 32a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 33a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Request; 34a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Response; 35a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 368c2e8227SGreg Roach 378c2e8227SGreg Roach/** 388c2e8227SGreg Roach * Class SiteMapModule 398c2e8227SGreg Roach */ 40c1010edaSGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface 41c1010edaSGreg Roach{ 4216d6367aSGreg Roach private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits. 4316d6367aSGreg Roach private const CACHE_LIFE = 1209600; // Two weeks 448c2e8227SGreg Roach 45a5f7ed67SGreg Roach /** 46a5f7ed67SGreg Roach * How should this module be labelled on tabs, menus, etc.? 47a5f7ed67SGreg Roach * 48a5f7ed67SGreg Roach * @return string 49a5f7ed67SGreg Roach */ 508f53f488SRico Sonntag public function getTitle(): string 51c1010edaSGreg Roach { 52bbb76c12SGreg Roach /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */ 53bbb76c12SGreg Roach return I18N::translate('Sitemaps'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 56a5f7ed67SGreg Roach /** 57a5f7ed67SGreg Roach * A sentence describing what this module does. 58a5f7ed67SGreg Roach * 59a5f7ed67SGreg Roach * @return string 60a5f7ed67SGreg Roach */ 618f53f488SRico Sonntag public function getDescription(): string 62c1010edaSGreg Roach { 63bbb76c12SGreg Roach /* I18N: Description of the “Sitemaps” module */ 64bbb76c12SGreg Roach return I18N::translate('Generate sitemap files for search engines.'); 658c2e8227SGreg Roach } 668c2e8227SGreg Roach 6776692c8bSGreg Roach /** 68a5f7ed67SGreg Roach * The URL to a page where the user can modify the configuration of this module. 6976692c8bSGreg Roach * 70a5f7ed67SGreg Roach * @return string 7176692c8bSGreg Roach */ 728f53f488SRico Sonntag public function getConfigLink(): string 73c1010edaSGreg Roach { 74c1010edaSGreg Roach return route('module', [ 75c1010edaSGreg Roach 'module' => $this->getName(), 76c1010edaSGreg Roach 'action' => 'Admin', 77c1010edaSGreg Roach ]); 788c2e8227SGreg Roach } 798c2e8227SGreg Roach 808c2e8227SGreg Roach /** 81a5f7ed67SGreg Roach * @return Response 828c2e8227SGreg Roach */ 8336e59714SGreg Roach public function getAdminAction(): Response 84c1010edaSGreg Roach { 85a5f7ed67SGreg Roach $this->layout = 'layouts/administration'; 86a5f7ed67SGreg Roach 87c1010edaSGreg Roach $sitemap_url = route('module', [ 88c1010edaSGreg Roach 'module' => 'sitemap', 89c1010edaSGreg Roach 'action' => 'Index', 90c1010edaSGreg Roach ]); 91a5f7ed67SGreg Roach 92a5f7ed67SGreg Roach // This list comes from http://en.wikipedia.org/wiki/Sitemaps 93a5f7ed67SGreg Roach $submit_urls = [ 94a5f7ed67SGreg Roach 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 95a5f7ed67SGreg Roach 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 96a5f7ed67SGreg Roach ]; 97a5f7ed67SGreg Roach 98291c1b19SGreg Roach return $this->viewResponse('modules/sitemap/config', [ 99a5f7ed67SGreg Roach 'all_trees' => Tree::getAll(), 100a5f7ed67SGreg Roach 'sitemap_url' => $sitemap_url, 101a5f7ed67SGreg Roach 'submit_urls' => $submit_urls, 102a5f7ed67SGreg Roach 'title' => $this->getTitle(), 103a5f7ed67SGreg Roach ]); 1048c2e8227SGreg Roach } 1058c2e8227SGreg Roach 1068c2e8227SGreg Roach /** 107a5f7ed67SGreg Roach * @param Request $request 108a5f7ed67SGreg Roach * 109a5f7ed67SGreg Roach * @return RedirectResponse 1108c2e8227SGreg Roach */ 111c1010edaSGreg Roach public function postAdminAction(Request $request): RedirectResponse 112c1010edaSGreg Roach { 1138c2e8227SGreg Roach foreach (Tree::getAll() as $tree) { 11472cf66d4SGreg Roach $include_in_sitemap = (bool) $request->get('sitemap' . $tree->id()); 115a5f7ed67SGreg Roach $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 1168c2e8227SGreg Roach } 117a5f7ed67SGreg Roach 118291c1b19SGreg Roach FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->getTitle()), 'success'); 119a5f7ed67SGreg Roach 120a5f7ed67SGreg Roach return new RedirectResponse($this->getConfigLink()); 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 1238c2e8227SGreg Roach /** 124a5f7ed67SGreg Roach * @return Response 1258c2e8227SGreg Roach */ 12636e59714SGreg Roach public function getIndexAction(): Response 127c1010edaSGreg Roach { 128a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap.timestamp'); 129a5f7ed67SGreg Roach 130a5f7ed67SGreg Roach if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) { 131a5f7ed67SGreg Roach $content = $this->getPreference('sitemap.xml'); 1328c2e8227SGreg Roach } else { 133fa17fb66SGreg Roach $count_individuals = DB::table('individuals') 134fa17fb66SGreg Roach ->groupBy('i_file') 135fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'i_file']) 136fa17fb66SGreg Roach ->pluck('total', 'i_file'); 137a5f7ed67SGreg Roach 138fa17fb66SGreg Roach $count_media = DB::table('media') 139fa17fb66SGreg Roach ->groupBy('m_file') 140fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'm_file']) 141fa17fb66SGreg Roach ->pluck('total', 'm_file'); 142a5f7ed67SGreg Roach 143fa17fb66SGreg Roach $count_notes = DB::table('other') 144fa17fb66SGreg Roach ->where('o_type', '=', 'NOTE') 145fa17fb66SGreg Roach ->groupBy('o_file') 146fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 147fa17fb66SGreg Roach ->pluck('total', 'o_file'); 148a5f7ed67SGreg Roach 149fa17fb66SGreg Roach $count_repositories = DB::table('other') 150fa17fb66SGreg Roach ->where('o_type', '=', 'REPO') 151fa17fb66SGreg Roach ->groupBy('o_file') 152fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 153fa17fb66SGreg Roach ->pluck('total', 'o_file'); 154a5f7ed67SGreg Roach 155fa17fb66SGreg Roach $count_sources = DB::table('sources') 156fa17fb66SGreg Roach ->groupBy('s_file') 157fa17fb66SGreg Roach ->select([DB::raw('COUNT(*) AS total'), 's_file']) 158fa17fb66SGreg Roach ->pluck('total', 's_file'); 159a5f7ed67SGreg Roach 160a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-index.xml', [ 161a5f7ed67SGreg Roach 'all_trees' => Tree::getAll(), 162a5f7ed67SGreg Roach 'count_individuals' => $count_individuals, 163a5f7ed67SGreg Roach 'count_media' => $count_media, 164a5f7ed67SGreg Roach 'count_notes' => $count_notes, 165a5f7ed67SGreg Roach 'count_repositories' => $count_repositories, 166a5f7ed67SGreg Roach 'count_sources' => $count_sources, 167a5f7ed67SGreg Roach 'last_mod' => date('Y-m-d'), 168a5f7ed67SGreg Roach 'records_per_volume' => self::RECORDS_PER_VOLUME, 169a5f7ed67SGreg Roach ]); 170a5f7ed67SGreg Roach 171a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 172a5f7ed67SGreg Roach } 173a5f7ed67SGreg Roach 174a5f7ed67SGreg Roach return new Response($content, Response::HTTP_OK, [ 175a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 176a5f7ed67SGreg Roach ]); 177a5f7ed67SGreg Roach } 178a5f7ed67SGreg Roach 179a5f7ed67SGreg Roach /** 180a5f7ed67SGreg Roach * @param Request $request 181a5f7ed67SGreg Roach * 182a5f7ed67SGreg Roach * @return Response 183a5f7ed67SGreg Roach */ 184c1010edaSGreg Roach public function getFileAction(Request $request): Response 185c1010edaSGreg Roach { 186a5f7ed67SGreg Roach $file = $request->get('file', ''); 187a5f7ed67SGreg Roach 188a5f7ed67SGreg Roach if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) { 189a5f7ed67SGreg Roach throw new NotFoundHttpException('Bad sitemap file'); 190a5f7ed67SGreg Roach } 191a5f7ed67SGreg Roach 192a5f7ed67SGreg Roach $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp'); 193a5f7ed67SGreg Roach 194a5f7ed67SGreg Roach if ($timestamp > WT_TIMESTAMP - self::CACHE_LIFE) { 195a5f7ed67SGreg Roach $content = $this->getPreference('sitemap-' . $file . '.xml'); 196a5f7ed67SGreg Roach } else { 197a5f7ed67SGreg Roach $tree = Tree::findById((int) $match[1]); 198a5f7ed67SGreg Roach 199a5f7ed67SGreg Roach if ($tree === null) { 200a5f7ed67SGreg Roach throw new NotFoundHttpException('No such tree'); 201a5f7ed67SGreg Roach } 202a5f7ed67SGreg Roach 203bdb3725aSGreg Roach $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]); 204a5f7ed67SGreg Roach 205a37bbafbSGreg Roach $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]); 206a5f7ed67SGreg Roach 207a5f7ed67SGreg Roach $this->setPreference('sitemap.xml', $content); 208a5f7ed67SGreg Roach } 209a5f7ed67SGreg Roach 210a5f7ed67SGreg Roach return new Response($content, Response::HTTP_OK, [ 211a5f7ed67SGreg Roach 'Content-Type' => 'application/xml', 212a5f7ed67SGreg Roach ]); 213a5f7ed67SGreg Roach } 214a5f7ed67SGreg Roach 215a5f7ed67SGreg Roach /** 216a5f7ed67SGreg Roach * @param Tree $tree 217a5f7ed67SGreg Roach * @param string $type 218a5f7ed67SGreg Roach * @param int $limit 219a5f7ed67SGreg Roach * @param int $offset 220a5f7ed67SGreg Roach * 221886b77daSGreg Roach * @return Collection|GedcomRecord[] 222a5f7ed67SGreg Roach */ 223886b77daSGreg Roach private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection 224c1010edaSGreg Roach { 225a5f7ed67SGreg Roach switch ($type) { 2268c2e8227SGreg Roach case 'i': 227a5f7ed67SGreg Roach $records = $this->sitemapIndividuals($tree, $limit, $offset); 228a5f7ed67SGreg Roach break; 229a5f7ed67SGreg Roach 230a5f7ed67SGreg Roach case 'm': 231a5f7ed67SGreg Roach $records = $this->sitemapMedia($tree, $limit, $offset); 232a5f7ed67SGreg Roach break; 233a5f7ed67SGreg Roach 234a5f7ed67SGreg Roach case 'n': 235a5f7ed67SGreg Roach $records = $this->sitemapNotes($tree, $limit, $offset); 236a5f7ed67SGreg Roach break; 237a5f7ed67SGreg Roach 238a5f7ed67SGreg Roach case 'r': 239a5f7ed67SGreg Roach $records = $this->sitemapRepositories($tree, $limit, $offset); 240a5f7ed67SGreg Roach break; 241a5f7ed67SGreg Roach 242a5f7ed67SGreg Roach case 's': 243a5f7ed67SGreg Roach $records = $this->sitemapSources($tree, $limit, $offset); 244a5f7ed67SGreg Roach break; 245a5f7ed67SGreg Roach 246a5f7ed67SGreg Roach default: 247a5f7ed67SGreg Roach throw new NotFoundHttpException('Invalid record type: ' . $type); 248a5f7ed67SGreg Roach } 249a5f7ed67SGreg Roach 250a5f7ed67SGreg Roach // Skip private records. 251886b77daSGreg Roach $records = $records->filter(GedcomRecord::filter()); 252a5f7ed67SGreg Roach 253a5f7ed67SGreg Roach return $records; 254a5f7ed67SGreg Roach } 255a5f7ed67SGreg Roach 256a5f7ed67SGreg Roach /** 257a5f7ed67SGreg Roach * @param Tree $tree 258a5f7ed67SGreg Roach * @param int $limit 259a5f7ed67SGreg Roach * @param int $offset 260a5f7ed67SGreg Roach * 261886b77daSGreg Roach * @return Collection|Individual[] 262a5f7ed67SGreg Roach */ 263886b77daSGreg Roach private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection 264c1010edaSGreg Roach { 265886b77daSGreg Roach return DB::table('individuals') 266fa17fb66SGreg Roach ->where('i_file', '=', $tree->id()) 267fa17fb66SGreg Roach ->orderBy('i_id') 268fa17fb66SGreg Roach ->skip($offset) 269fa17fb66SGreg Roach ->take($limit) 270886b77daSGreg Roach ->get() 271*c0804649SGreg Roach ->map(Individual::rowMapper()); 2728c2e8227SGreg Roach } 273a5f7ed67SGreg Roach 274a5f7ed67SGreg Roach /** 275a5f7ed67SGreg Roach * @param Tree $tree 276a5f7ed67SGreg Roach * @param int $limit 277a5f7ed67SGreg Roach * @param int $offset 278a5f7ed67SGreg Roach * 279886b77daSGreg Roach * @return Collection|Media[] 280a5f7ed67SGreg Roach */ 281886b77daSGreg Roach private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection 282c1010edaSGreg Roach { 283886b77daSGreg Roach return DB::table('media') 284fa17fb66SGreg Roach ->where('m_file', '=', $tree->id()) 285fa17fb66SGreg Roach ->orderBy('m_id') 286fa17fb66SGreg Roach ->skip($offset) 287fa17fb66SGreg Roach ->take($limit) 288886b77daSGreg Roach ->get() 289*c0804649SGreg Roach ->map(Media::rowMapper()); 2908c2e8227SGreg Roach } 2918c2e8227SGreg Roach 2928c2e8227SGreg Roach /** 293a5f7ed67SGreg Roach * @param Tree $tree 294a5f7ed67SGreg Roach * @param int $limit 295a5f7ed67SGreg Roach * @param int $offset 296a5f7ed67SGreg Roach * 297886b77daSGreg Roach * @return Collection|Note[] 2988c2e8227SGreg Roach */ 299886b77daSGreg Roach private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection 300c1010edaSGreg Roach { 301886b77daSGreg Roach return DB::table('other') 302fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 303fa17fb66SGreg Roach ->where('o_type', '=', 'NOTE') 304fa17fb66SGreg Roach ->orderBy('o_id') 305fa17fb66SGreg Roach ->skip($offset) 306fa17fb66SGreg Roach ->take($limit) 307886b77daSGreg Roach ->get() 308*c0804649SGreg Roach ->map(Note::rowMapper()); 3098c2e8227SGreg Roach } 3108c2e8227SGreg Roach 311a5f7ed67SGreg Roach /** 312a5f7ed67SGreg Roach * @param Tree $tree 313a5f7ed67SGreg Roach * @param int $limit 314a5f7ed67SGreg Roach * @param int $offset 315a5f7ed67SGreg Roach * 316886b77daSGreg Roach * @return Collection|Repository[] 317a5f7ed67SGreg Roach */ 318886b77daSGreg Roach private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection 319c1010edaSGreg Roach { 320886b77daSGreg Roach return DB::table('other') 321fa17fb66SGreg Roach ->where('o_file', '=', $tree->id()) 322fa17fb66SGreg Roach ->where('o_type', '=', 'REPO') 323fa17fb66SGreg Roach ->orderBy('o_id') 324fa17fb66SGreg Roach ->skip($offset) 325fa17fb66SGreg Roach ->take($limit) 326886b77daSGreg Roach ->get() 327*c0804649SGreg Roach ->map(Repository::rowMapper()); 328a5f7ed67SGreg Roach } 329a5f7ed67SGreg Roach 330a5f7ed67SGreg Roach /** 331a5f7ed67SGreg Roach * @param Tree $tree 332a5f7ed67SGreg Roach * @param int $limit 333a5f7ed67SGreg Roach * @param int $offset 334a5f7ed67SGreg Roach * 335886b77daSGreg Roach * @return Collection|Source[] 336a5f7ed67SGreg Roach */ 337886b77daSGreg Roach private function sitemapSources(Tree $tree, int $limit, int $offset): Collection 338c1010edaSGreg Roach { 339886b77daSGreg Roach return DB::table('sources') 340fa17fb66SGreg Roach ->where('s_file', '=', $tree->id()) 341fa17fb66SGreg Roach ->orderBy('s_id') 342fa17fb66SGreg Roach ->skip($offset) 343fa17fb66SGreg Roach ->take($limit) 344886b77daSGreg Roach ->get() 345*c0804649SGreg Roach ->map(Source::rowMapper()); 3468c2e8227SGreg Roach } 3478c2e8227SGreg Roach} 348