Files
dusuniot/scripts/repair-content-terms.ts
T
bigcode 27fa9e605f v0.10.18: H18 最新博文(8bc6b68)完成 + 文章分类关系数据修复
- PostsWidget 忠实重写(skin cards):预取架构 loader 按 widget 收集术语→查文章→
  PostsDataContext 分发;卡 DOM/缩略图比率 0.66/NEWS 徽章(uppercase)/日期 F j,Y/
  摘要 18 词(空回退正文);右列纯文本卡(44px 两行钳制+箭头图标,custom_css 注入)
- posts cards 基础 CSS + slim 白名单 cards_*/_skin/posts_include* 全键
- scripts/repair-content-terms.ts:从 legacy dump wp_term_relationships 提取 67709 对,
  经 legacy_id 映射补插 1162 条 content_terms(Blog 302/News 22 篇,原仅 61 篇有关联)
- 补 icon-arrow-light-orange.png;双端节内几何/样式全等(sec 521),截图目视一致
2026-11-02 00:27:15 +08:00

56 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* # 脚本 修复 posts 的分类关联(content_terms)
* 背景:导入管线只落了部分文章的分类(609 篇中 548 篇无关联),首页 H18 posts widget
* (按 Blog/News 分类查最新文章)因此查不到数据。原站完整关系在 legacy 镜像
* dusuniot-db-20261029.sql 的 wp_term_relationships INSERT 段中((post_id,term_id,0) 行)。
* 做法:从 dump 提取全部 (wp_post_id, wp_term_id) 对 → 经 contents.legacy_id /
* categories.legacy_id 映射为本库 id → 补插缺失的 content_terms(幂等)。
* 运行:bun scripts/repair-content-terms.ts
*/
import { SQL } from "bun";
const DUMP = "/home/bigcode/data/dev/sites/dusuniot/legacy/dusuniot-db-20261029.sql";
const db = new SQL("postgres://dusuniot:dusuniot@127.0.0.1:25432/dusuniot");
// 1) 提取 dump 中的关系对
const text = await Bun.file(DUMP).text();
const pairs = new Set<string>();
const re = /\((\d+),(\d+),0\)/g;
for (const m of text.matchAll(re)) {
pairs.add(`${m[1]}:${m[2]}`);
}
console.log("dump 关系对:", pairs.size);
// 2) 本库映射表:legacyId → id
const contents = await db`SELECT id, legacy_id FROM contents WHERE legacy_id IS NOT NULL`;
const contentMap = new Map<number, number>(contents.map((r) => [Number(r.legacy_id), Number(r.id)]));
const cats = await db`SELECT id, legacy_id FROM categories WHERE legacy_id IS NOT NULL`;
const catMap = new Map<number, number>(cats.map((r) => [Number(r.legacy_id), Number(r.id)]));
// 3) 已有关联集合(幂等)
const existing = await db`SELECT content_id, category_id FROM content_terms`;
const have = new Set(existing.map((r) => `${r.content_id}:${r.category_id}`));
// 4) 补插
let inserted = 0;
for (const p of pairs) {
const [wpPost, wpTerm] = p.split(":").map(Number);
const cid = contentMap.get(wpPost);
const tid = catMap.get(wpTerm);
if (!cid || !tid) continue;
if (have.has(`${cid}:${tid}`)) continue;
await db`INSERT INTO content_terms (content_id, category_id) VALUES (${cid}, ${tid})`;
have.add(`${cid}:${tid}`);
inserted++;
}
console.log(`补插 ${inserted} 条 content_terms`);
// 5) 验证:Blog(53)/News(54) 分类下的文章数
const check = await db`
SELECT ca.name, COUNT(*) AS n FROM content_terms ct
JOIN categories ca ON ca.id = ct.category_id AND ca.legacy_id IN (53, 54)
JOIN contents c ON c.id = ct.content_id AND c.type = 'post' AND c.status = 'published'
GROUP BY ca.name`;
console.log("验证:", JSON.stringify(check));
await db.end();