xref: /webtrees/app/Module/SiteMapModule.php (revision 0cfd6963ac65bd7fe86283b801b4f23d665c6004)
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
204459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
21a5f7ed67SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
22a5f7ed67SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23b1b85189SGreg Roachuse Fisharebest\Webtrees\Html;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media;
270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Note;
280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Repository;
290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Source;
300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Tree;
31fa17fb66SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
32886b77daSGreg Roachuse Illuminate\Support\Collection;
33a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
34a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Request;
35a5f7ed67SGreg Roachuse Symfony\Component\HttpFoundation\Response;
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    /**
49*0cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
50a5f7ed67SGreg Roach     *
51a5f7ed67SGreg Roach     * @return string
52a5f7ed67SGreg Roach     */
5349a243cbSGreg Roach    public function title(): string
54c1010edaSGreg Roach    {
55bbb76c12SGreg Roach        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
56bbb76c12SGreg Roach        return I18N::translate('Sitemaps');
578c2e8227SGreg Roach    }
588c2e8227SGreg Roach
59a5f7ed67SGreg Roach    /**
60a5f7ed67SGreg Roach     * A sentence describing what this module does.
61a5f7ed67SGreg Roach     *
62a5f7ed67SGreg Roach     * @return string
63a5f7ed67SGreg Roach     */
6449a243cbSGreg Roach    public function description(): string
65c1010edaSGreg Roach    {
66bbb76c12SGreg Roach        /* I18N: Description of the “Sitemaps” module */
67bbb76c12SGreg Roach        return I18N::translate('Generate sitemap files for search engines.');
688c2e8227SGreg Roach    }
698c2e8227SGreg Roach
7076692c8bSGreg Roach    /**
71a5f7ed67SGreg Roach     * @return Response
728c2e8227SGreg Roach     */
7336e59714SGreg Roach    public function getAdminAction(): Response
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    /**
97a5f7ed67SGreg Roach     * @param Request $request
98a5f7ed67SGreg Roach     *
99a5f7ed67SGreg Roach     * @return RedirectResponse
1008c2e8227SGreg Roach     */
101c1010edaSGreg Roach    public function postAdminAction(Request $request): RedirectResponse
102c1010edaSGreg Roach    {
1038b67c11aSGreg Roach        foreach (Tree::all() as $tree) {
10472cf66d4SGreg Roach            $include_in_sitemap = (bool) $request->get('sitemap' . $tree->id());
105a5f7ed67SGreg Roach            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
1068c2e8227SGreg Roach        }
107a5f7ed67SGreg Roach
10849a243cbSGreg Roach        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
109a5f7ed67SGreg Roach
110a5f7ed67SGreg Roach        return new RedirectResponse($this->getConfigLink());
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
1138c2e8227SGreg Roach    /**
114a5f7ed67SGreg Roach     * @return Response
1158c2e8227SGreg Roach     */
11636e59714SGreg Roach    public function getIndexAction(): Response
117c1010edaSGreg Roach    {
118a5f7ed67SGreg Roach        $timestamp = (int) $this->getPreference('sitemap.timestamp');
119a5f7ed67SGreg Roach
1204459dc9aSGreg Roach        if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) {
121a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap.xml');
1228c2e8227SGreg Roach        } else {
123fa17fb66SGreg Roach            $count_individuals = DB::table('individuals')
124fa17fb66SGreg Roach                ->groupBy('i_file')
125fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'i_file'])
126fa17fb66SGreg Roach                ->pluck('total', 'i_file');
127a5f7ed67SGreg Roach
128fa17fb66SGreg Roach            $count_media = DB::table('media')
129fa17fb66SGreg Roach                ->groupBy('m_file')
130fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'm_file'])
131fa17fb66SGreg Roach                ->pluck('total', 'm_file');
132a5f7ed67SGreg Roach
133fa17fb66SGreg Roach            $count_notes = DB::table('other')
134fa17fb66SGreg Roach                ->where('o_type', '=', 'NOTE')
135fa17fb66SGreg Roach                ->groupBy('o_file')
136fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'o_file'])
137fa17fb66SGreg Roach                ->pluck('total', 'o_file');
138a5f7ed67SGreg Roach
139fa17fb66SGreg Roach            $count_repositories = DB::table('other')
140fa17fb66SGreg Roach                ->where('o_type', '=', 'REPO')
141fa17fb66SGreg Roach                ->groupBy('o_file')
142fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 'o_file'])
143fa17fb66SGreg Roach                ->pluck('total', 'o_file');
144a5f7ed67SGreg Roach
145fa17fb66SGreg Roach            $count_sources = DB::table('sources')
146fa17fb66SGreg Roach                ->groupBy('s_file')
147fa17fb66SGreg Roach                ->select([DB::raw('COUNT(*) AS total'), 's_file'])
148fa17fb66SGreg Roach                ->pluck('total', 's_file');
149a5f7ed67SGreg Roach
150a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-index.xml', [
1518b67c11aSGreg Roach                'all_trees'          => Tree::all(),
152a5f7ed67SGreg Roach                'count_individuals'  => $count_individuals,
153a5f7ed67SGreg Roach                'count_media'        => $count_media,
154a5f7ed67SGreg Roach                'count_notes'        => $count_notes,
155a5f7ed67SGreg Roach                'count_repositories' => $count_repositories,
156a5f7ed67SGreg Roach                'count_sources'      => $count_sources,
157a5f7ed67SGreg Roach                'last_mod'           => date('Y-m-d'),
158a5f7ed67SGreg Roach                'records_per_volume' => self::RECORDS_PER_VOLUME,
159a5f7ed67SGreg Roach            ]);
160a5f7ed67SGreg Roach
161a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
162a5f7ed67SGreg Roach        }
163a5f7ed67SGreg Roach
164a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
165a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
166a5f7ed67SGreg Roach        ]);
167a5f7ed67SGreg Roach    }
168a5f7ed67SGreg Roach
169a5f7ed67SGreg Roach    /**
170a5f7ed67SGreg Roach     * @param Request $request
171a5f7ed67SGreg Roach     *
172a5f7ed67SGreg Roach     * @return Response
173a5f7ed67SGreg Roach     */
174c1010edaSGreg Roach    public function getFileAction(Request $request): Response
175c1010edaSGreg Roach    {
176a5f7ed67SGreg Roach        $file = $request->get('file', '');
177a5f7ed67SGreg Roach
178a5f7ed67SGreg Roach        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
179a5f7ed67SGreg Roach            throw new NotFoundHttpException('Bad sitemap file');
180a5f7ed67SGreg Roach        }
181a5f7ed67SGreg Roach
182a5f7ed67SGreg Roach        $timestamp   = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
1834459dc9aSGreg Roach        $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix();
184a5f7ed67SGreg Roach
185ad98d39dSGreg Roach        if ($timestamp > $expiry_time) {
186a5f7ed67SGreg Roach            $content = $this->getPreference('sitemap-' . $file . '.xml');
187a5f7ed67SGreg Roach        } else {
188a5f7ed67SGreg Roach            $tree = Tree::findById((int) $match[1]);
189a5f7ed67SGreg Roach
190a5f7ed67SGreg Roach            if ($tree === null) {
191a5f7ed67SGreg Roach                throw new NotFoundHttpException('No such tree');
192a5f7ed67SGreg Roach            }
193a5f7ed67SGreg Roach
194bdb3725aSGreg Roach            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
195a5f7ed67SGreg Roach
196a37bbafbSGreg Roach            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
197a5f7ed67SGreg Roach
198a5f7ed67SGreg Roach            $this->setPreference('sitemap.xml', $content);
199a5f7ed67SGreg Roach        }
200a5f7ed67SGreg Roach
201a5f7ed67SGreg Roach        return new Response($content, Response::HTTP_OK, [
202a5f7ed67SGreg Roach            'Content-Type' => 'application/xml',
203a5f7ed67SGreg Roach        ]);
204a5f7ed67SGreg Roach    }
205a5f7ed67SGreg Roach
206a5f7ed67SGreg Roach    /**
207a5f7ed67SGreg Roach     * @param Tree   $tree
208a5f7ed67SGreg Roach     * @param string $type
209a5f7ed67SGreg Roach     * @param int    $limit
210a5f7ed67SGreg Roach     * @param int    $offset
211a5f7ed67SGreg Roach     *
212886b77daSGreg Roach     * @return Collection|GedcomRecord[]
213a5f7ed67SGreg Roach     */
214886b77daSGreg Roach    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection
215c1010edaSGreg Roach    {
216a5f7ed67SGreg Roach        switch ($type) {
2178c2e8227SGreg Roach            case 'i':
218a5f7ed67SGreg Roach                $records = $this->sitemapIndividuals($tree, $limit, $offset);
219a5f7ed67SGreg Roach                break;
220a5f7ed67SGreg Roach
221a5f7ed67SGreg Roach            case 'm':
222a5f7ed67SGreg Roach                $records = $this->sitemapMedia($tree, $limit, $offset);
223a5f7ed67SGreg Roach                break;
224a5f7ed67SGreg Roach
225a5f7ed67SGreg Roach            case 'n':
226a5f7ed67SGreg Roach                $records = $this->sitemapNotes($tree, $limit, $offset);
227a5f7ed67SGreg Roach                break;
228a5f7ed67SGreg Roach
229a5f7ed67SGreg Roach            case 'r':
230a5f7ed67SGreg Roach                $records = $this->sitemapRepositories($tree, $limit, $offset);
231a5f7ed67SGreg Roach                break;
232a5f7ed67SGreg Roach
233a5f7ed67SGreg Roach            case 's':
234a5f7ed67SGreg Roach                $records = $this->sitemapSources($tree, $limit, $offset);
235a5f7ed67SGreg Roach                break;
236a5f7ed67SGreg Roach
237a5f7ed67SGreg Roach            default:
238a5f7ed67SGreg Roach                throw new NotFoundHttpException('Invalid record type: ' . $type);
239a5f7ed67SGreg Roach        }
240a5f7ed67SGreg Roach
241a5f7ed67SGreg Roach        // Skip private records.
2424146fabcSGreg Roach        $records = $records->filter(GedcomRecord::accessFilter());
243a5f7ed67SGreg Roach
244a5f7ed67SGreg Roach        return $records;
245a5f7ed67SGreg Roach    }
246a5f7ed67SGreg Roach
247a5f7ed67SGreg Roach    /**
248a5f7ed67SGreg Roach     * @param Tree $tree
249a5f7ed67SGreg Roach     * @param int  $limit
250a5f7ed67SGreg Roach     * @param int  $offset
251a5f7ed67SGreg Roach     *
252886b77daSGreg Roach     * @return Collection|Individual[]
253a5f7ed67SGreg Roach     */
254886b77daSGreg Roach    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection
255c1010edaSGreg Roach    {
256886b77daSGreg Roach        return DB::table('individuals')
257fa17fb66SGreg Roach            ->where('i_file', '=', $tree->id())
258fa17fb66SGreg Roach            ->orderBy('i_id')
259fa17fb66SGreg Roach            ->skip($offset)
260fa17fb66SGreg Roach            ->take($limit)
261886b77daSGreg Roach            ->get()
262c0804649SGreg Roach            ->map(Individual::rowMapper());
2638c2e8227SGreg Roach    }
264a5f7ed67SGreg Roach
265a5f7ed67SGreg Roach    /**
266a5f7ed67SGreg Roach     * @param Tree $tree
267a5f7ed67SGreg Roach     * @param int  $limit
268a5f7ed67SGreg Roach     * @param int  $offset
269a5f7ed67SGreg Roach     *
270886b77daSGreg Roach     * @return Collection|Media[]
271a5f7ed67SGreg Roach     */
272886b77daSGreg Roach    private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection
273c1010edaSGreg Roach    {
274886b77daSGreg Roach        return DB::table('media')
275fa17fb66SGreg Roach            ->where('m_file', '=', $tree->id())
276fa17fb66SGreg Roach            ->orderBy('m_id')
277fa17fb66SGreg Roach            ->skip($offset)
278fa17fb66SGreg Roach            ->take($limit)
279886b77daSGreg Roach            ->get()
280c0804649SGreg Roach            ->map(Media::rowMapper());
2818c2e8227SGreg Roach    }
2828c2e8227SGreg Roach
2838c2e8227SGreg Roach    /**
284a5f7ed67SGreg Roach     * @param Tree $tree
285a5f7ed67SGreg Roach     * @param int  $limit
286a5f7ed67SGreg Roach     * @param int  $offset
287a5f7ed67SGreg Roach     *
288886b77daSGreg Roach     * @return Collection|Note[]
2898c2e8227SGreg Roach     */
290886b77daSGreg Roach    private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection
291c1010edaSGreg Roach    {
292886b77daSGreg Roach        return DB::table('other')
293fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
294fa17fb66SGreg Roach            ->where('o_type', '=', 'NOTE')
295fa17fb66SGreg Roach            ->orderBy('o_id')
296fa17fb66SGreg Roach            ->skip($offset)
297fa17fb66SGreg Roach            ->take($limit)
298886b77daSGreg Roach            ->get()
299c0804649SGreg Roach            ->map(Note::rowMapper());
3008c2e8227SGreg Roach    }
3018c2e8227SGreg Roach
302a5f7ed67SGreg Roach    /**
303a5f7ed67SGreg Roach     * @param Tree $tree
304a5f7ed67SGreg Roach     * @param int  $limit
305a5f7ed67SGreg Roach     * @param int  $offset
306a5f7ed67SGreg Roach     *
307886b77daSGreg Roach     * @return Collection|Repository[]
308a5f7ed67SGreg Roach     */
309886b77daSGreg Roach    private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection
310c1010edaSGreg Roach    {
311886b77daSGreg Roach        return DB::table('other')
312fa17fb66SGreg Roach            ->where('o_file', '=', $tree->id())
313fa17fb66SGreg Roach            ->where('o_type', '=', 'REPO')
314fa17fb66SGreg Roach            ->orderBy('o_id')
315fa17fb66SGreg Roach            ->skip($offset)
316fa17fb66SGreg Roach            ->take($limit)
317886b77daSGreg Roach            ->get()
318c0804649SGreg Roach            ->map(Repository::rowMapper());
319a5f7ed67SGreg Roach    }
320a5f7ed67SGreg Roach
321a5f7ed67SGreg Roach    /**
322a5f7ed67SGreg Roach     * @param Tree $tree
323a5f7ed67SGreg Roach     * @param int  $limit
324a5f7ed67SGreg Roach     * @param int  $offset
325a5f7ed67SGreg Roach     *
326886b77daSGreg Roach     * @return Collection|Source[]
327a5f7ed67SGreg Roach     */
328886b77daSGreg Roach    private function sitemapSources(Tree $tree, int $limit, int $offset): Collection
329c1010edaSGreg Roach    {
330886b77daSGreg Roach        return DB::table('sources')
331fa17fb66SGreg Roach            ->where('s_file', '=', $tree->id())
332fa17fb66SGreg Roach            ->orderBy('s_id')
333fa17fb66SGreg Roach            ->skip($offset)
334fa17fb66SGreg Roach            ->take($limit)
335886b77daSGreg Roach            ->get()
336c0804649SGreg Roach            ->map(Source::rowMapper());
3378c2e8227SGreg Roach    }
3388c2e8227SGreg Roach}
339