Build a full-width CMS canvas and restore featured home products
This commit is contained in:
@@ -40,9 +40,13 @@ final class Database
|
||||
|
||||
self::write(function (PDO $pdo) use ($root, $seed): void {
|
||||
$pdo->exec(file_get_contents($root . '/resources/schema.sql'));
|
||||
if (!in_array('status', array_column($pdo->query('PRAGMA table_info(pages)')->fetchAll(), 'name'), true)) {
|
||||
$pageColumns = array_column($pdo->query('PRAGMA table_info(pages)')->fetchAll(), 'name');
|
||||
if (!in_array('status', $pageColumns, true)) {
|
||||
$pdo->exec("ALTER TABLE pages ADD COLUMN status TEXT NOT NULL DEFAULT 'published' CHECK (status IN ('draft', 'published'))");
|
||||
}
|
||||
if (!in_array('image_scale', $pageColumns, true)) {
|
||||
$pdo->exec('ALTER TABLE pages ADD COLUMN image_scale INTEGER NOT NULL DEFAULT 0');
|
||||
}
|
||||
$site = $pdo->prepare('INSERT OR IGNORE INTO site_settings (id, payload) VALUES (1, ?)');
|
||||
$site->execute([self::json($seed['site'])]);
|
||||
|
||||
@@ -92,6 +96,10 @@ final class Database
|
||||
if ($migration->rowCount() === 1) {
|
||||
DesignMigration::apply($pdo);
|
||||
}
|
||||
$migration->execute(['restore_home_featured_products']);
|
||||
if ($migration->rowCount() === 1) {
|
||||
DesignMigration::restoreFeaturedProducts($pdo, $seed);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ use PDO;
|
||||
|
||||
final class DesignMigration
|
||||
{
|
||||
private const DIRECTORY_SUMMARY = '<p>浏览来笙的建站工具、智能 DNS 和软件著作权服务。</p><a class="button button-primary" href="/services">查看产品与服务</a>';
|
||||
|
||||
public static function apply(PDO $pdo): void
|
||||
{
|
||||
$stock = json_decode(file_get_contents(dirname(__DIR__, 2) . '/resources/seed.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
@@ -40,7 +42,7 @@ final class DesignMigration
|
||||
'/assets/original/shnu-hero.webp', '', '/shnu-sc', '查看学生方案', 30]);
|
||||
$pdo->prepare("UPDATE sections SET body = ?, version = version + 1
|
||||
WHERE page_slug = 'home' AND key = 'product' AND version = 0 AND body = ?")
|
||||
->execute(['<p>浏览来笙的建站工具、智能 DNS 和软件著作权服务。</p><a class="button button-primary" href="/services">查看产品与服务</a>', $stockBody]);
|
||||
->execute([self::DIRECTORY_SUMMARY, $stockBody]);
|
||||
|
||||
$choices = $pdo->query("SELECT page_slug, body FROM sections WHERE key = 'why' AND page_slug IN ('sc', 'shnu-sc')");
|
||||
$updateChoice = $pdo->prepare("UPDATE sections SET body = ?, version = version + 1 WHERE page_slug = ? AND key = 'why'");
|
||||
@@ -64,4 +66,18 @@ final class DesignMigration
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function restoreFeaturedProducts(PDO $pdo, array $seed): void
|
||||
{
|
||||
$home = array_column($seed['pages'], null, 'slug')['home'];
|
||||
$stockBody = Html::sanitize(array_column($home['sections'], 'body', 'key')['product']);
|
||||
$doc = new DOMDocument('1.0', 'UTF-8');
|
||||
$doc->loadHTML('<meta charset="UTF-8">' . $stockBody, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
||||
$projects = $doc->getElementsByTagName('article');
|
||||
$body = '<div class="project-stack">' . $doc->saveHTML($projects->item(0))
|
||||
. $doc->saveHTML($projects->item(1)) . '</div>';
|
||||
$pdo->prepare("UPDATE sections SET body = ?, version = version + 1
|
||||
WHERE page_slug = 'home' AND key = 'product' AND body = ?")
|
||||
->execute([Html::sanitize($body), self::DIRECTORY_SUMMARY]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMNode;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use RuntimeException;
|
||||
|
||||
final class Html
|
||||
{
|
||||
@@ -49,6 +50,37 @@ final class Html
|
||||
return str_replace(['<h1>', '</h1>'], ['<h2>', '</h2>'], $html);
|
||||
}
|
||||
|
||||
public static function applyCanvasEdits(string $body, array $edits): string
|
||||
{
|
||||
if (!array_is_list($edits) || count($edits) > 512) {
|
||||
throw new RuntimeException('正文增量无效。');
|
||||
}
|
||||
$size = strlen($body);
|
||||
$cursor = 0;
|
||||
$result = '';
|
||||
foreach ($edits as $edit) {
|
||||
if (!is_array($edit) || !array_is_list($edit) || count($edit) !== 3) {
|
||||
throw new RuntimeException('正文增量无效。');
|
||||
}
|
||||
[$offset, $remove, $insert] = $edit;
|
||||
if (!is_int($offset) || !is_int($remove) || !is_string($insert)
|
||||
|| $offset < $cursor || $offset > $size || $remove < 0 || $remove > $size - $offset) {
|
||||
throw new RuntimeException('正文增量无效。');
|
||||
}
|
||||
$result .= substr($body, $cursor, $offset - $cursor) . $insert;
|
||||
$cursor = $offset + $remove;
|
||||
if (strlen($result) > 300000) {
|
||||
throw new RuntimeException('正文太长。');
|
||||
}
|
||||
}
|
||||
$result .= substr($body, $cursor);
|
||||
if (strlen($result) > 300000 || !mb_check_encoding($result, 'UTF-8')) {
|
||||
throw new RuntimeException('正文太长或编码无效。');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function sanitize(string $html): string
|
||||
{
|
||||
$document = new DOMDocument('1.0', 'UTF-8');
|
||||
|
||||
+31
-16
@@ -65,15 +65,19 @@ final class Repository
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections): void {
|
||||
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
image = ?, alt = ?, actions = ?, quicklinks = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
|
||||
image = ?, alt = ?, image_scale = ?, actions = ?, quicklinks = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
|
||||
$query->execute([
|
||||
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$data['image'], $data['alt'], Database::json($data['actions']), Database::json($data['quicklinks']),
|
||||
$data['image'], $data['alt'], $data['image_scale'], Database::json($data['actions']), Database::json($data['quicklinks']),
|
||||
$data['status'], self::now(), $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
foreach ($sections as $section) {
|
||||
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
|
||||
if ($section['version'] === null) {
|
||||
self::insertSection($pdo, $slug, $section['key'], $section);
|
||||
} else {
|
||||
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -97,19 +101,28 @@ final class Repository
|
||||
public function createSection(string $page, string $key, string $title, string $style, string $body): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body): 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, $title, '', $style, $body, $page]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body') + ['lead' => '']);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
|
||||
public function arrangeSection(string $page, string $key, string $operation, int $version): void
|
||||
private static function insertSection(PDO $pdo, string $page, string $key, array $data): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $operation, $version): 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]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
}
|
||||
|
||||
public function arrangeSection(string $page, string $key, string $operation, int $version, int $pageVersion): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $operation, $version, $pageVersion): void {
|
||||
$current = $pdo->prepare('SELECT version FROM pages WHERE slug = ?');
|
||||
$current->execute([$page]);
|
||||
$revision = $current->fetchColumn();
|
||||
self::checkVersion($revision !== false && (int) $revision === $pageVersion ? 1 : 0);
|
||||
if ($operation === 'delete') {
|
||||
$query = $pdo->prepare('DELETE FROM sections WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$page, $key, $version]);
|
||||
@@ -118,13 +131,15 @@ final class Repository
|
||||
|
||||
return;
|
||||
}
|
||||
$query = $pdo->prepare('SELECT key FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
$query = $pdo->prepare('SELECT key, version FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
$query->execute([$page]);
|
||||
$keys = $query->fetchAll(PDO::FETCH_COLUMN);
|
||||
$index = array_search($key, $keys, true);
|
||||
if ($index === false) {
|
||||
$revisions = $query->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
if (!array_key_exists($key, $revisions)) {
|
||||
throw new RuntimeException('找不到该区块。');
|
||||
}
|
||||
self::checkVersion((int) $revisions[$key] === $version ? 1 : 0);
|
||||
$keys = array_map('strval', array_keys($revisions));
|
||||
$index = array_search($key, $keys, true);
|
||||
$other = $index + ($operation === 'up' ? -1 : 1);
|
||||
if (!isset($keys[$other])) {
|
||||
return;
|
||||
@@ -134,7 +149,7 @@ final class Repository
|
||||
foreach ($keys as $position => $section) {
|
||||
$query->execute([$position, $page, $section]);
|
||||
}
|
||||
self::touchPage($pdo, $page);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user