xref: /webtrees/app/Http/RequestHandlers/UpgradeWizardPage.php (revision 45ebc6c6221f90468111ed2015b8449dca331d93)
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\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     * UpgradeController constructor.
59     *
60     * @param TreeService    $tree_service
61     * @param UpgradeService $upgrade_service
62     */
63    public function __construct(TreeService $tree_service, UpgradeService $upgrade_service)
64    {
65        $this->tree_service    = $tree_service;
66        $this->upgrade_service = $upgrade_service;
67    }
68
69    /**
70     * @param ServerRequestInterface $request
71     *
72     * @return ResponseInterface
73     */
74    public function handle(ServerRequestInterface $request): ResponseInterface
75    {
76        $this->layout = 'layouts/administration';
77
78        $continue = Validator::queryParams($request)->string('continue', '');
79
80        $title = I18N::translate('Upgrade wizard');
81
82        $latest_version = $this->upgrade_service->latestVersion();
83
84        $upgrade_available = version_compare($latest_version, Webtrees::VERSION) > 0;
85
86        if ($upgrade_available && $continue === '1') {
87            return $this->viewResponse('admin/upgrade/steps', [
88                'steps' => $this->wizardSteps(),
89                'title' => $title,
90            ]);
91        }
92
93        return $this->viewResponse('admin/upgrade/wizard', [
94            'current_version' => Webtrees::VERSION,
95            'latest_version'  => $latest_version,
96            'title'           => $title,
97        ]);
98    }
99
100
101    /**
102     * @return array<string>
103     */
104    private function wizardSteps(): array
105    {
106        $download_url = $this->upgrade_service->downloadUrl();
107
108        $export_steps = [];
109
110        foreach ($this->tree_service->all() as $tree) {
111            $route = route(UpgradeWizardStep::class, [
112                'step' => self::STEP_EXPORT,
113                'tree' => $tree->name(),
114            ]);
115
116            $export_steps[$route] = I18N::translate('Export all the family trees to GEDCOM files…') . ' ' . e($tree->title());
117        }
118
119        return [
120                route(UpgradeWizardStep::class, ['step' => self::STEP_CHECK])   => I18N::translate('Upgrade wizard'),
121                route(UpgradeWizardStep::class, ['step' => self::STEP_PREPARE]) => I18N::translate('Create a temporary folder…'),
122                route(UpgradeWizardStep::class, ['step' => self::STEP_PENDING]) => I18N::translate('Check for pending changes…'),
123            ] + $export_steps + [
124                route(UpgradeWizardStep::class, ['step' => self::STEP_DOWNLOAD]) => I18N::translate('Download %s…', e($download_url)),
125                route(UpgradeWizardStep::class, ['step' => self::STEP_UNZIP])    => I18N::translate('Unzip %s to a temporary folder…', e(basename($download_url))),
126                route(UpgradeWizardStep::class, ['step' => self::STEP_COPY])     => I18N::translate('Copy files…'),
127            ];
128    }
129}
130