diff --git a/README.md b/README.md index 45013f5..45cd3d8 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ php bin/admin.php reset admin@example.com - **分类文章**支持在任意页面添加,选中动态模块后可用浮动工具条打开设置、复制或删除,随页面一起保存。文章列表、产品目录和项目与伙伴卡片共用分类设置。「查看全部」和翻页只列出该区块所选的分类。 - **产品与服务**(`/services`)和**支持项目与合作伙伴**(`/network`)由文章分类生成。通过文章的目录卡片设置图片、链接和排序,使用草稿状态可从目录撤下。 - **网站设置**分别编辑页头标志、导航及按钮,和页脚的公司信息、产品、公司及资源链接和备注。页脚设置可恢复原站内容。CDN 设置接受 HTTPS 源站地址。浏览器看到的公开 `/assets/…` 路径使用该源站。 -- 文章支持摘要和标签。状态选「草稿」可撤回公开文章。已发布文章改网址标识后,旧标识会自动跳转。 +- 文章支持摘要和标签。状态选「草稿」可撤回公开文章。编辑页底部可永久删除文章,重复安装也会保留删除结果。已发布文章改网址标识后,旧标识会自动跳转。 - 首页动态、分类列表和站点地图随发布状态更新。源代码和 SQLite 数据分开管理。安装命令可重复执行,已编辑的内容会保留。 ## 生产运行 diff --git a/app/Content/Database.php b/app/Content/Database.php index 6beb057..4e0c36f 100644 --- a/app/Content/Database.php +++ b/app/Content/Database.php @@ -10,6 +10,10 @@ use Throwable; final class Database { + // 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'; + private static ?PDO $connection = null; public static function connect(): PDO @@ -94,8 +98,10 @@ final class Database $category->execute([$slug, $name]); } $article = $pdo->prepare("INSERT OR IGNORE INTO articles - (slug, title, summary, body, body_format, card, tags, status, published_at, updated_at, legacy_path) - SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM article_redirects WHERE slug = ?)"); + (id, slug, title, summary, body, body_format, card, tags, status, published_at, updated_at, legacy_path) + SELECT " . self::NEXT_ARTICLE_ID . ", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + WHERE NOT EXISTS (SELECT 1 FROM article_redirects WHERE slug = ?) + AND NOT EXISTS (SELECT 1 FROM article_deletions WHERE slug = ?)"); $link = $pdo->prepare('INSERT INTO article_category_links (article_id, category_slug) VALUES (?, ?)'); foreach ($seed['articles'] as $item) { $format = $item['body_format'] ?? 'markdown'; @@ -104,7 +110,7 @@ final class Database $item['slug'], $item['title'], $item['summary'] ?? '', $format === 'html' ? Html::sanitize($item['body']) : $item['body'], $format, self::json($item['card'] ?? []), self::json($item['tags'] ?? []), - $item['status'] ?? 'published', $date, $date ?? gmdate(DATE_ATOM), $item['legacy'] ?? null, $item['slug'], + $item['status'] ?? 'published', $date, $date ?? gmdate(DATE_ATOM), $item['legacy'] ?? null, $item['slug'], $item['slug'], ]); if ($article->rowCount() === 1) { $id = $pdo->lastInsertId(); diff --git a/app/Content/Repository.php b/app/Content/Repository.php index 77a8ca1..ef44a2f 100644 --- a/app/Content/Repository.php +++ b/app/Content/Repository.php @@ -264,8 +264,8 @@ final class Repository ]; if ($id === null) { $query = $pdo->prepare('INSERT INTO articles - (slug, title, summary, body, body_format, card, tags, status, published_at, updated_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); + (id, slug, title, summary, body, body_format, card, tags, status, published_at, updated_at) + VALUES (' . Database::NEXT_ARTICLE_ID . ', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); $query->execute($values); $id = (int) $pdo->lastInsertId(); } else { @@ -289,6 +289,22 @@ final class Repository }); } + public function deleteArticle(int $id, int $version): void + { + Database::write(function (PDO $pdo) use ($id, $version): void { + // Keep the original seed slug even if the article has been renamed. + $deletions = $pdo->prepare('INSERT INTO article_deletions (slug, article_id) + SELECT slug, id FROM articles WHERE id = :id + UNION ALL SELECT slug, article_id FROM article_redirects WHERE article_id = :id + ON CONFLICT (slug) DO UPDATE SET article_id = excluded.article_id'); + $deletions->execute(['id' => $id]); + + $delete = $pdo->prepare('DELETE FROM articles WHERE id = ? AND version = ?'); + $delete->execute([$id, $version]); + self::checkVersion($delete->rowCount()); + }); + } + public function sitemapArticles(): iterable { $query = $this->pdo->query("SELECT slug, updated_at FROM articles WHERE status = 'published' diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index 128b17f..f28e7c4 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -135,6 +135,7 @@ class AdminController return $this->render($request, 'admin-articles', [ 'title' => '文章与动态', 'articles' => array_slice($articles, 0, 30), 'pageNumber' => $page, 'hasNext' => count($articles) > 30, 'categories' => $categories, 'category' => $category, + 'deleted' => $request->get('deleted') === '1', ]); } @@ -221,6 +222,21 @@ class AdminController ], $error !== '' ? 422 : 200); } + public function deleteArticle(Request $request, string $id): Response + { + if ($failure = $this->guard($request)) { + return $failure; + } + + try { + (new Repository())->deleteArticle((int) $id, $this->version($request)); + } catch (RuntimeException $exception) { + return $this->error($request, $this->validationMessage($exception), $exception instanceof \PDOException ? 500 : 409); + } + + return $this->success($request, '/admin/articles?deleted=1'); + } + public function page(Request $request, string $slug): Response { if ($failure = $this->guard($request)) { diff --git a/app/view/admin-article.php b/app/view/admin-article.php index ceb9e94..1d00a00 100644 --- a/app/view/admin-article.php +++ b/app/view/admin-article.php @@ -51,3 +51,14 @@ $e = View::escape(...); + +
+ 删除文章 +
+ + +

删除后,文章及其目录卡片会从网站移除,且无法恢复。

+ +
+
+ diff --git a/app/view/admin-articles.php b/app/view/admin-articles.php index cc13ab2..f314cd7 100644 --- a/app/view/admin-articles.php +++ b/app/view/admin-articles.php @@ -12,6 +12,7 @@ $filters = $category ? 'category=' . rawurlencode($category) . '&' : ''; 新建文章 +

文章已删除。