@kidecms/wordpress 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/verify.ts ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * `kide-wp verify`: read every planned document back through the CMS API and
3
+ * reconcile it with the plan — present, every planned field still filled, media
4
+ * resolved, translations stored, and the source text actually in the database.
5
+ */
6
+ import { getTranslatableFieldNames } from "@kidecms/core";
7
+ import { createCmsContext } from "@kidecms/core/context";
8
+ import { isEmpty, outputStrings, textRecall } from "./domain";
9
+ import { MEDIA_PREFIX, MEDIA_TOKEN, normalizePath, tokenKey } from "./plan";
10
+ import { loadPlan } from "./report";
11
+ import { getMeta, loadRecords, type Staging } from "./staging";
12
+
13
+ export type VerifyReport = {
14
+ checked: number;
15
+ ok: number;
16
+ problems: Array<{ docId: string; collection: string; message: string }>;
17
+ byCollection: Record<string, { planned: number; found: number; translations: number }>;
18
+ };
19
+
20
+ export const verifyImport = async (staging: Staging, options: { minRecall?: number } = {}): Promise<VerifyReport> => {
21
+ const plan = loadPlan(staging);
22
+ if (!plan) throw new Error("No plan yet — run `kide-wp plan` first.");
23
+ const minRecall = options.minRecall ?? 0.95;
24
+ const { cms, config, dispose } = await createCmsContext();
25
+ const records = new Map(loadRecords(staging).map((r) => [r.key, r]));
26
+ const changedTo = new Map(plan.urlChanges.map((c) => [c.key, c.to]));
27
+ const defaultLocale = config.locales?.default;
28
+ const api = cms as unknown as Record<string, any>;
29
+ const report: VerifyReport = { checked: 0, ok: 0, problems: [], byCollection: {} };
30
+ const ctx = { _system: true };
31
+ const storedAt = new Map(
32
+ (
33
+ staging.prepare("SELECT key, storage_path FROM media_state").all() as Array<{ key: string; storage_path: string }>
34
+ ).map((r) => [r.key, r.storage_path]),
35
+ );
36
+ /** A planned value as the CMS stores it: trimmed text, media as its (content-deduped) file. */
37
+ const asStored = (value: unknown) =>
38
+ JSON.stringify(value ?? null, (_, v) => (typeof v === "string" ? v.trim() : v)).replace(
39
+ MEDIA_TOKEN,
40
+ (token, encoded: string) => storedAt.get(tokenKey(encoded)) ?? token,
41
+ );
42
+ const gone = new Set(JSON.parse(getMeta(staging, "missingMedia") ?? "[]") as string[]);
43
+ /** A planned value that only pointed at files missing at the source is legitimately empty. */
44
+ const onlyGoneMedia = (value: unknown) => {
45
+ const keys = [...JSON.stringify(value).matchAll(MEDIA_TOKEN)].map((m) => tokenKey(m[1]));
46
+ return keys.length > 0 && keys.every((k) => gone.has(k));
47
+ };
48
+
49
+ try {
50
+ for (const doc of plan.docs) {
51
+ report.checked++;
52
+ const stats = (report.byCollection[doc.collection] ??= { planned: 0, found: 0, translations: 0 });
53
+ stats.planned++;
54
+ const problem = (message: string) =>
55
+ report.problems.push({ docId: doc.docId, collection: doc.collection, message });
56
+ const before = report.problems.length;
57
+
58
+ // Check what editors see and, for published documents, what the site serves (a stale live snapshot shows up there).
59
+ const views: Array<[string, string]> = [["any", ""]];
60
+ if (doc.data._status === "published") views.push(["published", "live: "]);
61
+ let stored: Record<string, unknown> | null = null;
62
+ let baseRow: Record<string, unknown> | null = null;
63
+ for (const [status, label] of views) {
64
+ const row = await api[doc.collection].findById(doc.docId, { status }, ctx);
65
+ if (!row) {
66
+ problem(`${label}missing in the CMS`);
67
+ break;
68
+ }
69
+ baseRow ??= row;
70
+ const fieldsOnly = Object.fromEntries(Object.entries(row).filter(([k]) => !k.startsWith("_")));
71
+ stored ??= fieldsOnly;
72
+ for (const [field, value] of Object.entries(doc.data)) {
73
+ if (field.startsWith("_") || isEmpty(value)) continue;
74
+ if (isEmpty(fieldsOnly[field]) && !onlyGoneMedia(value))
75
+ problem(`${label}field "${field}" was planned but is empty in the CMS`);
76
+ }
77
+ if (JSON.stringify(fieldsOnly).includes(MEDIA_PREFIX)) problem(`${label}unresolved media placeholder stored`);
78
+ const base = textRecall(doc.expected.base ?? [], outputStrings(fieldsOnly), doc.textMatch);
79
+ if (base.recall < minRecall)
80
+ problem(
81
+ `${label}stored text recall ${(base.recall * 100).toFixed(1)}% (missing e.g. ${base.missing.join(", ")})`,
82
+ );
83
+ }
84
+ if (!stored) continue;
85
+ stats.found++;
86
+
87
+ const locales = Object.keys(doc.translations);
88
+ const translations: Record<string, Record<string, unknown>> = locales.length
89
+ ? await api[doc.collection].getTranslations(doc.docId, ctx)
90
+ : {};
91
+
92
+ // The stored document must be served at its old WordPress URL, or at the URL the plan announced.
93
+ if (api.meta.getCollection(doc.collection).fields.slug) {
94
+ for (const key of doc.sourceKeys) {
95
+ const record = records.get(key);
96
+ if (typeof record?.permalink !== "string") continue;
97
+ const locale = record.locale ?? defaultLocale;
98
+ const slug =
99
+ locale === baseRow?._sourceLocale ? baseRow?.slug : (translations[String(locale)]?.slug ?? baseRow?.slug);
100
+ const route = api.meta.getRouteForDocument(doc.collection, { slug }, locale);
101
+ const expected = changedTo.get(key) ?? normalizePath(record.permalink);
102
+ if (route !== expected) problem(`${locale} route ${route} — expected ${expected}`);
103
+ }
104
+ }
105
+
106
+ if (locales.length) {
107
+ for (const locale of locales) {
108
+ const overlay = translations[locale];
109
+ if (!overlay) {
110
+ problem(`translation ${locale} missing`);
111
+ continue;
112
+ }
113
+ stats.translations++;
114
+ // Read the locale the way the site serves it, so a translation that hides the original shows up.
115
+ const served = await api[doc.collection].findById(
116
+ doc.docId,
117
+ { locale, status: doc.data._status === "published" ? "published" : "any" },
118
+ ctx,
119
+ );
120
+ const planned: Record<string, unknown> = { ...doc.data, ...doc.translations[locale] };
121
+ const blocks = (v: unknown) => (JSON.stringify(v ?? null).match(/"type":"block"/g) ?? []).length;
122
+ for (const field of getTranslatableFieldNames(api.meta.getCollection(doc.collection))) {
123
+ if (isEmpty(planned[field]) || onlyGoneMedia(planned[field])) continue;
124
+ if (isEmpty(served?.[field]))
125
+ problem(`${locale} field "${field}" was planned but the site serves it empty`);
126
+ else if (blocks(served?.[field]) < blocks(planned[field]))
127
+ problem(
128
+ `${locale} field "${field}" serves ${blocks(served?.[field])} of ${blocks(planned[field])} blocks`,
129
+ );
130
+ else if (
131
+ field in (doc.translations[locale] ?? {}) &&
132
+ asStored(doc.translations[locale][field]) !== asStored(doc.data[field]) &&
133
+ JSON.stringify(served?.[field]) === JSON.stringify(stored?.[field])
134
+ )
135
+ problem(
136
+ `${locale} field "${field}" has its own translation in the plan but the site serves the original`,
137
+ );
138
+ }
139
+ const recall = textRecall(
140
+ doc.expected[locale] ?? [],
141
+ outputStrings(Object.fromEntries(Object.entries(served ?? {}).filter(([k]) => !k.startsWith("_")))),
142
+ doc.textMatch,
143
+ );
144
+ if (recall.recall < minRecall)
145
+ problem(
146
+ `${locale} text recall ${(recall.recall * 100).toFixed(1)}% (missing e.g. ${recall.missing.join(", ")})`,
147
+ );
148
+ }
149
+ }
150
+ if (report.problems.length === before) report.ok++;
151
+ }
152
+ } finally {
153
+ await dispose();
154
+ }
155
+ return report;
156
+ };
@@ -0,0 +1,472 @@
1
+ /**
2
+ * WordPress source adapter: streams a mysqldump into the staging DB and builds
3
+ * one clean record per post/term (ACF meta nested, Polylang/WPML locales and
4
+ * translation groups resolved, attachments split out as media).
5
+ */
6
+ import { readMysqlDump, type SqlValue } from "./mysql-dump";
7
+ import { nestAcf } from "./gutenberg";
8
+ import { maybeUnserialize } from "./php";
9
+ import { resetSource, setMeta, type Staging, type StagedRecord } from "./staging";
10
+
11
+ const KINDS = [
12
+ "posts",
13
+ "postmeta",
14
+ "terms",
15
+ "term_taxonomy",
16
+ "term_relationships",
17
+ "termmeta",
18
+ "options",
19
+ "icl_translations",
20
+ "yoast_indexable",
21
+ "gf_form",
22
+ "gf_form_meta",
23
+ "redirection_items",
24
+ ] as const;
25
+ type Kind = (typeof KINDS)[number];
26
+ const TABLE = new RegExp(`^(.*?)(${KINDS.join("|")})$`);
27
+
28
+ const INTERNAL_POST_TYPES = new Set([
29
+ "revision",
30
+ "customize_changeset",
31
+ "oembed_cache",
32
+ "user_request",
33
+ "custom_css",
34
+ "wp_global_styles",
35
+ "wp_template",
36
+ "wp_template_part",
37
+ "wp_font_family",
38
+ "wp_font_face",
39
+ "acf-field",
40
+ "acf-field-group",
41
+ "acf-taxonomy",
42
+ "acf-post-type",
43
+ "acf-ui-options-page",
44
+ "scheduled-action",
45
+ ]);
46
+ const SKIPPED_STATUSES = new Set(["auto-draft", "trash", "inherit"]);
47
+ const LOCALE_TAXONOMIES = new Set(["language", "post_translations", "term_language", "term_translations"]);
48
+ const INTERNAL_TAXONOMIES = new Set([
49
+ ...LOCALE_TAXONOMIES,
50
+ "post_format",
51
+ "wp_theme",
52
+ "wp_template_part_area",
53
+ "wp_pattern_category",
54
+ "link_category",
55
+ ]);
56
+ const INTERNAL_META =
57
+ /^(_edit_lock|_edit_last|_wp_old_slug|_wp_old_date|_encloseme|_pingme|_wp_trash_.*|_wp_desired_post_slug|_oembed_.*|_wp_attachment_metadata|_wp_attached_file|_wp_attachment_image_alt|_dp_original|_pll_.*|_wpml_.*|_icl_.*|_menu_item_(xfn|classes)|_relevanssi_noindex_reason|_yoast_wpseo_(content_score|linkdex|estimated-reading-time-minutes|seo_title_score|meta_description_score|inclusive_language_score|is_content_planner_.*|wordproof_timestamp|schema_page_type|schema_article_type))$/;
58
+
59
+ /** Gravity Forms choices; Chained Selects nest the next level's choices under each one. */
60
+ const choiceTree = (choices: unknown): Array<Record<string, unknown>> =>
61
+ Array.isArray(choices)
62
+ ? choices.map((c: any) => ({
63
+ text: c.text ?? "",
64
+ value: c.value ?? "",
65
+ ...(Array.isArray(c.choices) && c.choices.length ? { choices: choiceTree(c.choices) } : {}),
66
+ }))
67
+ : [];
68
+
69
+ const LIST_FIELDS = new Set(["repeater", "flexible_content", "gallery", "relationship", "checkbox"]);
70
+
71
+ export type ScoutOptions = { prefix?: string; uploadsDir?: string; mediaBaseUrl?: string; siteUrl?: string };
72
+
73
+ export type ScoutSummary = {
74
+ prefix: string;
75
+ siteUrl: string;
76
+ records: number;
77
+ media: number;
78
+ types: Record<string, number>;
79
+ };
80
+
81
+ const isoDate = (gmt: unknown, local: unknown): string | null => {
82
+ const pick = typeof gmt === "string" && !gmt.startsWith("0000") ? `${gmt.replace(" ", "T")}Z` : local;
83
+ if (typeof pick !== "string" || pick.startsWith("0000")) return null;
84
+ return pick.includes("T") ? pick : pick.replace(" ", "T");
85
+ };
86
+
87
+ // Per prefix: plugin tables like `wp_pmxe_posts` also end in a WP table name but have their own columns.
88
+ const rawTable = (prefix: string, kind: string) => `raw_${prefix.replace(/[^a-zA-Z0-9]/g, "_")}_${kind}`;
89
+
90
+ // WordPress stores non-ASCII slugs percent-encoded, sometimes in decomposed Unicode.
91
+ const decodeSlug = (raw: unknown) => {
92
+ const s = String(raw ?? "");
93
+ try {
94
+ return decodeURIComponent(s).normalize("NFC");
95
+ } catch {
96
+ return s;
97
+ }
98
+ };
99
+
100
+ const nonZero = (v: unknown) => (v === 0 || v === "0" || v === "" || v == null ? null : Number(v));
101
+
102
+ export const scoutWordPress = async (
103
+ db: Staging,
104
+ dumpFile: string,
105
+ options: ScoutOptions = {},
106
+ ): Promise<ScoutSummary> => {
107
+ resetSource(db);
108
+ const created = new Set<string>();
109
+ const postCounts = new Map<string, number>();
110
+
111
+ for await (const batch of readMysqlDump(dumpFile, (t) => TABLE.test(t))) {
112
+ const [, prefix, kind] = TABLE.exec(batch.table)!;
113
+ if (options.prefix && prefix !== options.prefix) continue;
114
+ if (!batch.columns.length || !batch.rows.length) continue;
115
+ const table = rawTable(prefix, kind);
116
+ if (!created.has(table)) {
117
+ db.exec(`CREATE TABLE IF NOT EXISTS ${table} (prefix TEXT, ${batch.columns.map((c) => `"${c}"`).join(", ")})`);
118
+ created.add(table);
119
+ }
120
+ const insert = db.prepare(
121
+ `INSERT INTO ${table} (prefix, ${batch.columns.map((c) => `"${c}"`).join(", ")}) VALUES (?, ${batch.columns.map(() => "?").join(", ")})`,
122
+ );
123
+ db.transaction((rows: SqlValue[][]) => {
124
+ for (const row of rows) insert.run(prefix, ...row);
125
+ })(batch.rows);
126
+ if (kind === "posts") postCounts.set(prefix, (postCounts.get(prefix) ?? 0) + batch.rows.length);
127
+ }
128
+
129
+ const prefix = options.prefix ?? [...postCounts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
130
+ if (prefix === undefined) throw new Error(`No WordPress posts table found in ${dumpFile}.`);
131
+ const has = (kind: Kind) => created.has(rawTable(prefix, kind));
132
+ const all = <T = Record<string, any>>(kind: Kind, where = "", ...params: unknown[]): T[] =>
133
+ has(kind)
134
+ ? (db.prepare(`SELECT * FROM ${rawTable(prefix, kind)} WHERE prefix = ? ${where}`).all(prefix, ...params) as T[])
135
+ : [];
136
+
137
+ const options_ = Object.fromEntries(
138
+ all(
139
+ "options",
140
+ "AND option_name IN ('siteurl','home','upload_path','upload_url_path','page_on_front','show_on_front','polylang')",
141
+ ).map((o) => [o.option_name, String(o.option_value ?? "")]),
142
+ );
143
+ // Bedrock installs WordPress under /wp, serves uploads from /app/uploads and sets `home` from the env.
144
+ const bedrock = /\/wp\/?$/.test(options_.siteurl ?? "");
145
+ const home = options_.home && options_.home !== options_.siteurl ? options_.home : undefined;
146
+ const siteUrl = (
147
+ options.siteUrl ||
148
+ home ||
149
+ (bedrock ? options_.siteurl.replace(/\/wp\/?$/, "") : options_.home || options_.siteurl) ||
150
+ ""
151
+ ).replace(/\/$/, "");
152
+ const uploadPath =
153
+ options_.upload_path && !options_.upload_path.startsWith("/")
154
+ ? options_.upload_path
155
+ : bedrock
156
+ ? "app/uploads"
157
+ : "wp-content/uploads";
158
+ const uploadsUrl = (options.mediaBaseUrl || options_.upload_url_path || `${siteUrl}/${uploadPath}`).replace(
159
+ /\/$/,
160
+ "",
161
+ );
162
+
163
+ // --- terms ---------------------------------------------------------------
164
+ const terms = new Map(all("terms").map((t) => [Number(t.term_id), t]));
165
+ const taxonomies = all("term_taxonomy");
166
+ const ttById = new Map(taxonomies.map((tt) => [Number(tt.term_taxonomy_id), tt]));
167
+ const termMeta = new Map<number, Record<string, unknown>>();
168
+ for (const m of all("termmeta")) {
169
+ const bucket = termMeta.get(Number(m.term_id)) ?? {};
170
+ bucket[m.meta_key] = maybeUnserialize(m.meta_value);
171
+ termMeta.set(Number(m.term_id), bucket);
172
+ }
173
+
174
+ const postLocale = new Map<number, string>();
175
+ const postGroup = new Map<number, string>();
176
+ const termLocale = new Map<number, string>();
177
+ const termGroup = new Map<number, string>();
178
+ const postTerms = new Map<number, Record<string, Array<{ id: number; slug: string; name: string }>>>();
179
+
180
+ for (const rel of all("term_relationships")) {
181
+ const tt = ttById.get(Number(rel.term_taxonomy_id));
182
+ if (!tt) continue;
183
+ const term = terms.get(Number(tt.term_id));
184
+ const objectId = Number(rel.object_id);
185
+ if (tt.taxonomy === "language" && term) postLocale.set(objectId, String(term.slug));
186
+ else if (tt.taxonomy === "post_translations") postGroup.set(objectId, `pll:${tt.term_taxonomy_id}`);
187
+ else if (tt.taxonomy === "term_language" && term) termLocale.set(objectId, String(term.slug).replace(/^pll_/, ""));
188
+ else if (tt.taxonomy === "term_translations") termGroup.set(objectId, `pll-term:${tt.term_taxonomy_id}`);
189
+ else if (!INTERNAL_TAXONOMIES.has(tt.taxonomy) && term) {
190
+ const bucket = postTerms.get(objectId) ?? {};
191
+ (bucket[tt.taxonomy] ??= []).push({ id: Number(term.term_id), slug: String(term.slug), name: String(term.name) });
192
+ postTerms.set(objectId, bucket);
193
+ }
194
+ }
195
+ for (const row of all("icl_translations")) {
196
+ const type = String(row.element_type ?? "");
197
+ const id = Number(row.element_id);
198
+ if (type.startsWith("post_")) {
199
+ postLocale.set(id, String(row.language_code));
200
+ postGroup.set(id, `wpml:${row.trid}`);
201
+ } else if (type.startsWith("tax_")) {
202
+ const tt = ttById.get(id);
203
+ if (tt) {
204
+ termLocale.set(Number(tt.term_id), String(row.language_code));
205
+ termGroup.set(Number(tt.term_id), `wpml:${row.trid}`);
206
+ }
207
+ }
208
+ }
209
+
210
+ // --- permalinks (Yoast indexables are the URLs WordPress actually serves) --------
211
+ const toPath = (url: unknown) => {
212
+ if (typeof url !== "string" || !url) return null;
213
+ try {
214
+ return decodeURI(new URL(url, "https://x.invalid").pathname);
215
+ } catch {
216
+ return null;
217
+ }
218
+ };
219
+ const postPermalink = new Map<number, string>();
220
+ const termPermalink = new Map<number, string>();
221
+ for (const row of all("yoast_indexable")) {
222
+ const p = toPath(row.permalink);
223
+ if (!p || row.object_id == null) continue;
224
+ if (row.object_type === "post") postPermalink.set(Number(row.object_id), p);
225
+ else if (row.object_type === "term") termPermalink.set(Number(row.object_id), p);
226
+ }
227
+
228
+ // --- Polylang menu locations: one menu per location, translated per language -----
229
+ const polylang = maybeUnserialize(options_.polylang) as Record<string, any> | string | undefined;
230
+ const menuLocation = new Map<number, { location: string; locale?: string; group?: string }>();
231
+ const navMenus = typeof polylang === "object" && polylang ? polylang.nav_menus : undefined;
232
+ for (const locations of Object.values((navMenus ?? {}) as Record<string, Record<string, Record<string, number>>>)) {
233
+ for (const [location, byLang] of Object.entries(locations ?? {})) {
234
+ const ids = new Set(Object.values(byLang ?? {}).map(Number));
235
+ for (const [lang, menuId] of Object.entries(byLang ?? {})) {
236
+ if (menuLocation.has(Number(menuId))) continue;
237
+ menuLocation.set(
238
+ Number(menuId),
239
+ ids.size > 1 ? { location, locale: lang, group: `menu:${location}` } : { location },
240
+ );
241
+ }
242
+ }
243
+ }
244
+
245
+ // --- posts + meta ----------------------------------------------------------
246
+ const posts = all("posts");
247
+ // ACF stores an empty repeater/gallery as "": its field definition says the value is a list.
248
+ const listFields = new Set(
249
+ posts
250
+ .filter((p) => p.post_type === "acf-field")
251
+ .filter((p) => LIST_FIELDS.has(String((maybeUnserialize(p.post_content) as { type?: unknown })?.type)))
252
+ .map((p) => String(p.post_name)),
253
+ );
254
+ const metaByPost = new Map<number, Record<string, unknown>>();
255
+ for (const m of all("postmeta")) {
256
+ const bucket = metaByPost.get(Number(m.post_id)) ?? {};
257
+ bucket[m.meta_key] = maybeUnserialize(m.meta_value);
258
+ metaByPost.set(Number(m.post_id), bucket);
259
+ }
260
+
261
+ const insertRecord = db.prepare("INSERT OR REPLACE INTO records (key, type, data) VALUES (?, ?, ?)");
262
+ const insertMedia = db.prepare(
263
+ "INSERT OR REPLACE INTO media (key, attachment_id, file, url, mime, alt) VALUES (?, ?, ?, ?, ?, ?)",
264
+ );
265
+ const types: Record<string, number> = {};
266
+ let mediaCount = 0;
267
+
268
+ db.transaction(() => {
269
+ for (const p of posts) {
270
+ const id = Number(p.ID);
271
+ const meta = metaByPost.get(id) ?? {};
272
+ if (p.post_type === "attachment") {
273
+ const file = typeof meta._wp_attached_file === "string" ? meta._wp_attached_file : null;
274
+ const url = file ? `${uploadsUrl}/${file}` : String(p.guid ?? "");
275
+ insertMedia.run(
276
+ `att:${id}`,
277
+ id,
278
+ file,
279
+ url,
280
+ p.post_mime_type || null,
281
+ (meta._wp_attachment_image_alt as string) || null,
282
+ );
283
+ mediaCount++;
284
+ continue;
285
+ }
286
+ if (INTERNAL_POST_TYPES.has(p.post_type) || SKIPPED_STATUSES.has(p.post_status)) continue;
287
+
288
+ const cleaned: Record<string, unknown> = {};
289
+ for (const [k, v] of Object.entries(meta))
290
+ // A Yoast `%%title%% %%sep%% %%sitename%%` value is the site's default pattern, not content.
291
+ if (!INTERNAL_META.test(k) && !(k.startsWith("_yoast_wpseo_") && String(v).includes("%%"))) cleaned[k] = v;
292
+ const { _thumbnail_id, _wp_page_template, ...rest } = cleaned;
293
+ const record: StagedRecord = {
294
+ key: `post:${id}`,
295
+ type: String(p.post_type),
296
+ id,
297
+ status: String(p.post_status),
298
+ locale: postLocale.get(id),
299
+ group: postGroup.get(id),
300
+ title: p.post_title ?? "",
301
+ slug: decodeSlug(p.post_name),
302
+ content: p.post_content ?? "",
303
+ excerpt: p.post_excerpt ?? "",
304
+ date: isoDate(p.post_date_gmt, p.post_date),
305
+ modified: isoDate(p.post_modified_gmt, p.post_modified),
306
+ parent: nonZero(p.post_parent),
307
+ menuOrder: nonZero(p.menu_order),
308
+ author: nonZero(p.post_author),
309
+ template: _wp_page_template && _wp_page_template !== "default" ? _wp_page_template : null,
310
+ featuredImage: nonZero(_thumbnail_id),
311
+ terms: postTerms.get(id) ?? {},
312
+ meta: nestAcf(rest, listFields),
313
+ permalink: postPermalink.get(id) ?? null,
314
+ };
315
+ // A menu item's content lives in its `_menu_item_*` meta; the post columns are bookkeeping.
316
+ if (record.type === "nav_menu_item")
317
+ for (const k of ["slug", "content", "excerpt", "date", "parent", "author"]) delete record[k];
318
+ insertRecord.run(record.key, record.type, JSON.stringify(record));
319
+ types[record.type] = (types[record.type] ?? 0) + 1;
320
+ }
321
+
322
+ for (const tt of taxonomies) {
323
+ if (INTERNAL_TAXONOMIES.has(tt.taxonomy)) continue;
324
+ const term = terms.get(Number(tt.term_id));
325
+ if (!term) continue;
326
+ const id = Number(term.term_id);
327
+ const menu = tt.taxonomy === "nav_menu" ? menuLocation.get(id) : undefined;
328
+ const record: StagedRecord = {
329
+ key: `term:${id}`,
330
+ type: `term:${tt.taxonomy}`,
331
+ id,
332
+ status: "publish",
333
+ locale: menu ? menu.locale : termLocale.get(id),
334
+ group: menu ? menu.group : termGroup.get(id),
335
+ ...(tt.taxonomy === "nav_menu" ? { location: menu?.location ?? null } : {}),
336
+ permalink: termPermalink.get(id) ?? null,
337
+ name: term.name ?? "",
338
+ slug: term.slug ?? "",
339
+ description: tt.description ?? "",
340
+ parent: nonZero(tt.parent),
341
+ meta: nestAcf(termMeta.get(id) ?? {}, listFields),
342
+ };
343
+ insertRecord.run(record.key, record.type, JSON.stringify(record));
344
+ types[record.type] = (types[record.type] ?? 0) + 1;
345
+ }
346
+
347
+ // One record per taxonomy and locale, so a whole term tree maps to one Kide `taxonomies` document.
348
+ const taxonomyLocales = new Map<string, Set<string | undefined>>();
349
+ for (const tt of taxonomies) {
350
+ if (INTERNAL_TAXONOMIES.has(tt.taxonomy) || tt.taxonomy === "nav_menu") continue;
351
+ const set = taxonomyLocales.get(tt.taxonomy) ?? new Set();
352
+ set.add(termLocale.get(Number(tt.term_id)));
353
+ taxonomyLocales.set(tt.taxonomy, set);
354
+ }
355
+ let syntheticId = 0;
356
+ for (const [taxonomy, locales] of taxonomyLocales) {
357
+ for (const locale of locales) {
358
+ const record: StagedRecord = {
359
+ key: `taxonomy:${taxonomy}${locale ? `:${locale}` : ""}`,
360
+ type: "taxonomy",
361
+ id: ++syntheticId,
362
+ status: "publish",
363
+ locale,
364
+ group: locale ? `taxonomy:${taxonomy}` : undefined,
365
+ taxonomy,
366
+ };
367
+ insertRecord.run(record.key, record.type, JSON.stringify(record));
368
+ types.taxonomy = (types.taxonomy ?? 0) + 1;
369
+ }
370
+ }
371
+
372
+ // Gravity Forms definitions, curated to what a form needs (entries are personal data and never staged).
373
+ const formMeta = new Map(all("gf_form_meta").map((m) => [Number(m.form_id), m]));
374
+ for (const form of all("gf_form")) {
375
+ if (Number(form.is_trash)) continue;
376
+ const meta = formMeta.get(Number(form.id));
377
+ const parse = (raw: unknown) => {
378
+ try {
379
+ return typeof raw === "string" ? JSON.parse(raw) : {};
380
+ } catch {
381
+ return {};
382
+ }
383
+ };
384
+ const display = parse(meta?.display_meta);
385
+ const confirmations = Object.values(parse(meta?.confirmations) as Record<string, any>);
386
+ const confirmation = confirmations.find((c) => c?.isDefault) ?? confirmations[0];
387
+ const notification = Object.values(parse(meta?.notifications) as Record<string, any>).find(
388
+ (n) => n?.isActive !== false && n?.toType === "email" && typeof n?.to === "string",
389
+ );
390
+ const record: StagedRecord = {
391
+ key: `form:${form.id}`,
392
+ type: "form",
393
+ id: Number(form.id),
394
+ status: Number(form.is_active) ? "publish" : "draft",
395
+ title: String(form.title ?? ""),
396
+ description: display.description ?? "",
397
+ button: display.button?.text ?? "",
398
+ fields: (Array.isArray(display.fields) ? display.fields : []).map((f: any) => ({
399
+ id: f.id,
400
+ type: f.type,
401
+ label: f.label ?? "",
402
+ adminLabel: f.adminLabel ?? "",
403
+ isRequired: !!f.isRequired,
404
+ placeholder: f.placeholder ?? "",
405
+ description: f.description ?? "",
406
+ content: f.content ?? "",
407
+ maxLength: f.maxLength ?? "",
408
+ checkboxLabel: f.checkboxLabel ?? "",
409
+ choices: choiceTree(f.choices),
410
+ inputs: Array.isArray(f.inputs)
411
+ ? f.inputs.filter((i: any) => !i.isHidden).map((i: any) => ({ id: i.id, label: i.label ?? "" }))
412
+ : [],
413
+ })),
414
+ confirmation: confirmation
415
+ ? {
416
+ type: confirmation.type ?? "message",
417
+ message: confirmation.message ?? "",
418
+ url: confirmation.url ?? "",
419
+ pageId: nonZero(confirmation.pageId),
420
+ }
421
+ : {},
422
+ notifyEmail: notification?.to ?? "",
423
+ };
424
+ insertRecord.run(record.key, record.type, JSON.stringify(record));
425
+ types.form = (types.form ?? 0) + 1;
426
+ }
427
+
428
+ // Redirection plugin rules.
429
+ for (const r of all("redirection_items")) {
430
+ if (r.action_type !== "url") continue;
431
+ const record: StagedRecord = {
432
+ key: `redirect:${r.id}`,
433
+ type: "redirect",
434
+ id: Number(r.id),
435
+ status: r.status === "enabled" ? "publish" : "draft",
436
+ from: String(r.url ?? ""),
437
+ to: String(r.action_data ?? ""),
438
+ code: Number(r.action_code) || 301,
439
+ regex: !!Number(r.regex),
440
+ hits: Number(r.last_count) || null,
441
+ };
442
+ insertRecord.run(record.key, record.type, JSON.stringify(record));
443
+ types.redirect = (types.redirect ?? 0) + 1;
444
+ }
445
+ })();
446
+
447
+ // The front page (and its translations) is the Kide `home` route.
448
+ const frontId = options_.show_on_front === "page" ? Number(options_.page_on_front) : 0;
449
+ if (frontId) {
450
+ const frontGroup = postGroup.get(frontId);
451
+ const members = [...postGroup.entries()].filter(([, g]) => frontGroup && g === frontGroup).map(([id]) => id);
452
+ for (const id of new Set([frontId, ...members])) {
453
+ const row = db.prepare("SELECT data FROM records WHERE key = ?").get(`post:${id}`) as
454
+ { data: string } | undefined;
455
+ if (!row) continue;
456
+ const data = JSON.parse(row.data);
457
+ data.frontPage = true;
458
+ db.prepare("UPDATE records SET data = ? WHERE key = ?").run(JSON.stringify(data), `post:${id}`);
459
+ }
460
+ }
461
+
462
+ for (const table of created) db.exec(`DROP TABLE ${table}`);
463
+ setMeta(db, "source", "wordpress");
464
+ setMeta(db, "dump", dumpFile);
465
+ setMeta(db, "prefix", prefix);
466
+ setMeta(db, "siteUrl", siteUrl);
467
+ setMeta(db, "uploadsUrl", uploadsUrl);
468
+ if (options.uploadsDir) setMeta(db, "uploadsDir", options.uploadsDir);
469
+ setMeta(db, "scoutedAt", new Date().toISOString());
470
+
471
+ return { prefix, siteUrl, records: Object.values(types).reduce((a, b) => a + b, 0), media: mediaCount, types };
472
+ };