xref: /webtrees/app/Module/SiteMapModule.php (revision b6b9dcc9af05c3f136144fb4f6f9b69a8abd1995)
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;
33886b77daSGreg Roachuse Illuminate\Support\Collection;
346ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
36a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
378c2e8227SGreg Roach
388c2e8227SGreg Roach/**
398c2e8227SGreg Roach * Class SiteMapModule
408c2e8227SGreg Roach */
4137eb8894SGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface
42c1010edaSGreg Roach{
4349a243cbSGreg Roach    use ModuleConfigTrait;
4449a243cbSGreg Roach
4516d6367aSGreg Roach    private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
4616d6367aSGreg Roach    private const CACHE_LIFE         = 1209600; // Two weeks
478c2e8227SGreg Roach
48a5f7ed67SGreg Roach    /**
49a5f7ed67SGreg Roach     * A sentence describing what this module does.
50a5f7ed67SGreg Roach     *
51a5f7ed67SGreg Roach     * @return string
52a5f7ed67SGreg Roach     */
5349a243cbSGreg Roach    public function description(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
56bbb76c12SGreg Roach        return I18N::translate('Generate sitemap files for search engines.');
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
5976692c8bSGreg Roach    /**
60abafa13cSGreg Roach     * Should this module be enabled when it is first installed?
61abafa13cSGreg Roach     *
62abafa13cSGreg Roach     * @return bool
63abafa13cSGreg Roach     */
64abafa13cSGreg Roach    public function isEnabledByDefault(): bool
65abafa13cSGreg Roach    {
66abafa13cSGreg Roach        return false;
67abafa13cSGreg Roach    }
68abafa13cSGreg Roach
69abafa13cSGreg Roach    /**
706ccdf4f0SGreg Roach     * @return ResponseInterface
718c2e8227SGreg Roach     */
726ccdf4f0SGreg Roach    public function getAdminAction(): ResponseInterface
73c1010edaSGreg Roach    {
74a5f7ed67SGreg Roach        $this->layout = 'layouts/administration';
75a5f7ed67SGreg Roach
76c1010edaSGreg Roach        $sitemap_url = route('module', [
7726684e68SGreg Roach            'module' => $this->name(),
78c1010edaSGreg Roach            'action' => 'Index',
79c1010edaSGreg Roach        ]);
80a5f7ed67SGreg Roach
81a5f7ed67SGreg Roach        // This list comes from http://en.wikipedia.org/wiki/Sitemaps
82a5f7ed67SGreg Roach        $submit_urls = [
83a5f7ed67SGreg Roach            'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]),
84a5f7ed67SGreg Roach            'Google'     => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]),
85a5f7ed67SGreg Roach        ];
86a5f7ed67SGreg Roach
87291c1b19SGreg Roach        return $this->viewResponse('modules/sitemap/config', [
888b67c11aSGreg Roach            'all_trees'   => Tree::all(),
89a5f7ed67SGreg Roach            'sitemap_url' => $sitemap_url,
90a5f7ed67SGreg Roach            'submit_urls' => $submit_urls,
9149a243cbSGreg Roach            'title'       => $this->title(),
92a5f7ed67SGreg Roach        ]);
938c2e8227SGreg Roach    }
948c2e8227SGreg Roach
958c2e8227SGreg Roach    /**
966ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
97a5f7ed67SGreg Roach     *
986ccdf4f0SGreg Roach     * @return string
998c2e8227SGreg Roach     */
1006ccdf4f0SGreg Roach    public function title(): string
1016ccdf4f0SGreg Roach    {
1026ccdf4f0SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
1036ccdf4f0SGreg Roach        return I18N::translate('Sitemaps');
1046ccdf4f0SGreg Roach    }
1056ccdf4f0SGreg Roach
1066ccdf4f0SGreg Roach    /**
1076ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1086ccdf4f0SGreg Roach     *
1096ccdf4f0SGreg Roach     * @return ResponseInterface
1106ccdf4f0SGreg Roach     */
1116ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
112c1010edaSGreg Roach    {
113*b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
114*b6b9dcc9SGreg Roach
1158b67c11aSGreg Roach        foreach (Tree::all() as $tree) {
116*b6b9dcc9SGreg Roach            $include_in_sitemap = (bool) ($params['sitemap' . $tree->id()] ?? false);
117a5f7ed67SGreg Roach            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
1188c2e8227SGreg Roach        }
119a5f7ed67SGreg Roach
12049a243cbSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
121a5f7ed67SGreg Roach
1226ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
1238c2e8227SGreg Roach    }
1248c2e8227SGreg Roach
1258c2e8227SGreg Roach    /**
1266ccdf4f0SGreg Roach     * @return ResponseInterface
1278c2e8227SGreg Roach     */
1286ccdf4f0SGreg Roach    public function getIndexAction(): ResponseInterface
129c1010edaSGreg Roach    {
130a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap.timestamp');
131a5f7ed67SGreg Roach
1324459dc9aSGreg Roach        if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) {
133a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1348c2e8227SGreg Roach        } else {
135fa17fb66SGreg Roach            $count_individuals = DB::table('individuals')
136fa17fb66SGreg Roach                ->groupBy('i_file')
137fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'i_file'])
138fa17fb66SGreg Roach                ->pluck('total', 'i_file');
139a5f7ed67SGreg Roach
140fa17fb66SGreg Roach            $count_media = DB::table('media')
141fa17fb66SGreg Roach                ->groupBy('m_file')
142fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'm_file'])
143fa17fb66SGreg Roach                ->pluck('total', 'm_file');
144a5f7ed67SGreg Roach
145fa17fb66SGreg Roach            $count_notes = DB::table('other')
146fa17fb66SGreg Roach                ->where('o_type', '=', 'NOTE')
147fa17fb66SGreg Roach                ->groupBy('o_file')
148fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'o_file'])
149fa17fb66SGreg Roach                ->pluck('total', 'o_file');
150a5f7ed67SGreg Roach
151fa17fb66SGreg Roach            $count_repositories = DB::table('other')
152fa17fb66SGreg Roach                ->where('o_type', '=', 'REPO')
153fa17fb66SGreg Roach                ->groupBy('o_file')
154fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'o_file'])
155fa17fb66SGreg Roach                ->pluck('total', 'o_file');
156a5f7ed67SGreg Roach
157fa17fb66SGreg Roach            $count_sources = DB::table('sources')
158fa17fb66SGreg Roach                ->groupBy('s_file')
159fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 's_file'])
160fa17fb66SGreg Roach                ->pluck('total', 's_file');
161a5f7ed67SGreg Roach
162a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
1638b67c11aSGreg Roach                'all_trees'          => Tree::all(),
164a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
165a5f7ed67SGreg Roach                'count_media'        => $count_media,
166a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
167a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
168a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
169a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
170a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
171a5f7ed67SGreg Roach            ]);
172a5f7ed67SGreg Roach
173a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
174a5f7ed67SGreg Roach        }
175a5f7ed67SGreg Roach
1766ccdf4f0SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
177a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
178a5f7ed67SGreg Roach        ]);
179a5f7ed67SGreg Roach    }
180a5f7ed67SGreg Roach
181a5f7ed67SGreg Roach    /**
1826ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
183a5f7ed67SGreg Roach     *
1846ccdf4f0SGreg Roach     * @return ResponseInterface
185a5f7ed67SGreg Roach     */
1866ccdf4f0SGreg Roach    public function getFileAction(ServerRequestInterface $request): ResponseInterface
187c1010edaSGreg Roach    {
188*b6b9dcc9SGreg Roach        $file = $request->getQueryParams()['file'];
189a5f7ed67SGreg Roach
190a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
191a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
192a5f7ed67SGreg Roach        }
193a5f7ed67SGreg Roach
194a5f7ed67SGreg Roach        $timestamp   = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
1954459dc9aSGreg Roach        $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix();
196a5f7ed67SGreg Roach
197ad98d39dSGreg Roach        if ($timestamp > $expiry_time) {
198a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
199a5f7ed67SGreg Roach        } else {
200a5f7ed67SGreg Roach            $tree = Tree::findById((int) $match[1]);
201a5f7ed67SGreg Roach
202a5f7ed67SGreg Roach            if ($tree === null) {
203a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
204a5f7ed67SGreg Roach            }
205a5f7ed67SGreg Roach
206bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
207a5f7ed67SGreg Roach
208a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
209a5f7ed67SGreg Roach
210a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
211a5f7ed67SGreg Roach        }
212a5f7ed67SGreg Roach
2136ccdf4f0SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
214a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
215a5f7ed67SGreg Roach        ]);
216a5f7ed67SGreg Roach    }
217a5f7ed67SGreg Roach
218a5f7ed67SGreg Roach    /**
219a5f7ed67SGreg Roach     * @param Tree   $tree
220a5f7ed67SGreg Roach     * @param string $type
221a5f7ed67SGreg Roach     * @param int    $limit
222a5f7ed67SGreg Roach     * @param int    $offset
223a5f7ed67SGreg Roach     *
22454c7f8dfSGreg Roach     * @return Collection
22554c7f8dfSGreg Roach     * @return GedcomRecord[]
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
26654c7f8dfSGreg Roach     * @return Individual[]
267a5f7ed67SGreg Roach     */
268886b77daSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection
269c1010edaSGreg Roach    {
270886b77daSGreg Roach        return DB::table('individuals')
271fa17fb66SGreg Roach            ->where('i_file', '=', $tree->id())
272fa17fb66SGreg Roach            ->orderBy('i_id')
273fa17fb66SGreg Roach            ->skip($offset)
274fa17fb66SGreg Roach            ->take($limit)
275886b77daSGreg Roach            ->get()
276c0804649SGreg Roach            ->map(Individual::rowMapper());
2778c2e8227SGreg Roach    }
278a5f7ed67SGreg Roach
279a5f7ed67SGreg Roach    /**
280a5f7ed67SGreg Roach     * @param Tree $tree
281a5f7ed67SGreg Roach     * @param int  $limit
282a5f7ed67SGreg Roach     * @param int  $offset
283a5f7ed67SGreg Roach     *
28454c7f8dfSGreg Roach     * @return Collection
28554c7f8dfSGreg Roach     * @return Media[]
286a5f7ed67SGreg Roach     */
287886b77daSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection
288c1010edaSGreg Roach    {
289886b77daSGreg Roach        return DB::table('media')
290fa17fb66SGreg Roach            ->where('m_file', '=', $tree->id())
291fa17fb66SGreg Roach            ->orderBy('m_id')
292fa17fb66SGreg Roach            ->skip($offset)
293fa17fb66SGreg Roach            ->take($limit)
294886b77daSGreg Roach            ->get()
295c0804649SGreg Roach            ->map(Media::rowMapper());
2968c2e8227SGreg Roach    }
2978c2e8227SGreg Roach
2988c2e8227SGreg Roach    /**
299a5f7ed67SGreg Roach     * @param Tree $tree
300a5f7ed67SGreg Roach     * @param int  $limit
301a5f7ed67SGreg Roach     * @param int  $offset
302a5f7ed67SGreg Roach     *
30354c7f8dfSGreg Roach     * @return Collection
30454c7f8dfSGreg Roach     * @return Note[]
3058c2e8227SGreg Roach     */
306886b77daSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection
307c1010edaSGreg Roach    {
308886b77daSGreg Roach        return DB::table('other')
309fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
310fa17fb66SGreg Roach            ->where('o_type', '=', 'NOTE')
311fa17fb66SGreg Roach            ->orderBy('o_id')
312fa17fb66SGreg Roach            ->skip($offset)
313fa17fb66SGreg Roach            ->take($limit)
314886b77daSGreg Roach            ->get()
315c0804649SGreg Roach            ->map(Note::rowMapper());
3168c2e8227SGreg Roach    }
3178c2e8227SGreg Roach
318a5f7ed67SGreg Roach    /**
319a5f7ed67SGreg Roach     * @param Tree $tree
320a5f7ed67SGreg Roach     * @param int  $limit
321a5f7ed67SGreg Roach     * @param int  $offset
322a5f7ed67SGreg Roach     *
32354c7f8dfSGreg Roach     * @return Collection
32454c7f8dfSGreg Roach     * @return Repository[]
325a5f7ed67SGreg Roach     */
326886b77daSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection
327c1010edaSGreg Roach    {
328886b77daSGreg Roach        return DB::table('other')
329fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
330fa17fb66SGreg Roach            ->where('o_type', '=', 'REPO')
331fa17fb66SGreg Roach            ->orderBy('o_id')
332fa17fb66SGreg Roach            ->skip($offset)
333fa17fb66SGreg Roach            ->take($limit)
334886b77daSGreg Roach            ->get()
335c0804649SGreg Roach            ->map(Repository::rowMapper());
336a5f7ed67SGreg Roach    }
337a5f7ed67SGreg Roach
338a5f7ed67SGreg Roach    /**
339a5f7ed67SGreg Roach     * @param Tree $tree
340a5f7ed67SGreg Roach     * @param int  $limit
341a5f7ed67SGreg Roach     * @param int  $offset
342a5f7ed67SGreg Roach     *
34354c7f8dfSGreg Roach     * @return Collection
34454c7f8dfSGreg Roach     * @return Source[]
345a5f7ed67SGreg Roach     */
346886b77daSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): Collection
347c1010edaSGreg Roach    {
348886b77daSGreg Roach        return DB::table('sources')
349fa17fb66SGreg Roach            ->where('s_file', '=', $tree->id())
350fa17fb66SGreg Roach            ->orderBy('s_id')
351fa17fb66SGreg Roach            ->skip($offset)
352fa17fb66SGreg Roach            ->take($limit)
353886b77daSGreg Roach            ->get()
354c0804649SGreg Roach            ->map(Source::rowMapper());
3558c2e8227SGreg Roach    }
3568c2e8227SGreg Roach}
357