Rebuild Laysense site with Webman and SQLite CMS

This commit is contained in:
2026-11-21 17:29:24 +08:00
commit d9bda755d1
99 changed files with 4280 additions and 0 deletions
+221
View File
@@ -0,0 +1,221 @@
<?php
declare(strict_types=1);
namespace App\Content;
use PDO;
use RuntimeException;
final class Repository
{
public const CATEGORIES = ['announcement' => '公告', 'news' => '新闻', 'product' => '产品'];
private PDO $pdo;
public function __construct()
{
$this->pdo = Database::connect();
}
public function site(): array
{
return json_decode((string) $this->pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
}
public function pages(): array
{
return $this->pdo->query('SELECT slug, name, title, updated_at FROM pages ORDER BY rowid')->fetchAll();
}
public function page(string $slug): ?array
{
$page = $this->one('SELECT * FROM pages WHERE slug = ?', [$slug]);
if (!$page) {
return null;
}
foreach (['actions', 'quicklinks', 'partners'] 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');
$query->execute([$slug]);
$page['sections'] = $query->fetchAll();
return $page;
}
public function section(string $page, string $key): ?array
{
return $this->one('SELECT * FROM sections WHERE page_slug = ? AND key = ?', [$page, $key]);
}
public function savePage(string $slug, array $data, int $version): void
{
$query = $this->pdo->prepare('UPDATE pages SET title = ?, description = ?, heading = ?, accent = ?, lead = ?,
image = ?, alt = ?, actions = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
$query->execute([
$data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
$data['image'], $data['alt'], Database::json($data['actions']), self::now(), $slug, $version,
]);
self::checkVersion($query->rowCount());
}
public function saveSection(string $page, string $key, array $data, int $version): void
{
Database::write(function (PDO $pdo) use ($page, $key, $data, $version): void {
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, version = version + 1
WHERE page_slug = ? AND key = ? AND version = ?');
$query->execute([$data['title'], $data['lead'], $data['body'], $page, $key, $version]);
self::checkVersion($query->rowCount());
$query = $pdo->prepare('UPDATE pages SET updated_at = ? WHERE slug = ?');
$query->execute([self::now(), $page]);
});
}
public function articles(?string $category = null, ?string $tag = null, bool $public = true, int $limit = 30, int $offset = 0): array
{
$sql = 'SELECT * FROM articles WHERE 1 = 1';
$args = [];
if ($public) {
$sql .= " AND status = 'published' AND published_at IS NOT NULL";
}
if ($category !== null) {
$sql .= ' AND category = ?';
$args[] = $category;
}
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 ?');
$query->execute([...$args, $limit, $offset]);
return array_map(self::decodeArticle(...), $query->fetchAll());
}
public function articleById(int $id): ?array
{
return self::decodeArticle($this->one('SELECT * 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]));
}
public function articleRedirect(string $slug): ?array
{
return $this->one("SELECT articles.slug FROM article_redirects
JOIN articles ON articles.id = article_redirects.article_id
WHERE article_redirects.slug = ? AND articles.status = 'published'", [$slug]);
}
public function legacyArticle(string $path): ?array
{
return $this->one("SELECT slug FROM articles WHERE legacy_path = ? AND status = 'published'", [$path]);
}
public function saveArticle(?int $id, array $data, int $version): int
{
return Database::write(function (PDO $pdo) use ($id, $data, $version): int {
$old = $id !== null ? $this->articleById($id) : null;
if ($id !== null && (!$old || $old['version'] !== $version)) {
self::checkVersion(0);
}
$existing = $this->one('SELECT id FROM articles WHERE slug = ?', [$data['slug']]);
$redirect = $this->one('SELECT article_id FROM article_redirects WHERE slug = ?', [$data['slug']]);
if (($existing && $existing['id'] !== $id) || ($redirect && $redirect['article_id'] !== $id)) {
throw new RuntimeException('此网址标识已被使用。');
}
$publishedAt = $old['published_at'] ?? ($data['status'] === 'published' ? self::now() : null);
$values = [
$data['slug'], $data['title'], $data['category'], $data['summary'], $data['body'],
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 (?, ?, ?, ?, ?, ?, ?, ?, ?)');
$query->execute($values);
return (int) $pdo->lastInsertId();
}
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 = ?, 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;
});
}
public function sitemapArticles(): iterable
{
$query = $this->pdo->query("SELECT slug, updated_at FROM articles WHERE status = 'published' AND trim(body) <> '' ORDER BY slug");
yield from $query;
}
public function administrator(string $email): ?array
{
return $this->one('SELECT id, email, password_hash FROM users WHERE email = ?', [$email]);
}
public function administratorById(int $id): ?array
{
return $this->one('SELECT id, email, password_hash FROM users WHERE id = ?', [$id]);
}
public function recordLoginAttempt(string $address): bool
{
return Database::write(function (PDO $pdo) use ($address): bool {
$now = time();
$pdo->prepare('DELETE FROM login_attempts WHERE expires_at <= ?')->execute([$now]);
$current = $this->one('SELECT attempts FROM login_attempts WHERE address = ?', [$address]);
if ($current && $current['attempts'] >= 12) {
return false;
}
$query = $pdo->prepare('INSERT INTO login_attempts (address, attempts, expires_at) VALUES (?, 1, ?)
ON CONFLICT (address) DO UPDATE SET attempts = attempts + 1');
$query->execute([$address, $now + 900]);
return true;
});
}
public function clearLoginAttempts(string $address): void
{
$this->pdo->prepare('DELETE FROM login_attempts WHERE address = ?')->execute([$address]);
}
private function one(string $sql, array $args): ?array
{
$query = $this->pdo->prepare($sql);
$query->execute($args);
return $query->fetch() ?: null;
}
private static function decodeArticle(?array $article): ?array
{
if ($article !== null) {
$article['tags'] = json_decode($article['tags'], true, flags: JSON_THROW_ON_ERROR);
}
return $article;
}
private static function now(): string
{
return date('Y-m-d H:i:s');
}
private static function checkVersion(int $count): void
{
if ($count !== 1) {
throw new RuntimeException('此内容已在其他窗口更新。请保留你的修改,重新打开后再合并。');
}
}
}