xref: /webtrees/app/Http/RequestHandlers/UpgradeWizardPage.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
14b3ef6caSGreg Roach<?php
24b3ef6caSGreg Roach
34b3ef6caSGreg Roach/**
44b3ef6caSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
26*748dbe15SGreg 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.
454b3ef6caSGreg Roach    private const STEP_CHECK    = 'Check';
464b3ef6caSGreg Roach    private const STEP_PREPARE  = 'Prepare';
474b3ef6caSGreg Roach    private const STEP_PENDING  = 'Pending';
484b3ef6caSGreg Roach    private const STEP_EXPORT   = 'Export';
494b3ef6caSGreg Roach    private const STEP_DOWNLOAD = 'Download';
504b3ef6caSGreg Roach    private const STEP_UNZIP    = 'Unzip';
514b3ef6caSGreg Roach    private const 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     * UpgradeController constructor.
594b3ef6caSGreg Roach     *
604b3ef6caSGreg Roach     * @param TreeService    $tree_service
614b3ef6caSGreg Roach     * @param UpgradeService $upgrade_service
624b3ef6caSGreg Roach     */
634b3ef6caSGreg Roach    public function __construct(TreeService $tree_service, UpgradeService $upgrade_service)
644b3ef6caSGreg Roach    {
654b3ef6caSGreg Roach        $this->tree_service    = $tree_service;
664b3ef6caSGreg Roach        $this->upgrade_service = $upgrade_service;
674b3ef6caSGreg Roach    }
684b3ef6caSGreg Roach
694b3ef6caSGreg Roach    /**
704b3ef6caSGreg Roach     * @param ServerRequestInterface $request
714b3ef6caSGreg Roach     *
724b3ef6caSGreg Roach     * @return ResponseInterface
734b3ef6caSGreg Roach     */
744b3ef6caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
754b3ef6caSGreg Roach    {
764b3ef6caSGreg Roach        $this->layout = 'layouts/administration';
774b3ef6caSGreg Roach
78*748dbe15SGreg Roach        $continue = Validator::queryParams($request)->string('continue', '');
794b3ef6caSGreg Roach
804b3ef6caSGreg Roach        $title = I18N::translate('Upgrade wizard');
814b3ef6caSGreg Roach
824b3ef6caSGreg Roach        $latest_version = $this->upgrade_service->latestVersion();
834b3ef6caSGreg Roach
844b3ef6caSGreg Roach        $upgrade_available = version_compare($latest_version, Webtrees::VERSION) > 0;
854b3ef6caSGreg Roach
864b3ef6caSGreg Roach        if ($upgrade_available && $continue === '1') {
874b3ef6caSGreg Roach            return $this->viewResponse('admin/upgrade/steps', [
884b3ef6caSGreg Roach                'steps' => $this->wizardSteps(),
894b3ef6caSGreg Roach                'title' => $title,
904b3ef6caSGreg Roach            ]);
914b3ef6caSGreg Roach        }
924b3ef6caSGreg Roach
934b3ef6caSGreg Roach        return $this->viewResponse('admin/upgrade/wizard', [
944b3ef6caSGreg Roach            'current_version' => Webtrees::VERSION,
954b3ef6caSGreg Roach            'latest_version'  => $latest_version,
964b3ef6caSGreg Roach            'title'           => $title,
974b3ef6caSGreg Roach        ]);
984b3ef6caSGreg Roach    }
994b3ef6caSGreg Roach
1004b3ef6caSGreg Roach
1014b3ef6caSGreg Roach    /**
10224f2a3afSGreg Roach     * @return array<string>
1034b3ef6caSGreg Roach     */
1044b3ef6caSGreg Roach    private function wizardSteps(): array
1054b3ef6caSGreg Roach    {
1064b3ef6caSGreg Roach        $download_url = $this->upgrade_service->downloadUrl();
1074b3ef6caSGreg Roach
1084b3ef6caSGreg Roach        $export_steps = [];
1094b3ef6caSGreg Roach
1104b3ef6caSGreg Roach        foreach ($this->tree_service->all() as $tree) {
1114b3ef6caSGreg Roach            $route = route(UpgradeWizardStep::class, [
1124b3ef6caSGreg Roach                'step' => self::STEP_EXPORT,
1134b3ef6caSGreg Roach                'tree' => $tree->name(),
1144b3ef6caSGreg Roach            ]);
1154b3ef6caSGreg Roach
1164b3ef6caSGreg Roach            $export_steps[$route] = I18N::translate('Export all the family trees to GEDCOM files…') . ' ' . e($tree->title());
1174b3ef6caSGreg Roach        }
1184b3ef6caSGreg Roach
1194b3ef6caSGreg Roach        return [
1204b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_CHECK])   => I18N::translate('Upgrade wizard'),
1214b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_PREPARE]) => I18N::translate('Create a temporary folder…'),
1224b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_PENDING]) => I18N::translate('Check for pending changes…'),
1234b3ef6caSGreg Roach            ] + $export_steps + [
1244b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_DOWNLOAD]) => I18N::translate('Download %s…', e($download_url)),
1254b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_UNZIP])    => I18N::translate('Unzip %s to a temporary folder…', e(basename($download_url))),
1264b3ef6caSGreg Roach                route(UpgradeWizardStep::class, ['step' => self::STEP_COPY])     => I18N::translate('Copy files…'),
1274b3ef6caSGreg Roach            ];
1284b3ef6caSGreg Roach    }
1294b3ef6caSGreg Roach}
130