Add canvas element tools and categorized article editing

This commit is contained in:
2026-11-21 22:24:58 +08:00
parent 6efcdefadf
commit a5b3408358
45 changed files with 1607 additions and 660 deletions
+73 -34
View File
@@ -9,7 +9,13 @@ use RuntimeException;
final class Repository
{
public const CATEGORIES = ['announcement' => '公告', 'news' => '新闻', 'product' => '产品'];
public const DIRECTORIES = [
'services' => '产品与服务', 'supported-projects' => '来笙支持项目', 'partners' => '来笙合作伙伴',
];
public const CATEGORIES = ['announcement' => '公告', 'news' => '新闻', 'product' => '产品'] + self::DIRECTORIES;
private const ARTICLE_FIELDS = 'articles.*, (SELECT json_group_object(c.slug, c.name)
FROM article_category_links l JOIN article_categories c ON c.slug = l.category_slug
WHERE l.article_id = articles.id) AS categories';
private PDO $pdo;
public function __construct()
@@ -32,8 +38,8 @@ final class Repository
public function createPage(string $slug, string $name): void
{
$query = $this->pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent,
lead, image, alt, actions, quicklinks, partners, status, updated_at)
VALUES (?, ?, ?, '', ?, '', '', '', '', '[]', '[]', '[]', 'draft', ?)");
lead, image, alt, actions, quicklinks, status, updated_at)
VALUES (?, ?, ?, '', ?, '', '', '', '', '[]', '[]', 'draft', ?)");
$query->execute([$slug, $name, $name . ' · 来笙', $name, self::now()]);
if ($query->rowCount() !== 1) {
throw new RuntimeException('此页面地址已存在。');
@@ -46,7 +52,7 @@ final class Repository
if (!$page) {
return null;
}
foreach (['actions', 'quicklinks', 'partners'] as $key) {
foreach (['actions', 'quicklinks'] 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');
@@ -92,25 +98,25 @@ final class Repository
private static function updateSection(PDO $pdo, string $page, string $key, array $data, int $version): void
{
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, version = version + 1
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, feed = ?, version = version + 1
WHERE page_slug = ? AND key = ? AND version = ?');
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $page, $key, $version]);
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $data['feed'] ?? null, $page, $key, $version]);
self::checkVersion($query->rowCount());
}
public function createSection(string $page, string $key, string $title, string $style, string $body): void
public function createSection(string $page, string $key, string $title, string $style, string $body, ?string $feed = null): void
{
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body): void {
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body') + ['lead' => '']);
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body, $feed): void {
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body', 'feed') + ['lead' => '']);
self::touchPage($pdo, $page, structureChanged: true);
});
}
private static function insertSection(PDO $pdo, string $page, string $key, array $data): void
{
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, position)
SELECT ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $page]);
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, feed, position)
SELECT ?, ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $data['feed'] ?? null, $page]);
if ($query->rowCount() !== 1) {
throw new RuntimeException('此区块锚点已被使用。');
}
@@ -159,23 +165,44 @@ final class Repository
$pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]);
}
public function articles(?string $category = null, ?string $tag = null, bool $public = true, int $limit = 30, int $offset = 0): array
public function categories(): array
{
$sql = 'SELECT * FROM articles WHERE 1 = 1';
return $this->pdo->query('SELECT slug, name FROM article_categories ORDER BY rowid')->fetchAll(PDO::FETCH_KEY_PAIR);
}
public function createCategory(string $slug, string $name): void
{
$query = $this->pdo->prepare('INSERT OR IGNORE INTO article_categories (slug, name) VALUES (?, ?)');
$query->execute([$slug, $name]);
if ($query->rowCount() !== 1) {
throw new RuntimeException('此分类网址标识已被使用。');
}
}
public function articles(string|array|null $category = null, ?string $tag = null, bool $public = true,
int $limit = 30, int $offset = 0, bool $directoryOrder = false): array
{
if ($category === []) {
return [];
}
$sql = 'SELECT ' . self::ARTICLE_FIELDS . ' FROM articles WHERE 1 = 1';
$args = [];
if ($public) {
$sql .= " AND status = 'published' AND published_at IS NOT NULL";
$sql .= " AND status = 'published'";
}
if ($category !== null) {
$sql .= ' AND category = ?';
$args[] = $category;
$args = (array) $category;
$marks = implode(', ', array_fill(0, count($args), '?'));
$sql .= " AND EXISTS (SELECT 1 FROM article_category_links
WHERE article_id = articles.id AND category_slug IN ($marks))";
}
if ($tag !== null) {
$sql .= ' AND EXISTS (SELECT 1 FROM json_each(articles.tags) WHERE value = ?)';
$args[] = $tag;
}
$order = $public ? 'published_at' : 'updated_at';
$query = $this->pdo->prepare($sql . ' ORDER BY ' . $order . ' DESC, id DESC LIMIT ? OFFSET ?');
$order = $directoryOrder ? "COALESCE(json_extract(card, '$.position'), 0), id" : "$order DESC, id DESC";
$query = $this->pdo->prepare($sql . ' ORDER BY ' . $order . ' LIMIT ? OFFSET ?');
$query->execute([...$args, $limit, $offset]);
return array_map(self::decodeArticle(...), $query->fetchAll());
@@ -183,12 +210,12 @@ final class Repository
public function articleById(int $id): ?array
{
return self::decodeArticle($this->one('SELECT * FROM articles WHERE id = ?', [$id]));
return self::decodeArticle($this->one('SELECT ' . self::ARTICLE_FIELDS . ' FROM articles WHERE id = ?', [$id]));
}
public function published(string $slug): ?array
{
return self::decodeArticle($this->one("SELECT * FROM articles WHERE slug = ? AND status = 'published'", [$slug]));
return self::decodeArticle($this->one('SELECT ' . self::ARTICLE_FIELDS . " FROM articles WHERE slug = ? AND status = 'published'", [$slug]));
}
public function articleRedirect(string $slug): ?array
@@ -215,26 +242,34 @@ final class Repository
if (($existing && $existing['id'] !== $id) || ($redirect && $redirect['article_id'] !== $id)) {
throw new RuntimeException('此网址标识已被使用。');
}
$publishedAt = $old['published_at'] ?? ($data['status'] === 'published' ? self::now() : null);
$publishedAt = $old['published_at'] ?? ($data['status'] === 'published' && ($old['status'] ?? '') !== 'published' ? self::now() : null);
$values = [
$data['slug'], $data['title'], $data['category'], $data['summary'], $data['body'],
$data['slug'], $data['title'], $data['summary'], $data['body'],
$data['body_format'], Database::json($data['card']),
Database::json($data['tags']), $data['status'], $publishedAt, self::now(),
];
if ($id === null) {
$query = $pdo->prepare('INSERT INTO articles
(slug, title, category, summary, body, tags, status, published_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
(slug, title, summary, body, body_format, card, tags, status, published_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$query->execute($values);
return (int) $pdo->lastInsertId();
$id = (int) $pdo->lastInsertId();
} else {
if ($old['slug'] !== $data['slug']) {
$pdo->prepare('DELETE FROM article_redirects WHERE slug = ? AND article_id = ?')->execute([$data['slug'], $id]);
$pdo->prepare('INSERT INTO article_redirects (slug, article_id) VALUES (?, ?)')->execute([$old['slug'], $id]);
}
$query = $pdo->prepare('UPDATE articles SET slug = ?, title = ?, summary = ?, body = ?,
body_format = ?, card = ?, tags = ?, status = ?, published_at = ?, updated_at = ?, version = version + 1
WHERE id = ? AND version = ?');
$query->execute([...$values, $id, $version]);
self::checkVersion($query->rowCount());
$pdo->prepare('DELETE FROM article_category_links WHERE article_id = ?')->execute([$id]);
}
if ($old['slug'] !== $data['slug']) {
$pdo->prepare('DELETE FROM article_redirects WHERE slug = ? AND article_id = ?')->execute([$data['slug'], $id]);
$pdo->prepare('INSERT INTO article_redirects (slug, article_id) VALUES (?, ?)')->execute([$old['slug'], $id]);
$link = $pdo->prepare('INSERT INTO article_category_links (article_id, category_slug) VALUES (?, ?)');
foreach ($data['categories'] as $category) {
$link->execute([$id, $category]);
}
$query = $pdo->prepare('UPDATE articles SET slug = ?, title = ?, category = ?, summary = ?, body = ?, tags = ?,
status = ?, published_at = ?, updated_at = ?, version = version + 1 WHERE id = ? AND version = ?');
$query->execute([...$values, $id, $version]);
self::checkVersion($query->rowCount());
return $id;
});
@@ -242,7 +277,9 @@ final class Repository
public function sitemapArticles(): iterable
{
$query = $this->pdo->query("SELECT slug, updated_at FROM articles WHERE status = 'published' AND trim(body) <> '' ORDER BY slug");
$query = $this->pdo->query("SELECT slug, updated_at FROM articles WHERE status = 'published'
AND (trim(body) <> '' OR trim(summary) <> '' OR json_extract(card, '$.image') <> '' OR json_extract(card, '$.url') <> '')
ORDER BY slug");
yield from $query;
}
@@ -297,7 +334,9 @@ final class Repository
private static function decodeArticle(?array $article): ?array
{
if ($article !== null) {
$article['tags'] = json_decode($article['tags'], true, flags: JSON_THROW_ON_ERROR);
foreach (['tags', 'categories', 'card'] as $field) {
$article[$field] = json_decode($article[$field], true, flags: JSON_THROW_ON_ERROR);
}
}
return $article;