-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathSiteMapModule.php
476 lines (415 loc) · 15.6 KB
/
SiteMapModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Module;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Submitter;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Validator;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Collection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function date;
use function redirect;
use function response;
use function route;
use function view;
class SiteMapModule extends AbstractModule implements ModuleConfigInterface, RequestHandlerInterface
{
use ModuleConfigTrait;
private const int RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
private const int CACHE_LIFE = 209600; // Two weeks
private const array PRIORITY = [
Family::RECORD_TYPE => 0.7,
Individual::RECORD_TYPE => 0.9,
Media::RECORD_TYPE => 0.5,
Note::RECORD_TYPE => 0.3,
Repository::RECORD_TYPE => 0.5,
Source::RECORD_TYPE => 0.5,
Submitter::RECORD_TYPE => 0.3,
];
private TreeService $tree_service;
/**
* @param TreeService $tree_service
*/
public function __construct(TreeService $tree_service)
{
$this->tree_service = $tree_service;
}
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
Registry::routeFactory()->routeMap()
->get('sitemap-style', '/sitemap.xsl', $this);
Registry::routeFactory()->routeMap()
->get('sitemap-index', '/sitemap.xml', $this);
Registry::routeFactory()->routeMap()
->get('sitemap-file', '/sitemap-{tree}-{type}-{page}.xml', $this);
}
public function description(): string
{
/* I18N: Description of the “Sitemaps” module */
return I18N::translate('Generate sitemap files for search engines.');
}
/**
* Should this module be enabled when it is first installed?
*
* @return bool
*/
public function isEnabledByDefault(): bool
{
return false;
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function getAdminAction(ServerRequestInterface $request): ResponseInterface
{
$this->layout = 'layouts/administration';
$sitemap_url = route('sitemap-index');
return $this->viewResponse('modules/sitemap/config', [
'all_trees' => $this->tree_service->all(),
'sitemap_url' => $sitemap_url,
'title' => $this->title(),
]);
}
/**
* How should this module be identified in the control panel, etc.?
*
* @return string
*/
public function title(): string
{
/* I18N: Name of a module - see https://en.wikipedia.org/wiki/Sitemaps */
return I18N::translate('Sitemaps');
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
foreach ($this->tree_service->all() as $tree) {
$include_in_sitemap = Validator::parsedBody($request)->boolean('sitemap' . $tree->id(), false);
$tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
}
FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
return redirect($this->getConfigLink());
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$route = Validator::attributes($request)->route();
if ($route->name === 'sitemap-style') {
$content = view('modules/sitemap/sitemap-xsl');
return response($content, StatusCodeInterface::STATUS_OK, [
'content-type' => 'application/xml',
]);
}
if ($route->name === 'sitemap-index') {
return $this->siteMapIndex($request);
}
return $this->siteMapFile($request);
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
private function siteMapIndex(ServerRequestInterface $request): ResponseInterface
{
$content = Registry::cache()->file()->remember('sitemap.xml', function (): string {
// Which trees have sitemaps enabled?
$tree_ids = $this->tree_service->all()
->filter(static fn (Tree $tree): bool => $tree->getPreference('include_in_sitemap') === '1')
->map(static fn (Tree $tree): int => $tree->id());
$count_families = DB::table('families')
->join('gedcom', 'f_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
$count_individuals = DB::table('individuals')
->join('gedcom', 'i_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
$count_media = DB::table('media')
->join('gedcom', 'm_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
$count_notes = DB::table('other')
->join('gedcom', 'o_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->where('o_type', '=', Note::RECORD_TYPE)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
$count_repositories = DB::table('other')
->join('gedcom', 'o_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->where('o_type', '=', Repository::RECORD_TYPE)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
$count_sources = DB::table('sources')
->join('gedcom', 's_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
$count_submitters = DB::table('other')
->join('gedcom', 'o_file', '=', 'gedcom_id')
->whereIn('gedcom_id', $tree_ids)
->where('o_type', '=', Submitter::RECORD_TYPE)
->groupBy(['gedcom_id'])
->pluck(new Expression('COUNT(*) AS total'), 'gedcom_name');
// Versions 2.0.1 and earlier of this module stored large amounts of data in the settings.
DB::table('module_setting')
->where('module_name', '=', $this->name())
->delete();
return view('modules/sitemap/sitemap-index-xml', [
'all_trees' => $this->tree_service->all(),
'count_families' => $count_families,
'count_individuals' => $count_individuals,
'count_media' => $count_media,
'count_notes' => $count_notes,
'count_repositories' => $count_repositories,
'count_sources' => $count_sources,
'count_submitters' => $count_submitters,
'last_mod' => date('Y-m-d'),
'records_per_volume' => self::RECORDS_PER_VOLUME,
'sitemap_xsl' => route('sitemap-style'),
]);
}, self::CACHE_LIFE);
return response($content, StatusCodeInterface::STATUS_OK, [
'content-type' => 'application/xml',
]);
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
private function siteMapFile(ServerRequestInterface $request): ResponseInterface
{
$tree = Validator::attributes($request)->tree('tree');
$type = Validator::attributes($request)->string('type');
$page = Validator::attributes($request)->integer('page');
if ($tree->getPreference('include_in_sitemap') !== '1') {
throw new HttpNotFoundException();
}
$cache_key = 'sitemap/' . $tree->id() . '/' . $type . '/' . $page . '.xml';
$content = Registry::cache()->file()->remember($cache_key, function () use ($tree, $type, $page): string {
$records = $this->sitemapRecords($tree, $type, self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $page);
return view('modules/sitemap/sitemap-file-xml', [
'priority' => self::PRIORITY[$type],
'records' => $records,
'sitemap_xsl' => route('sitemap-style'),
'tree' => $tree,
]);
}, self::CACHE_LIFE);
return response($content, StatusCodeInterface::STATUS_OK, [
'content-type' => 'application/xml',
]);
}
/**
* @param Tree $tree
* @param string $type
* @param int $limit
* @param int $offset
*
* @return Collection<int,GedcomRecord>
*/
private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection
{
switch ($type) {
case Family::RECORD_TYPE:
$records = $this->sitemapFamilies($tree, $limit, $offset);
break;
case Individual::RECORD_TYPE:
$records = $this->sitemapIndividuals($tree, $limit, $offset);
break;
case Media::RECORD_TYPE:
$records = $this->sitemapMedia($tree, $limit, $offset);
break;
case Note::RECORD_TYPE:
$records = $this->sitemapNotes($tree, $limit, $offset);
break;
case Repository::RECORD_TYPE:
$records = $this->sitemapRepositories($tree, $limit, $offset);
break;
case Source::RECORD_TYPE:
$records = $this->sitemapSources($tree, $limit, $offset);
break;
case Submitter::RECORD_TYPE:
$records = $this->sitemapSubmitters($tree, $limit, $offset);
break;
default:
throw new HttpNotFoundException('Invalid record type: ' . $type);
}
// Skip private records.
$records = $records->filter(static fn (GedcomRecord $record): bool => $record->canShow(Auth::PRIV_PRIVATE));
return $records;
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Family>
*/
private function sitemapFamilies(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('families')
->where('f_file', '=', $tree->id())
->orderBy('f_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::familyFactory()->mapper($tree));
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Individual>
*/
private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('individuals')
->where('i_file', '=', $tree->id())
->orderBy('i_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::individualFactory()->mapper($tree));
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Media>
*/
private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('media')
->where('m_file', '=', $tree->id())
->orderBy('m_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::mediaFactory()->mapper($tree));
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Note>
*/
private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Note::RECORD_TYPE)
->orderBy('o_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::noteFactory()->mapper($tree));
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Repository>
*/
private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Repository::RECORD_TYPE)
->orderBy('o_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::repositoryFactory()->mapper($tree));
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Source>
*/
private function sitemapSources(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('sources')
->where('s_file', '=', $tree->id())
->orderBy('s_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::sourceFactory()->mapper($tree));
}
/**
* @param Tree $tree
* @param int $limit
* @param int $offset
*
* @return Collection<int,Submitter>
*/
private function sitemapSubmitters(Tree $tree, int $limit, int $offset): Collection
{
return DB::table('other')
->where('o_file', '=', $tree->id())
->where('o_type', '=', Submitter::RECORD_TYPE)
->orderBy('o_id')
->skip($offset)
->take($limit)
->get()
->map(Registry::submitterFactory()->mapper($tree));
}
}