|
| 1 | +"""Map MediaWiki accounts and ORCID profiles to proposed OSW items. |
| 2 | +
|
| 3 | +These functions are pure: they compute the field values and deterministic ids of |
| 4 | +the items to store, without instantiating the pydantic models (which requires an |
| 5 | +open OSW connection) or touching the network. The store step builds the actual |
| 6 | +``User`` / ``Organization`` entities from these proposals. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import uuid |
| 12 | +from dataclasses import dataclass, field |
| 13 | +from typing import List, Optional, Tuple |
| 14 | + |
| 15 | +from osw.data.import_utility import uuid_to_full_page_title |
| 16 | + |
| 17 | +from .config import ORGANIZATION_CATEGORY, USER_CATEGORY |
| 18 | +from .sources import MwUser, OrcidAffiliation, OrcidProfile |
| 19 | + |
| 20 | + |
| 21 | +def _category_namespace(category: str) -> uuid.UUID: |
| 22 | + """Derive the uuid5 namespace of a category from its page title.""" |
| 23 | + return uuid.UUID(category.split("OSW")[-1]) |
| 24 | + |
| 25 | + |
| 26 | +USER_NAMESPACE = _category_namespace(USER_CATEGORY) |
| 27 | +ORGANIZATION_NAMESPACE = _category_namespace(ORGANIZATION_CATEGORY) |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class ProposedOrganization: |
| 32 | + """An Organization item derived from an ORCID affiliation.""" |
| 33 | + |
| 34 | + uuid: uuid.UUID |
| 35 | + name: str |
| 36 | + ror_id: Optional[str] = None |
| 37 | + |
| 38 | + @property |
| 39 | + def full_page_title(self) -> str: |
| 40 | + return uuid_to_full_page_title(self.uuid) |
| 41 | + |
| 42 | + |
| 43 | +@dataclass |
| 44 | +class ProposedUser: |
| 45 | + """A User item derived from a MediaWiki account and optional ORCID data.""" |
| 46 | + |
| 47 | + uuid: uuid.UUID |
| 48 | + username: str |
| 49 | + first_name: str |
| 50 | + surname: str |
| 51 | + label: str |
| 52 | + orcid: Optional[str] = None |
| 53 | + emails: List[str] = field(default_factory=list) |
| 54 | + websites: List[str] = field(default_factory=list) |
| 55 | + organizations: List[str] = field(default_factory=list) |
| 56 | + placeholder_name: bool = False |
| 57 | + |
| 58 | + @property |
| 59 | + def full_page_title(self) -> str: |
| 60 | + return uuid_to_full_page_title(self.uuid) |
| 61 | + |
| 62 | + |
| 63 | +def normalize_org_name(name: str) -> str: |
| 64 | + """Collapse whitespace and lowercase, for a stable org identity key.""" |
| 65 | + return " ".join(name.lower().split()) |
| 66 | + |
| 67 | + |
| 68 | +def user_uuid(mw_user: MwUser) -> uuid.UUID: |
| 69 | + """Deterministic uuid5: keyed on ORCID iD for ORCID users, else username.""" |
| 70 | + key = mw_user.orcid_id or mw_user.name |
| 71 | + return uuid.uuid5(USER_NAMESPACE, key) |
| 72 | + |
| 73 | + |
| 74 | +def organization_uuid(affiliation: OrcidAffiliation) -> uuid.UUID: |
| 75 | + """Deterministic uuid5: keyed on ROR id when present, else the org name.""" |
| 76 | + key = affiliation.ror_id or normalize_org_name(affiliation.organization) |
| 77 | + return uuid.uuid5(ORGANIZATION_NAMESPACE, key) |
| 78 | + |
| 79 | + |
| 80 | +def map_organization(affiliation: OrcidAffiliation) -> ProposedOrganization: |
| 81 | + return ProposedOrganization( |
| 82 | + uuid=organization_uuid(affiliation), |
| 83 | + name=affiliation.organization, |
| 84 | + ror_id=affiliation.ror_id, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def _derive_names( |
| 89 | + profile: Optional[OrcidProfile], username: str |
| 90 | +) -> Tuple[str, str, str, bool]: |
| 91 | + """Return (first_name, surname, label, is_placeholder). |
| 92 | +
|
| 93 | + Prefers ORCID given/family names, then a split of the display name, then a |
| 94 | + username-based placeholder when no name information is available. |
| 95 | + """ |
| 96 | + given = profile.given_names if profile else None |
| 97 | + family = profile.family_name if profile else None |
| 98 | + display = profile.display_name if profile else None |
| 99 | + if given and family: |
| 100 | + return given, family, display or f"{given} {family}", False |
| 101 | + if display: |
| 102 | + parts = display.split() |
| 103 | + first = given or parts[0] |
| 104 | + surname = family or (parts[-1] if len(parts) > 1 else parts[0]) |
| 105 | + return first, surname, display, False |
| 106 | + if given or family: |
| 107 | + first = given or family |
| 108 | + surname = family or given |
| 109 | + return first, surname, f"{first} {surname}", False |
| 110 | + return username, username, username, True |
| 111 | + |
| 112 | + |
| 113 | +def map_user( |
| 114 | + mw_user: MwUser, |
| 115 | + profile: Optional[OrcidProfile] = None, |
| 116 | + link_organizations: bool = True, |
| 117 | +) -> Tuple[ProposedUser, List[ProposedOrganization]]: |
| 118 | + """Build a ProposedUser (and any linked organizations) from the sources.""" |
| 119 | + orcid_uri = f"https://orcid.org/{mw_user.orcid_id}" if mw_user.orcid_id else None |
| 120 | + first, surname, label, placeholder = _derive_names(profile, mw_user.name) |
| 121 | + |
| 122 | + organizations: List[ProposedOrganization] = [] |
| 123 | + org_titles: List[str] = [] |
| 124 | + if link_organizations and profile: |
| 125 | + seen = set() |
| 126 | + for affiliation in profile.affiliations: |
| 127 | + org = map_organization(affiliation) |
| 128 | + if org.uuid in seen: |
| 129 | + continue |
| 130 | + seen.add(org.uuid) |
| 131 | + organizations.append(org) |
| 132 | + org_titles.append(org.full_page_title) |
| 133 | + |
| 134 | + proposed = ProposedUser( |
| 135 | + uuid=user_uuid(mw_user), |
| 136 | + username=mw_user.name, |
| 137 | + first_name=first, |
| 138 | + surname=surname, |
| 139 | + label=label, |
| 140 | + orcid=orcid_uri, |
| 141 | + emails=list(profile.emails) if profile else [], |
| 142 | + websites=list(profile.urls) if profile else [], |
| 143 | + organizations=org_titles, |
| 144 | + placeholder_name=placeholder, |
| 145 | + ) |
| 146 | + return proposed, organizations |
0 commit comments