{"version":3,"file":"execute.mjs","names":[],"sources":["../../../../../../src/astro/routes/api/import/wordpress/execute.ts"],"sourcesContent":["/**\n * WordPress WXR execute import endpoint\n *\n * POST /_emdash/api/import/wordpress/execute\n *\n * Accepts WXR file and import configuration, imports content into the database.\n */\n\nimport { gutenbergToPortableText } from \"@premium-cms/gutenberg-to-portable-text\";\nimport type { APIRoute } from \"astro\";\nimport {\n\tparseWxrString,\n\tContentRepository,\n\timportReusableBlocksAsSections,\n\ttype WxrPost,\n\tparseWxrDate,\n} from \"@premium-cms/emdash\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError } from \"#api/error.js\";\nimport { BylineRepository } from \"#db/repositories/byline.js\";\nimport { resolveImportByline } from \"#import/utils.js\";\nimport {\n\tattachPostTaxonomies,\n\tisWxrTaxonomyConflictError,\n\tmirrorTermsToLocales,\n\tpreImportWxrTaxonomies,\n\tsetPostTermAssignmentsReplacing,\n\ttype TaxonomyImportPlan,\n} from \"#import/wxr-taxonomies.js\";\nimport type { EmDashHandlers, EmDashManifest } from \"#types\";\nimport { slugify } from \"#utils/slugify.js\";\n\nimport { sanitizeSlug } from \"./analyze.js\";\n\nexport const prerender = false;\n\nexport interface ImportConfig {\n\t/** Map WordPress post types to EmDash collections */\n\tpostTypeMappings: Record<\n\t\tstring,\n\t\t{\n\t\t\tcollection: string;\n\t\t\tenabled: boolean;\n\t\t}\n\t>;\n\t/** Whether to skip items that already exist (by slug) */\n\tskipExisting: boolean;\n\t/** Whether to import reusable blocks (wp_block) as sections */\n\timportSections?: boolean;\n\t/** Author mappings (WP author login -> EmDash user ID) */\n\tauthorMappings?: Record<string, string | null>;\n\t/** BCP 47 locale for all imported items. When omitted, defaults to defaultLocale. */\n\tlocale?: string;\n}\n\nexport interface ImportResult {\n\tsuccess: boolean;\n\timported: number;\n\tskipped: number;\n\terrors: Array<{ title: string; error: string }>;\n\tbyCollection: Record<string, number>;\n\t/** Sections import results (if enabled) */\n\tsections?: {\n\t\tcreated: number;\n\t\tskipped: number;\n\t};\n\t/** Taxonomy import results (categories, tags, custom taxonomies). */\n\ttaxonomies?: {\n\t\t/** Terms newly created during this import, keyed by taxonomy name. */\n\t\ttermsCreated: Record<string, number>;\n\t\t/** Existing terms that were re-used, keyed by taxonomy name. */\n\t\ttermsReused: Record<string, number>;\n\t\t/** Total pivot rows (post <-> term) written to `content_taxonomies`. */\n\t\tassignments: number;\n\t\t/**\n\t\t * Custom taxonomy names from the WXR file that had no matching EmDash\n\t\t * definition and were therefore skipped. Lets the admin UI surface a\n\t\t * \"create taxonomy X first\" hint without re-running the import.\n\t\t */\n\t\tmissingTaxonomies: string[];\n\t};\n}\n\nexport const POST: APIRoute = async ({ request, locals }) => {\n\tconst { emdash, user } = locals;\n\n\tconst denied = requirePerm(user, \"import:execute\");\n\tif (denied) return denied;\n\n\tif (!emdash?.handleContentCreate) {\n\t\treturn apiError(\"NOT_CONFIGURED\", \"EmDash not configured\", 500);\n\t}\n\n\ttry {\n\t\tconst emdashManifest = await emdash.getManifest();\n\n\t\tconst formData = await request.formData();\n\t\tconst fileEntry = formData.get(\"file\");\n\t\tconst file = fileEntry instanceof File ? fileEntry : null;\n\t\tconst configEntry = formData.get(\"config\");\n\t\tconst configJson = typeof configEntry === \"string\" ? configEntry : null;\n\n\t\tif (!file) {\n\t\t\treturn apiError(\"VALIDATION_ERROR\", \"No file provided\", 400);\n\t\t}\n\n\t\tif (!configJson) {\n\t\t\treturn apiError(\"VALIDATION_ERROR\", \"No config provided\", 400);\n\t\t}\n\n\t\tconst config: ImportConfig = JSON.parse(configJson);\n\n\t\t// Parse WXR\n\t\tconst text = await file.text();\n\t\tconst wxr = await parseWxrString(text);\n\n\t\t// Build attachment ID -> URL map for featured images\n\t\tconst attachmentMap = new Map<string, string>();\n\t\tfor (const att of wxr.attachments) {\n\t\t\tif (att.id && att.url) {\n\t\t\t\tattachmentMap.set(String(att.id), att.url);\n\t\t\t}\n\t\t}\n\n\t\t// Build author login -> display name map\n\t\tconst authorDisplayNames = new Map<string, string>();\n\t\tfor (const author of wxr.authors) {\n\t\t\tif (!author.login) continue;\n\t\t\tauthorDisplayNames.set(author.login, author.displayName || author.login);\n\t\t}\n\n\t\t// Pre-create taxonomy terms (categories, tags, custom taxonomies) so\n\t\t// per-post assignments can resolve to existing rows. Done before any\n\t\t// content insert because WXR exports list terms at the top of the\n\t\t// file but per-item assignments only reference them by slug.\n\t\tconst taxonomyPlan = await preImportWxrTaxonomies(\n\t\t\temdash.db,\n\t\t\twxr.posts,\n\t\t\twxr.categories,\n\t\t\twxr.tags,\n\t\t\twxr.terms,\n\t\t\tconfig.locale,\n\t\t);\n\n\t\t// Multilingual imports (WPML / Polylang -- see #1080) need a term\n\t\t// row at each per-post locale, all sharing the canonical term's\n\t\t// `translation_group`. Without this, `getTermsForEntry(..., locale)`\n\t\t// on non-canonical translations comes back empty.\n\t\t//\n\t\t// The mirror raises `WxrTaxonomyConflictError` with an operator-\n\t\t// actionable message when an existing locale row has an\n\t\t// incompatible group. Surface its `publicMessage` directly so the\n\t\t// admin UI can tell the user which (taxonomy, slug, locale) needs\n\t\t// reconciliation. Other errors (DB connectivity, unexpected\n\t\t// repository failures) re-throw to the outer catch where\n\t\t// `handleError` masks them with the generic \"Failed to import\n\t\t// content\" -- exposing raw DB errors to clients would leak schema\n\t\t// names and bypass the AGENTS.md \"never expose error.message\" rule.\n\t\tconst postLocales = new Set<string>();\n\t\tfor (const post of wxr.posts) {\n\t\t\tif (post.locale) postLocales.add(post.locale);\n\t\t}\n\t\tif (postLocales.size > 0) {\n\t\t\ttry {\n\t\t\t\tawait mirrorTermsToLocales(emdash.db, taxonomyPlan, postLocales, config.locale);\n\t\t\t} catch (mirrorError) {\n\t\t\t\tif (isWxrTaxonomyConflictError(mirrorError)) {\n\t\t\t\t\tconsole.error(\"[WXR_IMPORT_TAXONOMY_CONFLICT]\", mirrorError);\n\t\t\t\t\treturn apiError(\"WXR_IMPORT_TAXONOMY_CONFLICT\", mirrorError.publicMessage, 409);\n\t\t\t\t}\n\t\t\t\tthrow mirrorError;\n\t\t\t}\n\t\t}\n\n\t\t// Import content (locale from config scopes all items)\n\t\tconst result = await importContent(\n\t\t\twxr.posts,\n\t\t\tconfig,\n\t\t\temdash,\n\t\t\temdashManifest,\n\t\t\tattachmentMap,\n\t\t\tconfig.locale,\n\t\t\tauthorDisplayNames,\n\t\t\ttaxonomyPlan,\n\t\t);\n\n\t\t// Import reusable blocks as sections (if enabled)\n\t\tif (config.importSections !== false) {\n\t\t\tconst sectionsResult = await importReusableBlocksAsSections(wxr.posts, emdash.db);\n\t\t\tresult.sections = {\n\t\t\t\tcreated: sectionsResult.sectionsCreated,\n\t\t\t\tskipped: sectionsResult.sectionsSkipped,\n\t\t\t};\n\t\t\t// Add section errors to main errors array\n\t\t\tresult.errors.push(...sectionsResult.errors);\n\t\t\tif (sectionsResult.errors.length > 0) {\n\t\t\t\tresult.success = false;\n\t\t\t}\n\t\t}\n\n\t\treturn apiSuccess(result);\n\t} catch (error) {\n\t\treturn handleError(error, \"Failed to import content\", \"WXR_IMPORT_ERROR\");\n\t}\n};\n\nexport async function importContent(\n\tposts: WxrPost[],\n\tconfig: ImportConfig,\n\temdash: EmDashHandlers,\n\tmanifest: EmDashManifest,\n\tattachmentMap: Map<string, string>,\n\tlocale: string | undefined,\n\tauthorDisplayNames: Map<string, string> | undefined,\n\ttaxonomyPlan: TaxonomyImportPlan,\n): Promise<ImportResult> {\n\tconst result: ImportResult = {\n\t\tsuccess: true,\n\t\timported: 0,\n\t\tskipped: 0,\n\t\terrors: [],\n\t\tbyCollection: {},\n\t\ttaxonomies: {\n\t\t\ttermsCreated: taxonomyPlan.termsCreated,\n\t\t\ttermsReused: taxonomyPlan.termsReused,\n\t\t\tassignments: 0,\n\t\t\tmissingTaxonomies: taxonomyPlan.missingTaxonomies,\n\t\t},\n\t};\n\n\t// Create content repository for checking existing items\n\tconst contentRepo = new ContentRepository(emdash.db);\n\tconst bylineRepo = new BylineRepository(emdash.db);\n\tconst bylineCache = new Map<string, string>();\n\n\t// Source-side translation group ID -> the EmDash ID of the first post we\n\t// imported for that group. Subsequent translations are linked via\n\t// `translationOf` so they share a `translation_group` on the EmDash side.\n\tconst translationGroupMap = new Map<string, string>();\n\n\tfor (const post of posts) {\n\t\tconst postType = post.postType || \"post\";\n\t\tconst mapping = config.postTypeMappings[postType];\n\n\t\t// Skip if not mapped or disabled\n\t\tif (!mapping || !mapping.enabled) {\n\t\t\tresult.skipped++;\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Defensive: mapping.collection is already sanitized by prepare, but the user\n\t\t// could manually edit the import config between prepare and execute.\n\t\tconst collection = sanitizeSlug(mapping.collection);\n\n\t\t// Check if collection exists in manifest\n\t\tif (!manifest?.collections[collection]) {\n\t\t\tresult.errors.push({\n\t\t\t\ttitle: post.title || \"Untitled\",\n\t\t\t\terror: `Collection \"${collection}\" does not exist`,\n\t\t\t});\n\t\t\tcontinue;\n\t\t}\n\n\t\ttry {\n\t\t\t// Convert content to Portable Text\n\t\t\tconst content = post.content ? gutenbergToPortableText(post.content) : [];\n\n\t\t\t// Generate slug from post name or title\n\t\t\tconst slug = post.postName || slugify(post.title || `post-${post.id || Date.now()}`);\n\n\t\t\t// Per-post locale: prefer the value extracted from WPML/Polylang\n\t\t\t// metadata; fall back to the upload-wide locale. Two translations\n\t\t\t// sharing `post_name` (e.g. /en/hello + /ar/hello) collide on the\n\t\t\t// `UNIQUE(slug, locale)` constraint when they share a locale, so\n\t\t\t// honouring the per-post value is what makes multilingual imports\n\t\t\t// land correctly. See issue #1080.\n\t\t\tconst postLocale = post.locale ?? locale;\n\n\t\t\t// Check if already exists (idempotency). Match against the\n\t\t\t// per-post locale so the same slug in different locales doesn't\n\t\t\t// false-positive as duplicate.\n\t\t\tif (config.skipExisting) {\n\t\t\t\tconst existing = await contentRepo.findBySlug(collection, slug, postLocale);\n\t\t\t\tif (existing) {\n\t\t\t\t\t// Record the translation group mapping so later\n\t\t\t\t\t// translations in this WXR can link to the existing\n\t\t\t\t\t// item. We deliberately trust the WXR's grouping over\n\t\t\t\t\t// the existing row's `translation_group`: a singleton\n\t\t\t\t\t// existing row gets folded into the WXR's group when\n\t\t\t\t\t// `handleContentCreate` resolves the new translation's\n\t\t\t\t\t// `translationOf`. Pre-existing translations that\n\t\t\t\t\t// already belong to a different group are left alone --\n\t\t\t\t\t// the user is responsible for reconciling those through\n\t\t\t\t\t// the admin if they don't match the WXR.\n\t\t\t\t\tif (post.translationGroup) {\n\t\t\t\t\t\ttranslationGroupMap.set(post.translationGroup, existing.id);\n\t\t\t\t\t}\n\t\t\t\t\tresult.skipped++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Resolve translation group: if this post belongs to a group and\n\t\t\t// we've already imported one of its translations, link to it.\n\t\t\tlet translationOf: string | undefined;\n\t\t\tif (post.translationGroup) {\n\t\t\t\ttranslationOf = translationGroupMap.get(post.translationGroup);\n\t\t\t}\n\n\t\t\t// Map WordPress status to EmDash status\n\t\t\tconst status = mapStatus(post.status);\n\n\t\t\t// Build data object with required fields\n\t\t\tconst data: Record<string, unknown> = {\n\t\t\t\ttitle: post.title || \"Untitled\",\n\t\t\t\tcontent,\n\t\t\t\texcerpt: post.excerpt || undefined,\n\t\t\t};\n\n\t\t\t// Only add featured_image if the collection has this field and we have a value\n\t\t\tconst collectionSchema = manifest.collections[collection];\n\t\t\tconst hasFeaturedImageField = collectionSchema?.fields\n\t\t\t\t? \"featured_image\" in collectionSchema.fields\n\t\t\t\t: false;\n\t\t\tif (hasFeaturedImageField) {\n\t\t\t\tconst thumbnailId = post.meta.get(\"_thumbnail_id\");\n\t\t\t\tconst featuredImage = thumbnailId ? attachmentMap.get(String(thumbnailId)) : undefined;\n\t\t\t\tif (featuredImage) {\n\t\t\t\t\tdata.featured_image = featuredImage;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Resolve author ID from mappings\n\t\t\tlet authorId: string | undefined;\n\t\t\tif (config.authorMappings && post.creator) {\n\t\t\t\tconst mappedUserId = config.authorMappings[post.creator];\n\t\t\t\tif (mappedUserId !== undefined && mappedUserId !== null) {\n\t\t\t\t\tauthorId = mappedUserId;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst bylineId = await resolveImportByline(\n\t\t\t\tpost.creator,\n\t\t\t\tauthorDisplayNames?.get(post.creator ?? \"\") ?? post.creator,\n\t\t\t\tauthorId,\n\t\t\t\tbylineRepo,\n\t\t\t\tbylineCache,\n\t\t\t);\n\n\t\t\t// Preserve original WordPress dates using the shared WXR date parser.\n\t\t\t// Fallback chain: postDateGmt (UTC) → pubDate (RFC 2822) → postDate (site-local).\n\t\t\tconst parsedDate = parseWxrDate(post.postDateGmt, post.pubDate, post.postDate);\n\t\t\tconst createdAt = parsedDate ? parsedDate.toISOString() : undefined;\n\t\t\tconst publishedAt = status === \"published\" && createdAt ? createdAt : undefined;\n\n\t\t\t// Create the content item\n\t\t\tconst createResult = await emdash.handleContentCreate(collection, {\n\t\t\t\tdata,\n\t\t\t\tslug,\n\t\t\t\tstatus,\n\t\t\t\tauthorId,\n\t\t\t\tbylines: bylineId ? [{ bylineId }] : undefined,\n\t\t\t\tlocale: postLocale,\n\t\t\t\ttranslationOf,\n\t\t\t\tcreatedAt,\n\t\t\t\tpublishedAt,\n\t\t\t});\n\n\t\t\tif (createResult.success) {\n\t\t\t\tresult.imported++;\n\t\t\t\tresult.byCollection[collection] = (result.byCollection[collection] || 0) + 1;\n\n\t\t\t\t// `handleContentCreate` returns `data: { item, _rev? }` on\n\t\t\t\t// success (see `ApiResult<ContentResponse>` in\n\t\t\t\t// `api/handlers/content.ts`). `HandlerResponse.data` is\n\t\t\t\t// typed as `unknown` to avoid coupling the route surface to\n\t\t\t\t// internal handler types, so we narrow here.\n\t\t\t\t// eslint-disable-next-line typescript/no-unsafe-type-assertion -- handler contract documented at handleContentCreate\n\t\t\t\tconst createdItem = (createResult.data as { item: { id: string } } | undefined)?.item;\n\n\t\t\t\t// Track translation group: the first imported post in a group\n\t\t\t\t// becomes the anchor that later translations link to.\n\t\t\t\tif (\n\t\t\t\t\tcreatedItem &&\n\t\t\t\t\tpost.translationGroup &&\n\t\t\t\t\t!translationGroupMap.has(post.translationGroup)\n\t\t\t\t) {\n\t\t\t\t\ttranslationGroupMap.set(post.translationGroup, createdItem.id);\n\t\t\t\t}\n\n\t\t\t\t// Attach taxonomy assignments parsed from the WXR's per-item\n\t\t\t\t// <category> elements.\n\t\t\t\t//\n\t\t\t\t// Anchors (no `translationOf`) get an additive attach -- the\n\t\t\t\t// row is fresh, no inherited pivots to consider.\n\t\t\t\t//\n\t\t\t\t// Translations replace only taxonomies they explicitly carry.\n\t\t\t\t// Since assignments belong to the content translation_group,\n\t\t\t\t// that replacement is reflected in every sibling locale.\n\t\t\t\tif (createdItem) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst written = translationOf\n\t\t\t\t\t\t\t? await setPostTermAssignmentsReplacing(\n\t\t\t\t\t\t\t\t\temdash.db,\n\t\t\t\t\t\t\t\t\tcollection,\n\t\t\t\t\t\t\t\t\tcreatedItem.id,\n\t\t\t\t\t\t\t\t\tpost,\n\t\t\t\t\t\t\t\t\ttaxonomyPlan,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: await attachPostTaxonomies(\n\t\t\t\t\t\t\t\t\temdash.db,\n\t\t\t\t\t\t\t\t\tcollection,\n\t\t\t\t\t\t\t\t\tcreatedItem.id,\n\t\t\t\t\t\t\t\t\tpost,\n\t\t\t\t\t\t\t\t\ttaxonomyPlan,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\tif (result.taxonomies) {\n\t\t\t\t\t\t\tresult.taxonomies.assignments += written;\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (taxError) {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`Failed to attach taxonomies for \"${post.title || \"Untitled\"}\":`,\n\t\t\t\t\t\t\ttaxError,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tresult.errors.push({\n\t\t\t\t\t\t\ttitle: post.title || \"Untitled\",\n\t\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\ttaxError instanceof Error && taxError.message\n\t\t\t\t\t\t\t\t\t? `Imported but failed to attach taxonomies: ${taxError.message}`\n\t\t\t\t\t\t\t\t\t: \"Imported but failed to attach taxonomies\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tresult.errors.push({\n\t\t\t\t\ttitle: post.title || \"Untitled\",\n\t\t\t\t\terror:\n\t\t\t\t\t\ttypeof createResult.error === \"object\" && createResult.error !== null\n\t\t\t\t\t\t\t? (createResult.error as { message?: string }).message || \"Unknown error\"\n\t\t\t\t\t\t\t: String(createResult.error),\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(`Import error for \"${post.title || \"Untitled\"}\":`, error);\n\t\t\tresult.errors.push({\n\t\t\t\ttitle: post.title || \"Untitled\",\n\t\t\t\terror: error instanceof Error && error.message ? error.message : \"Failed to import item\",\n\t\t\t});\n\t\t}\n\t}\n\n\tresult.success = result.errors.length === 0;\n\treturn result;\n}\n\nfunction mapStatus(wpStatus: string | undefined): string {\n\tswitch (wpStatus) {\n\t\tcase \"publish\":\n\t\t\treturn \"published\";\n\t\tcase \"draft\":\n\t\t\treturn \"draft\";\n\t\tcase \"pending\":\n\t\t\treturn \"draft\";\n\t\tcase \"private\":\n\t\t\treturn \"draft\";\n\t\tdefault:\n\t\t\treturn \"draft\";\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAa,YAAY;AAiDzB,MAAa,OAAiB,OAAO,EAAE,SAAS,aAAa;CAC5D,MAAM,EAAE,QAAQ,SAAS;CAEzB,MAAM,SAAS,YAAY,MAAM,iBAAiB;AAClD,KAAI,OAAQ,QAAO;AAEnB,KAAI,CAAC,QAAQ,oBACZ,QAAO,S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