xref: /webtrees/app/Http/RequestHandlers/UpgradeWizardPage.php (revision e873f434551745f888937263ff89e80db3b0f785)
14b3ef6caSGreg Roach<?php
24b3ef6caSGreg Roach
34b3ef6caSGreg Roach/**
44b3ef6caSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
64b3ef6caSGreg Roach * This program is free software: you can redistribute it and/or modify
74b3ef6caSGreg Roach * it under the terms of the GNU General Public License as published by
84b3ef6caSGreg Roach * the Free Software Foundation, either version 3 of the License, or
94b3ef6caSGreg Roach * (at your option) any later version.
104b3ef6caSGreg Roach * This program is distributed in the hope that it will be useful,
114b3ef6caSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124b3ef6caSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134b3ef6caSGreg Roach * GNU General Public License for more details.
144b3ef6caSGreg Roach * You should have received a copy of the GNU General Public License
154b3ef6caSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164b3ef6caSGreg Roach */
174b3ef6caSGreg Roach
184b3ef6caSGreg Roachdeclare(strict_types=1);
194b3ef6caSGreg Roach
204b3ef6caSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
214b3ef6caSGreg Roach
224b3ef6caSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
234b3ef6caSGreg Roachuse Fisharebest\Webtrees\I18N;
244b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
254b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\UpgradeService;
26748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
274b3ef6caSGreg Roachuse Fisharebest\Webtrees\Webtrees;
284b3ef6caSGreg Roachuse Psr\Http\Message\ResponseInterface;
294b3ef6caSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
304b3ef6caSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
314b3ef6caSGreg Roach
324b3ef6caSGreg Roachuse function basename;
334b3ef6caSGreg Roachuse function e;
344b3ef6caSGreg Roachuse function route;
354b3ef6caSGreg Roachuse function version_compare;
364b3ef6caSGreg Roach
374b3ef6caSGreg Roach/**
384b3ef6caSGreg Roach * Upgrade to a new version of webtrees.
394b3ef6caSGreg Roach */
404b3ef6caSGreg Roachclass UpgradeWizardPage implements RequestHandlerInterface
414b3ef6caSGreg Roach{
424b3ef6caSGreg Roach    use ViewResponseTrait;
434b3ef6caSGreg Roach
444b3ef6caSGreg Roach    // We make the upgrade in a number of small steps to keep within server time limits.
45*e873f434SGreg Roach    private const string STEP_CHECK    = 'Check';
46*e873f434SGreg Roach    private const string STEP_PREPARE  = 'Prepare';
47*e873f434SGreg Roach    private const string STEP_PENDING  = 'Pending';
48*e873f434SGreg Roach    private const string STEP_EXPORT   = 'Export';
49*e873f434SGreg Roach    private const string STEP_DOWNLOAD = 'Download';
50*e873f434SGreg Roach    private const string STEP_UNZIP    = 'Unzip';
51*e873f434SGreg Roach    private const string STEP_COPY     = 'Copy';
524b3ef6caSGreg Roach
53c4943cffSGreg Roach    private TreeService $tree_service;
544b3ef6caSGreg Roach
55c4943cffSGreg Roach    private UpgradeService $upgrade_service;
564b3ef6caSGreg Roach
574b3ef6caSGreg Roach    /**
584b3ef6caSGreg Roach     * @param TreeService    $tree_service
594b3ef6caSGreg Roach     * @param UpgradeService $upgrade_service
604b3ef6caSGreg Roach     */
614b3ef6caSGreg Roach    public function __construct(TreeService $tree_service, UpgradeService $upgrade_service)
624b3ef6caSGreg Roach    {
634b3ef6caSGreg Roach        $this->tree_service    = $tree_service;
644b3ef6caSGreg Roach        $this->upgrade_service = $upgrade_service;
654b3ef6caSGreg Roach    }
664b3ef6caSGreg Roach
674b3ef6caSGreg Roach    /**
684b3ef6caSGreg Roach     * @param ServerRequestInterface $request
694b3ef6caSGreg Roach     *
704b3ef6caSGreg Roach     * @return ResponseInterface
714b3ef6caSGreg Roach     */
724b3ef6caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
734b3ef6caSGreg Roach    {
744b3ef6caSGreg Roach        $this->layout = 'layouts/administration';
754b3ef6caSGreg Roach
76748dbe15SGreg Roach        $continue = Validator::queryParams($request)->string('continue', '');
774b3ef6caSGreg Roach
784b3ef6caSGreg Roach        $title = I18N::translate('Upgrade wizard');
794b3ef6caSGreg Roach
804b3ef6caSGreg Roach        $latest_version = $this->upgrade_service->latestVersion();
814b3ef6caSGreg Roach
824b3ef6caSGreg Roach        $upgrade_available = version_compare($latest_version, Webtrees::VERSION) > 0;
834b3ef6caSGreg Roach
844b3ef6caSGreg Roach        if ($upgrade_available && $continue === '1') {
854b3ef6caSGreg Roach            return $this->viewResponse('admin/upgrade/steps', [
864b3ef6caSGreg Roach                'steps' => $this->wizardSteps(),
874b3ef6caSGreg Roach                'title' => $title,
884b3ef6caSGreg Roach            ]);
894b3ef6caSGreg Roach        }
904b3ef6caSGreg Roach
914b3ef6caSGreg Roach        return $this->viewResponse('admin/upgrade/wizard', [
924b3ef6caSGreg Roach            'current_version' => Webtrees::VERSION,
934b3ef6caSGreg Roach            'latest_version'  => $latest_version,
944b3ef6caSGreg Roach            'title'           => $title,
954b3ef6caSGreg Roach        ]);
964b3ef6caSGreg Roach    }
974b3ef6caSGreg Roach
984b3ef6caSGreg Roach    /**
9924f2a3afSGreg Roach     * @return array<string>
1004b3ef6caSGreg Roach     */
1014b3ef6caSGreg Roach    private function wizardSteps(): array
1024b3ef6caSGreg Roach    {
1034b3ef6caSGreg Roach        $download_url = $this->upgrade_service->downloadUrl();
1044b3ef6caSGreg Roach
1054b3ef6caSGreg Roach        $export_steps = [];
1064b3ef6caSGreg Roach
1074b3ef6caSGreg Roach        foreach ($this->tree_service->all() as $tree) {
1084b3ef6caSGreg Roach            $route = route(UpgradeWizardStep::class, [
1094b3ef6caSGreg Roach                'step' => self::STEP_EXPORT,
1104b3ef6caSGreg Roach                'tree' => $tree->name(),
1114b3ef6caSGreg Roach            ]);
1124b3ef6caSGreg Roach
1134b3ef6caSGreg Roach            $export_steps[$route] = I18N::translate('Export all the family trees to GEDCOM files…') . ' ' . e($tree->title());
1144b3ef6caSGreg Roach        }
1154b3ef6caSGreg Roach
1164b3ef6caSGreg Roach        return [
1174b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_CHECK])   => I18N::translate('Upgrade wizard'),
1184b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_PREPARE]) => I18N::translate('Create a temporary folder…'),
1194b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_PENDING]) => I18N::translate('Check for pending changes…'),
1204b3ef6caSGreg Roach            ] + $export_steps + [
1214b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_DOWNLOAD]) => I18N::translate('Download %s…', e($download_url)),
1224b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_UNZIP])    => I18N::translate('Unzip %s to a temporary folder…', e(basename($download_url))),
1234b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_COPY])     => I18N::translate('Copy files…'),
1244b3ef6caSGreg Roach            ];
1254b3ef6caSGreg Roach    }
1264b3ef6caSGreg Roach}
127