diff --git a/assets/js/components/deposit/fileFormats/authorXml/index.js b/assets/js/components/deposit/fileFormats/authorXml/index.js new file mode 100644 index 00000000..22d3f8ed --- /dev/null +++ b/assets/js/components/deposit/fileFormats/authorXml/index.js @@ -0,0 +1,48 @@ +// This file is part of CDS-RDM +// Copyright (C) 2026 CERN. +// +// CDS-RDM is free software; you can redistribute it and/or modify it +// under the terms of the GPL-2.0 License; see LICENSE file for more details. + +import { parseAuthorXml } from "./parse"; + +const AUTHOR_XML_IMPORT_EXAMPLE = ` + + + + CERN + https://ror.org/01ggx4157 + + + + + Jane Smith + Jane + Smith + J. Smith + + + + + + + 0000-0002-1825-0097 + + + +`; + +/** + * The author.xml list format + * offered by the "add authors/contributors from file" dialog. + */ +export const authorXmlFileFormat = { + id: "author_xml", + label: "XML (author.xml)", + extension: ".xml", + example: AUTHOR_XML_IMPORT_EXAMPLE, + note: "Author identifiers are read from the ORCID, GND, ISNI and ROR sources. Roles are read from the author's position, if present.", + parse: parseAuthorXml, +}; diff --git a/assets/js/components/deposit/fileFormats/authorXml/parse.js b/assets/js/components/deposit/fileFormats/authorXml/parse.js new file mode 100644 index 00000000..00248925 --- /dev/null +++ b/assets/js/components/deposit/fileFormats/authorXml/parse.js @@ -0,0 +1,282 @@ +/* + * SPDX-FileCopyrightText: 2026 CERN. + * SPDX-License-Identifier: MIT + */ + +import { CREATIBUTOR_TYPE } from "@js/invenio_rdm_records"; + +export const AUTHOR_XML_IDENTIFIER_SCHEMES = Object.freeze([ + "orcid", + "gnd", + "isni", + "ror", +]); + +const AUTHOR_LIST_ROOT_ELEMENT = "collaborationauthorlist"; + +const ELEMENT_NODE = 1; + +const localName = (element) => + (element.localName || element.nodeName || "").toLowerCase(); + +const textOf = (element) => (element?.textContent ?? "").trim(); + +const childElements = (node) => + Array.from(node?.childNodes ?? []).filter((child) => child.nodeType === ELEMENT_NODE); + +const childrenNamed = (node, ...names) => { + const wanted = names.map((name) => name.toLowerCase()); + return childElements(node).filter((child) => wanted.includes(localName(child))); +}; + +const firstChildNamed = (node, ...names) => childrenNamed(node, ...names)[0] ?? null; + +const entriesOf = (node, containerName, entryName) => [ + ...childrenNamed(node, entryName), + ...childrenNamed(firstChildNamed(node, containerName), entryName), +]; + +const indexElements = (node, index = new Map()) => { + childElements(node).forEach((child) => { + const name = localName(child); + const elements = index.get(name); + if (elements) { + elements.push(child); + } else { + index.set(name, [child]); + } + indexElements(child, index); + }); + return index; +}; + +const attributeOf = (element, name) => { + if (!element) return null; + const value = element.getAttribute(name); + if (value !== null) return value; + const wanted = name.toLowerCase(); + const match = Array.from(element.attributes ?? []).find( + (attribute) => + attribute.name.toLowerCase() === wanted || + attribute.localName?.toLowerCase() === wanted + ); + return match?.value ?? null; +}; + +const normalized = (value) => (value ?? "").trim(); + +const toSourceKey = (value) => normalized(value).toUpperCase(); + +const pick = (...values) => values.find((value) => value) ?? undefined; + +// Resolve identifiers URLs if provided instead of bare ids. +const IDENTIFIER_URL_PREFIX = + /^https?:\/\/(www\.)?(orcid\.org|isni\.org(\/isni)?|d-nb\.info\/gnd|ror\.org)\//i; + +const normalizeIdentifier = (value) => + normalized(value) + .replace(IDENTIFIER_URL_PREFIX, "") + .replace(/^ror:/i, "") + .replace(/\/+$/, ""); + +const normalizeRor = (value) => normalizeIdentifier(value) || null; + +/** + * Split a free-text name. From e.g. `` or `` + * into a given and family name. + */ +const splitName = (value) => { + const name = normalized(value); + if (!name) return {}; + const commaIndex = name.indexOf(","); + if (commaIndex !== -1) { + return { + family_name: name.slice(0, commaIndex).trim() || undefined, + given_name: name.slice(commaIndex + 1).trim() || undefined, + }; + } + const parts = name.split(/\s+/); + if (parts.length === 1) return { family_name: parts[0] }; + return { + given_name: parts.slice(0, -1).join(" "), + family_name: parts[parts.length - 1], + }; +}; + +/** + * Map the author's name, which the format distributes over up to five elements: + * ``/`` (structured, `givenName` optional), + * ``/`` + * , `` (free text, full names) and `` (free text, initials). + */ +const parseName = (personElement) => { + const givenName = textOf(firstChildNamed(personElement, "givenname")); + const familyName = textOf(firstChildNamed(personElement, "familyname")); + if (givenName && familyName) { + return { given_name: givenName, family_name: familyName }; + } + + const paperGiven = textOf(firstChildNamed(personElement, "authornamepapergiven")); + const paperFamily = textOf(firstChildNamed(personElement, "authornamepaperfamily")); + const freeText = splitName(textOf(firstChildNamed(personElement, "name"))); + const paperText = splitName( + textOf(firstChildNamed(personElement, "authornamepaper")) + ); + + return { + given_name: pick(givenName, freeText.given_name, paperGiven, paperText.given_name), + family_name: pick( + familyName, + paperFamily, + freeText.family_name, + paperText.family_name + ), + }; +}; + +const parseIdentifiers = (personElement) => { + const identifiers = []; + const seen = new Set(); + entriesOf(personElement, "authorids", "authorid").forEach((authorIdElement) => { + const scheme = normalized(attributeOf(authorIdElement, "source")).toLowerCase(); + if (!AUTHOR_XML_IDENTIFIER_SCHEMES.includes(scheme)) return; + const identifier = normalizeIdentifier(textOf(authorIdElement)); + if (!identifier) return; + const key = `${scheme}:${identifier.toLowerCase()}`; + if (seen.has(key)) return; + seen.add(key); + identifiers.push({ scheme, identifier }); + }); + return identifiers; +}; + +/** + * `` values mapped to contributor role ids. + * any other value is imported as "other". + */ +const AUTHOR_POSITION_ROLES = Object.freeze({ + "contact person": "contactperson", + "editor": "editor", +}); + +const OTHER_ROLE = "other"; + +const parseRole = (personElement) => { + for (const collaborationElement of childrenNamed( + personElement, + "authorcollaboration" + )) { + const position = normalized(attributeOf(collaborationElement, "position")) + .toLowerCase() + .replace(/\s+/g, " "); + if (position) return AUTHOR_POSITION_ROLES[position] ?? OTHER_ROLE; + } + return undefined; +}; + +const organizationName = (organizationElement) => + textOf(firstChildNamed(organizationElement, "name")) || undefined; + +const organizationRor = (organizationElement) => { + for (const orgName of childrenNamed(organizationElement, "orgname")) { + const source = toSourceKey(attributeOf(orgName, "source")); + const value = textOf(orgName); + if (source === "ROR" || (!source && /^ror:/i.test(value))) { + const ror = normalizeRor(value); + if (ror) return ror; + } + } + return null; +}; + +/** + * Map `` by ``, which is what + * `` refers to. + */ +const parseOrganizations = (organizationElements) => { + const organizations = new Map(); + organizationElements.forEach((organizationElement) => { + const id = normalized(attributeOf(organizationElement, "id")); + if (!id) return; + organizations.set(id, { + name: organizationName(organizationElement), + ror: organizationRor(organizationElement), + }); + }); + return organizations; +}; + +const parseAffiliations = (personElement, organizations) => { + const affiliations = []; + const seen = new Set(); + + entriesOf(personElement, "authoraffiliations", "authoraffiliation").forEach( + (affiliationElement) => { + const organization = organizations.get( + normalized(attributeOf(affiliationElement, "organizationid")) + ); + if (!organization) return; + const key = (organization.ror || organization.name || "").toLowerCase(); + if (!key || seen.has(key)) return; + seen.add(key); + const affiliation = {}; + if (organization.ror) affiliation.id = organization.ror; + if (organization.name) affiliation.name = organization.name; + affiliations.push(affiliation); + } + ); + + return affiliations; +}; + +const parsePerson = (personElement, organizations) => { + const name = parseName(personElement); + if (!name.given_name && !name.family_name) return null; + + const personOrOrg = { + type: CREATIBUTOR_TYPE.PERSON, + ...name, + }; + const identifiers = parseIdentifiers(personElement); + if (identifiers.length > 0) personOrOrg.identifiers = identifiers; + + const entry = { person_or_org: personOrOrg }; + const affiliations = parseAffiliations(personElement, organizations); + if (affiliations.length > 0) entry.affiliations = affiliations; + const role = parseRole(personElement); + if (role) entry.role = role; + return entry; +}; + +const parseXmlDocument = (text) => { + if (typeof DOMParser === "undefined") return null; + const document = new DOMParser().parseFromString(text, "application/xml"); + if (!document?.documentElement) return null; + const elements = indexElements(document); + // Browsers report malformed XML by embedding a `parsererror` element in the + // resulting document instead of throwing. + if (elements.has("parsererror")) return null; + return { root: document.documentElement, elements }; +}; + +/** + * Parse `author.xml` into an array of creatibutors. + * + * @param {string} text - raw XML content of the author list + * @returns {Array|null} the creatibutors, or `null` when the content is not a + * readable author list in XML format + */ +export const parseAuthorXml = (text) => { + if (typeof text !== "string") return null; + const parsed = parseXmlDocument(text); + if (!parsed) return null; + + const { root, elements } = parsed; + if (localName(root) !== AUTHOR_LIST_ROOT_ELEMENT) return null; + + const organizations = parseOrganizations(elements.get("organization") ?? []); + + return (elements.get("person") ?? []) + .map((personElement) => parsePerson(personElement, organizations)) + .filter((entry) => entry !== null); +}; diff --git a/assets/js/invenio_app_rdm/overridableRegistry/mapping.js b/assets/js/invenio_app_rdm/overridableRegistry/mapping.js index 81d20f16..d21e4068 100644 --- a/assets/js/invenio_app_rdm/overridableRegistry/mapping.js +++ b/assets/js/invenio_app_rdm/overridableRegistry/mapping.js @@ -5,6 +5,12 @@ // under the terms of the GPL-2.0 License; see LICENSE file for more details. import React, { useContext } from "react"; +import { + CREATIBUTORS_FILE_MODAL_ID, + CreatibutorsFileModal, + jsonFileFormat, +} from "@js/invenio_rdm_records"; +import { parametrize } from "react-overridable"; import { DatasetContext } from "@js/invenio_requests/data"; import { i18next as requestsI18next } from "@translations/invenio_requests/i18next"; import { Label } from "semantic-ui-react"; @@ -28,6 +34,12 @@ import { LockRequestComponent } from "../../components/requests/overrides/LockRe import { TimelineEventBodyComponent } from "../../components/requests/overrides/TimelineEventBody"; import { RecordVersionItemContent } from "../../components/record_details/RecordVersionItem"; import { NewVersionButton } from "../../components/record_details/NewVersionButton"; +import { authorXmlFileFormat } from "../../components/deposit/fileFormats/authorXml"; + +// Add support for the author.xml format to the CreatibutorsFileModal component +const CreatibutorsFileModalComponent = parametrize(CreatibutorsFileModal, { + fileFormats: [jsonFileFormat, authorXmlFileFormat], +}); const RecordManagementContainer = (props) => ( <> @@ -68,6 +80,7 @@ export const overriddenComponents = { CDSRecordsResultsListItemDescription, "InvenioAppRdm.RecordLandingPage.RecordManagement.container": RecordManagementContainer, + [CREATIBUTORS_FILE_MODAL_ID]: CreatibutorsFileModalComponent, "InvenioRdmRecords.SubmitReviewModal.container": SubmitReviewModalComponent, "InvenioRdmRecords.PublishModal.container": PublishModalComponent, "InvenioRdmRecords.RecordLandingPage.RecordManagement.NewVersionButton":