Migrate Laysense to unmodified LaySite 1.5.2

This commit is contained in:
2026-11-28 14:44:34 +08:00
parent e39439467d
commit b08008c9ca
134 changed files with 3485 additions and 754 deletions
+26 -15
View File
@@ -12,7 +12,7 @@ use Throwable;
final class SiteBackup
{
private const FORMAT = 'laysite-site';
private const VERSION = 4;
private const VERSION = 6;
private const TABLES = [
'site_settings', 'design_presets', 'pages', 'sections', 'article_categories', 'articles',
'article_category_links', 'article_redirects', 'article_deletions', 'media',
@@ -111,6 +111,7 @@ final class SiteBackup
}
Database::write(function (PDO $pdo) use ($tables, $staged, $directory, &$installed): void {
self::replaceTables($pdo, $tables);
DesignLibrary::ensureDefaultPalette($pdo);
foreach ($staged as $name => $source) {
$target = $directory . '/' . $name;
if (is_file($target) && !is_link($target) && hash_file('sha256', $target) === hash_file('sha256', $source)) {
@@ -140,7 +141,7 @@ final class SiteBackup
private static function prepare(mixed $manifest): array
{
if (!is_array($manifest) || ($manifest['format'] ?? '') !== self::FORMAT
|| !in_array($manifest['version'] ?? null, [1, 2, 3, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) {
|| !in_array($manifest['version'] ?? null, [1, 2, 3, 4, 5, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) {
throw new RuntimeException('备份格式不受支持,请使用相同或更新版本的 LaySite。');
}
$tables = $manifest['tables'];
@@ -157,8 +158,8 @@ final class SiteBackup
}
}
}
if (count($tables['site_settings']) !== 1 || !in_array('home', array_column($tables['pages'], 'slug'), true)) {
throw new RuntimeException('备份缺少站点身份或首页。');
if (count($tables['site_settings']) !== 1) {
throw new RuntimeException('备份缺少站点身份。');
}
$designs = [];
foreach ($tables['design_presets'] as &$row) {
@@ -172,6 +173,11 @@ final class SiteBackup
throw new RuntimeException('备份的站点名称无效。');
}
$site = ['name' => $settings['name'], 'cdn' => ['origin' => Assets::cdn($settings['cdn']['origin'] ?? '')], 'revisions' => ['general' => time()]];
$site['head_html'] = PageHead::normalize($settings['head_html'] ?? '');
$site['favicon'] = SiteIcon::normalize($settings['favicon'] ?? '');
if (array_key_exists('palette_id', $settings)) {
$site['palette_id'] = self::selection($designs, 'palette', $settings['palette_id']);
}
$layoutDefaults = $settings['layout_defaults'] ?? [];
if (!is_array($layoutDefaults)) {
throw new RuntimeException('备份的基础页头页脚配置无效。');
@@ -188,21 +194,25 @@ final class SiteBackup
foreach ($tables['pages'] as &$row) {
$layout = $row['layout_id'] ?? null;
$row['page_type'] ??= 'page';
if ($row['page_type'] !== 'page') {
if ($layout !== null || !is_string($row['page_type']) || !SystemPage::slugMatches($row['page_type'], $row['slug'] ?? null)) {
throw new RuntimeException('备份包含无效的系统页面。');
}
if (($row['status'] ?? null) === 'published') {
$publishedSystemPages[$row['slug']] = $row['page_type'];
}
} elseif ($layout === null) {
PageDraft::slug($row['slug'] ?? null, array_diff(PageDraft::RESERVED, ['home']));
} else {
PageDraft::slug($row['slug'] ?? null, includeSpecial: true);
if ($manifest['version'] < 6 && $layout === null && $row['slug'] === 'home' && $row['page_type'] === 'page') {
$row['page_type'] = 'home';
}
if (!is_string($row['page_type'])) {
throw new RuntimeException('备份包含无效的页面类型。');
}
if ($layout !== null) {
$kind = is_int($layout) ? ($designs[$layout] ?? null) : null;
if (!in_array($kind, ['header', 'footer'], true) || ($row['slug'] ?? null) !== LayoutPage::slug($kind, $layout)) {
if (!in_array($kind, ['header', 'footer'], true) || $row['page_type'] !== 'page') {
throw new RuntimeException('备份包含无效的页头或页脚页面。');
}
$row['status'] = 'published';
} elseif (isset(SystemPage::TYPES[$row['page_type']])) {
if (($row['status'] ?? null) === 'published') {
$publishedSystemPages[$row['slug']] = $row['page_type'];
}
} elseif (!in_array($row['page_type'], ['page', 'home'], true)) {
throw new RuntimeException('备份包含无效的页面类型。');
}
self::validateAssetUrl($row['image'] ?? '', true);
$row['actions'] = Database::json(Buttons::validate(self::decode($row['actions'] ?? '[]')));
@@ -210,6 +220,7 @@ final class SiteBackup
$row['visibility'] = Database::json(array_intersect_key(self::decode($row['visibility'] ?? '{}'), array_flip(['header', 'hero', 'footer'])));
$row['alignments'] = Database::json(self::decode($row['alignments'] ?? '{}'));
$row['presentation'] = CanvasStyle::encode(CanvasStyle::normalize(self::decode($row['presentation'] ?? '{}'), CanvasStyle::PAGE));
$row['head_html'] = $layout === null ? PageHead::normalize($row['head_html'] ?? '') : '';
if (($row['theme'] ?? '') !== '' && !isset(PageTemplates::groups()[$row['theme']])) {
$row['theme'] = '';
}