Add page publication history and reusable blocks
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use PDO;
|
||||
use RuntimeException;
|
||||
|
||||
final class PageRevisions
|
||||
{
|
||||
public static function seed(PDO $pdo): void
|
||||
{
|
||||
$repo = new Repository();
|
||||
foreach ($pdo->query('SELECT slug, status FROM pages WHERE NOT EXISTS
|
||||
(SELECT 1 FROM page_revisions WHERE draft_slug = pages.slug) ORDER BY rowid')->fetchAll() as $row) {
|
||||
$page = $repo->page($row['slug']);
|
||||
if ($row['status'] === 'published') {
|
||||
self::publish($pdo, $page);
|
||||
} else {
|
||||
self::record($pdo, $page, 'initial');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function record(PDO $pdo, array $page, string $kind): int
|
||||
{
|
||||
$page = ReusableBlocks::resolve($pdo, $page, $kind !== 'published');
|
||||
foreach ($page['sections'] as &$section) {
|
||||
unset($section['mirror_source']);
|
||||
}
|
||||
unset($section);
|
||||
$pdo->prepare('INSERT INTO page_revisions (draft_slug, kind, created_at, payload) VALUES (?, ?, ?, ?)')
|
||||
->execute([$page['slug'], $kind, gmdate('c'), Database::json($page)]);
|
||||
|
||||
return (int) $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
public static function publish(PDO $pdo, array $page): void
|
||||
{
|
||||
$reserved = $pdo->prepare('SELECT 1 FROM page_publications WHERE route_slug = ? AND draft_slug <> ?');
|
||||
$reserved->execute([$page['slug'], $page['slug']]);
|
||||
if ($reserved->fetchColumn()) {
|
||||
throw new RuntimeException('此网址仍由另一页面的公开版本使用。');
|
||||
}
|
||||
$write = $pdo->prepare('INSERT INTO published_blocks (main_id, payload) VALUES (?, ?)
|
||||
ON CONFLICT(main_id) DO UPDATE SET payload = excluded.payload');
|
||||
foreach ($page['sections'] as $section) {
|
||||
if (isset($section['main_id'])) {
|
||||
$write->execute([$section['main_id'], Database::json($section)]);
|
||||
}
|
||||
}
|
||||
$lookup = $pdo->prepare('SELECT 1 FROM published_blocks WHERE main_id = ?');
|
||||
foreach ($page['sections'] as $section) {
|
||||
if (!isset($section['mirror_id'])) {
|
||||
continue;
|
||||
}
|
||||
$lookup->execute([$section['mirror_id']]);
|
||||
if (!$lookup->fetchColumn()) {
|
||||
throw new RuntimeException('请先发布镜像对应的主区块所在页面,再发布本页。');
|
||||
}
|
||||
}
|
||||
$info = self::publicationInfo($pdo, $page['slug']);
|
||||
if ($info && isset(SystemPage::TYPES[$info['page_type']])) {
|
||||
if ($info['page_type'] !== $page['page_type']) {
|
||||
SiteDesign::replaceSystemPage($pdo, $info['slug']);
|
||||
} elseif ($info['slug'] !== $page['slug']) {
|
||||
SiteDesign::replaceSystemPage($pdo, $info['slug'], replacement: $page['slug']);
|
||||
}
|
||||
}
|
||||
$id = self::record($pdo, $page, 'published');
|
||||
$pdo->prepare('INSERT INTO page_publications (draft_slug, route_slug, revision_id) VALUES (?, ?, ?)
|
||||
ON CONFLICT(draft_slug) DO UPDATE SET route_slug = excluded.route_slug, revision_id = excluded.revision_id')
|
||||
->execute([$page['slug'], $page['slug'], $id]);
|
||||
}
|
||||
|
||||
public static function publicationInfo(PDO $pdo, string $slug): ?array
|
||||
{
|
||||
$query = $pdo->prepare("SELECT p.route_slug AS slug, json_extract(r.payload, '$.page_type') AS page_type,
|
||||
json_extract(r.payload, '$.layout_kind') AS layout_kind, json_extract(r.payload, '$.layout_id') AS layout_id,
|
||||
json_extract(r.payload, '$.version') AS version
|
||||
FROM page_publications p JOIN page_revisions r ON r.id = p.revision_id WHERE p.draft_slug = ?");
|
||||
$query->execute([$slug]);
|
||||
|
||||
return $query->fetch() ?: null;
|
||||
}
|
||||
|
||||
public static function history(PDO $pdo, string $slug): array
|
||||
{
|
||||
$query = $pdo->prepare('SELECT r.id, r.kind, r.created_at, r.id = COALESCE(p.revision_id, -1) AS live
|
||||
FROM page_revisions r LEFT JOIN page_publications p ON p.draft_slug = r.draft_slug
|
||||
WHERE r.draft_slug = ? ORDER BY r.id DESC');
|
||||
$query->execute([$slug]);
|
||||
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
public static function find(PDO $pdo, string $slug, int $id): ?array
|
||||
{
|
||||
$query = $pdo->prepare('SELECT payload FROM page_revisions WHERE draft_slug = ? AND id = ?');
|
||||
$query->execute([$slug, $id]);
|
||||
$payload = $query->fetchColumn();
|
||||
|
||||
return $payload === false ? null : json_decode($payload, true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
public static function unpublish(PDO $pdo, array $page): void
|
||||
{
|
||||
$info = self::publicationInfo($pdo, $page['slug']);
|
||||
$pdo->prepare('DELETE FROM page_publications WHERE draft_slug = ?')->execute([$page['slug']]);
|
||||
if ($info && isset(SystemPage::TYPES[$info['page_type']])) {
|
||||
SiteDesign::replaceSystemPage($pdo, $info['slug']);
|
||||
}
|
||||
self::record($pdo, $page, 'unpublished');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user