xref: /webtrees/app/Module/SiteMapModule.php (revision 7f5c2944f84cdc914a20ed1e961f9b81e401f78b)
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
206ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
22a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
23a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24b1b85189SGreg Roachuse Fisharebest\Webtrees\Html;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
32fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
33a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
34886b77daSGreg Roachuse Illuminate\Support\Collection;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
366ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
37a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
388c2e8227SGreg Roach
398c2e8227SGreg Roach/**
408c2e8227SGreg Roach * Class SiteMapModule
418c2e8227SGreg Roach */
4237eb8894SGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleConfigTrait;
4549a243cbSGreg Roach
4616d6367aSGreg Roach    private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
4716d6367aSGreg Roach    private const CACHE_LIFE         = 1209600; // Two weeks
488c2e8227SGreg Roach
49a5f7ed67SGreg Roach    /**
50a5f7ed67SGreg Roach     * A sentence describing what this module does.
51a5f7ed67SGreg Roach     *
52a5f7ed67SGreg Roach     * @return string
53a5f7ed67SGreg Roach     */
5449a243cbSGreg Roach    public function description(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
57bbb76c12SGreg Roach        return I18N::translate('Generate sitemap files for search engines.');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6076692c8bSGreg Roach    /**
61abafa13cSGreg Roach     * Should this module be enabled when it is first installed?
62abafa13cSGreg Roach     *
63abafa13cSGreg Roach     * @return bool
64abafa13cSGreg Roach     */
65abafa13cSGreg Roach    public function isEnabledByDefault(): bool
66abafa13cSGreg Roach    {
67abafa13cSGreg Roach        return false;
68abafa13cSGreg Roach    }
69abafa13cSGreg Roach
70abafa13cSGreg Roach    /**
716ccdf4f0SGreg Roach     * @return ResponseInterface
728c2e8227SGreg Roach     */
736ccdf4f0SGreg Roach    public function getAdminAction(): ResponseInterface
74c1010edaSGreg Roach    {
75a5f7ed67SGreg Roach        $this->layout = 'layouts/administration';
76a5f7ed67SGreg Roach
77c1010edaSGreg Roach        $sitemap_url = route('module', [
7826684e68SGreg Roach            'module' => $this->name(),
79c1010edaSGreg Roach            'action' => 'Index',
80c1010edaSGreg Roach        ]);
81a5f7ed67SGreg Roach
82a5f7ed67SGreg Roach        // This list comes from http://en.wikipedia.org/wiki/Sitemaps
83a5f7ed67SGreg Roach        $submit_urls = [
84a5f7ed67SGreg Roach            'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]),
85a5f7ed67SGreg Roach            'Google'     => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]),
86a5f7ed67SGreg Roach        ];
87a5f7ed67SGreg Roach
88291c1b19SGreg Roach        return $this->viewResponse('modules/sitemap/config', [
898b67c11aSGreg Roach            'all_trees'   => Tree::all(),
90a5f7ed67SGreg Roach            'sitemap_url' => $sitemap_url,
91a5f7ed67SGreg Roach            'submit_urls' => $submit_urls,
9249a243cbSGreg Roach            'title'       => $this->title(),
93a5f7ed67SGreg Roach        ]);
948c2e8227SGreg Roach    }
958c2e8227SGreg Roach
968c2e8227SGreg Roach    /**
976ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
98a5f7ed67SGreg Roach     *
996ccdf4f0SGreg Roach     * @return string
1008c2e8227SGreg Roach     */
1016ccdf4f0SGreg Roach    public function title(): string
1026ccdf4f0SGreg Roach    {
1036ccdf4f0SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
1046ccdf4f0SGreg Roach        return I18N::translate('Sitemaps');
1056ccdf4f0SGreg Roach    }
1066ccdf4f0SGreg Roach
1076ccdf4f0SGreg Roach    /**
1086ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1096ccdf4f0SGreg Roach     *
1106ccdf4f0SGreg Roach     * @return ResponseInterface
1116ccdf4f0SGreg Roach     */
1126ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
113c1010edaSGreg Roach    {
114b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
115b6b9dcc9SGreg Roach
1168b67c11aSGreg Roach        foreach (Tree::all() as $tree) {
117b6b9dcc9SGreg Roach            $include_in_sitemap = (bool) ($params['sitemap' . $tree->id()] ?? false);
118a5f7ed67SGreg Roach            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
1198c2e8227SGreg Roach        }
120a5f7ed67SGreg Roach
12149a243cbSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
122a5f7ed67SGreg Roach
1236ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
1248c2e8227SGreg Roach    }
1258c2e8227SGreg Roach
1268c2e8227SGreg Roach    /**
1276ccdf4f0SGreg Roach     * @return ResponseInterface
1288c2e8227SGreg Roach     */
1296ccdf4f0SGreg Roach    public function getIndexAction(): ResponseInterface
130c1010edaSGreg Roach    {
131a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap.timestamp');
132a5f7ed67SGreg Roach
1334459dc9aSGreg Roach        if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) {
134a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1358c2e8227SGreg Roach        } else {
136fa17fb66SGreg Roach            $count_individuals = DB::table('individuals')
137*7f5c2944SGreg Roach                ->groupBy(['i_file'])
138a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'i_file'])
139fa17fb66SGreg Roach                ->pluck('total', 'i_file');
140a5f7ed67SGreg Roach
141fa17fb66SGreg Roach            $count_media = DB::table('media')
142*7f5c2944SGreg Roach                ->groupBy(['m_file'])
143a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'm_file'])
144fa17fb66SGreg Roach                ->pluck('total', 'm_file');
145a5f7ed67SGreg Roach
146fa17fb66SGreg Roach            $count_notes = DB::table('other')
147fa17fb66SGreg Roach                ->where('o_type', '=', 'NOTE')
148*7f5c2944SGreg Roach                ->groupBy(['o_file'])
149a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'o_file'])
150fa17fb66SGreg Roach                ->pluck('total', 'o_file');
151a5f7ed67SGreg Roach
152fa17fb66SGreg Roach            $count_repositories = DB::table('other')
153fa17fb66SGreg Roach                ->where('o_type', '=', 'REPO')
154*7f5c2944SGreg Roach                ->groupBy(['o_file'])
155a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'o_file'])
156fa17fb66SGreg Roach                ->pluck('total', 'o_file');
157a5f7ed67SGreg Roach
158fa17fb66SGreg Roach            $count_sources = DB::table('sources')
159*7f5c2944SGreg Roach                ->groupBy(['s_file'])
160a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 's_file'])
161fa17fb66SGreg Roach                ->pluck('total', 's_file');
162a5f7ed67SGreg Roach
163a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
1648b67c11aSGreg Roach                'all_trees'          => Tree::all(),
165a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
166a5f7ed67SGreg Roach                'count_media'        => $count_media,
167a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
168a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
169a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
170a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
171a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
172a5f7ed67SGreg Roach            ]);
173a5f7ed67SGreg Roach
174a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
175a5f7ed67SGreg Roach        }
176a5f7ed67SGreg Roach
1776ccdf4f0SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
178a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
179a5f7ed67SGreg Roach        ]);
180a5f7ed67SGreg Roach    }
181a5f7ed67SGreg Roach
182a5f7ed67SGreg Roach    /**
1836ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
184a5f7ed67SGreg Roach     *
1856ccdf4f0SGreg Roach     * @return ResponseInterface
186a5f7ed67SGreg Roach     */
1876ccdf4f0SGreg Roach    public function getFileAction(ServerRequestInterface $request): ResponseInterface
188c1010edaSGreg Roach    {
189b6b9dcc9SGreg Roach        $file = $request->getQueryParams()['file'];
190a5f7ed67SGreg Roach
191a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
192a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
193a5f7ed67SGreg Roach        }
194a5f7ed67SGreg Roach
195a5f7ed67SGreg Roach        $timestamp   = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
1964459dc9aSGreg Roach        $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix();
197a5f7ed67SGreg Roach
198ad98d39dSGreg Roach        if ($timestamp > $expiry_time) {
199a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
200a5f7ed67SGreg Roach        } else {
201a5f7ed67SGreg Roach            $tree = Tree::findById((int) $match[1]);
202a5f7ed67SGreg Roach
203a5f7ed67SGreg Roach            if ($tree === null) {
204a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
205a5f7ed67SGreg Roach            }
206a5f7ed67SGreg Roach
207bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
208a5f7ed67SGreg Roach
209a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
210a5f7ed67SGreg Roach
211a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
212a5f7ed67SGreg Roach        }
213a5f7ed67SGreg Roach
2146ccdf4f0SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
215a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
216a5f7ed67SGreg Roach        ]);
217a5f7ed67SGreg Roach    }
218a5f7ed67SGreg Roach
219a5f7ed67SGreg Roach    /**
220a5f7ed67SGreg Roach     * @param Tree   $tree
221a5f7ed67SGreg Roach     * @param string $type
222a5f7ed67SGreg Roach     * @param int    $limit
223a5f7ed67SGreg Roach     * @param int    $offset
224a5f7ed67SGreg Roach     *
22554c7f8dfSGreg Roach     * @return Collection
226a5f7ed67SGreg Roach     */
227886b77daSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection
228c1010edaSGreg Roach    {
229a5f7ed67SGreg Roach        switch ($type) {
2308c2e8227SGreg Roach            case 'i':
231a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
232a5f7ed67SGreg Roach                break;
233a5f7ed67SGreg Roach
234a5f7ed67SGreg Roach            case 'm':
235a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
236a5f7ed67SGreg Roach                break;
237a5f7ed67SGreg Roach
238a5f7ed67SGreg Roach            case 'n':
239a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
240a5f7ed67SGreg Roach                break;
241a5f7ed67SGreg Roach
242a5f7ed67SGreg Roach            case 'r':
243a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
244a5f7ed67SGreg Roach                break;
245a5f7ed67SGreg Roach
246a5f7ed67SGreg Roach            case 's':
247a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
248a5f7ed67SGreg Roach                break;
249a5f7ed67SGreg Roach
250a5f7ed67SGreg Roach            default:
251a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
252a5f7ed67SGreg Roach        }
253a5f7ed67SGreg Roach
254a5f7ed67SGreg Roach        // Skip private records.
2554146fabcSGreg Roach        $records = $records->filter(GedcomRecord::accessFilter());
256a5f7ed67SGreg Roach
257a5f7ed67SGreg Roach        return $records;
258a5f7ed67SGreg Roach    }
259a5f7ed67SGreg Roach
260a5f7ed67SGreg Roach    /**
261a5f7ed67SGreg Roach     * @param Tree $tree
262a5f7ed67SGreg Roach     * @param int  $limit
263a5f7ed67SGreg Roach     * @param int  $offset
264a5f7ed67SGreg Roach     *
26554c7f8dfSGreg Roach     * @return Collection
266a5f7ed67SGreg Roach     */
267886b77daSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection
268c1010edaSGreg Roach    {
269886b77daSGreg Roach        return DB::table('individuals')
270fa17fb66SGreg Roach            ->where('i_file', '=', $tree->id())
271fa17fb66SGreg Roach            ->orderBy('i_id')
272fa17fb66SGreg Roach            ->skip($offset)
273fa17fb66SGreg Roach            ->take($limit)
274886b77daSGreg Roach            ->get()
275c0804649SGreg Roach            ->map(Individual::rowMapper());
2768c2e8227SGreg Roach    }
277a5f7ed67SGreg Roach
278a5f7ed67SGreg Roach    /**
279a5f7ed67SGreg Roach     * @param Tree $tree
280a5f7ed67SGreg Roach     * @param int  $limit
281a5f7ed67SGreg Roach     * @param int  $offset
282a5f7ed67SGreg Roach     *
28354c7f8dfSGreg Roach     * @return Collection
284a5f7ed67SGreg Roach     */
285886b77daSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection
286c1010edaSGreg Roach    {
287886b77daSGreg Roach        return DB::table('media')
288fa17fb66SGreg Roach            ->where('m_file', '=', $tree->id())
289fa17fb66SGreg Roach            ->orderBy('m_id')
290fa17fb66SGreg Roach            ->skip($offset)
291fa17fb66SGreg Roach            ->take($limit)
292886b77daSGreg Roach            ->get()
293c0804649SGreg Roach            ->map(Media::rowMapper());
2948c2e8227SGreg Roach    }
2958c2e8227SGreg Roach
2968c2e8227SGreg Roach    /**
297a5f7ed67SGreg Roach     * @param Tree $tree
298a5f7ed67SGreg Roach     * @param int  $limit
299a5f7ed67SGreg Roach     * @param int  $offset
300a5f7ed67SGreg Roach     *
30154c7f8dfSGreg Roach     * @return Collection
3028c2e8227SGreg Roach     */
303886b77daSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection
304c1010edaSGreg Roach    {
305886b77daSGreg Roach        return DB::table('other')
306fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
307fa17fb66SGreg Roach            ->where('o_type', '=', 'NOTE')
308fa17fb66SGreg Roach            ->orderBy('o_id')
309fa17fb66SGreg Roach            ->skip($offset)
310fa17fb66SGreg Roach            ->take($limit)
311886b77daSGreg Roach            ->get()
312c0804649SGreg Roach            ->map(Note::rowMapper());
3138c2e8227SGreg Roach    }
3148c2e8227SGreg Roach
315a5f7ed67SGreg Roach    /**
316a5f7ed67SGreg Roach     * @param Tree $tree
317a5f7ed67SGreg Roach     * @param int  $limit
318a5f7ed67SGreg Roach     * @param int  $offset
319a5f7ed67SGreg Roach     *
32054c7f8dfSGreg Roach     * @return Collection
321a5f7ed67SGreg Roach     */
322886b77daSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection
323c1010edaSGreg Roach    {
324886b77daSGreg Roach        return DB::table('other')
325fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
326fa17fb66SGreg Roach            ->where('o_type', '=', 'REPO')
327fa17fb66SGreg Roach            ->orderBy('o_id')
328fa17fb66SGreg Roach            ->skip($offset)
329fa17fb66SGreg Roach            ->take($limit)
330886b77daSGreg Roach            ->get()
331c0804649SGreg Roach            ->map(Repository::rowMapper());
332a5f7ed67SGreg Roach    }
333a5f7ed67SGreg Roach
334a5f7ed67SGreg Roach    /**
335a5f7ed67SGreg Roach     * @param Tree $tree
336a5f7ed67SGreg Roach     * @param int  $limit
337a5f7ed67SGreg Roach     * @param int  $offset
338a5f7ed67SGreg Roach     *
33954c7f8dfSGreg Roach     * @return Collection
340a5f7ed67SGreg Roach     */
341886b77daSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): Collection
342c1010edaSGreg Roach    {
343886b77daSGreg Roach        return DB::table('sources')
344fa17fb66SGreg Roach            ->where('s_file', '=', $tree->id())
345fa17fb66SGreg Roach            ->orderBy('s_id')
346fa17fb66SGreg Roach            ->skip($offset)
347fa17fb66SGreg Roach            ->take($limit)
348886b77daSGreg Roach            ->get()
349c0804649SGreg Roach            ->map(Source::rowMapper());
3508c2e8227SGreg Roach    }
3518c2e8227SGreg Roach}
352