xref: /webtrees/app/Http/RequestHandlers/SynchronizeTrees.php (revision 09523927627d8824ee0799f8137d6de75b8b7921)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Services\AdminService;
27use Fisharebest\Webtrees\Services\TimeoutService;
28use Fisharebest\Webtrees\Services\TreeService;
29use League\Flysystem\FilesystemException;
30use League\Flysystem\UnableToReadFile;
31use League\Flysystem\UnableToRetrieveMetadata;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Message\StreamFactoryInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function redirect;
38use function route;
39
40/**
41 * Synchronize GEDCOM files with trees.
42 */
43class SynchronizeTrees implements RequestHandlerInterface
44{
45    private AdminService $admin_service;
46
47    private StreamFactoryInterface $stream_factory;
48
49    private TimeoutService $timeout_service;
50
51    private TreeService $tree_service;
52
53    /**
54     * @param AdminService        $admin_service
55     * @param StreamFactoryInterface $stream_factory
56     * @param TimeoutService      $timeout_service
57     * @param TreeService         $tree_service
58     */
59    public function __construct(
60        AdminService $admin_service,
61        StreamFactoryInterface $stream_factory,
62        TimeoutService $timeout_service,
63        TreeService $tree_service
64    ) {
65        $this->admin_service   = $admin_service;
66        $this->stream_factory  = $stream_factory;
67        $this->timeout_service = $timeout_service;
68        $this->tree_service    = $tree_service;
69    }
70
71    /**
72     * @param ServerRequestInterface $request
73     *
74     * @return ResponseInterface
75     */
76    public function handle(ServerRequestInterface $request): ResponseInterface
77    {
78        $data_filesystem = Registry::filesystem()->data();
79
80        $gedcom_files = $this->admin_service->gedcomFiles($data_filesystem);
81
82        foreach ($gedcom_files as $gedcom_file) {
83            // Only import files that have changed
84            try {
85                $filemtime = (string) $data_filesystem->lastModified($gedcom_file);
86
87                $tree = $this->tree_service->all()->get($gedcom_file) ?? $this->tree_service->create($gedcom_file, $gedcom_file);
88
89                if ($tree->getPreference('filemtime') !== $filemtime) {
90                    $resource = $data_filesystem->readStream($gedcom_file);
91                    $stream   = $this->stream_factory->createStreamFromResource($resource);
92                    $this->tree_service->importGedcomFile($tree, $stream, $gedcom_file, '');
93                    $stream->close();
94                    $tree->setPreference('filemtime', $filemtime);
95
96                    FlashMessages::addMessage(I18N::translate('The GEDCOM file “%s” has been imported.', e($gedcom_file)), 'success');
97
98                    if ($this->timeout_service->isTimeNearlyUp(10.0)) {
99                        return redirect(route(self::class), StatusCodeInterface::STATUS_TEMPORARY_REDIRECT);
100                    }
101                }
102            } catch (FilesystemException | UnableToRetrieveMetadata | UnableToReadFile) {
103                // Can't read the file - skip it.
104            }
105        }
106
107        foreach ($this->tree_service->all() as $tree) {
108            if (!$gedcom_files->containsStrict($tree->name())) {
109                $this->tree_service->delete($tree);
110                FlashMessages::addMessage(I18N::translate('The family tree “%s” has been deleted.', e($tree->title())), 'success');
111
112                if ($this->timeout_service->isTimeNearlyUp(10.0)) {
113                    return redirect(route(self::class), StatusCodeInterface::STATUS_TEMPORARY_REDIRECT);
114                }
115            }
116        }
117
118        return redirect(route(ManageTrees::class, ['tree' => $this->tree_service->all()->first()->name()]));
119    }
120}
121