From 2b01537cf3a1cd4fdc55c779a0570d60cec963b1 Mon Sep 17 00:00:00 2001 From: ChatGPT Date: Sat, 21 Nov 2026 23:36:20 +0800 Subject: [PATCH] Audit merged directory links and test rendered fallbacks --- app/Content/DirectoryCard.php | 39 ++++++++++++++ app/view/network-groups.php | 85 +++++++++++++++--------------- app/view/services.php | 98 ++++++++++++++++++----------------- public/assets/site.css | 4 +- tests/dircheck.php | 18 ------- tests/directory-links.php | 56 ++++++++++++++++++++ tests/directory-links.sh | 69 ++---------------------- 7 files changed, 193 insertions(+), 176 deletions(-) create mode 100644 app/Content/DirectoryCard.php delete mode 100644 tests/dircheck.php create mode 100644 tests/directory-links.php diff --git a/app/Content/DirectoryCard.php b/app/Content/DirectoryCard.php new file mode 100644 index 0000000..0219b7a --- /dev/null +++ b/app/Content/DirectoryCard.php @@ -0,0 +1,39 @@ + $primary, 'detail' => $primary === $detail ? '' : $detail]; + } + + private static function hasText(string $text): bool + { + return preg_match('/[^\s\p{Z}\p{Cf}]/u', $text) === 1; + } + + private static function hasBody(string $html): bool + { + if (self::hasText(html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8'))) { + return true; + } + if (!str_contains($html, 'src=')) { + return false; + } + $document = new DOMDocument(); + $document->loadHTML('' . $html, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET); + $xpath = new DOMXPath($document); + + return (bool) $xpath->evaluate('boolean(//img[@src] | //audio[@src] | //video[@src] | //audio/source[@src] | //video/source[@src])'); + } +} diff --git a/app/view/network-groups.php b/app/view/network-groups.php index 8189600..39d659f 100644 --- a/app/view/network-groups.php +++ b/app/view/network-groups.php @@ -1,43 +1,42 @@ - -
-
- -
-

支持项目与合作伙伴

- 查看全部 -
- - $group): ?> -
-

-

暂无已发布条目。

- -
- -
-
+ +
+
+ +
+

支持项目与合作伙伴

+ 查看全部 +
+ + $group): ?> +
+

+

暂无已发布条目。

+
    + + $target, 'detail' => $detail] = DirectoryCard::links($item, Html::articleBody($item)); + ?> +
  • + + + + + + + + 查看详情 + +
  • + +
+
+ +
+
diff --git a/app/view/services.php b/app/view/services.php index 64a123a..c7b8163 100644 --- a/app/view/services.php +++ b/app/view/services.php @@ -1,48 +1,50 @@ - -
-
- -

产品与服务

-

自托管建站、开源智能 DNS 与软件著作权代理服务。

-
-
-
-
-

暂无可查看的产品和服务。

-
- - -
- -
<?= $e($card['alt'] ?? '') ?>
- -
-

-

- -

- -
- - -
- - -
- -
-
- -
-
-
+ +
+
+ +

产品与服务

+

自托管建站、开源智能 DNS 与软件著作权代理服务。

+
+
+
+
+

暂无可查看的产品和服务。

+
+ + $target, 'detail' => $detail] = DirectoryCard::links($product, $body); + $extra = $card['extra_url'] ?? ''; + ?> + + +
+
+
diff --git a/public/assets/site.css b/public/assets/site.css index 28d978e..8d7107e 100644 --- a/public/assets/site.css +++ b/public/assets/site.css @@ -282,7 +282,9 @@ progress { width: 100%; height: .8rem; accent-color: var(--accent); } .network-group + .network-group { margin-top: 2.2rem; } .network-group h3 { margin-bottom: .8rem; } .network-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); gap: .7rem; } -.network-list li > * { display: flex; align-items: center; gap: .75rem; min-height: 4rem; height: 100%; border-radius: .65rem; background: var(--white); border: 1px solid var(--line); color: var(--ink); text-decoration: none; padding: .75rem; transition: transform 160ms ease, border-color 160ms ease; } +.network-list li { display: flex; flex-direction: column; gap: .35rem; } +.network-list li > * { display: flex; align-items: center; gap: .75rem; min-height: 4rem; border-radius: .65rem; background: var(--white); border: 1px solid var(--line); color: var(--ink); text-decoration: none; padding: .75rem; transition: transform 160ms ease, border-color 160ms ease; } +.network-list li > :first-child { flex: 1; } .network-list a:hover { transform: translateY(-2px); border-color: var(--accent); color: var(--accent); } .network-list img { max-width: 3rem; max-height: 1.8rem; object-fit: contain; object-position: left; } .network-list img[src$="oneprovider_invert.svg"] { background: #29354a; border-radius: .25rem; padding: .21rem; } diff --git a/tests/dircheck.php b/tests/dircheck.php deleted file mode 100644 index b1d352c..0000000 --- a/tests/dircheck.php +++ /dev/null @@ -1,18 +0,0 @@ -卡片网址是否已发布 -$pdo = Database::connect(); -$query = $pdo->query("SELECT a.slug, a.card, a.status FROM articles a - WHERE EXISTS (SELECT 1 FROM article_category_links l WHERE l.article_id = a.id - AND l.category_slug IN ('services', 'supported-projects', 'partners')) - ORDER BY a.id"); -foreach ($query as $row) { - $card = json_decode($row['card'], true) ?: []; - echo $row['slug'], '|', $card['url'] ?? '', '|', $row['status'], "\n"; -} diff --git a/tests/directory-links.php b/tests/directory-links.php new file mode 100644 index 0000000..75a1a50 --- /dev/null +++ b/tests/directory-links.php @@ -0,0 +1,56 @@ + [['card' => ['url' => $primary], 'body' => '

有正文

'], $primary, true], + 'configured-only' => [['card' => ['url' => '/pagepan']], '/pagepan', false], + 'summary-only' => [['summary' => '仅摘要'], '/articles/summary-only', false], + 'editor-placeholder' => [['body' => '


 ​

'], '', false], + 'markdown-comment' => [['body' => '', 'body_format' => 'markdown'], '', false], + 'media-body' => [['body' => ''], '/articles/media-body', false], + 'unconfigured' => [[], '', false], +]; +$items = []; +foreach ($cases as $slug => [$fields]) { + $items[] = $fields + ['slug' => $slug, 'title' => $slug, 'card' => [], 'summary' => '', 'body' => '', 'body_format' => 'html']; +} + +function parseFragment(string $html): DOMXPath +{ + $document = new DOMDocument(); + $document->loadHTML('' . $html, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET); + + return new DOMXPath($document); +} + +function equal(mixed $actual, mixed $expected, string $message): void +{ + if ($actual !== $expected) { + throw new RuntimeException($message . ' ' . json_encode([$expected, $actual], JSON_UNESCAPED_UNICODE)); + } +} + +$services = parseFragment(View::fragment('services', ['products' => $items])); +$network = parseFragment(View::fragment('network-groups', [ + 'network' => ['partners' => ['name' => '合作伙伴', 'articles' => $items]], 'standalone' => true, +])); +foreach ($cases as $slug => [, $target, $detail]) { + $card = "//article[h2='$slug' or div/h2='$slug']"; + equal($services->evaluate("count($card)"), 1.0, "$slug 服务卡片存在"); + equal($services->evaluate("string($card/div/h2/a/@href)"), $target, "$slug 服务标题目标"); + equal($services->evaluate("string($card/div/div/a[contains(@class, 'button-primary')]/@href)"), $target, "$slug 服务按钮目标"); + equal($services->evaluate("boolean($card/div/div/a[@class='catalog-detail'][@href='/articles/$slug'])"), $detail, "$slug 服务独立详情"); + + $item = "//li[*[1]/span='$slug']"; + equal($network->evaluate("count($item)"), 1.0, "$slug 伙伴卡片存在"); + equal($network->evaluate("string($item/*[1]/@href)"), $target, "$slug 伙伴卡片目标"); + equal($network->evaluate("boolean($item/a[@class='network-detail'][@href='/articles/$slug'])"), $detail, "$slug 伙伴独立详情"); +} + +printf("通过 %d 组目录卡片测试。\n", count($cases)); diff --git a/tests/directory-links.sh b/tests/directory-links.sh index 872b302..6c4b361 100644 --- a/tests/directory-links.sh +++ b/tests/directory-links.sh @@ -1,67 +1,4 @@ #!/usr/bin/env bash -# 目录页(/services、/network)的卡片链接必须指向卡片配置的网址,而不是 /articles/ -set -u -cd "$(dirname "$0")/.." || exit 1 -BASE=http://127.0.0.1:8787 -PASS=0; FAIL=0 -ok(){ PASS=$((PASS+1)); printf ' ok %s\n' "$1"; } -no(){ FAIL=$((FAIL+1)); printf ' FAIL %s -> %s\n' "$1" "${2:-}"; } - -SERVICES=$(curl -s "$BASE/services") -NETWORK=$(curl -s "$BASE/network") -ROWS=$(php tests/dircheck.php) - -echo "== /services 卡片链接 ==" -while IFS='|' read -r slug url status; do - [ -z "$slug" ] && continue - [ -z "$url" ] && continue - if echo "$SERVICES" | grep -q "href=\"$url\""; then ok "$slug 标题/按钮指向 $url"; else no "$slug 未指向 $url"; fi - if echo "$SERVICES" | grep -q "href=\"/articles/$slug\""; then no "$slug 仍出现 /articles/$slug"; else ok "$slug 不再指向文章页"; fi -done <<< "$(echo "$ROWS" | grep -E '^service-')" - -echo "== /network 卡片链接 ==" -while IFS='|' read -r slug url status; do - [ -z "$slug" ] && continue - [ -z "$url" ] && continue - if echo "$NETWORK" | grep -q "href=\"$url\""; then ok "$slug 指向 $url"; else no "$slug 未指向 $url"; fi - if echo "$NETWORK" | grep -q "href=\"/articles/$slug\""; then no "$slug 仍出现 /articles/$slug"; else ok "$slug 不再指向文章页"; fi -done <<< "$(echo "$ROWS" | grep -E '^network-')" - -echo "== 标题链接与按钮链接一致性 ==" -python3 - "$BASE" <<'PY' -import re, sys, html, urllib.request -base = sys.argv[1] -def get(path): - return urllib.request.urlopen(base + path, timeout=10).read().decode() -page = get('/services') -cards = re.findall(r'
.*?
', page, re.S) -bad = 0 -for card in cards: - links = re.findall(r']*href="([^"]+)"', card) - title = re.search(r'