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