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