xref: /webtrees/app/Module/SiteMapModule.php (revision 6dee9a8c9b87a4759535afc29fe1e81073a98265)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fig\Http\Message\StatusCodeInterface;
21use Fisharebest\Webtrees\Carbon;
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\Html;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Media;
28use Fisharebest\Webtrees\Note;
29use Fisharebest\Webtrees\Repository;
30use Fisharebest\Webtrees\Source;
31use Fisharebest\Webtrees\Tree;
32use Illuminate\Database\Capsule\Manager as DB;
33use Illuminate\Support\Collection;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37
38/**
39 * Class SiteMapModule
40 */
41class SiteMapModule extends AbstractModule implements ModuleConfigInterface
42{
43    use ModuleConfigTrait;
44
45    private const RECORDS_PER_VOLUME = 500; // Keep sitemap files small, for memory, CPU and max_allowed_packet limits.
46    private const CACHE_LIFE         = 1209600; // Two weeks
47
48    /**
49     * A sentence describing what this module does.
50     *
51     * @return string
52     */
53    public function description(): string
54    {
55        /* I18N: Description of the “Sitemaps” module */
56        return I18N::translate('Generate sitemap files for search engines.');
57    }
58
59    /**
60     * Should this module be enabled when it is first installed?
61     *
62     * @return bool
63     */
64    public function isEnabledByDefault(): bool
65    {
66        return false;
67    }
68
69    /**
70     * @return ResponseInterface
71     */
72    public function getAdminAction(): ResponseInterface
73    {
74        $this->layout = 'layouts/administration';
75
76        $sitemap_url = route('module', [
77            'module' => $this->name(),
78            'action' => 'Index',
79        ]);
80
81        // This list comes from http://en.wikipedia.org/wiki/Sitemaps
82        $submit_urls = [
83            'Bing/Yahoo' => Html::url('https://www.bing.com/webmaster/ping.aspx', ['siteMap' => $sitemap_url]),
84            'Google'     => Html::url('https://www.google.com/webmasters/tools/ping', ['sitemap' => $sitemap_url]),
85        ];
86
87        return $this->viewResponse('modules/sitemap/config', [
88            'all_trees'   => Tree::all(),
89            'sitemap_url' => $sitemap_url,
90            'submit_urls' => $submit_urls,
91            'title'       => $this->title(),
92        ]);
93    }
94
95    /**
96     * How should this module be identified in the control panel, etc.?
97     *
98     * @return string
99     */
100    public function title(): string
101    {
102        /* I18N: Name of a module - see http://en.wikipedia.org/wiki/Sitemaps */
103        return I18N::translate('Sitemaps');
104    }
105
106    /**
107     * @param ServerRequestInterface $request
108     *
109     * @return ResponseInterface
110     */
111    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
112    {
113        foreach (Tree::all() as $tree) {
114            $include_in_sitemap = (bool) $request->get('sitemap' . $tree->id());
115            $tree->setPreference('include_in_sitemap', (string) $include_in_sitemap);
116        }
117
118        FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.', $this->title()), 'success');
119
120        return redirect($this->getConfigLink());
121    }
122
123    /**
124     * @return ResponseInterface
125     */
126    public function getIndexAction(): ResponseInterface
127    {
128        $timestamp = (int) $this->getPreference('sitemap.timestamp');
129
130        if ($timestamp > Carbon::now()->subSeconds(self::CACHE_LIFE)->unix()) {
131            $content = $this->getPreference('sitemap.xml');
132        } else {
133            $count_individuals = DB::table('individuals')
134                ->groupBy('i_file')
135                ->select([DB::raw('COUNT(*) AS total'), 'i_file'])
136                ->pluck('total', 'i_file');
137
138            $count_media = DB::table('media')
139                ->groupBy('m_file')
140                ->select([DB::raw('COUNT(*) AS total'), 'm_file'])
141                ->pluck('total', 'm_file');
142
143            $count_notes = DB::table('other')
144                ->where('o_type', '=', 'NOTE')
145                ->groupBy('o_file')
146                ->select([DB::raw('COUNT(*) AS total'), 'o_file'])
147                ->pluck('total', 'o_file');
148
149            $count_repositories = DB::table('other')
150                ->where('o_type', '=', 'REPO')
151                ->groupBy('o_file')
152                ->select([DB::raw('COUNT(*) AS total'), 'o_file'])
153                ->pluck('total', 'o_file');
154
155            $count_sources = DB::table('sources')
156                ->groupBy('s_file')
157                ->select([DB::raw('COUNT(*) AS total'), 's_file'])
158                ->pluck('total', 's_file');
159
160            $content = view('modules/sitemap/sitemap-index.xml', [
161                'all_trees'          => Tree::all(),
162                'count_individuals'  => $count_individuals,
163                'count_media'        => $count_media,
164                'count_notes'        => $count_notes,
165                'count_repositories' => $count_repositories,
166                'count_sources'      => $count_sources,
167                'last_mod'           => date('Y-m-d'),
168                'records_per_volume' => self::RECORDS_PER_VOLUME,
169            ]);
170
171            $this->setPreference('sitemap.xml', $content);
172        }
173
174        return response($content, StatusCodeInterface::STATUS_OK, [
175            'Content-Type' => 'application/xml',
176        ]);
177    }
178
179    /**
180     * @param ServerRequestInterface $request
181     *
182     * @return ResponseInterface
183     */
184    public function getFileAction(ServerRequestInterface $request): ResponseInterface
185    {
186        $file = $request->get('file', '');
187
188        if (!preg_match('/^(\d+)-([imnrs])-(\d+)$/', $file, $match)) {
189            throw new NotFoundHttpException('Bad sitemap file');
190        }
191
192        $timestamp   = (int) $this->getPreference('sitemap-' . $file . '.timestamp');
193        $expiry_time = Carbon::now()->subSeconds(self::CACHE_LIFE)->unix();
194
195        if ($timestamp > $expiry_time) {
196            $content = $this->getPreference('sitemap-' . $file . '.xml');
197        } else {
198            $tree = Tree::findById((int) $match[1]);
199
200            if ($tree === null) {
201                throw new NotFoundHttpException('No such tree');
202            }
203
204            $records = $this->sitemapRecords($tree, $match[2], self::RECORDS_PER_VOLUME, self::RECORDS_PER_VOLUME * $match[3]);
205
206            $content = view('modules/sitemap/sitemap-file.xml', ['records' => $records]);
207
208            $this->setPreference('sitemap.xml', $content);
209        }
210
211        return response($content, StatusCodeInterface::STATUS_OK, [
212            'Content-Type' => 'application/xml',
213        ]);
214    }
215
216    /**
217     * @param Tree   $tree
218     * @param string $type
219     * @param int    $limit
220     * @param int    $offset
221     *
222     * @return Collection
223     * @return GedcomRecord[]
224     */
225    private function sitemapRecords(Tree $tree, string $type, int $limit, int $offset): Collection
226    {
227        switch ($type) {
228            case 'i':
229                $records = $this->sitemapIndividuals($tree, $limit, $offset);
230                break;
231
232            case 'm':
233                $records = $this->sitemapMedia($tree, $limit, $offset);
234                break;
235
236            case 'n':
237                $records = $this->sitemapNotes($tree, $limit, $offset);
238                break;
239
240            case 'r':
241                $records = $this->sitemapRepositories($tree, $limit, $offset);
242                break;
243
244            case 's':
245                $records = $this->sitemapSources($tree, $limit, $offset);
246                break;
247
248            default:
249                throw new NotFoundHttpException('Invalid record type: ' . $type);
250        }
251
252        // Skip private records.
253        $records = $records->filter(GedcomRecord::accessFilter());
254
255        return $records;
256    }
257
258    /**
259     * @param Tree $tree
260     * @param int  $limit
261     * @param int  $offset
262     *
263     * @return Collection
264     * @return Individual[]
265     */
266    private function sitemapIndividuals(Tree $tree, int $limit, int $offset): Collection
267    {
268        return DB::table('individuals')
269            ->where('i_file', '=', $tree->id())
270            ->orderBy('i_id')
271            ->skip($offset)
272            ->take($limit)
273            ->get()
274            ->map(Individual::rowMapper());
275    }
276
277    /**
278     * @param Tree $tree
279     * @param int  $limit
280     * @param int  $offset
281     *
282     * @return Collection
283     * @return Media[]
284     */
285    private function sitemapMedia(Tree $tree, int $limit, int $offset): Collection
286    {
287        return DB::table('media')
288            ->where('m_file', '=', $tree->id())
289            ->orderBy('m_id')
290            ->skip($offset)
291            ->take($limit)
292            ->get()
293            ->map(Media::rowMapper());
294    }
295
296    /**
297     * @param Tree $tree
298     * @param int  $limit
299     * @param int  $offset
300     *
301     * @return Collection
302     * @return Note[]
303     */
304    private function sitemapNotes(Tree $tree, int $limit, int $offset): Collection
305    {
306        return DB::table('other')
307            ->where('o_file', '=', $tree->id())
308            ->where('o_type', '=', 'NOTE')
309            ->orderBy('o_id')
310            ->skip($offset)
311            ->take($limit)
312            ->get()
313            ->map(Note::rowMapper());
314    }
315
316    /**
317     * @param Tree $tree
318     * @param int  $limit
319     * @param int  $offset
320     *
321     * @return Collection
322     * @return Repository[]
323     */
324    private function sitemapRepositories(Tree $tree, int $limit, int $offset): Collection
325    {
326        return DB::table('other')
327            ->where('o_file', '=', $tree->id())
328            ->where('o_type', '=', 'REPO')
329            ->orderBy('o_id')
330            ->skip($offset)
331            ->take($limit)
332            ->get()
333            ->map(Repository::rowMapper());
334    }
335
336    /**
337     * @param Tree $tree
338     * @param int  $limit
339     * @param int  $offset
340     *
341     * @return Collection
342     * @return Source[]
343     */
344    private function sitemapSources(Tree $tree, int $limit, int $offset): Collection
345    {
346        return DB::table('sources')
347            ->where('s_file', '=', $tree->id())
348            ->orderBy('s_id')
349            ->skip($offset)
350            ->take($limit)
351            ->get()
352            ->map(Source::rowMapper());
353    }
354}
355