Reflow home shortcuts and support article deletion

This commit is contained in:
2026-11-22 02:00:29 +08:00
parent 02e8685bc2
commit 4db9af6404
11 changed files with 110 additions and 16 deletions
+9 -3
View File
@@ -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();
+18 -2
View File
@@ -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'