1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Carbon; 21use Fisharebest\Webtrees\FlashMessages; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\Html; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Media; 27use Fisharebest\Webtrees\Note; 28use Fisharebest\Webtrees\Repository; 29use Fisharebest\Webtrees\Source; 30use Fisharebest\Webtrees\Tree; 31use Illuminate\Database\Capsule\Manager as DB; 32use Illuminate\Support\Collection; 33use Symfony\Component\HttpFoundation\RedirectResponse; 34use Symfony\Component\HttpFoundation\Request; 35use Symfony\Component\HttpFoundation\Response; 36use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 37 38/** 39 * Class SiteMapModule 40 */ 41class SiteMapModule extends AbstractModule implements ModuleConfigInterface 42{ 43 use ModuleConfigTrait; 44 45 private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits. 46 private const CACHE_LIFE = 1209600; // Two weeks 47 48 /** 49 * How should this module be identified in the control panel, etc.? 50 * 51 * @return string 52 */ 53 public function title(): string 54 { 55 /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */ 56 return I18N::translate('Sitemaps'); 57 } 58 59 /** 60 * A sentence describing what this module does. 61 * 62 * @return string 63 */ 64 public function description(): string 65 { 66 /* I18N: Description of the “Sitemaps” module */ 67 return I18N::translate('Generate sitemap files for search engines.'); 68 } 69 70 /** 71 * Should this module be enabled when it is first installed? 72 * 73 * @return bool 74 */ 75 public function isEnabledByDefault(): bool 76 { 77 return false; 78 } 79 80 /** 81 * @return Response 82 */ 83 public function getAdminAction(): Response 84 { 85 $this->layout = 'layouts/administration'; 86 87 $sitemap_url = route('module', [ 88 'module' => $this->name(), 89 'action' => 'Index', 90 ]); 91 92 // This list comes from http://en.wikipedia.org/wiki/Sitemaps 93 $submit_urls = [ 94 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 95 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 96 ]; 97 98 return $this->viewResponse('modules/sitemap/config', [ 99 'all_trees' => Tree::all(), 100 'sitemap_url' => $sitemap_url, 101 'submit_urls' => $submit_urls, 102 'title' => $this->title(), 103 ]); 104 } 105 106 /** 107 * @param Request $request 108 * 109 * @return RedirectResponse 110 */ 111 public function postAdminAction(Request $request): RedirectResponse 112 { 113 foreach (Tree::all() as $tree) { 114 $include_in_sitemap = (bool) $request->get('sitemap' . $tree->id()); 115 $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 116 } 117 118 FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 119 120 return new RedirectResponse($this->getConfigLink()); 121 } 122 123 /** 124 * @return Response 125 */ 126 public function getIndexAction(): Response 127 { 128 $timestamp = (int) $this->getPreference('sitemap.timestamp'); 129 130 if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) { 131 $content = $this->getPreference('sitemap.xml'); 132 } else { 133 $count_individuals = DB::table('individuals') 134 ->groupBy('i_file') 135 ->select([DB::raw('COUNT(*) AS total'), 'i_file']) 136 ->pluck('total', 'i_file'); 137 138 $count_media = DB::table('media') 139 ->groupBy('m_file') 140 ->select([DB::raw('COUNT(*) AS total'), 'm_file']) 141 ->pluck('total', 'm_file'); 142 143 $count_notes = DB::table('other') 144 ->where('o_type', '=', 'NOTE') 145 ->groupBy('o_file') 146 ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 147 ->pluck('total', 'o_file'); 148 149 $count_repositories = DB::table('other') 150 ->where('o_type', '=', 'REPO') 151 ->groupBy('o_file') 152 ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 153 ->pluck('total', 'o_file'); 154 155 $count_sources = DB::table('sources') 156 ->groupBy('s_file') 157 ->select([DB::raw('COUNT(*) AS total'), 's_file']) 158 ->pluck('total', 's_file'); 159 160 $content = view('modules/sitemap/sitemap-index.xml', [ 161 'all_trees' => Tree::all(), 162 'count_individuals' => $count_individuals, 163 'count_media' => $count_media, 164 'count_notes' => $count_notes, 165 'count_repositories' => $count_repositories, 166 'count_sources' => $count_sources, 167 'last_mod' => date('Y-m-d'), 168 'records_per_volume' => self::RECORDS_PER_VOLUME, 169 ]); 170 171 $this->setPreference('sitemap.xml', $content); 172 } 173 174 return new Response($content, Response::HTTP_OK, [ 175 'Content-Type' => 'application/xml', 176 ]); 177 } 178 179 /** 180 * @param Request $request 181 * 182 * @return Response 183 */ 184 public function getFileAction(Request $request): Response 185 { 186 $file = $request->get('file', ''); 187 188 if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) { 189 throw new NotFoundHttpException('Bad sitemap file'); 190 } 191 192 $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp'); 193 $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix(); 194 195 if ($timestamp > $expiry_time) { 196 $content = $this->getPreference('sitemap-' . $file . '.xml'); 197 } else { 198 $tree = Tree::findById((int) $match[1]); 199 200 if ($tree === null) { 201 throw new NotFoundHttpException('No such tree'); 202 } 203 204 $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]); 205 206 $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]); 207 208 $this->setPreference('sitemap.xml', $content); 209 } 210 211 return new Response($content, Response::HTTP_OK, [ 212 'Content-Type' => 'application/xml', 213 ]); 214 } 215 216 /** 217 * @param Tree $tree 218 * @param string $type 219 * @param int $limit 220 * @param int $offset 221 * 222 * @return Collection 223 * @return GedcomRecord[] 224 */ 225 private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection 226 { 227 switch ($type) { 228 case 'i': 229 $records = $this->sitemapIndividuals($tree, $limit, $offset); 230 break; 231 232 case 'm': 233 $records = $this->sitemapMedia($tree, $limit, $offset); 234 break; 235 236 case 'n': 237 $records = $this->sitemapNotes($tree, $limit, $offset); 238 break; 239 240 case 'r': 241 $records = $this->sitemapRepositories($tree, $limit, $offset); 242 break; 243 244 case 's': 245 $records = $this->sitemapSources($tree, $limit, $offset); 246 break; 247 248 default: 249 throw new NotFoundHttpException('Invalid record type: ' . $type); 250 } 251 252 // Skip private records. 253 $records = $records->filter(GedcomRecord::accessFilter()); 254 255 return $records; 256 } 257 258 /** 259 * @param Tree $tree 260 * @param int $limit 261 * @param int $offset 262 * 263 * @return Collection 264 * @return Individual[] 265 */ 266 private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection 267 { 268 return DB::table('individuals') 269 ->where('i_file', '=', $tree->id()) 270 ->orderBy('i_id') 271 ->skip($offset) 272 ->take($limit) 273 ->get() 274 ->map(Individual::rowMapper()); 275 } 276 277 /** 278 * @param Tree $tree 279 * @param int $limit 280 * @param int $offset 281 * 282 * @return Collection 283 * @return Media[] 284 */ 285 private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection 286 { 287 return DB::table('media') 288 ->where('m_file', '=', $tree->id()) 289 ->orderBy('m_id') 290 ->skip($offset) 291 ->take($limit) 292 ->get() 293 ->map(Media::rowMapper()); 294 } 295 296 /** 297 * @param Tree $tree 298 * @param int $limit 299 * @param int $offset 300 * 301 * @return Collection 302 * @return Note[] 303 */ 304 private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection 305 { 306 return DB::table('other') 307 ->where('o_file', '=', $tree->id()) 308 ->where('o_type', '=', 'NOTE') 309 ->orderBy('o_id') 310 ->skip($offset) 311 ->take($limit) 312 ->get() 313 ->map(Note::rowMapper()); 314 } 315 316 /** 317 * @param Tree $tree 318 * @param int $limit 319 * @param int $offset 320 * 321 * @return Collection 322 * @return Repository[] 323 */ 324 private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection 325 { 326 return DB::table('other') 327 ->where('o_file', '=', $tree->id()) 328 ->where('o_type', '=', 'REPO') 329 ->orderBy('o_id') 330 ->skip($offset) 331 ->take($limit) 332 ->get() 333 ->map(Repository::rowMapper()); 334 } 335 336 /** 337 * @param Tree $tree 338 * @param int $limit 339 * @param int $offset 340 * 341 * @return Collection 342 * @return Source[] 343 */ 344 private function sitemapSources(Tree $tree, int $limit, int $offset): Collection 345 { 346 return DB::table('sources') 347 ->where('s_file', '=', $tree->id()) 348 ->orderBy('s_id') 349 ->skip($offset) 350 ->take($limit) 351 ->get() 352 ->map(Source::rowMapper()); 353 } 354} 355