xref: /webtrees/app/Http/RequestHandlers/RedirectNotePhp.php (revision 15c4f62c40de0c794536302daf7d5ec0548a1973)
1f2ab2ba5SGreg Roach<?php
2f2ab2ba5SGreg Roach
3f2ab2ba5SGreg Roach/**
4f2ab2ba5SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6f2ab2ba5SGreg Roach * This program is free software: you can redistribute it and/or modify
7f2ab2ba5SGreg Roach * it under the terms of the GNU General Public License as published by
8f2ab2ba5SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f2ab2ba5SGreg Roach * (at your option) any later version.
10f2ab2ba5SGreg Roach * This program is distributed in the hope that it will be useful,
11f2ab2ba5SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f2ab2ba5SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f2ab2ba5SGreg Roach * GNU General Public License for more details.
14f2ab2ba5SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16f2ab2ba5SGreg Roach */
17fcfa147eSGreg Roach
18f2ab2ba5SGreg Roachdeclare(strict_types=1);
19f2ab2ba5SGreg Roach
20f2ab2ba5SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21f2ab2ba5SGreg Roach
22f2ab2ba5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23*15c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
24d7bd16c3SGreg Roachuse Fisharebest\Webtrees\Note;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26d7bd16c3SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
27399d26a6SGreg Roachuse Fisharebest\Webtrees\Site;
28d7bd16c3SGreg Roachuse Fisharebest\Webtrees\Tree;
2981b729d3SGreg Roachuse Fisharebest\Webtrees\Validator;
30f2ab2ba5SGreg Roachuse Psr\Http\Message\ResponseInterface;
31f2ab2ba5SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32f2ab2ba5SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33f2ab2ba5SGreg Roach
34f2ab2ba5SGreg Roach/**
35f2ab2ba5SGreg Roach * Redirect URLs created by webtrees 1.x (and PhpGedView).
36f2ab2ba5SGreg Roach */
37f2ab2ba5SGreg Roachclass RedirectNotePhp implements RequestHandlerInterface
38f2ab2ba5SGreg Roach{
39*15c4f62cSGreg Roach    public function __construct(
40*15c4f62cSGreg Roach        private readonly TreeService $tree_service,
41*15c4f62cSGreg Roach    ) {
42d7bd16c3SGreg Roach    }
43d7bd16c3SGreg Roach
44f2ab2ba5SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
45f2ab2ba5SGreg Roach    {
46399d26a6SGreg Roach        $ged  = Validator::queryParams($request)->string('ged', Site::getPreference('DEFAULT_GEDCOM'));
47b55cbc6bSGreg Roach        $nid  = Validator::queryParams($request)->isXref()->string('nid');
48d7bd16c3SGreg Roach        $tree = $this->tree_service->all()->get($ged);
49f2ab2ba5SGreg Roach
50d7bd16c3SGreg Roach        if ($tree instanceof Tree) {
516b9cb339SGreg Roach            $note = Registry::noteFactory()->make($nid, $tree);
52f3874e19SGreg Roach
53d7bd16c3SGreg Roach            if ($note instanceof Note) {
54*15c4f62cSGreg Roach                $url = $note->url();
55*15c4f62cSGreg Roach
56*15c4f62cSGreg Roach                return Registry::responseFactory()
57*15c4f62cSGreg Roach                    ->redirectUrl($url, StatusCodeInterface::STATUS_MOVED_PERMANENTLY)
58*15c4f62cSGreg Roach                    ->withHeader('Link', '<' . $url . '>; rel="canonical"');
59d7bd16c3SGreg Roach            }
60d7bd16c3SGreg Roach        }
61f3874e19SGreg Roach
62*15c4f62cSGreg Roach        throw new HttpGoneException();
63f2ab2ba5SGreg Roach    }
64f2ab2ba5SGreg Roach}
65