forked from laysense/laysite
Prepare LaySite 1.5.1 page routing and template updates
This commit is contained in:
@@ -12,7 +12,7 @@ use Throwable;
|
||||
final class Database
|
||||
{
|
||||
public const MIGRATION_REQUIRED = '数据库尚未完成升级。请在站点目录运行 php bin/install.php。';
|
||||
private const SCHEMA_VERSION = 8;
|
||||
private const SCHEMA_VERSION = 9;
|
||||
// Avoid reusing a deleted ID that may still be open in an editing form.
|
||||
public const NEXT_ARTICLE_ID = 'MAX(COALESCE((SELECT MAX(id) FROM articles), 0),
|
||||
COALESCE((SELECT MAX(article_id) FROM article_deletions), 0)) + 1';
|
||||
@@ -54,7 +54,8 @@ final class Database
|
||||
if ($version > self::SCHEMA_VERSION) {
|
||||
throw new RuntimeException('当前 SQLite 数据库需要更新版本的 LaySite。');
|
||||
}
|
||||
$pdo->exec(file_get_contents($root . '/resources/schema.sql'));
|
||||
$schema = file_get_contents($root . '/resources/schema.sql');
|
||||
$pdo->exec($schema);
|
||||
if ($version < 3) {
|
||||
foreach (['pages', 'sections'] as $table) {
|
||||
$columns = $pdo->query('PRAGMA table_info(' . $table . ')')->fetchAll();
|
||||
@@ -101,12 +102,32 @@ final class Database
|
||||
if ($version < 5) {
|
||||
LayoutPage::migrate($pdo);
|
||||
}
|
||||
if ($version < 9) {
|
||||
self::migratePageRoutes($pdo, $schema);
|
||||
DesignLibrary::ensureDefaultPalette($pdo);
|
||||
}
|
||||
if ($version < self::SCHEMA_VERSION) {
|
||||
$pdo->exec('PRAGMA user_version = ' . self::SCHEMA_VERSION);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static function migratePageRoutes(PDO $pdo, string $schema): void
|
||||
{
|
||||
// Renaming the parent also retargets the old child table's foreign key.
|
||||
$pdo->exec('ALTER TABLE pages RENAME TO pages_before_routes');
|
||||
$pdo->exec('ALTER TABLE sections RENAME TO sections_before_routes');
|
||||
$pdo->exec($schema);
|
||||
foreach (['pages', 'sections'] as $table) {
|
||||
$columns = implode(', ', array_column($pdo->query("PRAGMA table_info($table)")->fetchAll(), 'name'));
|
||||
$pdo->exec("INSERT INTO $table (rowid, $columns) SELECT rowid, $columns FROM {$table}_before_routes");
|
||||
}
|
||||
$pdo->exec('DROP TABLE sections_before_routes');
|
||||
$pdo->exec('DROP TABLE pages_before_routes');
|
||||
$pdo->exec('CREATE UNIQUE INDEX pages_layout_id ON pages(layout_id)');
|
||||
$pdo->exec("UPDATE pages SET page_type = 'home' WHERE slug = 'home' AND page_type = 'page' AND layout_id IS NULL");
|
||||
}
|
||||
|
||||
public static function write(callable $operation): mixed
|
||||
{
|
||||
$pdo = self::connect();
|
||||
|
||||
@@ -39,9 +39,27 @@ final class DesignLibrary
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
public static function ensureDefaultPalette(PDO $pdo): void
|
||||
{
|
||||
$site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
if (isset($site['palette_id'])) {
|
||||
return;
|
||||
}
|
||||
$pdo->prepare("INSERT INTO design_presets (kind, name, preset, payload) VALUES ('palette', ?, 'custom', ?)")
|
||||
->execute(['默认配色', Database::json(self::defaults('palette'))]);
|
||||
$site['palette_id'] = (int) $pdo->lastInsertId();
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
}
|
||||
|
||||
public static function layouts(): array
|
||||
{
|
||||
return Database::connect()->query("SELECT d.id, d.kind, d.name, pages.slug FROM design_presets d
|
||||
JOIN pages ON pages.layout_id = d.id WHERE d.kind IN ('header', 'footer') ORDER BY d.kind, d.id")->fetchAll();
|
||||
}
|
||||
|
||||
public static function palettes(): array
|
||||
{
|
||||
$rows = Database::connect()->query("SELECT id, name, preset, payload FROM design_presets
|
||||
$rows = Database::connect()->query("SELECT id, name, preset, payload, version FROM design_presets
|
||||
WHERE kind = 'palette' ORDER BY id")->fetchAll();
|
||||
foreach ($rows as &$row) {
|
||||
$row['colors'] = self::normalize('palette', $row['preset'], json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR));
|
||||
|
||||
@@ -9,25 +9,14 @@ use RuntimeException;
|
||||
|
||||
final class LayoutPage
|
||||
{
|
||||
public const TYPES = ['page' => '普通页面', 'header' => '页头', 'footer' => '页脚'];
|
||||
public const TYPES = ['page' => '普通页面', 'home' => '首页', 'header' => '页头', 'footer' => '页脚'];
|
||||
|
||||
public static function slug(string $kind, int $id): string
|
||||
{
|
||||
return "layout--$kind-$id";
|
||||
}
|
||||
|
||||
public static function create(string $kind, string $name): string
|
||||
{
|
||||
return Database::write(static function (PDO $pdo) use ($kind, $name): string {
|
||||
$id = self::insertPreset($pdo, $kind, $name);
|
||||
$slug = self::slug($kind, $id);
|
||||
Repository::insertPage($pdo, $slug, $name, ['visibility' => ['hero' => false]], $id);
|
||||
|
||||
return $slug;
|
||||
});
|
||||
}
|
||||
|
||||
private static function insertPreset(PDO $pdo, string $kind, string $name): int
|
||||
public static function insertPreset(PDO $pdo, string $kind, string $name): int
|
||||
{
|
||||
$pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')
|
||||
->execute([$kind, $name, array_key_first(DesignLibrary::PRESETS[$kind]), Database::json(DesignLibrary::defaults($kind))]);
|
||||
|
||||
+43
-11
@@ -30,15 +30,26 @@ final class Repository
|
||||
|
||||
public function pages(bool $publishedOnly = false): array
|
||||
{
|
||||
$where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type = 'page'" : '';
|
||||
$where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type IN ('page', 'home')" : '';
|
||||
|
||||
return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, pages.page_type, d.kind AS layout_kind
|
||||
return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, pages.page_type, pages.layout_id, d.kind AS layout_kind
|
||||
FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id' . $where . ' ORDER BY pages.rowid')->fetchAll();
|
||||
}
|
||||
|
||||
public function homepage(): ?array
|
||||
{
|
||||
$result = $this->one("SELECT slug FROM pages WHERE page_type = 'home'
|
||||
AND layout_id IS NULL AND status = 'published' ORDER BY RANDOM() LIMIT 1", []);
|
||||
|
||||
return $result ? $this->page($result['slug']) : null;
|
||||
}
|
||||
|
||||
public function createPage(string $slug, string $name, array $template = [], string $type = 'page'): void
|
||||
{
|
||||
Database::write(static fn (PDO $pdo) => self::insertPage($pdo, $slug, $name, $template, type: $type));
|
||||
Database::write(static function (PDO $pdo) use ($slug, $name, $template, $type): void {
|
||||
$layout = in_array($type, ['header', 'footer'], true) ? LayoutPage::insertPreset($pdo, $type, $name) : null;
|
||||
self::insertPage($pdo, $slug, $name, $template, $layout, $layout === null ? $type : 'page');
|
||||
});
|
||||
}
|
||||
|
||||
public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null, string $type = 'page'): void
|
||||
@@ -98,7 +109,7 @@ final class Repository
|
||||
$catalog = $feed['mode'] === 'catalog';
|
||||
$grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1);
|
||||
$archivePath = '/articles/feed/' . $page['slug'] . '/' . $section['key'];
|
||||
$section['archivePath'] = $feed['categories'] && empty($page['layout_kind']) && ($page['page_type'] ?? 'page') === 'page' && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['archivePath'] = $feed['categories'] && empty($page['layout_kind']) && in_array($page['page_type'] ?? 'page', ['page', 'home'], true) && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['articleFeed'] = [];
|
||||
$section['networkFeed'] = $network;
|
||||
$section['catalogFeed'] = $catalog;
|
||||
@@ -125,29 +136,50 @@ final class Repository
|
||||
{
|
||||
return $this->one("SELECT sections.title, sections.feed FROM sections
|
||||
JOIN pages ON pages.slug = sections.page_slug
|
||||
WHERE pages.slug = ? AND pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type = 'page'
|
||||
WHERE pages.slug = ? AND pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type IN ('page', 'home')
|
||||
AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]);
|
||||
}
|
||||
|
||||
public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): array
|
||||
{
|
||||
return Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): array {
|
||||
$existing = $this->one('SELECT pages.version, pages.page_type, pages.layout_id, d.kind AS layout_kind
|
||||
FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id WHERE pages.slug = ?', [$slug]);
|
||||
self::checkVersion($existing !== null && $existing['version'] === $version ? 1 : 0);
|
||||
$target = $data['slug'] ?? $slug;
|
||||
if ($target !== $slug && $this->one('SELECT 1 FROM pages WHERE slug = ?', [$target])) {
|
||||
throw new RuntimeException('此页面地址已存在。');
|
||||
}
|
||||
$previousType = $existing['layout_kind'] ?? $existing['page_type'];
|
||||
$type = $data['type'] ?? $previousType;
|
||||
$layout = $existing['layout_id'];
|
||||
if ($type !== $previousType) {
|
||||
$layout = in_array($type, ['header', 'footer'], true) ? LayoutPage::insertPreset($pdo, $type, $data['name']) : null;
|
||||
}
|
||||
$head = $data['head_html'] ?? null;
|
||||
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
$query = $pdo->prepare('UPDATE pages SET slug = ?, page_type = ?, layout_id = ?, name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
image = ?, alt = ?, image_scale = ?, alignments = ?, actions = ?, quicklinks = ?, visibility = ?, header_id = ?, footer_id = ?, palette_id = ?, status = ?, updated_at = ?,
|
||||
presentation = COALESCE(?, presentation), ' . ($head === null ? '' : 'head_html = ?, ')
|
||||
. 'version = version + 1 WHERE slug = ? AND version = ?');
|
||||
$query->execute([
|
||||
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$target, $layout === null ? $type : 'page', $layout, $data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$data['image'], $data['alt'], $data['image_scale'], Database::json($data['alignments']), Database::json($data['actions']), Database::json($data['quicklinks']),
|
||||
Database::json($data['visibility']), $data['header_id'], $data['footer_id'], $data['palette_id'], $data['status'], self::now(),
|
||||
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null,
|
||||
...($head === null ? [] : [$head]), $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
if ($data['status'] === 'draft' && str_starts_with($slug, 'system--')) {
|
||||
SiteDesign::unsetSystemPage($pdo, $slug);
|
||||
if (isset(SystemPage::TYPES[$previousType])) {
|
||||
if ($data['status'] === 'draft' || $previousType !== $type) {
|
||||
SiteDesign::replaceSystemPage($pdo, $slug);
|
||||
} elseif ($target !== $slug) {
|
||||
SiteDesign::replaceSystemPage($pdo, $slug, replacement: $target);
|
||||
}
|
||||
}
|
||||
if ($existing['layout_id'] !== null && $layout !== $existing['layout_id']) {
|
||||
LayoutPage::deletePreset($pdo, $existing['layout_kind'], $existing['layout_id']);
|
||||
}
|
||||
$slug = $target;
|
||||
if ($replaceDraftSections) {
|
||||
$pdo->prepare('DELETE FROM sections WHERE page_slug = ?')->execute([$slug]);
|
||||
}
|
||||
@@ -164,7 +196,7 @@ final class Repository
|
||||
self::writeSectionOrder($pdo, $slug, $sectionOrder);
|
||||
}
|
||||
$page = $this->page($slug);
|
||||
if (isset($data['layoutConfig'])) {
|
||||
if ($type === $previousType && $layout !== null && isset($data['layoutConfig'])) {
|
||||
LayoutPage::saveSettings($pdo, $page, $data['layoutConfig']);
|
||||
}
|
||||
|
||||
@@ -186,7 +218,7 @@ final class Repository
|
||||
if ($page['layout_id'] !== null) {
|
||||
LayoutPage::deletePreset($pdo, $page['layout_kind'], $page['layout_id']);
|
||||
}
|
||||
SiteDesign::unsetSystemPage($pdo, $slug);
|
||||
SiteDesign::replaceSystemPage($pdo, $slug);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
+21
-13
@@ -12,7 +12,7 @@ use Throwable;
|
||||
final class SiteBackup
|
||||
{
|
||||
private const FORMAT = 'laysite-site';
|
||||
private const VERSION = 5;
|
||||
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, 4, 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'];
|
||||
@@ -174,6 +175,9 @@ final class SiteBackup
|
||||
$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 (isset($settings['palette_id'])) {
|
||||
$site['palette_id'] = self::selection($designs, 'palette', $settings['palette_id']);
|
||||
}
|
||||
$layoutDefaults = $settings['layout_defaults'] ?? [];
|
||||
if (!is_array($layoutDefaults)) {
|
||||
throw new RuntimeException('备份的基础页头页脚配置无效。');
|
||||
@@ -190,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);
|
||||
} 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'] ?? '[]')));
|
||||
|
||||
@@ -63,16 +63,20 @@ final class SiteDesign
|
||||
});
|
||||
}
|
||||
|
||||
public static function unsetSystemPage(PDO $pdo, string $slug): void
|
||||
public static function replaceSystemPage(PDO $pdo, string $slug, ?string $replacement = null): void
|
||||
{
|
||||
$site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
$type = array_search($slug, $site['system_pages'] ?? [], true);
|
||||
if ($type === false) {
|
||||
return;
|
||||
}
|
||||
unset($site['system_pages'][$type]);
|
||||
if ($type === 'maintenance') {
|
||||
$site['maintenance_enabled'] = false;
|
||||
if ($replacement !== null) {
|
||||
$site['system_pages'][$type] = $replacement;
|
||||
} else {
|
||||
unset($site['system_pages'][$type]);
|
||||
if ($type === 'maintenance') {
|
||||
$site['maintenance_enabled'] = false;
|
||||
}
|
||||
}
|
||||
$site['revisions']['general'] = ($site['revisions']['general'] ?? 0) + 1;
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
|
||||
@@ -16,7 +16,7 @@ final class SystemPage
|
||||
|
||||
public static function published(Repository $repo, string $type, mixed $slug): ?array
|
||||
{
|
||||
if (!self::slugMatches($type, $slug)) {
|
||||
if (!isset(self::TYPES[$type]) || !is_string($slug) || $slug === '') {
|
||||
return null;
|
||||
}
|
||||
$page = $repo->page($slug);
|
||||
|
||||
Reference in New Issue
Block a user