From 884ce1078981b7833038aef0deb2adde2bd67e9f Mon Sep 17 00:00:00 2001 From: ChatGPT Date: Wed, 25 Nov 2026 15:36:01 +0800 Subject: [PATCH] Add maintenance and 404 pages, template suites, and MCP skill --- .agents/skills/laysite-mcp/SKILL.md | 30 ++++++++++ README.md | 18 +++++- VERSION | 2 +- app/Content/BlockTemplates.php | 18 ++++++ app/Content/Database.php | 5 +- app/Content/PageDraft.php | 13 +++- app/Content/Repository.php | 24 ++++---- app/Content/SiteBackup.php | 29 +++++++-- app/Content/SiteDesign.php | 30 +++++++++- app/Content/SystemPage.php | 26 ++++++++ app/Controller/AdminController.php | 2 +- app/Controller/DesignController.php | 21 ++++++- app/Controller/McpController.php | 2 +- app/Controller/SiteController.php | 11 ++-- app/Controller/StudioController.php | 22 ++++++- app/Middleware/SiteStatus.php | 47 +++++++++++++++ app/view/admin-dashboard.php | 28 ++++++--- app/view/admin-layout.php | 1 + app/view/admin-page.php | 7 ++- app/view/footer.php | 1 + app/view/page.php | 3 +- app/view/studio-new-page.php | 3 +- app/view/studio-settings.php | 16 +++++ config/middleware.php | 3 + config/route.php | 3 +- public/assets/admin.css | 4 +- .../page-templates/dispatch-feature.webp | Bin 0 -> 11244 bytes .../page-templates/dispatch-magazine.webp | Bin 0 -> 10978 bytes public/assets/page-templates/dispatch.webp | Bin 0 -> 9856 bytes .../page-templates/gather-conference.webp | Bin 0 -> 13088 bytes .../page-templates/gather-exhibition.webp | Bin 0 -> 13904 bytes public/assets/page-templates/gather.webp | Bin 0 -> 12456 bytes .../assets/page-templates/market-product.webp | Bin 0 -> 13264 bytes .../assets/page-templates/market-service.webp | Bin 0 -> 14600 bytes public/assets/page-templates/market.webp | Bin 0 -> 12664 bytes public/assets/page-themes.css | 56 ++++++++++++++++++ public/assets/site.css | 48 +++++++++++++++ .../template-previews/editorial-feature.webp | Bin 0 -> 7494 bytes .../template-previews/event-agenda.webp | Bin 0 -> 8360 bytes .../product-specifications.webp | Bin 0 -> 5254 bytes .../template-previews/product-spotlight.webp | Bin 0 -> 6556 bytes .../template-previews/reading-path.webp | Bin 0 -> 6946 bytes .../template-previews/service-process.webp | Bin 0 -> 8110 bytes .../template-previews/speaker-lineup.webp | Bin 0 -> 8630 bytes .../assets/template-previews/venue-guide.webp | Bin 0 -> 6830 bytes resources/page-templates.php | 48 +++++++++++++++ resources/schema.sql | 1 + 47 files changed, 470 insertions(+), 52 deletions(-) create mode 100644 .agents/skills/laysite-mcp/SKILL.md create mode 100644 app/Content/SystemPage.php create mode 100644 app/Middleware/SiteStatus.php create mode 100644 config/middleware.php create mode 100644 public/assets/page-templates/dispatch-feature.webp create mode 100644 public/assets/page-templates/dispatch-magazine.webp create mode 100644 public/assets/page-templates/dispatch.webp create mode 100644 public/assets/page-templates/gather-conference.webp create mode 100644 public/assets/page-templates/gather-exhibition.webp create mode 100644 public/assets/page-templates/gather.webp create mode 100644 public/assets/page-templates/market-product.webp create mode 100644 public/assets/page-templates/market-service.webp create mode 100644 public/assets/page-templates/market.webp create mode 100644 public/assets/template-previews/editorial-feature.webp create mode 100644 public/assets/template-previews/event-agenda.webp create mode 100644 public/assets/template-previews/product-specifications.webp create mode 100644 public/assets/template-previews/product-spotlight.webp create mode 100644 public/assets/template-previews/reading-path.webp create mode 100644 public/assets/template-previews/service-process.webp create mode 100644 public/assets/template-previews/speaker-lineup.webp create mode 100644 public/assets/template-previews/venue-guide.webp diff --git a/.agents/skills/laysite-mcp/SKILL.md b/.agents/skills/laysite-mcp/SKILL.md new file mode 100644 index 0000000..44e2101 --- /dev/null +++ b/.agents/skills/laysite-mcp/SKILL.md @@ -0,0 +1,30 @@ +--- +name: laysite-mcp +description: Design or revise website pages as LaySite drafts through its MCP server. Use when asked to create a site page, choose a template, assemble reusable components, or hand a draft over for editing in the LaySite visual canvas. +--- + +# LaySite MCP + +Use the configured LaySite MCP connection to the target site's `/mcp` endpoint. The server uses Streamable HTTP with a Bearer token. Keep the token in the client's credential configuration. Follow the repository's [MCP setup](../../../README.md#mcp) if the connection is missing. + +## Work with drafts + +1. Call `list_pages`, `list_templates`, and `list_designs`. Use `get_page` to inspect the page being revised and related site pages. Confirm the intended site and use the exact template keys and design IDs the server returns. +2. For a new page, call `create_draft` with a unique lowercase slug and a descriptive name. Pass `template` to start from a whole-page template. Keep the layout and content that fit the task. +3. For a revision, read the draft using `get_page` before calling `save_draft` with its latest `version`. Send only the fields that changed. When sending `sections`, include every section intended to remain, in display order. Keep existing keys and reconstruct dynamic feeds with their component's `preset` when that preset represents the current configuration. If the feed has custom settings the MCP schema cannot express, keep the existing section list and leave that adjustment for the canvas. +4. Read the draft back. Check content, section order, button destinations, design selections and image descriptions. Open the returned preview when the administrator session is available. Pass the returned `editor` and `preview` links to the human editor for refinement and publishing. + +Treat a version conflict as a signal to reread the draft and reconcile any intervening changes. Leave published pages, shared headers and footers, and site settings under human control. For a published page revision, create a separate draft when the user wants a reviewable version. + +## Keep page design consistent + +- Follow the existing site's palette, spacing, button styles, navigation and terminology. Start with a matching template or reusable component. Use `header_id`, `footer_id`, and `palette_id` only when the task calls for a specific saved design. +- Give the page one clear primary heading and one primary action. Use each section's title as its heading, then start headings in the body at `h3`. Use semantic paragraphs, lists, links, figures and tables. Write meaningful labels and use the visitor's language. +- Reuse LaySite's CSS classes for buttons and components. Use plain, sanitizable HTML for custom bodies, and keep everything editable in the canvas. Keep scripts, event handlers, inline CSS and embedded forms out of draft markup. Check that the design can stack on narrow screens and stays readable at mobile widths. +- For hero actions, send `[href, label, style, opensNewTab]`. Use `solid` for the primary action and `outline`, `underline`, or `plain` for secondary actions. Internal links normally open in the same tab. +- Use stable, unique section keys such as `getting-started`. Reserve `home`, `contact`, `main-content`, `primary-navigation`, and `preview` for the application. Confirm destinations exist and use descriptive alt text for informative images. +- Keep SEO titles and summaries specific to the page. Use provided facts, assets, links and claims. Flag missing dates, prices, contact details or media in the handoff. + +## Source changes + +When the user asks to extend LaySite itself, follow its repository instructions. Use Webman controllers, PHP views, SQLite migrations when changing persisted data, native CSS and the existing jQuery handlers. Reuse the template registry, canvas and sanitizer. Store built-in previews as system assets and keep uploads in the user media library. Keep page-theme assets conditional and keep public routes, status codes and backups aligned with data changes. diff --git a/README.md b/README.md index b8490a2..cf4c727 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LaySite -基于 PHP Webman、SQLite、原生 CSS 和 jQuery 的通用站点编辑器。页面由服务端渲染,公开页复用站点组件和配色。新装提供空白首页、可编辑的文章页,以及六套十二个整页模板。 +基于 PHP Webman、SQLite、原生 CSS 和 jQuery 的通用站点编辑器。页面由服务端渲染,公开页复用站点组件和配色。新装提供空白首页、可编辑的文章页,以及九套十八个整页模板。 ## 安装 @@ -36,13 +36,21 @@ php start.php start - 选中元素可编辑四边的内外边距。选中文字后可修改字号、粗体、斜体和删除线。Enter 或 Shift+Enter 在当前元素内换行。正文用 Ctrl+Enter(macOS 上为 ⌘+Enter)或工具栏「换段」新建段落,列表中则新增列表项。 - 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。 - 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。 -- 在「网站页面 → 新建页面」选择普通页面、页头或页脚。三种页面共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。 +- 在「网站页面 → 新建页面」选择普通页面、页头、页脚、维护页或 404 页。它们共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。页脚组件默认附带可编辑的 Powered by LaySite 链接。 - 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。配色方案在「站点设计」管理。 - 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。 - 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。 画布样式以白名单 CSS 类保存。间距与字号使用限定范围内的整数,服务端生成带版本指纹的样式表,画布和使用这些类的公开页按需加载。内容经过 HTML 清理,内联 `style` 属性会被移除。 +### 维护页与 404 页 + +在「网站页面 → 新建页面」选择相应类型,在画布中编辑并发布。然后在「站点设置 → 系统页面」选用已发布的页面。 + +- 勾选「开启维护模式」后,公开页面和 API 会返回维护页,HTTP 状态码为 `503`。后台与预览和画布所需的资源继续可用。若把当前维护页改回草稿,维护模式会自动关闭。 +- 选中的 404 页会在公开请求找不到内容时显示,HTTP 状态码仍为 `404`。留空选项或将所选页面改回草稿时使用系统 404 页。 +- 维护页和 404 页通过后台预览,站点地图只列出可独立访问的公开页面。使用 CDN 或反代缓存时,应保留源站的状态码和 `Cache-Control`,并在切换维护模式时清理已缓存的公开页面。 + ### 会员页头 在页头画布的「组件 → 页头」添加「会员导航」,即可打开「会员登录设置」。将配置好的页头用作默认,或应用于需要登录的页面。 @@ -83,11 +91,13 @@ php start.php start Agent 可以编辑草稿,管理员在画布里检查、细改及发布。草稿 HTML 使用与人工画布相同的 HTML 过滤器。按钮参数为 `[href, label, style, opensNewTab]`,样式可选 `solid`、`outline`、`underline`、`plain`。MCP 与站点发布共用 SQLite 和编辑器修订号。 +仓库自带 [`laysite-mcp` Agent Skill](.agents/skills/laysite-mcp/SKILL.md),涵盖模板复用、设计规范、草稿修订和人工交接。使用支持 `.agents/skills/` 的客户端时,可在项目中调用该 Skill。其他支持 Agent Skills 的客户端可将整个 `laysite-mcp` 目录导入其技能目录。 + ## 站点备份与更新 「站点设计 → 站点备份」支持导出和确认后覆盖导入。备份包是包含版本化 JSON 清单和校验和的 TAR,包含页面、区块、文章、站点设置、布局、配色和媒体数据。管理员密码、OAuth 客户端密钥、运行环境密钥和登录会话由目标安装独立管理。导入到原站点时,身份提供商配置一致的同一套页头会保留客户端密钥。导入到新站点后,在对应会员页头重新填写。备份存放在站点公开目录外。 -当前导出格式为 v3,导入兼容 v1 和 v2。导入 v2 后,原有页头和页脚会转换成共享页面,原有引用继续有效。遇到较新的不兼容格式时会提示升级。上传大小受 Webman 与外层代理限制,较大站点可使用 CLI。 +当前导出格式为 v4,包含维护页、404 页和对应站点设置。导入兼容 v1 至 v3。导入 v2 后,原有页头和页脚会转换成共享页面,原有引用继续有效。遇到较新的不兼容格式时会提示升级。上传大小受 Webman 与外层代理限制,较大站点可使用 CLI。 ```sh php bin/backup.php export /srv/backups/laysite-2026-11.tar @@ -105,6 +115,8 @@ bin/update.sh v1.1.0 /srv/backups/pre-update.tar 从 v1.1.0 更新画布功能时,更新脚本会添加样式字段,并将已有页头和页脚迁到「网站页面」,保留页面引用和已有认证配置。手动更新源码后,运行 `php bin/install.php` 再启动 Webman。 +更新到 v1.3.0 时,SQLite 迁移会加入系统页面类型。升级后的站点可在「站点设置」选用维护页和 404 页。 + ## 生产反代 在 `.env` 配置 `LAYSITE_SITE_URL=https://example.com` 和 `LAYSITE_SECURE_COOKIES=1`。下列示例假定代理和 Webman 在同一主机。代理应以实际访客来源覆盖 `X-Real-IP`,并将项目文件、数据库和媒体存储保持在公开根目录之外。 diff --git a/VERSION b/VERSION index 26aaba0..f0bb29e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.0 +1.3.0 diff --git a/app/Content/BlockTemplates.php b/app/Content/BlockTemplates.php index 49bd894..45f31b9 100644 --- a/app/Content/BlockTemplates.php +++ b/app/Content/BlockTemplates.php @@ -24,6 +24,7 @@ final class BlockTemplates 'tag' => ['徽标', '

填写标签

'], 'large-number' => ['重点数字', '

填写数字

'], 'definition-list' => ['术语说明', '
填写术语
填写术语说明。
'], + 'paired-label' => ['标签与数值', '

开放时间周六 10:00–17:00

'], ]], 'link-elements' => ['链接与按钮', [ 'button' => ['主按钮', '

联系站点

'], @@ -33,6 +34,7 @@ final class BlockTemplates 'breadcrumbs' => ['面包屑', ''], 'download-link' => ['下载入口', '

查看下载

'], 'email-link' => ['邮件链接', '

发送邮件

'], + 'phone-link' => ['电话链接', '

致电咨询

'], ]], 'media-elements' => ['图片与影音', [ 'image' => ['图片', '', 'image/'], @@ -40,6 +42,7 @@ final class BlockTemplates 'audio' => ['音频', '', 'audio/'], 'qr-image' => ['二维码', '', 'image/'], 'image-link' => ['图片链接', '

版式示意

'], + 'captioned-image' => ['带图注的图片', '
示例界面
在这里注明图片内容或来源。
'], ]], 'structure-elements' => ['版式与数据', [ 'separator' => ['分割线', '
'], @@ -54,6 +57,7 @@ final class BlockTemplates 'empty-card' => ['空状态', '

暂无内容

填写访客可以执行的下一步。

查看站点动态
'], 'status-note' => ['状态提示', '

已完成

填写操作结果或服务状态。

'], 'disclosure' => ['折叠内容', '
填写折叠标题

填写展开后的内容。

'], + 'numbered-step' => ['带编号的步骤', '
01

准备资料

写明进入下一步前需要准备的信息。

'], ]], ]; @@ -100,6 +104,20 @@ final class BlockTemplates 'resources' => ['资料与链接', ''], 'contact-card' => ['联系方式', '

联系团队

填写咨询范围与回复时间。

电子邮件
hello@example.com
服务时间
填写工作时间
'], ]], + 'events' => ['活动与场地', [ + 'event-agenda' => ['当日议程', '
  1. 09:30

    签到与开场

    开放入口并介绍当天安排。

    主会场
  2. 10:30

    专题工作坊

    跟随主持人完成一次实践。

    工作区
  3. 14:00

    圆桌交流

    分享实践经验并回答问题。

    交流区
'], + 'speaker-lineup' => ['嘉宾阵容', '

主题分享

嘉宾姓名

填写嘉宾的角色和主题。

工作坊

主持人姓名

介绍实践主题和适合的听众。

圆桌交流

主持人姓名

介绍本场将讨论的议题。

'], + 'venue-guide' => ['到场指南', '

开放与入场

填写开放时间、预约方式和入口位置。

交通与无障碍

填写推荐交通方式和无障碍通道位置。

'], + ]], + 'commerce' => ['商品与服务', [ + 'product-spotlight' => ['商品展示', '
商品展示示例

系列名称

填写商品名称

说明规格、适用场景与供货信息。

联系订购
'], + 'product-specifications' => ['规格清单', '
材质
填写材料及维护方式。
尺寸
标注尺寸与单位。
交付
说明交付时间及适用范围。
'], + 'service-process' => ['服务流程', '
  1. 沟通需求

    说明首次沟通需要提供哪些资料。

  2. 确认方案

    列出范围、周期和交付计划。

  3. 交付与支持

    介绍验收方式和后续跟进安排。

'], + ]], + 'editorial' => ['专题与编辑部', [ + 'editorial-feature' => ['专题导读', '

本期主题

写下一句概括专题观点的话。

用一段话交代来由,并指出读者可以从哪里开始。

浏览文章
'], + 'reading-path' => ['阅读路径', '
  1. 01

    从背景开始

    了解这组文章要解决的问题。

  2. 02

    阅读主题文章

    按主题深入阅读,并查看相关材料。

'], + ]], ]; public static function groups(): array diff --git a/app/Content/Database.php b/app/Content/Database.php index 3fbff3d..3564539 100644 --- a/app/Content/Database.php +++ b/app/Content/Database.php @@ -11,7 +11,7 @@ use Throwable; final class Database { - private const SCHEMA_VERSION = 5; + private const SCHEMA_VERSION = 6; // 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'; @@ -89,6 +89,9 @@ final class Database } $pdo->exec('CREATE UNIQUE INDEX IF NOT EXISTS pages_layout_id ON pages(layout_id)'); } + if ($version < 6 && !in_array('page_type', array_column($pdo->query('PRAGMA table_info(pages)')->fetchAll(), 'name'), true)) { + $pdo->exec("ALTER TABLE pages ADD COLUMN page_type TEXT NOT NULL DEFAULT 'page' CHECK (page_type IN ('page', 'maintenance', 'not-found'))"); + } if ($version < 5) { LayoutPage::migrate($pdo); } diff --git a/app/Content/PageDraft.php b/app/Content/PageDraft.php index f36b746..097ecd2 100644 --- a/app/Content/PageDraft.php +++ b/app/Content/PageDraft.php @@ -10,10 +10,17 @@ final class PageDraft { public const RESERVED = ['admin', 'assets', 'home', 'account', 'mcp', 'palette']; - public static function slug(mixed $value, array $reserved = self::RESERVED, bool $includeLayouts = false): string + public static function slug(mixed $value, array $reserved = self::RESERVED, bool $includeSpecial = false): string { - if ($includeLayouts && is_string($value) && preg_match('/^layout--(?:header|footer)-[1-9][0-9]{0,18}$/D', $value)) { - return $value; + if ($includeSpecial && is_string($value)) { + if (preg_match('/^layout--(?:header|footer)-[1-9][0-9]{0,18}$/D', $value)) { + return $value; + } + foreach (array_keys(SystemPage::TYPES) as $type) { + if (SystemPage::slugMatches($type, $value)) { + return $value; + } + } } if (!is_string($value) || strlen($value) > 70 || !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) || in_array($value, $reserved, true)) { diff --git a/app/Content/Repository.php b/app/Content/Repository.php index 977e1ff..775c97a 100644 --- a/app/Content/Repository.php +++ b/app/Content/Repository.php @@ -30,28 +30,28 @@ final class Repository public function pages(bool $publishedOnly = false): array { - $where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL" : ''; + $where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type = 'page'" : ''; - return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, 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, 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 createPage(string $slug, string $name, array $template = []): void + 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)); + Database::write(static fn (PDO $pdo) => self::insertPage($pdo, $slug, $name, $template, type: $type)); } - public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null): void + public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null, string $type = 'page'): void { $query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent, - lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, status, updated_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, page_type, status, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $query->execute([ $slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name, $template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '', Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []), $template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null, - $template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $layoutId === null ? 'draft' : 'published', self::now(), + $template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $type, $layoutId === null ? 'draft' : 'published', self::now(), ]); if ($query->rowCount() !== 1) { throw new RuntimeException('此页面地址已存在。'); @@ -98,7 +98,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'] = empty($page['layout_kind']) && $page['status'] === 'published' ? $archivePath : null; + $section['archivePath'] = empty($page['layout_kind']) && ($page['page_type'] ?? 'page') === 'page' && $page['status'] === 'published' ? $archivePath : null; $section['articleFeed'] = []; $section['networkFeed'] = $network; $section['catalogFeed'] = $catalog; @@ -125,7 +125,8 @@ 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 sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]); + WHERE pages.slug = ? AND pages.status = 'published' AND pages.layout_id IS NULL AND pages.page_type = 'page' + 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): void @@ -140,6 +141,9 @@ final class Repository isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null, $slug, $version, ]); self::checkVersion($query->rowCount()); + if ($data['status'] === 'draft' && str_starts_with($slug, 'system--')) { + SiteDesign::unsetSystemPage($pdo, $slug); + } if ($replaceDraftSections) { $pdo->prepare('DELETE FROM sections WHERE page_slug = ?')->execute([$slug]); } diff --git a/app/Content/SiteBackup.php b/app/Content/SiteBackup.php index edb9ae6..7e55c10 100644 --- a/app/Content/SiteBackup.php +++ b/app/Content/SiteBackup.php @@ -12,7 +12,7 @@ use Throwable; final class SiteBackup { private const FORMAT = 'laysite-site'; - private const VERSION = 3; + private const VERSION = 4; private const TABLES = [ 'site_settings', 'design_presets', 'pages', 'sections', 'article_categories', 'articles', 'article_category_links', 'article_redirects', 'article_deletions', 'media', @@ -140,7 +140,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, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) { + || !in_array($manifest['version'] ?? null, [1, 2, 3, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) { throw new RuntimeException('备份格式不受支持,请使用相同或更新版本的 LaySite。'); } $tables = $manifest['tables']; @@ -184,10 +184,18 @@ final class SiteBackup $site['layout_defaults'][$kind] = self::selection($designs, $kind, $layoutDefaults[$kind]); } } - $tables['site_settings'] = [['id' => 1, 'payload' => Database::json($site)]]; + $publishedSystemPages = []; foreach ($tables['pages'] as &$row) { $layout = $row['layout_id'] ?? null; - if ($layout === 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 { $kind = is_int($layout) ? ($designs[$layout] ?? null) : null; @@ -210,6 +218,19 @@ final class SiteBackup } } unset($row); + $systemPages = $settings['system_pages'] ?? []; + $maintenance = $settings['maintenance_enabled'] ?? false; + if (!is_array($systemPages) || !is_bool($maintenance) || ($maintenance && !isset($systemPages['maintenance']))) { + throw new RuntimeException('备份包含无效的系统页面设置。'); + } + foreach ($systemPages as $type => $slug) { + if (!is_string($slug) || ($publishedSystemPages[$slug] ?? null) !== $type) { + throw new RuntimeException('备份引用了缺失或未发布的系统页面。'); + } + } + $site['system_pages'] = $systemPages; + $site['maintenance_enabled'] = $maintenance; + $tables['site_settings'] = [['id' => 1, 'payload' => Database::json($site)]]; foreach ($tables['sections'] as &$row) { PageDraft::slug($row['key'] ?? null, ['home', 'contact', 'main-content', 'primary-navigation', 'preview']); if (!in_array($row['style'] ?? '', ['plain', 'tinted', 'hero-links', 'layout'], true)) { diff --git a/app/Content/SiteDesign.php b/app/Content/SiteDesign.php index c519090..2cf3000 100644 --- a/app/Content/SiteDesign.php +++ b/app/Content/SiteDesign.php @@ -31,17 +31,43 @@ final class SiteDesign return implode("\n", array_map(static fn (array $link): string => $link[1] . ' | ' . $link[0], $links)); } - public static function saveGeneral(string $name, string $origin, int $version): void + public static function saveGeneral(string $name, string $origin, int $version, array $systemPages, bool $maintenance): void { - Database::write(function (PDO $pdo) use ($name, $origin, $version): void { + Database::write(function (PDO $pdo) use ($name, $origin, $version, $systemPages, $maintenance): void { $site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR); if (($site['revisions']['general'] ?? 0) !== $version) { throw new RuntimeException('站点设置已更新,请重新打开后再保存。'); } + $repo = new Repository(); + foreach ($systemPages as $type => $slug) { + if (!SystemPage::published($repo, $type, $slug)) { + throw new RuntimeException('请选择已发布的' . SystemPage::TYPES[$type] . '。'); + } + } + if ($maintenance && !isset($systemPages['maintenance'])) { + throw new RuntimeException('请先发布并选择维护页,再开启维护模式。'); + } $site['name'] = $name; $site['cdn'] = ['origin' => $origin]; + $site['system_pages'] = $systemPages; + $site['maintenance_enabled'] = $maintenance; $site['revisions']['general'] = $version + 1; $pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]); }); } + + public static function unsetSystemPage(PDO $pdo, string $slug): 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; + } + $site['revisions']['general'] = ($site['revisions']['general'] ?? 0) + 1; + $pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]); + } } diff --git a/app/Content/SystemPage.php b/app/Content/SystemPage.php new file mode 100644 index 0000000..7e8df8c --- /dev/null +++ b/app/Content/SystemPage.php @@ -0,0 +1,26 @@ + '维护页', 'not-found' => '404 页']; + + public static function slugMatches(string $type, mixed $slug): bool + { + return isset(self::TYPES[$type]) && is_string($slug) + && preg_match('/^system--' . $type . '-[a-f0-9]{16}$/D', $slug) === 1; + } + + public static function published(Repository $repo, string $type, mixed $slug): ?array + { + if (!self::slugMatches($type, $slug)) { + return null; + } + $page = $repo->page($slug); + + return $page && $page['page_type'] === $type && $page['status'] === 'published' ? $page : null; + } +} diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index 9c6026e..6e98212 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -323,7 +323,7 @@ class AdminController $data['quicklinks'] = $request->post('quicklinks') === null ? $page['quicklinks'] : SiteDesign::parseLinks($this->input($request, 'quicklinks')); if ($data['name'] === '' || mb_strlen($data['name']) > 100 || !in_array($data['status'], ['draft', 'published'], true) || $data['title'] === '' || mb_strlen($data['title']) > 160 || - (!$layout && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 || + (!$layout && $page['page_type'] === 'page' && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 || (($data['visibility']['hero'] ?? true) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 || mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 || ($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) || diff --git a/app/Controller/DesignController.php b/app/Controller/DesignController.php index 49cb6ad..5e3cff6 100644 --- a/app/Controller/DesignController.php +++ b/app/Controller/DesignController.php @@ -9,6 +9,7 @@ use App\Content\DesignFieldError; use App\Content\DesignLibrary; use App\Content\Repository; use App\Content\SiteDesign; +use App\Content\SystemPage; use App\Http\Request; use RuntimeException; use support\Response; @@ -76,10 +77,13 @@ final class DesignController extends AdminController if ($failure = $this->guard($request)) { return $failure; } - $site = (new Repository())->site(); + $repo = new Repository(); + $site = $repo->site(); $error = ''; $name = $site['name']; $cdn = $site['cdn']['origin'] ?? ''; + $systemPages = $site['system_pages'] ?? []; + $maintenance = $site['maintenance_enabled'] ?? false; $version = (int) ($site['revisions']['general'] ?? 0); if ($request->method() === 'POST') { try { @@ -88,7 +92,12 @@ final class DesignController extends AdminController if ($name === '' || mb_strlen($name) > 100) { throw new RuntimeException('站点名称必须为 1 至 100 个字符。'); } - SiteDesign::saveGeneral($name, $cdn, $this->version($request)); + $systemPages = array_filter([ + 'maintenance' => $this->input($request, 'maintenance_page'), + 'not-found' => $this->input($request, 'not_found_page'), + ], static fn (string $slug): bool => $slug !== ''); + $maintenance = $request->post('maintenance_enabled') === '1'; + SiteDesign::saveGeneral($name, $cdn, $this->version($request), $systemPages, $maintenance); return $this->success($request, '/admin/settings?saved=1'); } catch (RuntimeException $ex) { @@ -96,8 +105,16 @@ final class DesignController extends AdminController } } + $systemChoices = []; + foreach ($repo->pages() as $page) { + $type = $page['page_type']; + if (isset(SystemPage::TYPES[$type]) && $page['status'] === 'published') { + $systemChoices[$type][$page['slug']] = $page['name']; + } + } return $this->render($request, 'studio-settings', [ 'title' => '站点设置', 'name' => $name, 'cdn' => $cdn, 'version' => $version, 'error' => $error, + 'systemPages' => $systemPages, 'systemChoices' => $systemChoices, 'maintenance' => $maintenance, 'saved' => $request->get('saved') === '1', ], $error === '' ? 200 : 422); } diff --git a/app/Controller/McpController.php b/app/Controller/McpController.php index 326754a..cf954f7 100644 --- a/app/Controller/McpController.php +++ b/app/Controller/McpController.php @@ -122,7 +122,7 @@ final class McpController try { $data = match ($name) { 'list_pages' => $repo->pages(), - 'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeLayouts: true)), + 'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)), 'list_templates' => $this->templates(), 'list_designs' => DesignLibrary::all(), 'create_draft' => $this->create($repo, $args), diff --git a/app/Controller/SiteController.php b/app/Controller/SiteController.php index 7931bd4..be714e7 100644 --- a/app/Controller/SiteController.php +++ b/app/Controller/SiteController.php @@ -25,7 +25,8 @@ final class SiteController { $repo = new Repository(); $page = $repo->page($slug); - if (!$page || $page['layout_kind'] !== null || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') { + if (!$page || $page['layout_kind'] !== null || $page['page_type'] !== 'page' + || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') { return $this->missing($request); } if ($slug === 'articles' && $request->get('page') !== null) { @@ -41,7 +42,7 @@ final class SiteController return $this->renderPage($repo, $page); } - public function renderPage(Repository $repo, array $page, bool $preview = false, bool $canvas = false): Response + public function renderPage(Repository $repo, array $page, bool $preview = false, bool $canvas = false, int $status = 200): Response { $slug = $page['slug']; $isHome = $slug === 'home'; @@ -51,13 +52,13 @@ final class SiteController return View::render('page', [ 'site' => $repo->site(), 'page' => $page, - 'path' => $isHome ? '/' : '/' . $slug, + 'path' => ($page['page_type'] ?? 'page') !== 'page' ? null : ($isHome ? '/' : '/' . $slug), 'title' => $page['title'], 'description' => $page['description'], 'socialImage' => $showHero ? $page['image'] : '', 'socialAlt' => $showHero ? $page['alt'] : '', - 'previewMode' => $preview, 'canvas' => $preview && $canvas, 'noindex' => $preview, - ]); + 'previewMode' => $preview, 'canvas' => $preview && $canvas, 'noindex' => $preview || $status >= 400, + ], $status); } public function archive(Request $request, ?string $category = null, ?string $tag = null): Response diff --git a/app/Controller/StudioController.php b/app/Controller/StudioController.php index 9457dae..4ccd7c0 100644 --- a/app/Controller/StudioController.php +++ b/app/Controller/StudioController.php @@ -12,6 +12,7 @@ use App\Content\Media; use App\Content\PageDraft; use App\Content\PageTemplates; use App\Content\Repository; +use App\Content\SystemPage; use App\Http\Request; use RuntimeException; use support\Response; @@ -24,21 +25,36 @@ final class StudioController extends AdminController if ($failure = $this->guard($request)) { return $failure; } + $types = LayoutPage::TYPES + SystemPage::TYPES; $type = $request->get('type'); $data = [ 'title' => '新建页面', 'name' => '', 'slug' => '', 'error' => '', 'pageTemplateDrawer' => true, 'openTemplates' => $request->get('templates') === '1', - 'type' => is_string($type) && isset(LayoutPage::TYPES[$type]) ? $type : 'page', + 'type' => is_string($type) && isset($types[$type]) ? $type : 'page', + 'types' => $types, ]; if ($request->method() === 'POST') { $data['name'] = $this->input($request, 'name'); $data['slug'] = $this->input($request, 'slug'); $data['type'] = $request->post('type') === null ? 'page' : $this->input($request, 'type'); try { - if (!isset(LayoutPage::TYPES[$data['type']]) || $data['name'] === '' || mb_strlen($data['name']) > 100) { + if (!isset($types[$data['type']]) || $data['name'] === '' || mb_strlen($data['name']) > 100) { throw new RuntimeException('请填写页面名称并选择页面类型。'); } - if ($data['type'] !== 'page') { + if (isset(SystemPage::TYPES[$data['type']])) { + $type = $data['type']; + $slug = 'system--' . $type . '-' . bin2hex(random_bytes(8)); + $maintenance = $type === 'maintenance'; + (new Repository())->createPage($slug, $data['name'], [ + 'heading' => $maintenance ? '站点维护中' : '页面未找到', + 'lead' => $maintenance ? '我们正在维护站点,请稍后再访问。' : '你访问的页面可能已移动或删除。', + 'actions' => $maintenance ? [] : [['/', '返回首页', 'solid', false]], + 'visibility' => $maintenance ? ['header' => false, 'footer' => false] : [], + ], $type); + + return $this->success($request, '/admin/pages/' . $slug); + } + if (in_array($data['type'], ['header', 'footer'], true)) { $slug = LayoutPage::create($data['type'], $data['name']); return $this->success($request, '/admin/pages/' . $slug); diff --git a/app/Middleware/SiteStatus.php b/app/Middleware/SiteStatus.php new file mode 100644 index 0000000..4c84f5f --- /dev/null +++ b/app/Middleware/SiteStatus.php @@ -0,0 +1,47 @@ +path(); + if ($path === '/admin' || str_starts_with($path, '/admin/')) { + return $handler($request); + } + + $repo = new Repository(); + $site = $repo->site(); + $controller = new SiteController(); + $pages = $site['system_pages'] ?? []; + if (!empty($site['maintenance_enabled']) && ($page = SystemPage::published($repo, 'maintenance', $pages['maintenance'] ?? null))) { + // Media and styles must remain available for the maintenance page and the admin canvas. + if (in_array($request->method(), ['GET', 'HEAD'], true) + && preg_match('~^/(?:assets/uploads/[a-f0-9]{32}\.[a-z0-9]+|palette/[0-9]+\.css|canvas-style\.css|favicon\.ico)$~D', $path)) { + $asset = $handler($request); + if ($asset->getStatusCode() !== 404) { + return $asset; + } + } + + return $controller->renderPage($repo, $page, status: 503)->withHeader('Cache-Control', 'no-store'); + } + + $response = $handler($request); + if ($response->getStatusCode() === 404 && ($page = SystemPage::published($repo, 'not-found', $pages['not-found'] ?? null))) { + return $controller->renderPage($repo, $page, status: 404)->withHeader('Cache-Control', 'no-store'); + } + + return $response; + } +} diff --git a/app/view/admin-dashboard.php b/app/view/admin-dashboard.php index 4c88ef4..30d7434 100644 --- a/app/view/admin-dashboard.php +++ b/app/view/admin-dashboard.php @@ -1,9 +1,10 @@ $page['layout_kind'] === null); +$regular = array_filter($pages, static fn (array $page): bool => $page['layout_kind'] === null && $page['page_type'] === 'page'); $live = count(array_filter($regular, static fn (array $page): bool => $page['status'] === 'published')); $defaultSlugs = []; foreach (['header', 'footer'] as $kind) { @@ -24,31 +25,40 @@ foreach (['header', 'footer'] as $kind) {

网站页面

-

个已发布 · 个草稿 · 个页头页脚

+

个已发布 · 个草稿 · 个特殊页面

>