xref: /webtrees/app/Http/RequestHandlers/UpgradeWizardPage.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
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 Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\TreeService;
25use Fisharebest\Webtrees\Services\UpgradeService;
26use Fisharebest\Webtrees\Validator;
27use Fisharebest\Webtrees\Webtrees;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function basename;
33use function e;
34use function route;
35use function version_compare;
36
37/**
38 * Upgrade to a new version of webtrees.
39 */
40class UpgradeWizardPage implements RequestHandlerInterface
41{
42    use ViewResponseTrait;
43
44    // We make the upgrade in a number of small steps to keep within server time limits.
45    private const STEP_CHECK    = 'Check';
46    private const STEP_PREPARE  = 'Prepare';
47    private const STEP_PENDING  = 'Pending';
48    private const STEP_EXPORT   = 'Export';
49    private const STEP_DOWNLOAD = 'Download';
50    private const STEP_UNZIP    = 'Unzip';
51    private const STEP_COPY     = 'Copy';
52
53    private TreeService $tree_service;
54
55    private UpgradeService $upgrade_service;
56
57    /**
58     * @param TreeService    $tree_service
59     * @param UpgradeService $upgrade_service
60     */
61    public function __construct(TreeService $tree_service, UpgradeService $upgrade_service)
62    {
63        $this->tree_service    = $tree_service;
64        $this->upgrade_service = $upgrade_service;
65    }
66
67    /**
68     * @param ServerRequestInterface $request
69     *
70     * @return ResponseInterface
71     */
72    public function handle(ServerRequestInterface $request): ResponseInterface
73    {
74        $this->layout = 'layouts/administration';
75
76        $continue = Validator::queryParams($request)->string('continue', '');
77
78        $title = I18N::translate('Upgrade wizard');
79
80        $latest_version = $this->upgrade_service->latestVersion();
81
82        $upgrade_available = version_compare($latest_version, Webtrees::VERSION) > 0;
83
84        if ($upgrade_available && $continue === '1') {
85            return $this->viewResponse('admin/upgrade/steps', [
86                'steps' => $this->wizardSteps(),
87                'title' => $title,
88            ]);
89        }
90
91        return $this->viewResponse('admin/upgrade/wizard', [
92            'current_version' => Webtrees::VERSION,
93            'latest_version'  => $latest_version,
94            'title'           => $title,
95        ]);
96    }
97
98    /**
99     * @return array<string>
100     */
101    private function wizardSteps(): array
102    {
103        $download_url = $this->upgrade_service->downloadUrl();
104
105        $export_steps = [];
106
107        foreach ($this->tree_service->all() as $tree) {
108            $route = route(UpgradeWizardStep::class, [
109                'step' => self::STEP_EXPORT,
110                'tree' => $tree->name(),
111            ]);
112
113            $export_steps[$route] = I18N::translate('Export all the family trees to GEDCOM files…') . ' ' . e($tree->title());
114        }
115
116        return [
117                route(UpgradeWizardStep::class, ['step' => self::STEP_CHECK])   => I18N::translate('Upgrade wizard'),
118                route(UpgradeWizardStep::class, ['step' => self::STEP_PREPARE]) => I18N::translate('Create a temporary folder…'),
119                route(UpgradeWizardStep::class, ['step' => self::STEP_PENDING]) => I18N::translate('Check for pending changes…'),
120            ] + $export_steps + [
121                route(UpgradeWizardStep::class, ['step' => self::STEP_DOWNLOAD]) => I18N::translate('Download %s…', e($download_url)),
122                route(UpgradeWizardStep::class, ['step' => self::STEP_UNZIP])    => I18N::translate('Unzip %s to a temporary folder…', e(basename($download_url))),
123                route(UpgradeWizardStep::class, ['step' => self::STEP_COPY])     => I18N::translate('Copy files…'),
124            ];
125    }
126}
127