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 Fig\Http\Message\StatusCodeInterface; 21use Fisharebest\Webtrees\Carbon; 22use Fisharebest\Webtrees\FlashMessages; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\Html; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Media; 28use Fisharebest\Webtrees\Note; 29use Fisharebest\Webtrees\Repository; 30use Fisharebest\Webtrees\Source; 31use Fisharebest\Webtrees\Tree; 32use Illuminate\Database\Capsule\Manager as DB; 33use Illuminate\Support\Collection; 34use Psr\Http\Message\ResponseInterface; 35use Psr\Http\Message\ServerRequestInterface; 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 * A sentence describing what this module does. 50 * 51 * @return string 52 */ 53 public function description(): string 54 { 55 /* I18N: Description of the “Sitemaps” module */ 56 return I18N::translate('Generate sitemap files for search engines.'); 57 } 58 59 /** 60 * Should this module be enabled when it is first installed? 61 * 62 * @return bool 63 */ 64 public function isEnabledByDefault(): bool 65 { 66 return false; 67 } 68 69 /** 70 * @return ResponseInterface 71 */ 72 public function getAdminAction(): ResponseInterface 73 { 74 $this->layout = 'layouts/administration'; 75 76 $sitemap_url = route('module', [ 77 'module' => $this->name(), 78 'action' => 'Index', 79 ]); 80 81 // This list comes from http://en.wikipedia.org/wiki/Sitemaps 82 $submit_urls = [ 83 'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]), 84 'Google' => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]), 85 ]; 86 87 return $this->viewResponse('modules/sitemap/config', [ 88 'all_trees' => Tree::all(), 89 'sitemap_url' => $sitemap_url, 90 'submit_urls' => $submit_urls, 91 'title' => $this->title(), 92 ]); 93 } 94 95 /** 96 * How should this module be identified in the control panel, etc.? 97 * 98 * @return string 99 */ 100 public function title(): string 101 { 102 /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */ 103 return I18N::translate('Sitemaps'); 104 } 105 106 /** 107 * @param ServerRequestInterface $request 108 * 109 * @return ResponseInterface 110 */ 111 public function postAdminAction(ServerRequestInterface $request): ResponseInterface 112 { 113 $params = $request->getParsedBody(); 114 115 foreach (Tree::all() as $tree) { 116 $include_in_sitemap = (bool) ($params['sitemap' . $tree->id()] ?? false); 117 $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap); 118 } 119 120 FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success'); 121 122 return redirect($this->getConfigLink()); 123 } 124 125 /** 126 * @return ResponseInterface 127 */ 128 public function getIndexAction(): ResponseInterface 129 { 130 $timestamp = (int) $this->getPreference('sitemap.timestamp'); 131 132 if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) { 133 $content = $this->getPreference('sitemap.xml'); 134 } else { 135 $count_individuals = DB::table('individuals') 136 ->groupBy('i_file') 137 ->select([DB::raw('COUNT(*) AS total'), 'i_file']) 138 ->pluck('total', 'i_file'); 139 140 $count_media = DB::table('media') 141 ->groupBy('m_file') 142 ->select([DB::raw('COUNT(*) AS total'), 'm_file']) 143 ->pluck('total', 'm_file'); 144 145 $count_notes = DB::table('other') 146 ->where('o_type', '=', 'NOTE') 147 ->groupBy('o_file') 148 ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 149 ->pluck('total', 'o_file'); 150 151 $count_repositories = DB::table('other') 152 ->where('o_type', '=', 'REPO') 153 ->groupBy('o_file') 154 ->select([DB::raw('COUNT(*) AS total'), 'o_file']) 155 ->pluck('total', 'o_file'); 156 157 $count_sources = DB::table('sources') 158 ->groupBy('s_file') 159 ->select([DB::raw('COUNT(*) AS total'), 's_file']) 160 ->pluck('total', 's_file'); 161 162 $content = view('modules/sitemap/sitemap-index.xml', [ 163 'all_trees' => Tree::all(), 164 'count_individuals' => $count_individuals, 165 'count_media' => $count_media, 166 'count_notes' => $count_notes, 167 'count_repositories' => $count_repositories, 168 'count_sources' => $count_sources, 169 'last_mod' => date('Y-m-d'), 170 'records_per_volume' => self::RECORDS_PER_VOLUME, 171 ]); 172 173 $this->setPreference('sitemap.xml', $content); 174 } 175 176 return response($content, StatusCodeInterface::STATUS_OK, [ 177 'Content-Type' => 'application/xml', 178 ]); 179 } 180 181 /** 182 * @param ServerRequestInterface $request 183 * 184 * @return ResponseInterface 185 */ 186 public function getFileAction(ServerRequestInterface $request): ResponseInterface 187 { 188 $file = $request->getQueryParams()['file']; 189 190 if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) { 191 throw new NotFoundHttpException('Bad sitemap file'); 192 } 193 194 $timestamp = (int) $this->getPreference('sitemap-' . $file . '.timestamp'); 195 $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix(); 196 197 if ($timestamp > $expiry_time) { 198 $content = $this->getPreference('sitemap-' . $file . '.xml'); 199 } else { 200 $tree = Tree::findById((int) $match[1]); 201 202 if ($tree === null) { 203 throw new NotFoundHttpException('No such tree'); 204 } 205 206 $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]); 207 208 $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]); 209 210 $this->setPreference('sitemap.xml', $content); 211 } 212 213 return response($content, StatusCodeInterface::STATUS_OK, [ 214 'Content-Type' => 'application/xml', 215 ]); 216 } 217 218 /** 219 * @param Tree $tree 220 * @param string $type 221 * @param int $limit 222 * @param int $offset 223 * 224 * @return Collection 225 */ 226 private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection 227 { 228 switch ($type) { 229 case 'i': 230 $records = $this->sitemapIndividuals($tree, $limit, $offset); 231 break; 232 233 case 'm': 234 $records = $this->sitemapMedia($tree, $limit, $offset); 235 break; 236 237 case 'n': 238 $records = $this->sitemapNotes($tree, $limit, $offset); 239 break; 240 241 case 'r': 242 $records = $this->sitemapRepositories($tree, $limit, $offset); 243 break; 244 245 case 's': 246 $records = $this->sitemapSources($tree, $limit, $offset); 247 break; 248 249 default: 250 throw new NotFoundHttpException('Invalid record type: ' . $type); 251 } 252 253 // Skip private records. 254 $records = $records->filter(GedcomRecord::accessFilter()); 255 256 return $records; 257 } 258 259 /** 260 * @param Tree $tree 261 * @param int $limit 262 * @param int $offset 263 * 264 * @return Collection 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 */ 284 private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection 285 { 286 return DB::table('media') 287 ->where('m_file', '=', $tree->id()) 288 ->orderBy('m_id') 289 ->skip($offset) 290 ->take($limit) 291 ->get() 292 ->map(Media::rowMapper()); 293 } 294 295 /** 296 * @param Tree $tree 297 * @param int $limit 298 * @param int $offset 299 * 300 * @return Collection 301 */ 302 private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection 303 { 304 return DB::table('other') 305 ->where('o_file', '=', $tree->id()) 306 ->where('o_type', '=', 'NOTE') 307 ->orderBy('o_id') 308 ->skip($offset) 309 ->take($limit) 310 ->get() 311 ->map(Note::rowMapper()); 312 } 313 314 /** 315 * @param Tree $tree 316 * @param int $limit 317 * @param int $offset 318 * 319 * @return Collection 320 */ 321 private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection 322 { 323 return DB::table('other') 324 ->where('o_file', '=', $tree->id()) 325 ->where('o_type', '=', 'REPO') 326 ->orderBy('o_id') 327 ->skip($offset) 328 ->take($limit) 329 ->get() 330 ->map(Repository::rowMapper()); 331 } 332 333 /** 334 * @param Tree $tree 335 * @param int $limit 336 * @param int $offset 337 * 338 * @return Collection 339 */ 340 private function sitemapSources(Tree $tree, int $limit, int $offset): Collection 341 { 342 return DB::table('sources') 343 ->where('s_file', '=', $tree->id()) 344 ->orderBy('s_id') 345 ->skip($offset) 346 ->take($limit) 347 ->get() 348 ->map(Source::rowMapper()); 349 } 350} 351