xref: /webtrees/app/Module/SiteMapModule.php (revision d5ad3db07e8881039a1b485e037789531d265b51)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
226ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
25a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
26b1b85189SGreg Roachuse Fisharebest\Webtrees\Html;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note;
310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository;
323df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source;
340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
35fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
36a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
37886b77daSGreg Roachuse Illuminate\Support\Collection;
386ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
396ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
40a5f7ed67SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
418c2e8227SGreg Roach
423df1e584SGreg Roachuse function redirect;
433df1e584SGreg Roachuse function view;
443df1e584SGreg Roach
458c2e8227SGreg Roach/**
468c2e8227SGreg Roach * Class SiteMapModule
478c2e8227SGreg Roach */
4837eb8894SGreg Roachclass SiteMapModule extends AbstractModule implements ModuleConfigInterface
49c1010edaSGreg Roach{
5049a243cbSGreg Roach    use ModuleConfigTrait;
5149a243cbSGreg Roach
5216d6367aSGreg Roach    private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
5316d6367aSGreg Roach    private const CACHE_LIFE         = 1209600; // Two weeks
548c2e8227SGreg Roach
553df1e584SGreg Roach    /** @var TreeService */
563df1e584SGreg Roach    private $tree_service;
573df1e584SGreg Roach
583df1e584SGreg Roach    /**
593df1e584SGreg Roach     * TreesMenuModule constructor.
603df1e584SGreg Roach     *
613df1e584SGreg Roach     * @param TreeService $tree_service
623df1e584SGreg Roach     */
633df1e584SGreg Roach    public function __construct(TreeService $tree_service)
643df1e584SGreg Roach    {
653df1e584SGreg Roach        $this->tree_service = $tree_service;
663df1e584SGreg Roach    }
673df1e584SGreg Roach
68a5f7ed67SGreg Roach    /**
69a5f7ed67SGreg Roach     * A sentence describing what this module does.
70a5f7ed67SGreg Roach     *
71a5f7ed67SGreg Roach     * @return string
72a5f7ed67SGreg Roach     */
7349a243cbSGreg Roach    public function description(): string
74c1010edaSGreg Roach    {
75bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
76bbb76c12SGreg Roach        return I18N::translate('Generate sitemap files for search engines.');
778c2e8227SGreg Roach    }
788c2e8227SGreg Roach
7976692c8bSGreg Roach    /**
80abafa13cSGreg Roach     * Should this module be enabled when it is first installed?
81abafa13cSGreg Roach     *
82abafa13cSGreg Roach     * @return bool
83abafa13cSGreg Roach     */
84abafa13cSGreg Roach    public function isEnabledByDefault(): bool
85abafa13cSGreg Roach    {
86abafa13cSGreg Roach        return false;
87abafa13cSGreg Roach    }
88abafa13cSGreg Roach
89abafa13cSGreg Roach    /**
9057ab2231SGreg Roach     * @param ServerRequestInterface $request
9157ab2231SGreg Roach     *
926ccdf4f0SGreg Roach     * @return ResponseInterface
938c2e8227SGreg Roach     */
9457ab2231SGreg Roach    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
95c1010edaSGreg Roach    {
96a5f7ed67SGreg Roach        $this->layout = 'layouts/administration';
97a5f7ed67SGreg Roach
98c1010edaSGreg Roach        $sitemap_url = route('module', [
9926684e68SGreg Roach            'module' => $this->name(),
100c1010edaSGreg Roach            'action' => 'Index',
101c1010edaSGreg Roach        ]);
102a5f7ed67SGreg Roach
103a5f7ed67SGreg Roach        // This list comes from http://en.wikipedia.org/wiki/Sitemaps
104a5f7ed67SGreg Roach        $submit_urls = [
105a5f7ed67SGreg Roach            'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]),
106a5f7ed67SGreg Roach            'Google'     => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]),
107a5f7ed67SGreg Roach        ];
108a5f7ed67SGreg Roach
109291c1b19SGreg Roach        return $this->viewResponse('modules/sitemap/config', [
1103df1e584SGreg Roach            'all_trees'   => $this->tree_service->all(),
111a5f7ed67SGreg Roach            'sitemap_url' => $sitemap_url,
112a5f7ed67SGreg Roach            'submit_urls' => $submit_urls,
11349a243cbSGreg Roach            'title'       => $this->title(),
114a5f7ed67SGreg Roach        ]);
1158c2e8227SGreg Roach    }
1168c2e8227SGreg Roach
1178c2e8227SGreg Roach    /**
1186ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
119a5f7ed67SGreg Roach     *
1206ccdf4f0SGreg Roach     * @return string
1218c2e8227SGreg Roach     */
1226ccdf4f0SGreg Roach    public function title(): string
1236ccdf4f0SGreg Roach    {
1246ccdf4f0SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
1256ccdf4f0SGreg Roach        return I18N::translate('Sitemaps');
1266ccdf4f0SGreg Roach    }
1276ccdf4f0SGreg Roach
1286ccdf4f0SGreg Roach    /**
1296ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
1306ccdf4f0SGreg Roach     *
1316ccdf4f0SGreg Roach     * @return ResponseInterface
1326ccdf4f0SGreg Roach     */
1336ccdf4f0SGreg Roach    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
134c1010edaSGreg Roach    {
135b6b9dcc9SGreg Roach        $params = $request->getParsedBody();
136b6b9dcc9SGreg Roach
1373df1e584SGreg Roach        foreach ($this->tree_service->all() as $tree) {
138b6b9dcc9SGreg Roach            $include_in_sitemap = (bool) ($params['sitemap' . $tree->id()] ?? false);
139a5f7ed67SGreg Roach            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
1408c2e8227SGreg Roach        }
141a5f7ed67SGreg Roach
14249a243cbSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
143a5f7ed67SGreg Roach
1446ccdf4f0SGreg Roach        return redirect($this->getConfigLink());
1458c2e8227SGreg Roach    }
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach    /**
14857ab2231SGreg Roach     * @param ServerRequestInterface $request
14957ab2231SGreg Roach     *
1506ccdf4f0SGreg Roach     * @return ResponseInterface
1518c2e8227SGreg Roach     */
15257ab2231SGreg Roach    public function getIndexAction(ServerRequestInterface $request): ResponseInterface
153c1010edaSGreg Roach    {
154a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap.timestamp');
155a5f7ed67SGreg Roach
1564459dc9aSGreg Roach        if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) {
157a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1588c2e8227SGreg Roach        } else {
159fa17fb66SGreg Roach            $count_individuals = DB::table('individuals')
1607f5c2944SGreg Roach                ->groupBy(['i_file'])
161a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'i_file'])
162fa17fb66SGreg Roach                ->pluck('total', 'i_file');
163a5f7ed67SGreg Roach
164fa17fb66SGreg Roach            $count_media = DB::table('media')
1657f5c2944SGreg Roach                ->groupBy(['m_file'])
166a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'm_file'])
167fa17fb66SGreg Roach                ->pluck('total', 'm_file');
168a5f7ed67SGreg Roach
169fa17fb66SGreg Roach            $count_notes = DB::table('other')
170fa17fb66SGreg Roach                ->where('o_type', '=', 'NOTE')
1717f5c2944SGreg Roach                ->groupBy(['o_file'])
172a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'o_file'])
173fa17fb66SGreg Roach                ->pluck('total', 'o_file');
174a5f7ed67SGreg Roach
175fa17fb66SGreg Roach            $count_repositories = DB::table('other')
176fa17fb66SGreg Roach                ->where('o_type', '=', 'REPO')
1777f5c2944SGreg Roach                ->groupBy(['o_file'])
178a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 'o_file'])
179fa17fb66SGreg Roach                ->pluck('total', 'o_file');
180a5f7ed67SGreg Roach
181fa17fb66SGreg Roach            $count_sources = DB::table('sources')
1827f5c2944SGreg Roach                ->groupBy(['s_file'])
183a69f5655SGreg Roach                ->select([new Expression('COUNT(*) AS total'), 's_file'])
184fa17fb66SGreg Roach                ->pluck('total', 's_file');
185a5f7ed67SGreg Roach
186a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
1873df1e584SGreg Roach                'all_trees'          => $this->tree_service->all(),
188a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
189a5f7ed67SGreg Roach                'count_media'        => $count_media,
190a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
191a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
192a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
193a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
194a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
195a5f7ed67SGreg Roach            ]);
196a5f7ed67SGreg Roach
197a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
198a5f7ed67SGreg Roach        }
199a5f7ed67SGreg Roach
2006ccdf4f0SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
201a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
202a5f7ed67SGreg Roach        ]);
203a5f7ed67SGreg Roach    }
204a5f7ed67SGreg Roach
205a5f7ed67SGreg Roach    /**
2066ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
207a5f7ed67SGreg Roach     *
2086ccdf4f0SGreg Roach     * @return ResponseInterface
209a5f7ed67SGreg Roach     */
2106ccdf4f0SGreg Roach    public function getFileAction(ServerRequestInterface $request): ResponseInterface
211c1010edaSGreg Roach    {
212b6b9dcc9SGreg Roach        $file = $request->getQueryParams()['file'];
213a5f7ed67SGreg Roach
214a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
215a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
216a5f7ed67SGreg Roach        }
217a5f7ed67SGreg Roach
218a5f7ed67SGreg Roach        $timestamp   = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
2194459dc9aSGreg Roach        $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix();
220a5f7ed67SGreg Roach
221ad98d39dSGreg Roach        if ($timestamp > $expiry_time) {
222a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
223a5f7ed67SGreg Roach        } else {
224*d5ad3db0SGreg Roach            $tree = $this->tree_service->find((int) $match[1]);
225a5f7ed67SGreg Roach
226a5f7ed67SGreg Roach            if ($tree === null) {
227a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
228a5f7ed67SGreg Roach            }
229a5f7ed67SGreg Roach
230bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
231a5f7ed67SGreg Roach
232a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
233a5f7ed67SGreg Roach
234a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
235a5f7ed67SGreg Roach        }
236a5f7ed67SGreg Roach
2376ccdf4f0SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
238a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
239a5f7ed67SGreg Roach        ]);
240a5f7ed67SGreg Roach    }
241a5f7ed67SGreg Roach
242a5f7ed67SGreg Roach    /**
243a5f7ed67SGreg Roach     * @param Tree   $tree
244a5f7ed67SGreg Roach     * @param string $type
245a5f7ed67SGreg Roach     * @param int    $limit
246a5f7ed67SGreg Roach     * @param int    $offset
247a5f7ed67SGreg Roach     *
24854c7f8dfSGreg Roach     * @return Collection
249a5f7ed67SGreg Roach     */
250886b77daSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection
251c1010edaSGreg Roach    {
252a5f7ed67SGreg Roach        switch ($type) {
2538c2e8227SGreg Roach            case 'i':
254a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
255a5f7ed67SGreg Roach                break;
256a5f7ed67SGreg Roach
257a5f7ed67SGreg Roach            case 'm':
258a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
259a5f7ed67SGreg Roach                break;
260a5f7ed67SGreg Roach
261a5f7ed67SGreg Roach            case 'n':
262a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
263a5f7ed67SGreg Roach                break;
264a5f7ed67SGreg Roach
265a5f7ed67SGreg Roach            case 'r':
266a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
267a5f7ed67SGreg Roach                break;
268a5f7ed67SGreg Roach
269a5f7ed67SGreg Roach            case 's':
270a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
271a5f7ed67SGreg Roach                break;
272a5f7ed67SGreg Roach
273a5f7ed67SGreg Roach            default:
274a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
275a5f7ed67SGreg Roach        }
276a5f7ed67SGreg Roach
277a5f7ed67SGreg Roach        // Skip private records.
2784146fabcSGreg Roach        $records = $records->filter(GedcomRecord::accessFilter());
279a5f7ed67SGreg Roach
280a5f7ed67SGreg Roach        return $records;
281a5f7ed67SGreg Roach    }
282a5f7ed67SGreg Roach
283a5f7ed67SGreg Roach    /**
284a5f7ed67SGreg Roach     * @param Tree $tree
285a5f7ed67SGreg Roach     * @param int  $limit
286a5f7ed67SGreg Roach     * @param int  $offset
287a5f7ed67SGreg Roach     *
28854c7f8dfSGreg Roach     * @return Collection
289a5f7ed67SGreg Roach     */
290886b77daSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection
291c1010edaSGreg Roach    {
292886b77daSGreg Roach        return DB::table('individuals')
293fa17fb66SGreg Roach            ->where('i_file', '=', $tree->id())
294fa17fb66SGreg Roach            ->orderBy('i_id')
295fa17fb66SGreg Roach            ->skip($offset)
296fa17fb66SGreg Roach            ->take($limit)
297886b77daSGreg Roach            ->get()
298*d5ad3db0SGreg Roach            ->map(Individual::rowMapper($tree));
2998c2e8227SGreg Roach    }
300a5f7ed67SGreg Roach
301a5f7ed67SGreg Roach    /**
302a5f7ed67SGreg Roach     * @param Tree $tree
303a5f7ed67SGreg Roach     * @param int  $limit
304a5f7ed67SGreg Roach     * @param int  $offset
305a5f7ed67SGreg Roach     *
30654c7f8dfSGreg Roach     * @return Collection
307a5f7ed67SGreg Roach     */
308886b77daSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection
309c1010edaSGreg Roach    {
310886b77daSGreg Roach        return DB::table('media')
311fa17fb66SGreg Roach            ->where('m_file', '=', $tree->id())
312fa17fb66SGreg Roach            ->orderBy('m_id')
313fa17fb66SGreg Roach            ->skip($offset)
314fa17fb66SGreg Roach            ->take($limit)
315886b77daSGreg Roach            ->get()
316*d5ad3db0SGreg Roach            ->map(Media::rowMapper($tree));
3178c2e8227SGreg Roach    }
3188c2e8227SGreg Roach
3198c2e8227SGreg Roach    /**
320a5f7ed67SGreg Roach     * @param Tree $tree
321a5f7ed67SGreg Roach     * @param int  $limit
322a5f7ed67SGreg Roach     * @param int  $offset
323a5f7ed67SGreg Roach     *
32454c7f8dfSGreg Roach     * @return Collection
3258c2e8227SGreg Roach     */
326886b77daSGreg Roach    private function sitemapNotes(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', '=', 'NOTE')
331fa17fb66SGreg Roach            ->orderBy('o_id')
332fa17fb66SGreg Roach            ->skip($offset)
333fa17fb66SGreg Roach            ->take($limit)
334886b77daSGreg Roach            ->get()
335*d5ad3db0SGreg Roach            ->map(Note::rowMapper($tree));
3368c2e8227SGreg Roach    }
3378c2e8227SGreg Roach
338a5f7ed67SGreg Roach    /**
339a5f7ed67SGreg Roach     * @param Tree $tree
340a5f7ed67SGreg Roach     * @param int  $limit
341a5f7ed67SGreg Roach     * @param int  $offset
342a5f7ed67SGreg Roach     *
34354c7f8dfSGreg Roach     * @return Collection
344a5f7ed67SGreg Roach     */
345886b77daSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection
346c1010edaSGreg Roach    {
347886b77daSGreg Roach        return DB::table('other')
348fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
349fa17fb66SGreg Roach            ->where('o_type', '=', 'REPO')
350fa17fb66SGreg Roach            ->orderBy('o_id')
351fa17fb66SGreg Roach            ->skip($offset)
352fa17fb66SGreg Roach            ->take($limit)
353886b77daSGreg Roach            ->get()
354*d5ad3db0SGreg Roach            ->map(Repository::rowMapper($tree));
355a5f7ed67SGreg Roach    }
356a5f7ed67SGreg Roach
357a5f7ed67SGreg Roach    /**
358a5f7ed67SGreg Roach     * @param Tree $tree
359a5f7ed67SGreg Roach     * @param int  $limit
360a5f7ed67SGreg Roach     * @param int  $offset
361a5f7ed67SGreg Roach     *
36254c7f8dfSGreg Roach     * @return Collection
363a5f7ed67SGreg Roach     */
364886b77daSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): Collection
365c1010edaSGreg Roach    {
366886b77daSGreg Roach        return DB::table('sources')
367fa17fb66SGreg Roach            ->where('s_file', '=', $tree->id())
368fa17fb66SGreg Roach            ->orderBy('s_id')
369fa17fb66SGreg Roach            ->skip($offset)
370fa17fb66SGreg Roach            ->take($limit)
371886b77daSGreg Roach            ->get()
372*d5ad3db0SGreg Roach            ->map(Source::rowMapper($tree));
3738c2e8227SGreg Roach    }
3748c2e8227SGreg Roach}
375