forked from laysense/laysense.cn
Add visual CMS, media library and product directory
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMXPath;
|
||||
use PDO;
|
||||
|
||||
final class DesignMigration
|
||||
{
|
||||
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);
|
||||
$home = array_column($stock['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);
|
||||
$xpath = new DOMXPath($doc);
|
||||
$insert = $pdo->prepare('INSERT INTO catalog (name, category, body, image, alt, url, label, extra_url, extra_label, position)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
foreach ($xpath->query('//article[contains(concat(" ", @class, " "), " project ")]') as $position => $card) {
|
||||
$image = $xpath->query('.//img', $card)->item(0);
|
||||
$links = $xpath->query('.//a', $card);
|
||||
$body = '';
|
||||
foreach ($xpath->query('.//p', $card) as $paragraph) {
|
||||
$body .= $doc->saveHTML($paragraph);
|
||||
}
|
||||
$insert->execute([
|
||||
trim($xpath->evaluate('string(.//h3)', $card)), trim($xpath->evaluate('string(.//span)', $card)),
|
||||
Html::sanitize($body), $image->getAttribute('src'), $image->getAttribute('alt'),
|
||||
$links->item(0)->getAttribute('href'), trim($links->item(0)->textContent),
|
||||
$links->item(1)?->getAttribute('href') ?? '', trim($links->item(1)?->textContent ?? ''), $position * 10,
|
||||
]);
|
||||
}
|
||||
$pdo->prepare('INSERT INTO catalog (name, category, body, image, alt, url, label, position) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')
|
||||
->execute(['SHNU 学生软著服务', '校园服务', '<p>面向 SHNU 学生的软件著作权申请服务。</p>',
|
||||
'/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]);
|
||||
|
||||
$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'");
|
||||
foreach ($choices->fetchAll() as $section) {
|
||||
$body = preg_replace('~(<article\b[^>]*class="[^"]*\bfeature-card)("(?:[^>]*>)\s*<h[2-4]>选择来笙</h[2-4]>)~u',
|
||||
'$1 choice-card$2', $section['body']);
|
||||
if ($body !== null && $body !== $section['body']) {
|
||||
$updateChoice->execute([$body, $section['page_slug']]);
|
||||
}
|
||||
}
|
||||
|
||||
$icons = ['/assets/original/10-1.png' => '/assets/service/project.svg',
|
||||
'/assets/original/10-2.png' => '/assets/service/code.svg', '/assets/original/10-3.png' => '/assets/service/support.svg'];
|
||||
$sections = $pdo->query("SELECT page_slug, body FROM sections WHERE key = 'options' AND page_slug IN ('sc', 'shnu-sc')");
|
||||
$update = $pdo->prepare("UPDATE sections SET body = ?, version = version + 1 WHERE page_slug = ? AND key = 'options'");
|
||||
foreach ($sections->fetchAll() as $section) {
|
||||
$body = strtr($section['body'], $icons);
|
||||
$body = preg_replace('/(<article\b[^>]*>)\s*<li\b[^>]*>(.*?)<\/li>\s*(<\/article>)/s', '$1$2$3', $body);
|
||||
if ($body !== $section['body']) {
|
||||
$update->execute([$body, $section['page_slug']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user