Author SHA1 Message Date
hy d6fee652db Merge pull request '复核 #7、#8:修正目录详情判定和失效的回归测试' (#9) from ChatGPT/laysense.cn:audit-directory-links into main 2026-11-22 00:20:58 +08:00
ChatGPT 2b01537cf3 Audit merged directory links and test rendered fallbacks 2026-11-21 23:36:20 +08:00
DeepSeek 2ef38bd716 Merge pull request #8: 修复 /network 指向空白文章的死链,统一目录兜底规则
目录卡片:有网址用网址,无网址且有内容才回退文章页,否则不渲染链接。21 项断言 + 三套端到端回归通过。
2026-11-21 23:26:34 +08:00
7 changed files with 193 additions and 176 deletions
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Content;
use DOMDocument;
use DOMXPath;
final class DirectoryCard
{
public static function links(array $article, string $body): array
{
$detail = self::hasText($article['summary']) || self::hasBody($body) ? '/articles/' . $article['slug'] : '';
$primary = ($article['card']['url'] ?? '') ?: $detail;
return ['primary' => $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('<meta charset="utf-8">' . $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])');
}
}
+42 -43
View File
@@ -1,43 +1,42 @@
<?php
use App\Content\View;
$e = View::escape(...);
?>
<section class="section network" aria-label="支持项目与合作伙伴">
<div class="wrap">
<?php if (!$standalone): ?>
<div class="section-heading inline-heading">
<h2>支持项目与合作伙伴</h2>
<a class="button button-outline" href="/network">查看全部</a>
</div>
<?php endif ?>
<?php foreach ($network as $slug => $group): ?>
<div class="network-group" id="<?= $e($slug) ?>">
<?php if ($standalone): ?><h2><?= $e($group['name']) ?></h2><?php else: ?><h3><?= $e($group['name']) ?></h3><?php endif ?>
<?php if (!$group['articles']): ?><p class="empty-state">暂无已发布条目。</p><?php endif ?>
<ul class="network-list">
<?php foreach ($group['articles'] as $item): ?>
<?php
// 优先跳卡片配置的网址;没有网址时,只有文章本身有内容才回退到文章页,否则不渲染链接。
$card = $item['card'];
$articlePath = '/articles/' . $item['slug'];
$hasDetail = $item['body'] !== '' || $item['summary'] !== '';
$target = ($card['url'] ?? '') ?: ($hasDetail ? $articlePath : '');
?>
<li>
<?php if ($target !== ''): ?><a href="<?= $e($target) ?>"><?php else: ?><span class="network-static"><?php endif ?>
<?php if (!empty($card['image'])): ?>
<img src="<?= $e($card['image']) ?>" alt="" loading="lazy" width="76" height="28">
<?php endif ?>
<span><?= $e($item['title']) ?></span>
<?php if ($target !== ''): ?></a><?php else: ?></span><?php endif ?>
<?php if ($standalone && !empty($card['url']) && $hasDetail): ?>
<a class="network-detail" href="<?= $e($articlePath) ?>">查看详情</a>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
</div>
<?php endforeach ?>
</div>
</section>
<?php
use App\Content\DirectoryCard;
use App\Content\Html;
use App\Content\View;
$e = View::escape(...);
?>
<section class="section network" aria-label="支持项目与合作伙伴">
<div class="wrap">
<?php if (!$standalone): ?>
<div class="section-heading inline-heading">
<h2>支持项目与合作伙伴</h2>
<a class="button button-outline" href="/network">查看全部</a>
</div>
<?php endif ?>
<?php foreach ($network as $slug => $group): ?>
<div class="network-group" id="<?= $e($slug) ?>">
<?php if ($standalone): ?><h2><?= $e($group['name']) ?></h2><?php else: ?><h3><?= $e($group['name']) ?></h3><?php endif ?>
<?php if (!$group['articles']): ?><p class="empty-state">暂无已发布条目。</p><?php endif ?>
<ul class="network-list">
<?php foreach ($group['articles'] as $item): ?>
<?php
$card = $item['card'];
['primary' => $target, 'detail' => $detail] = DirectoryCard::links($item, Html::articleBody($item));
?>
<li>
<?php if ($target !== ''): ?><a href="<?= $e($target) ?>"><?php else: ?><span class="network-static"><?php endif ?>
<?php if (!empty($card['image'])): ?>
<img src="<?= $e($card['image']) ?>" alt="" loading="lazy" width="76" height="28">
<?php endif ?>
<span><?= $e($item['title']) ?></span>
<?php if ($target !== ''): ?></a><?php else: ?></span><?php endif ?>
<?php if ($standalone && $detail !== ''): ?>
<a class="network-detail" href="<?= $e($detail) ?>">查看详情</a>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
</div>
<?php endforeach ?>
</div>
</section>
+50 -48
View File
@@ -1,48 +1,50 @@
<?php
use App\Content\Html;
use App\Content\View;
$e = View::escape(...);
?>
<section class="section catalog-hero">
<div class="wrap">
<nav class="breadcrumbs" aria-label="面包屑"><a href="/">首页</a><span aria-hidden="true">/</span><span aria-current="page">产品与服务</span></nav>
<h1>产品与服务</h1>
<p>自托管建站、开源智能 DNS 与软件著作权代理服务。</p>
</div>
</section>
<section class="section catalog-listing" aria-label="产品与服务目录">
<div class="wrap">
<?php if (!$products): ?><p class="empty-state">暂无可查看的产品和服务。</p><?php endif ?>
<div class="catalog-grid">
<?php foreach ($products as $product): ?>
<?php
// 目录卡片指向卡片自己配置的网址;没有网址时,只有文章本身有内容才回退到文章页。
$card = $product['card'];
$hasDetail = $product['body'] !== '' || $product['summary'] !== '';
$target = ($card['url'] ?? '') ?: ($hasDetail ? '/articles/' . $product['slug'] : '');
?>
<article class="catalog-card">
<?php if (!empty($card['image'])): ?>
<div class="catalog-image"><img src="<?= $e($card['image']) ?>" alt="<?= $e($card['alt'] ?? '') ?>" loading="lazy"></div>
<?php endif ?>
<div class="catalog-content">
<?php if (!empty($card['category_label'])): ?><p class="overline"><?= $e($card['category_label']) ?></p><?php endif ?>
<h2><?php if ($target !== ''): ?><a href="<?= $e($target) ?>"><?= $e($product['title']) ?></a><?php else: ?><?= $e($product['title']) ?><?php endif ?></h2>
<?php if ($product['summary']): ?>
<p><?= $e($product['summary']) ?></p>
<?php else: ?>
<div class="rich"><?= Html::articleBody($product) ?></div>
<?php endif ?>
<?php if ($target !== '' || !empty($card['extra_url'])): ?>
<div class="actions">
<?php if ($target !== ''): ?><a class="button button-primary" href="<?= $e($target) ?>"><?= $e(($card['label'] ?? '') ?: '了解详情') ?></a><?php endif ?>
<?php if (!empty($card['extra_url'])): ?><a href="<?= $e($card['extra_url']) ?>"><?= $e(($card['extra_label'] ?? '') ?: '相关链接') ?></a><?php endif ?>
</div>
<?php endif ?>
</div>
</article>
<?php endforeach ?>
</div>
</div>
</section>
<?php
use App\Content\DirectoryCard;
use App\Content\Html;
use App\Content\View;
$e = View::escape(...);
?>
<section class="section catalog-hero">
<div class="wrap">
<nav class="breadcrumbs" aria-label="面包屑"><a href="/">首页</a><span aria-hidden="true">/</span><span aria-current="page">产品与服务</span></nav>
<h1>产品与服务</h1>
<p>自托管建站、开源智能 DNS 与软件著作权代理服务。</p>
</div>
</section>
<section class="section catalog-listing" aria-label="产品与服务目录">
<div class="wrap">
<?php if (!$products): ?><p class="empty-state">暂无可查看的产品和服务。</p><?php endif ?>
<div class="catalog-grid">
<?php foreach ($products as $product): ?>
<?php
$card = $product['card'];
$body = Html::articleBody($product);
['primary' => $target, 'detail' => $detail] = DirectoryCard::links($product, $body);
$extra = $card['extra_url'] ?? '';
?>
<article class="catalog-card">
<?php if (!empty($card['image'])): ?>
<div class="catalog-image"><img src="<?= $e($card['image']) ?>" alt="<?= $e($card['alt'] ?? '') ?>" loading="lazy"></div>
<?php endif ?>
<div class="catalog-content">
<?php if (!empty($card['category_label'])): ?><p class="overline"><?= $e($card['category_label']) ?></p><?php endif ?>
<h2><?php if ($target !== ''): ?><a href="<?= $e($target) ?>"><?= $e($product['title']) ?></a><?php else: ?><?= $e($product['title']) ?><?php endif ?></h2>
<?php if ($product['summary']): ?>
<p><?= $e($product['summary']) ?></p>
<?php else: ?>
<div class="rich"><?= $body ?></div>
<?php endif ?>
<?php if ($target !== '' || $extra !== ''): ?>
<div class="actions">
<?php if ($target !== ''): ?><a class="button button-primary" href="<?= $e($target) ?>"><?= $e(($card['label'] ?? '') ?: '了解详情') ?></a><?php endif ?>
<?php if ($extra !== '' && $extra !== $target): ?><a href="<?= $e($extra) ?>"><?= $e(($card['extra_label'] ?? '') ?: '相关链接') ?></a><?php endif ?>
<?php if ($detail !== '' && $detail !== $extra): ?><a class="catalog-detail" href="<?= $e($detail) ?>">查看详情</a><?php endif ?>
</div>
<?php endif ?>
</div>
</article>
<?php endforeach ?>
</div>
</div>
</section>
+3 -1
View File
@@ -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; }
-18
View File
@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use App\Content\Database;
// 输出目录类文章:slug<TAB>卡片网址<TAB>是否已发布
$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";
}
+56
View File
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Content\View;
$primary = 'https://example.com/product?a=1&b=2';
$cases = [
'configured-with-detail' => [['card' => ['url' => $primary], 'body' => '<p>有正文</p>'], $primary, true],
'configured-only' => [['card' => ['url' => '/pagepan']], '/pagepan', false],
'summary-only' => [['summary' => '仅摘要'], '/articles/summary-only', false],
'editor-placeholder' => [['body' => '<p><br></p><p>&nbsp;&#8203;</p>'], '', false],
'markdown-comment' => [['body' => '<!-- 草稿注释 -->', 'body_format' => 'markdown'], '', false],
'media-body' => [['body' => '<video controls><source src="/assets/demo.mp4"></video>'], '/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('<meta charset="utf-8">' . $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));
+3 -66
View File
@@ -1,67 +1,4 @@
#!/usr/bin/env bash
# 目录页(/services、/network)的卡片链接必须指向卡片配置的网址,而不是 /articles/<slug>
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'<article class="catalog-card">.*?</article>', page, re.S)
bad = 0
for card in cards:
links = re.findall(r'<a[^>]*href="([^"]+)"', card)
title = re.search(r'<h2><a href="([^"]+)"', card)
if not title:
print(' FAIL 卡片缺标题链接'); bad += 1; continue
primary = [l for l in links if not l.startswith('/articles/')]
if primary and title.group(1) != primary[0]:
print(' FAIL 标题 href=%s 与主按钮 href=%s 不一致' % (title.group(1), primary[0])); bad += 1
print(' ok %d 张卡片标题与主按钮一致' % (len(cards) - bad) if bad == 0 else ' 共 %d 张卡片,%d 张不一致' % (len(cards), bad))
sys.exit(0 if bad == 0 else 1)
PY
[ $? -eq 0 ] && ok "标题与按钮目标一致" || no "标题与主按钮目标不一致"
echo "== 参考信息:目录条目在文章流中的可见性(不判定) =="
PUB=$(echo "$ROWS" | awk -F'|' '$3 == "published" {print $1}')
N=0
for slug in $PUB; do
if curl -s "$BASE/articles" | grep -q "href=\"/articles/$slug\""; then N=$((N+1)); fi
done
echo " /articles 列表中出现的目录条目: $N / $(echo "$PUB" | wc -l)"
M=0
for slug in $PUB; do
if curl -s "$BASE/sitemap.xml" | grep -q "/articles/$slug"; then M=$((M+1)); fi
done
echo " sitemap 中的目录条目: $M / $(echo "$PUB" | wc -l)"
echo ""
echo "== 结果: $PASS 通过, $FAIL 失败 =="
[ $FAIL -eq 0 ] || exit 1
set -euo pipefail
cd "$(dirname "$0")/.."
exec "${PHP_BIN:-php}" tests/directory-links.php