1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2021 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Flysystem\Adapter\ChrootAdapter;
use Fisharebest\Webtrees\Http\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\GedcomExportService;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Services\UpgradeService;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Webtrees;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Support\Collection;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use function assert;
use function date;
use function e;
use function fclose;
use function intdiv;
use function microtime;
use function response;
use function route;
use function version_compare;
use function view;
/**
* Upgrade to a new version of webtrees.
*/
class UpgradeWizardStep implements RequestHandlerInterface
{
// We make the upgrade in a number of small steps to keep within server time limits.
private const STEP_CHECK = 'Check';
private const STEP_PREPARE = 'Prepare';
private const STEP_PENDING = 'Pending';
private const STEP_EXPORT = 'Export';
private const STEP_DOWNLOAD = 'Download';
private const STEP_UNZIP = 'Unzip';
private const STEP_COPY = 'Copy';
// Where to store our temporary files.
private const UPGRADE_FOLDER = 'data/tmp/upgrade/';
// Where to store the downloaded ZIP archive.
private const ZIP_FILENAME = 'data/tmp/webtrees.zip';
// The ZIP archive stores everything inside this top-level folder.
private const ZIP_FILE_PREFIX = 'webtrees';
// Cruft can accumulate after upgrades.
private const FOLDERS_TO_CLEAN = [
'app',
'resources',
'vendor',
];
private GedcomExportService $gedcom_export_service;
private UpgradeService $upgrade_service;
private TreeService $tree_service;
/**
* UpgradeController constructor.
*
* @param GedcomExportService $gedcom_export_service
* @param TreeService $tree_service
* @param UpgradeService $upgrade_service
*/
public function __construct(
GedcomExportService $gedcom_export_service,
TreeService $tree_service,
UpgradeService $upgrade_service
) {
$this->gedcom_export_service = $gedcom_export_service;
$this->tree_service = $tree_service;
$this->upgrade_service = $upgrade_service;
}
/**
* Perform one step of the wizard
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$root_filesystem = Registry::filesystem()->root();
$data_filesystem = Registry::filesystem()->data();
// Somewhere to unpack a .ZIP file
$temporary_filesystem = new Filesystem(new ChrootAdapter($root_filesystem, self::UPGRADE_FOLDER));
$zip_file = Webtrees::ROOT_DIR . self::ZIP_FILENAME;
$zip_folder = Webtrees::ROOT_DIR . self::UPGRADE_FOLDER;
$step = $request->getQueryParams()['step'] ?? self::STEP_CHECK;
switch ($step) {
case self::STEP_CHECK:
return $this->wizardStepCheck();
case self::STEP_PREPARE:
return $this->wizardStepPrepare($root_filesystem);
case self::STEP_PENDING:
return $this->wizardStepPending();
case self::STEP_EXPORT:
$tree_name = $request->getQueryParams()['tree'] ?? '';
$tree = $this->tree_service->all()[$tree_name];
assert($tree instanceof Tree);
return $this->wizardStepExport($tree, $data_filesystem);
case self::STEP_DOWNLOAD:
return $this->wizardStepDownload($root_filesystem);
case self::STEP_UNZIP:
return $this->wizardStepUnzip($zip_file, $zip_folder);
case self::STEP_COPY:
return $this->wizardStepCopyAndCleanUp($zip_file, $root_filesystem, $temporary_filesystem);
default:
return response('', StatusCodeInterface::STATUS_NO_CONTENT);
}
}
/**
* @return ResponseInterface
*/
private function wizardStepCheck(): ResponseInterface
{
$latest_version = $this->upgrade_service->latestVersion();
if ($latest_version === '') {
throw new HttpServerErrorException(I18N::translate('No upgrade information is available.'));
}
if (version_compare(Webtrees::VERSION, $latest_version) >= 0) {
$message = I18N::translate('This is the latest version of webtrees. No upgrade is available.');
throw new HttpServerErrorException($message);
}
/* I18N: %s is a version number, such as 1.2.3 */
$alert = I18N::translate('Upgrade to webtrees %s.', e($latest_version));
return response(view('components/alert-success', [
'alert' => $alert,
]));
}
/**
* Make sure the temporary folder exists.
*
* @param FilesystemOperator $root_filesystem
*
* @return ResponseInterface
*/
private function wizardStepPrepare(FilesystemOperator $root_filesystem): ResponseInterface
{
$root_filesystem->deleteDirectory(self::UPGRADE_FOLDER);
$root_filesystem->createDirectory(self::UPGRADE_FOLDER);
return response(view('components/alert-success', [
'alert' => I18N::translate('The folder %s has been created.', e(self::UPGRADE_FOLDER)),
]));
}
/**
* @return ResponseInterface
*/
private function wizardStepPending(): ResponseInterface
{
$changes = DB::table('change')->where('status', '=', 'pending')->exists();
if ($changes) {
return response(view('components/alert-danger', [
'alert' => I18N::translate('You should accept or reject all pending changes before upgrading.'),
]), StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
}
return response(view('components/alert-success', [
'alert' => I18N::translate('There are no pending changes.'),
]));
}
/**
* @param Tree $tree
* @param FilesystemOperator $data_filesystem
*
* @return ResponseInterface
*/
private function wizardStepExport(Tree $tree, FilesystemOperator $data_filesystem): ResponseInterface
{
$filename = $tree->name() . date('-Y-m-d') . '.ged';
$stream = $this->gedcom_export_service->export($tree);
$data_filesystem->writeStream($tree->name() . date('-Y-m-d') . '.ged', $stream);
fclose($stream);
return response(view('components/alert-success', [
'alert' => I18N::translate('The family tree has been exported to %s.', e($filename)),
]));
}
/**
* @param FilesystemOperator $root_filesystem
*
* @return ResponseInterface
*/
private function wizardStepDownload(FilesystemOperator $root_filesystem): ResponseInterface
{
$start_time = microtime(true);
$download_url = $this->upgrade_service->downloadUrl();
try {
$bytes = $this->upgrade_service->downloadFile($download_url, $root_filesystem, self::ZIP_FILENAME);
} catch (Throwable $exception) {
throw new HttpServerErrorException($exception->getMessage());
}
$kb = I18N::number(intdiv($bytes + 1023, 1024));
$end_time = microtime(true);
$seconds = I18N::number($end_time - $start_time, 2);
return response(view('components/alert-success', [
'alert' => I18N::translate('%1$s KB were downloaded in %2$s seconds.', $kb, $seconds),
]));
}
/**
* For performance reasons, we use direct filesystem access for this step.
*
* @param string $zip_file
* @param string $zip_folder
*
* @return ResponseInterface
*/
private function wizardStepUnzip(string $zip_file, string $zip_folder): ResponseInterface
{
$start_time = microtime(true);
$this->upgrade_service->extractWebtreesZip($zip_file, $zip_folder);
$count = $this->upgrade_service->webtreesZipContents($zip_file)->count();
$end_time = microtime(true);
$seconds = I18N::number($end_time - $start_time, 2);
/* I18N: …from the .ZIP file, %2$s is a (fractional) number of seconds */
$alert = I18N::plural('%1$s file was extracted in %2$s seconds.', '%1$s files were extracted in %2$s seconds.', $count, I18N::number($count), $seconds);
return response(view('components/alert-success', [
'alert' => $alert,
]));
}
/**
* @param string $zip_file
* @param FilesystemOperator $root_filesystem
* @param FilesystemOperator $temporary_filesystem
*
* @return ResponseInterface
*/
private function wizardStepCopyAndCleanUp(
string $zip_file,
FilesystemOperator $root_filesystem,
FilesystemOperator $temporary_filesystem
): ResponseInterface {
$source_filesystem = new Filesystem(new ChrootAdapter($temporary_filesystem, self::ZIP_FILE_PREFIX));
$this->upgrade_service->startMaintenanceMode();
$this->upgrade_service->moveFiles($source_filesystem, $root_filesystem);
$this->upgrade_service->endMaintenanceMode();
// While we have time, clean up any old files.
$files_to_keep = $this->upgrade_service->webtreesZipContents($zip_file);
$folders_to_clean = new Collection(self::FOLDERS_TO_CLEAN);
$this->upgrade_service->cleanFiles($root_filesystem, $folders_to_clean, $files_to_keep);
$url = route(ControlPanel::class);
$alert = I18N::translate('The upgrade is complete.');
$button = '<a href="' . e($url) . '" class="btn btn-primary">' . I18N::translate('continue') . '</a>';
return response(view('components/alert-success', [
'alert' => $alert . ' ' . $button,
]));
}
}
|