From d2eb528cbb49a8effa6c586e03ef5295ad2eae53 Mon Sep 17 00:00:00 2001 From: Mihai Cojocaru Date: Fri, 13 Nov 2026 07:41:11 +0200 Subject: [PATCH] feat(deposit): import authors from INSPIRE author.xml files Register the INSPIRE author list format (author.xml), used by the LHC collaborations, in the deposit form's "add authors/contributors from file" dialog, using the invenio-rdm-records file format registry. * Add the author.xml parser and its tests. * Register the format from its own module, imported by the overridable registry so the deposit page loads it; registration is idempotent. Depends on the creatibutor file format registry in invenio-rdm-records. --- .../deposit/fileFormats/authorXml/index.js | 51 +++ .../deposit/fileFormats/authorXml/parse.js | 266 ++++++++++++++++ .../fileFormats/authorXml/parse.test.js | 301 ++++++++++++++++++ .../components/deposit/fileFormats/index.js | 23 ++ .../overridableRegistry/mapping.js | 2 + 5 files changed, 643 insertions(+) create mode 100644 assets/js/components/deposit/fileFormats/authorXml/index.js create mode 100644 assets/js/components/deposit/fileFormats/authorXml/parse.js create mode 100644 assets/js/components/deposit/fileFormats/authorXml/parse.test.js create mode 100644 assets/js/components/deposit/fileFormats/index.js 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..8db63640 --- /dev/null +++ b/assets/js/components/deposit/fileFormats/authorXml/index.js @@ -0,0 +1,51 @@ +// 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 { i18next } from "@translations/invenio_app_rdm/i18next"; +import { DEFAULT_CREATIBUTOR_FILE_FORMAT_ORDER } from "@js/invenio_rdm_records"; +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 INSPIRE author list format (https://github.com/inspirehep/author.xml), + * offered by the "add authors/contributors from file" dialog. + */ +export const authorXmlFileFormat = { + id: "author_xml", + label: i18next.t("XML (author.xml)"), + extension: ".xml", + // Offered after the built-in JSON format. + order: DEFAULT_CREATIBUTOR_FILE_FORMAT_ORDER + 10, + example: AUTHOR_XML_IMPORT_EXAMPLE, + note: i18next.t( + "Author identifiers are read from the ORCID, GND, ISNI and ROR sources." + ), + 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..b8802a64 --- /dev/null +++ b/assets/js/components/deposit/fileFormats/authorXml/parse.js @@ -0,0 +1,266 @@ +// 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. + +// Parser of the INSPIRE author list format: +// https://github.com/inspirehep/author.xml + +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; + +/** + * `` and `` wrap the entries they hold. + * Accept the entries nested (what author.xml does) as well as directly under + * ``, as hand-written files tend to flatten them. + */ +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; +}; + +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; + 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`, as specified in + * https://github.com/inspirehep/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/components/deposit/fileFormats/authorXml/parse.test.js b/assets/js/components/deposit/fileFormats/authorXml/parse.test.js new file mode 100644 index 00000000..76d95641 --- /dev/null +++ b/assets/js/components/deposit/fileFormats/authorXml/parse.test.js @@ -0,0 +1,301 @@ +// 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"; + +// The fixtures follow the examples published in +// https://github.com/inspirehep/author.xml/tree/main/example-files +const MINIMAL_EXAMPLE = ` + + + + http://yerphi.am + Yerevan Phys. Inst. + Yerevan Phys. Inst. + https://ror.org/00ad27c73 + + + CERN + https://ror.org/01ggx4157 + + + + + Vardan Khachatryan + Vardan + Khachatryan + V. Khachatryan + V. + Khachatryan + + + + + https://orcid.org/0000-0002-1825-0097 + INSPIRE-00314584 + + + + Benjamin C. Shen + Benjamin C. + Shen + Deceased + B.C. Shen + + + + + +`; + +const MULTICOLLABORATION_EXAMPLE = ` + + + + http://www.infn.it/ + INFN, Florence + INFN, Florence + https://ror.org/04wgrhj04 + + + http://www.unifi.it/ + Florence U. + https://ror.org/00m9ns448 + + + http://www.fnal.gov/ + Fermilab + https://ror.org/020h68657 + + + + + Marcello Abbrescia + Marcello + Abbrescia + M. Abbrescia + + + + + + + INSPIRE-00125803 + + + +`; + +const wrapAuthors = (authors) => ` + + + ${authors} + +`; + +describe("parseAuthorXml", () => { + it("parses the example minimal author list", () => { + expect(parseAuthorXml(MINIMAL_EXAMPLE)).toEqual([ + { + person_or_org: { + type: "personal", + given_name: "Vardan", + family_name: "Khachatryan", + identifiers: [{ scheme: "orcid", identifier: "0000-0002-1825-0097" }], + }, + affiliations: [{ id: "00ad27c73", name: "Yerevan Phys. Inst." }], + }, + { + person_or_org: { + type: "personal", + given_name: "Benjamin C.", + family_name: "Shen", + }, + affiliations: [{ id: "01ggx4157", name: "CERN" }], + }, + ]); + }); + + it("resolves every affiliation of an author in a multicollaboration list", () => { + const [entry] = parseAuthorXml(MULTICOLLABORATION_EXAMPLE); + + expect(entry.affiliations).toEqual([ + { id: "04wgrhj04", name: "INFN, Florence" }, + { id: "00m9ns448", name: "Florence U." }, + { id: "020h68657", name: "Fermilab" }, + ]); + // The only identifier is an INSPIRE one, which has no scheme in InvenioRDM. + expect(entry.person_or_org).toEqual({ + type: "personal", + given_name: "Marcello", + family_name: "Abbrescia", + }); + }); + + it("imports the identifier schemes InvenioRDM knows, in any casing", () => { + const [entry] = parseAuthorXml( + wrapAuthors(` + Jane + Smith + + 0000-0002-1825-0097 + 0000-0002-1825-0097 + https://d-nb.info/gnd/118757989 + 0000 0001 2141 8603 + ror:04wx9ry42 + INSPIRE-00314584 + 123456 + + `) + ); + + expect(entry.person_or_org.identifiers).toEqual([ + { scheme: "orcid", identifier: "0000-0002-1825-0097" }, + { scheme: "gnd", identifier: "118757989" }, + { scheme: "isni", identifier: "0000 0001 2141 8603" }, + { scheme: "ror", identifier: "04wx9ry42" }, + ]); + }); + + it("falls back to the free-text name when the structured name is missing", () => { + const [entry] = parseAuthorXml( + wrapAuthors(` + Vardan Khachatryan + V. Khachatryan + `) + ); + + expect(entry.person_or_org).toEqual({ + type: "personal", + given_name: "Vardan", + family_name: "Khachatryan", + }); + expect(entry.affiliations).toBeUndefined(); + }); + + it("falls back to the paper name when the free-text name is missing", () => { + const [entry] = parseAuthorXml( + wrapAuthors(` + G.F. Donald + `) + ); + + expect(entry.person_or_org).toEqual({ + type: "personal", + given_name: "G.F.", + family_name: "Donald", + }); + }); + + it("prefers the explicit paper family name over splitting free text", () => { + const [entry] = parseAuthorXml( + wrapAuthors(` + Johannes Diderik van der Waals + Johannes Diderik + J.D. van der Waals + van der Waals + `) + ); + + expect(entry.person_or_org).toMatchObject({ + given_name: "Johannes Diderik", + family_name: "van der Waals", + }); + }); + + it("ignores organizations that no author refers to", () => { + expect( + parseAuthorXml(` + + + + CERN + + + + + Jane + Smith + + + `) + ).toEqual([ + { + person_or_org: { + type: "personal", + given_name: "Jane", + family_name: "Smith", + }, + }, + ]); + }); + + it("does not take an organization name from cal:orgName", () => { + const [entry] = parseAuthorXml(` + + + + https://ror.org/01ggx4157 + 808 + + + 809 + + + + + Jane + Smith + + + + + + + `); + + // The ROR becomes the identifier; neither the ROR URL nor the internal + // institution id is stored as an affiliation name. + expect(entry.affiliations).toEqual([{ id: "01ggx4157" }]); + }); + + it("returns an empty array for an author list without authors", () => { + expect(parseAuthorXml(wrapAuthors(""))).toEqual([]); + }); + + it("returns null when the XML is not an author list", () => { + expect( + parseAuthorXml(` + + JaneSmith + + `) + ).toBeNull(); + expect(parseAuthorXml("Smith, Jane")).toBeNull(); + }); + + it("returns null when the XML cannot be parsed", () => { + expect(parseAuthorXml("")).toBeNull(); + expect(parseAuthorXml("Smith, Jane")).toBeNull(); + expect(parseAuthorXml("")).toBeNull(); + expect(parseAuthorXml(null)).toBeNull(); + }); +}); diff --git a/assets/js/components/deposit/fileFormats/index.js b/assets/js/components/deposit/fileFormats/index.js new file mode 100644 index 00000000..97b7d22c --- /dev/null +++ b/assets/js/components/deposit/fileFormats/index.js @@ -0,0 +1,23 @@ +// 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. + +// Formats offered by the deposit form's "add authors/contributors from file" +// dialog, in addition to the built-in JSON format. Imported for its side effect +// by the overridable registry, which the deposit page loads. + +import { + getCreatibutorFileFormats, + registerCreatibutorFileFormat, +} from "@js/invenio_rdm_records"; +import { authorXmlFileFormat } from "./authorXml"; + +const isRegistered = ({ id }) => + getCreatibutorFileFormats().some((format) => format.id === id); + +// Registering is idempotent, so evaluating this module twice does not throw. +[authorXmlFileFormat].forEach((fileFormat) => { + if (!isRegistered(fileFormat)) registerCreatibutorFileFormat(fileFormat); +}); diff --git a/assets/js/invenio_app_rdm/overridableRegistry/mapping.js b/assets/js/invenio_app_rdm/overridableRegistry/mapping.js index 81d20f16..985887b1 100644 --- a/assets/js/invenio_app_rdm/overridableRegistry/mapping.js +++ b/assets/js/invenio_app_rdm/overridableRegistry/mapping.js @@ -28,6 +28,8 @@ 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"; +// Registers the extra "add authors/contributors from file" formats. +import "../../components/deposit/fileFormats"; const RecordManagementContainer = (props) => ( <>