Add page publication history and reusable blocks
This commit is contained in:
+226
-32
@@ -30,23 +30,61 @@ 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 IN ('page', 'home')" : '';
|
||||
if ($publishedOnly) {
|
||||
return $this->pdo->query("SELECT p.route_slug AS slug, json_extract(r.payload, '$.updated_at') AS updated_at,
|
||||
json_extract(r.payload, '$.page_type') AS page_type
|
||||
FROM page_publications p JOIN page_revisions r ON r.id = p.revision_id
|
||||
WHERE json_extract(r.payload, '$.layout_kind') IS NULL
|
||||
AND json_extract(r.payload, '$.page_type') IN ('page', 'home') ORDER BY p.rowid")->fetchAll();
|
||||
}
|
||||
|
||||
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();
|
||||
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,
|
||||
CASE WHEN json_extract(r.payload, '$.layout_kind') IS NULL
|
||||
AND json_extract(r.payload, '$.page_type') IN ('page', 'home') THEN p.route_slug END AS public_slug
|
||||
FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id
|
||||
LEFT JOIN page_publications p ON p.draft_slug = pages.slug LEFT JOIN page_revisions r ON r.id = p.revision_id
|
||||
ORDER BY pages.rowid")->fetchAll();
|
||||
}
|
||||
|
||||
public function systemPublications(): array
|
||||
{
|
||||
return $this->pdo->query("SELECT p.route_slug AS slug, json_extract(r.payload, '$.name') AS name,
|
||||
json_extract(r.payload, '$.page_type') AS page_type
|
||||
FROM page_publications p JOIN page_revisions r ON r.id = p.revision_id
|
||||
WHERE json_extract(r.payload, '$.page_type') IN ('maintenance', 'not-found')")->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", []);
|
||||
$row = $this->one("SELECT r.payload FROM page_publications p JOIN page_revisions r ON r.id = p.revision_id
|
||||
WHERE json_extract(r.payload, '$.layout_kind') IS NULL AND json_extract(r.payload, '$.page_type') = 'home'
|
||||
ORDER BY RANDOM() LIMIT 1", []);
|
||||
|
||||
return $result ? $this->page($result['slug']) : null;
|
||||
return $row ? $this->decodePublication($row) : null;
|
||||
}
|
||||
|
||||
public function publishedPage(string $slug): ?array
|
||||
{
|
||||
$row = $this->one('SELECT r.payload FROM page_publications p JOIN page_revisions r ON r.id = p.revision_id
|
||||
WHERE p.route_slug = ?', [$slug]);
|
||||
|
||||
return $row ? $this->decodePublication($row) : null;
|
||||
}
|
||||
|
||||
private function decodePublication(array $row): array
|
||||
{
|
||||
return ReusableBlocks::resolve($this->pdo, json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR), false);
|
||||
}
|
||||
|
||||
public function resolveDraft(array $page): array
|
||||
{
|
||||
return ReusableBlocks::resolve($this->pdo, $page, true);
|
||||
}
|
||||
|
||||
public function createPage(string $slug, string $name, array $template = [], string $type = 'page'): void
|
||||
{
|
||||
Database::write(static function (PDO $pdo) use ($slug, $name, $template, $type): void {
|
||||
Database::write(function (PDO $pdo) use ($slug, $name, $template, $type): void {
|
||||
if (($template['palette_id'] ?? null) === null && ($template['theme'] ?? '') !== '') {
|
||||
$suffix = ' · 模板配色';
|
||||
$paletteName = mb_substr($name, 0, 100 - mb_strlen($suffix)) . $suffix;
|
||||
@@ -54,7 +92,10 @@ final class Repository
|
||||
PageTemplates::palette($template['theme']), 0, false);
|
||||
}
|
||||
$layout = in_array($type, ['header', 'footer'], true) ? LayoutPage::insertPreset($pdo, $type, $name) : null;
|
||||
$this->checkRouteAvailable($slug);
|
||||
self::insertPage($pdo, $slug, $name, $template, $layout, $layout === null ? $type : 'page');
|
||||
$pdo->prepare("UPDATE pages SET status = 'draft' WHERE slug = ?")->execute([$slug]);
|
||||
PageRevisions::record($pdo, $this->page($slug), 'initial');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -80,6 +121,13 @@ final class Repository
|
||||
}
|
||||
}
|
||||
|
||||
private function checkRouteAvailable(string $slug): void
|
||||
{
|
||||
if ($this->one('SELECT 1 FROM page_publications WHERE route_slug = ?', [$slug])) {
|
||||
throw new RuntimeException('此网址仍由另一页面的公开版本使用。');
|
||||
}
|
||||
}
|
||||
|
||||
public function page(string $slug): ?array
|
||||
{
|
||||
$page = $this->one('SELECT pages.*, d.kind AS layout_kind FROM pages
|
||||
@@ -90,18 +138,125 @@ final class Repository
|
||||
foreach (['actions', 'quicklinks', 'alignments', 'visibility', 'presentation'] as $key) {
|
||||
$page[$key] = json_decode($page[$key], true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
$query = $this->pdo->prepare('SELECT * FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
$query = $this->pdo->prepare('SELECT sections.*, r.main_id, r.mirror_id FROM sections
|
||||
LEFT JOIN section_reuse r ON r.page_slug = sections.page_slug AND r.section_key = sections.key
|
||||
WHERE sections.page_slug = ? ORDER BY position, key');
|
||||
$query->execute([$slug]);
|
||||
$page['sections'] = $query->fetchAll();
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
public function layoutPage(int $id): ?array
|
||||
public function layoutPage(int $id, bool $preview = false): ?array
|
||||
{
|
||||
$row = $this->one('SELECT slug FROM pages WHERE layout_id = ?', [$id]);
|
||||
if ($preview) {
|
||||
$row = $this->one('SELECT slug FROM pages WHERE layout_id = ?', [$id]);
|
||||
|
||||
return $row ? $this->page($row['slug']) : null;
|
||||
return $row ? $this->resolveDraft($this->page($row['slug'])) : null;
|
||||
}
|
||||
$publication = $this->one("SELECT r.payload FROM page_publications p JOIN page_revisions r ON r.id = p.revision_id
|
||||
WHERE json_extract(r.payload, '$.layout_id') = CAST(? AS INTEGER)", [$id]);
|
||||
|
||||
return $publication ? $this->decodePublication($publication) : null;
|
||||
}
|
||||
|
||||
public function copyPage(string $slug, string $target, string $name, int $version): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($slug, $target, $name, $version): void {
|
||||
$page = $this->page($slug);
|
||||
self::checkVersion($page && $page['version'] === $version ? 1 : 0);
|
||||
$page = $this->resolveDraft($page);
|
||||
$layout = $page['layout_kind'] === null ? null : LayoutPage::insertPreset($pdo, $page['layout_kind'], $name);
|
||||
$page['sections'] = array_map(static function (array $section): array {
|
||||
unset($section['main_id'], $section['mirror_id'], $section['mirror_source']);
|
||||
|
||||
return $section;
|
||||
}, $page['sections']);
|
||||
$this->checkRouteAvailable($target);
|
||||
self::insertPage($pdo, $target, $name, $page, $layout, $page['page_type']);
|
||||
$pdo->prepare("UPDATE pages SET status = 'draft', image_scale = ?, alignments = ? WHERE slug = ?")
|
||||
->execute([$page['image_scale'], Database::json($page['alignments']), $target]);
|
||||
PageRevisions::record($pdo, $this->page($target), 'copied');
|
||||
});
|
||||
}
|
||||
|
||||
public function restorePage(string $slug, int $revision, int $version): bool
|
||||
{
|
||||
return Database::write(function (PDO $pdo) use ($slug, $revision, $version): bool {
|
||||
$page = $this->page($slug);
|
||||
self::checkVersion($page && $page['version'] === $version ? 1 : 0);
|
||||
$saved = PageRevisions::find($pdo, $slug, $revision);
|
||||
if ($saved === null) {
|
||||
return false;
|
||||
}
|
||||
$fields = ['name', 'title', 'description', 'heading', 'accent', 'lead', 'image', 'alt',
|
||||
'image_scale', 'head_html', 'theme', 'actions', 'quicklinks', 'alignments', 'visibility', 'presentation',
|
||||
'header_id', 'footer_id', 'palette_id'];
|
||||
foreach (['actions', 'quicklinks', 'alignments', 'visibility', 'presentation'] as $field) {
|
||||
$saved[$field] = Database::json($saved[$field]);
|
||||
}
|
||||
foreach (['header', 'footer', 'palette'] as $kind) {
|
||||
$field = $kind . '_id';
|
||||
if (isset($saved[$field]) && !DesignLibrary::find($saved[$field], $kind)) {
|
||||
$saved[$field] = null;
|
||||
}
|
||||
}
|
||||
$changes = implode(', ', array_map(static fn (string $field): string => $field . ' = ?', $fields));
|
||||
$pdo->prepare('UPDATE pages SET ' . $changes . ', updated_at = ?, version = version + 1 WHERE slug = ?')
|
||||
->execute([...array_map(static fn (string $field): mixed => $saved[$field], $fields), self::now(), $slug]);
|
||||
$currentVersions = array_column($page['sections'], 'version', 'key');
|
||||
$pdo->prepare('DELETE FROM sections WHERE page_slug = ?')->execute([$slug]);
|
||||
$updateVersion = $pdo->prepare('UPDATE sections SET version = ? WHERE page_slug = ? AND key = ?');
|
||||
foreach ($saved['sections'] as $section) {
|
||||
self::insertSection($pdo, $slug, $section['key'], $section);
|
||||
// Stale single-section forms must also conflict after a restore.
|
||||
$updateVersion->execute([max($version, (int) $section['version'], $currentVersions[$section['key']] ?? 0) + 1, $slug, $section['key']]);
|
||||
ReusableBlocks::restore($pdo, $slug, $section);
|
||||
}
|
||||
PageRevisions::record($pdo, $this->page($slug), 'restored');
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function editReusableBlock(string $slug, int $version, string $operation, string $value): string
|
||||
{
|
||||
return Database::write(function (PDO $pdo) use ($slug, $version, $operation, $value): string {
|
||||
$page = $this->page($slug);
|
||||
self::checkVersion($page && $page['version'] === $version ? 1 : 0);
|
||||
$key = $value;
|
||||
if ($operation === 'add') {
|
||||
$source = ReusableBlocks::source($pdo, $value, true);
|
||||
if (!$source || !$this->one('SELECT 1 FROM section_reuse WHERE main_id = ?', [$value])) {
|
||||
throw new RuntimeException('找不到此主区块。');
|
||||
}
|
||||
$key = 'block-' . bin2hex(random_bytes(16));
|
||||
$mirror = ReusableBlocks::materialize(['key' => $key], $source);
|
||||
self::insertSection($pdo, $slug, $key, $mirror);
|
||||
ReusableBlocks::set($pdo, $slug, $key, mirror: $value);
|
||||
} else {
|
||||
$section = array_column($page['sections'], null, 'key')[$key] ?? null;
|
||||
if (!$section || $section['style'] === 'hero-links') {
|
||||
throw new RuntimeException('找不到可复用的区块。');
|
||||
}
|
||||
if ($operation === 'main' && !isset($section['mirror_id'])) {
|
||||
if (!isset($section['main_id'])) {
|
||||
ReusableBlocks::set($pdo, $slug, $key, main: bin2hex(random_bytes(16)));
|
||||
}
|
||||
} elseif ($operation === 'detach' && isset($section['mirror_id'])) {
|
||||
$source = ReusableBlocks::source($pdo, $section['mirror_id'], true);
|
||||
$copy = $source ? ReusableBlocks::materialize($section, $source) : $section;
|
||||
ReusableBlocks::set($pdo, $slug, $key);
|
||||
unset($copy['mirror_id']);
|
||||
self::updateSection($pdo, $slug, $key, $copy, $section['version']);
|
||||
} else {
|
||||
throw new RuntimeException('此区块无法执行该操作。');
|
||||
}
|
||||
}
|
||||
self::touchPage($pdo, $slug);
|
||||
|
||||
return $key;
|
||||
});
|
||||
}
|
||||
|
||||
public function withSectionFeeds(array $page): array
|
||||
@@ -113,6 +268,7 @@ final class Repository
|
||||
}
|
||||
$categories ??= $this->categories();
|
||||
$feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$feed['categories'] = array_values(array_intersect($feed['categories'], array_keys($categories)));
|
||||
$network = $feed['mode'] === 'network';
|
||||
$catalog = $feed['mode'] === 'catalog';
|
||||
$grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1);
|
||||
@@ -137,15 +293,20 @@ final class Repository
|
||||
|
||||
public function section(string $page, string $key): ?array
|
||||
{
|
||||
return $this->one('SELECT * FROM sections WHERE page_slug = ? AND key = ?', [$page, $key]);
|
||||
return $this->one('SELECT sections.*, r.main_id, r.mirror_id FROM sections
|
||||
LEFT JOIN section_reuse r ON r.page_slug = sections.page_slug AND r.section_key = sections.key
|
||||
WHERE sections.page_slug = ? AND sections.key = ?', [$page, $key]);
|
||||
}
|
||||
|
||||
public function publishedFeed(string $page, string $key): ?array
|
||||
{
|
||||
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 IN ('page', 'home')
|
||||
AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]);
|
||||
$published = $this->publishedPage($page);
|
||||
if (!$published || $published['layout_kind'] !== null || !in_array($published['page_type'], ['page', 'home'], true)) {
|
||||
return null;
|
||||
}
|
||||
$section = array_column($published['sections'], null, 'key')[$key] ?? null;
|
||||
|
||||
return !empty($section['feed']) ? $section : null;
|
||||
}
|
||||
|
||||
public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): array
|
||||
@@ -157,8 +318,13 @@ final class Repository
|
||||
$this->checkRemovedPageLayouts($existing, $data['originalLayoutIds'] ?? []);
|
||||
}
|
||||
self::checkVersion($existing !== null && $existing['version'] === $version ? 1 : 0);
|
||||
$publication = PageRevisions::publicationInfo($pdo, $slug);
|
||||
$publicationAction = $data['publicationAction'] ?? '';
|
||||
$status = ($publicationAction === 'published' || ($publication !== null && $publicationAction !== 'draft'))
|
||||
? 'published' : 'draft';
|
||||
$target = $data['slug'] ?? $slug;
|
||||
if ($target !== $slug && $this->one('SELECT 1 FROM pages WHERE slug = ?', [$target])) {
|
||||
if ($target !== $slug && ($this->one('SELECT 1 FROM pages WHERE slug = ?', [$target])
|
||||
|| $this->one('SELECT 1 FROM page_publications WHERE route_slug = ? AND draft_slug <> ?', [$target, $slug]))) {
|
||||
throw new RuntimeException('此页面地址已存在。');
|
||||
}
|
||||
$previousType = $existing['layout_kind'] ?? $existing['page_type'];
|
||||
@@ -175,23 +341,20 @@ final class Repository
|
||||
$query->execute([
|
||||
$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(),
|
||||
Database::json($data['visibility']), $data['header_id'], $data['footer_id'], $data['palette_id'], $status, self::now(),
|
||||
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null,
|
||||
...($head === null ? [] : [$head]), $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
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']) {
|
||||
if ($existing['layout_id'] !== null && $layout !== $existing['layout_id']
|
||||
&& $existing['layout_id'] !== ($publication['layout_id'] ?? null)) {
|
||||
LayoutPage::deletePreset($pdo, $existing['layout_kind'], $existing['layout_id']);
|
||||
}
|
||||
$slug = $target;
|
||||
if ($replaceDraftSections) {
|
||||
if ($this->one('SELECT 1 FROM section_reuse WHERE page_slug = ?', [$slug])) {
|
||||
throw new RuntimeException('此页包含复用区块,请按锚点编辑普通区块。');
|
||||
}
|
||||
$pdo->prepare('DELETE FROM sections WHERE page_slug = ?')->execute([$slug]);
|
||||
}
|
||||
foreach ($sections as $section) {
|
||||
@@ -208,7 +371,22 @@ final class Repository
|
||||
}
|
||||
$page = $this->page($slug);
|
||||
if ($type === $previousType && $layout !== null && isset($data['layoutConfig'])) {
|
||||
LayoutPage::saveSettings($pdo, $page, $data['layoutConfig']);
|
||||
LayoutPage::stageSettings($pdo, $this->resolveDraft($page), $data['layoutConfig']);
|
||||
}
|
||||
|
||||
if ($publicationAction === 'published') {
|
||||
if ($layout !== null) {
|
||||
LayoutPage::publishSettings($pdo, $this->resolveDraft($page));
|
||||
}
|
||||
PageRevisions::publish($pdo, $page);
|
||||
} elseif ($publicationAction === 'draft') {
|
||||
PageRevisions::unpublish($pdo, $page);
|
||||
} else {
|
||||
PageRevisions::record($pdo, $page, 'saved');
|
||||
}
|
||||
|
||||
if ($publicationAction !== '' && isset($publication['layout_id']) && $publication['layout_id'] !== $layout) {
|
||||
LayoutPage::deletePreset($pdo, $publication['layout_kind'], $publication['layout_id']);
|
||||
}
|
||||
|
||||
return $page;
|
||||
@@ -234,13 +412,17 @@ final class Repository
|
||||
if ($page === null) {
|
||||
return false;
|
||||
}
|
||||
$publication = PageRevisions::publicationInfo($pdo, $slug);
|
||||
$delete = $pdo->prepare('DELETE FROM pages WHERE slug = ? AND version = ?');
|
||||
$delete->execute([$slug, $version]);
|
||||
self::checkVersion($delete->rowCount());
|
||||
if ($page['layout_id'] !== null) {
|
||||
LayoutPage::deletePreset($pdo, $page['layout_kind'], $page['layout_id']);
|
||||
}
|
||||
SiteDesign::replaceSystemPage($pdo, $slug);
|
||||
if (isset($publication['layout_id']) && $publication['layout_id'] !== $page['layout_id']) {
|
||||
LayoutPage::deletePreset($pdo, $publication['layout_kind'], $publication['layout_id']);
|
||||
}
|
||||
SiteDesign::replaceSystemPage($pdo, $publication['slug'] ?? $slug);
|
||||
|
||||
return true;
|
||||
});
|
||||
@@ -250,12 +432,15 @@ final class Repository
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $data, $version): void {
|
||||
self::updateSection($pdo, $page, $key, $data, $version);
|
||||
self::touchPage($pdo, $page, bumpVersion: false);
|
||||
self::touchPage($pdo, $page);
|
||||
});
|
||||
}
|
||||
|
||||
private static function updateSection(PDO $pdo, string $page, string $key, array $data, int $version): void
|
||||
{
|
||||
if (!empty($data['mirror_id']) || self::isMirror($pdo, $page, $key)) {
|
||||
throw new RuntimeException('镜像区块只读,修改请前往主区块或取消关联。');
|
||||
}
|
||||
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, feed = ?, alignments = COALESCE(?, alignments), presentation = COALESCE(?, presentation), version = version + 1
|
||||
WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $data['feed'] ?? null, $data['alignments'] ?? null, $data['presentation'] ?? null, $page, $key, $version]);
|
||||
@@ -355,10 +540,19 @@ final class Repository
|
||||
}
|
||||
}
|
||||
|
||||
private static function touchPage(PDO $pdo, string $slug, bool $bumpVersion = true): void
|
||||
private static function touchPage(PDO $pdo, string $slug): void
|
||||
{
|
||||
$revision = $bumpVersion ? ', version = version + 1' : '';
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]);
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ?, version = version + 1 WHERE slug = ?')
|
||||
->execute([self::now(), $slug]);
|
||||
PageRevisions::record($pdo, (new self())->page($slug), 'saved');
|
||||
}
|
||||
|
||||
private static function isMirror(PDO $pdo, string $page, string $key): bool
|
||||
{
|
||||
$query = $pdo->prepare('SELECT 1 FROM section_reuse WHERE page_slug = ? AND section_key = ? AND mirror_id IS NOT NULL');
|
||||
$query->execute([$page, $key]);
|
||||
|
||||
return (bool) $query->fetchColumn();
|
||||
}
|
||||
|
||||
public function categories(): array
|
||||
|
||||
Reference in New Issue
Block a user