All files / src/helpers wikidata.ts

93.17% Statements 191/205
80.28% Branches 57/71
100% Functions 11/11
93.17% Lines 191/205

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 3431x 1x 1x 1x 1x   1x   1x           1x   1x       1x 1x   1x       1x 1x   1x     1x 1x   1x                   1x 22x 22x 22x 22x 118x 118x 22x 22x     20x 22x 2x 2x 2x 22x   1x                   1x 5x 5x     5x 5x 1x 1x 4x 5x 5x   1x                   1x 22x 22x     22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 3x 3x 3x 3x 3x 3x 22x 22x   1x           1x 6x 6x 6x   6x 6x 6x 6x 6x 6x 2x 6x 6x 6x 6x 6x 1x 1x 5x 6x 1x 1x   6x 6x 6x 6x 6x 6x 6x 6x     4x   4x 4x 6x 2x 2x 1x 1x 2x   3x 6x 4x 4x 3x 3x   1x                                     1x 1x 1x                           1x                           1x 4x 4x 4x 4x   4x 4x 5x 4x 2x 4x   4x 4x 2x 4x 4x 4x   1x                 1x 3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 3x 3x 3x 3x   1x         1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 5x 5x 5x 5x 5x       4x 4x 4x 4x 4x 4x 5x     4x 5x 1x 1x 3x 3x   1x                                 1x 1x 1x 1x 1x 1x 1x  
import { wikidata as wikidataConfig } from '@/../config.json';
import { withTtlCache } from '@/helpers/requestCache';
import { isCAS, isPopulatedObject } from '@/utils/typeGuards/common';
import { Logger } from '@/utils/Logger';
import { getErrorMessage } from '@/helpers/exceptions';
 
const logger = new Logger('wikidata');
 
/**
 * @group Helpers
 * @groupDescription Resolves a chemical (by CAS number or English name) to its labels in another
 * language through Wikidata, so suppliers that only index local-language product names can still
 * be searched.
 * @source
 */
 
/**
 * Wikidata's MediaWiki API endpoint.
 * @see https://www.wikidata.org/w/api.php
 * @source
 */
const WIKIDATA_API = 'https://www.wikidata.org/w/api.php';
 
/**
 * Wikidata property holding a compound's CAS Registry Number. Restricting a search to items
 * with this property keeps it to chemical substances.
 * @source
 */
const CAS_PROPERTY = 'P231';
 
/**
 * How long a lookup stays cached, in ms.
 * @source
 */
const TTL_MS = wikidataConfig.ttlDays * 24 * 60 * 60 * 1000;
 
/**
 * Calls the Wikidata API and returns the parsed JSON body.
 * @param params - Query-string parameters (`format=json` is added)
 * @returns The parsed response, or undefined on a network/HTTP failure
 * @example
 * ```typescript
 * await queryWikidata({ action: "wbgetentities", ids: "Q2409", props: "labels" });
 * // Returns: { entities: { Q2409: { labels: { ... } } } }
 * ```
 * @source
 */
async function queryWikidata(params: Record<string, string>): Promise<unknown> {
  try {
    const url = new URL(WIKIDATA_API);
    for (const [key, value] of Object.entries({ ...params, format: 'json' })) {
      url.searchParams.set(key, value);
    }
    const response = await fetch(url);
    if (!response.ok) {
      return undefined;
    }
    return await response.json();
  } catch (error) {
    logger.error(`Error querying Wikidata: ${getErrorMessage(error)}`, { error });
    return undefined;
  }
}
 
/**
 * Extracts the first search hit's entity id (e.g. `Q2409`) from a `list=search` response.
 * @param data - The parsed `list=search` response
 * @returns The first hit's id, or undefined when there is none
 * @example
 * ```typescript
 * extractFirstEntityId({ query: { search: [{ title: "Q2409" }] } }); // Returns: "Q2409"
 * extractFirstEntityId({ query: { search: [] } }); // Returns: undefined
 * ```
 * @source
 */
function extractFirstEntityId(data: unknown): string | undefined {
  if (!isPopulatedObject(data) || !isPopulatedObject(data.query)) {
    return undefined;
  }
  const hits = data.query.search;
  if (!Array.isArray(hits) || !isPopulatedObject(hits[0])) {
    return undefined;
  }
  const title = hits[0].title;
  return typeof title === 'string' && /^Q\d+$/.test(title) ? title : undefined;
}
 
/**
 * Reads the label and aliases of one language out of a Wikidata entity.
 * @param entity - A Wikidata entity object
 * @param lang - Language code, e.g. `nl`
 * @returns The label first, then the aliases (empty when the entity has none in `lang`)
 * @example
 * ```typescript
 * entityNames({ labels: { nl: { value: "zoutzuur" } }, aliases: {} }, "nl"); // Returns: ["zoutzuur"]
 * ```
 * @source
 */
function entityNames(entity: unknown, lang: string): string[] {
  if (!isPopulatedObject(entity)) {
    return [];
  }
  const names: string[] = [];
  const labels = entity.labels;
  if (isPopulatedObject(labels) && isPopulatedObject(labels[lang])) {
    const value = labels[lang].value;
    if (typeof value === 'string') {
      names.push(value);
    }
  }
  const aliases = entity.aliases;
  if (isPopulatedObject(aliases) && Array.isArray(aliases[lang])) {
    for (const alias of aliases[lang]) {
      if (isPopulatedObject(alias) && typeof alias.value === 'string') {
        names.push(alias.value);
      }
    }
  }
  return names;
}
 
/**
 * Network implementation for {@link getLocalizedNames}; see it for details.
 * @param term - A CAS number or English chemical name
 * @param lang - Target language code
 * @returns The localized names (empty when there is no confident match), or undefined on failure
 * @source
 */
async function getLocalizedNamesUncached(
  term: string,
  lang: string,
): Promise<string[] | undefined> {
  const cleaned = term.trim();
  const byCas = isCAS(cleaned);
  const search = await queryWikidata({
    action: 'query',
    list: 'search',
    srsearch: byCas
      ? `haswbstatement:${CAS_PROPERTY}=${cleaned}`
      : `${cleaned} haswbstatement:${CAS_PROPERTY}`,
    srlimit: '1',
    srnamespace: '0',
  });
  if (search === undefined) {
    return undefined;
  }
  const id = extractFirstEntityId(search);
  if (id === undefined) {
    return [];
  }
 
  const languages = byCas ? lang : `en|${lang}`;
  const entities = await queryWikidata({
    action: 'wbgetentities',
    ids: id,
    props: 'labels|aliases',
    languages,
  });
  if (!isPopulatedObject(entities) || !isPopulatedObject(entities.entities)) {
    return undefined;
  }
  const entity = entities.entities[id];
 
  // A name search can land on the wrong compound, so require the English label or an alias
  // to equal the term before trusting the hit. A CAS match is exact already.
  if (!byCas) {
    const english = entityNames(entity, 'en').map((name) => name.toLowerCase());
    if (!english.includes(cleaned.toLowerCase())) {
      return [];
    }
  }
 
  const unique = new Map<string, string>();
  for (const name of entityNames(entity, lang)) {
    unique.set(name.toLowerCase(), name);
  }
  return [...unique.values()].slice(0, 1 + wikidataConfig.maxAliases);
}
 
/**
 * Resolves a CAS number or English chemical name to its Wikidata label and aliases in
 * another language. Cached for `wikidata.ttlDays` (config.json); a definitive "no match"
 * is cached as an empty array, a failed request (undefined) is not.
 * @category Helpers
 * @param term - A CAS number (`7647-01-0`) or an English chemical name (`hydrochloric acid`)
 * @param lang - Target language code, e.g. `nl`
 * @returns The label first, then up to `wikidata.maxAliases` aliases; an empty array when
 * Wikidata has no confident match; undefined if the lookup failed
 * @example
 * ```typescript
 * await getLocalizedNames("7647-01-0", "nl");
 * // Returns: ["zoutzuur"]
 * await getLocalizedNames("sodium chloride", "nl");
 * // Returns: ["natriumchloride"]
 * await getLocalizedNames("not a chemical", "nl");
 * // Returns: []
 * ```
 * @source
 */
export const getLocalizedNames: (term: string, lang: string) => Promise<string[] | undefined> =
  withTtlCache(getLocalizedNamesUncached, { namespace: 'wikidataNames', ttlMs: TTL_MS });
 
/**
 * A chemical resolved to its English name and CAS number. Both are absent for "no match".
 * @category Helpers
 * @group Types
 */
export interface EnglishChemical {
  /** The English label, e.g. `hydrochloric acid`. */
  name?: string;
  /** CAS Registry Number, e.g. `7647-01-0`. */
  cas?: string;
}
 
/**
 * Picks the search hit whose label (or, failing that, an alias) equals the term in English or
 * one of the configured source languages. Search ranking alone is unreliable: `azijnzuur`
 * ranks indole-3-acetic acid above acetic acid.
 * @param entities - The `wbgetentities` `entities` map
 * @param ids - Candidate ids, in search-rank order
 * @param term - The (lowercased) term being resolved
 * @returns The chosen entity id and its English label, or undefined when none matches
 * @example
 * ```typescript
 * pickMatchingEntity(entities, ["Q411208", "Q47512"], "azijnzuur");
 * // Returns: { id: "Q47512", name: "acetic acid" }
 * ```
 * @source
 */
function pickMatchingEntity(
  entities: Record<string, unknown>,
  ids: string[],
  term: string,
): { id: string; name: string } | undefined {
  const languages = ['en', ...wikidataConfig.sourceLanguages];
  const labelsOf = (id: string) =>
    languages.flatMap((lang) => entityNames(entities[id], lang).slice(0, 1));
  const aliasesOf = (id: string) =>
    languages.flatMap((lang) => entityNames(entities[id], lang).slice(1));
  const matches = (names: string[]) => names.some((name) => name.toLowerCase() === term);
 
  const id =
    ids.find((candidate) => matches(labelsOf(candidate))) ??
    ids.find((candidate) => matches(aliasesOf(candidate)));
  const name = id === undefined ? undefined : entityNames(entities[id], 'en')[0];
  return id === undefined || name === undefined ? undefined : { id, name };
}
 
/**
 * Reads the CAS number (property P231) of a Wikidata item.
 * @param id - The item id, e.g. `Q2409`
 * @returns The CAS number, or undefined when the item has none or the request failed
 * @example
 * ```typescript
 * await fetchCasOf("Q2409"); // Returns: "7647-01-0"
 * ```
 * @source
 */
async function fetchCasOf(id: string): Promise<string | undefined> {
  const data = await queryWikidata({
    action: 'wbgetclaims',
    entity: id,
    property: CAS_PROPERTY,
  });
  if (!isPopulatedObject(data) || !isPopulatedObject(data.claims)) {
    return undefined;
  }
  const claims = data.claims[CAS_PROPERTY];
  const snak =
    Array.isArray(claims) && isPopulatedObject(claims[0]) ? claims[0].mainsnak : undefined;
  const value =
    isPopulatedObject(snak) && isPopulatedObject(snak.datavalue) ? snak.datavalue.value : undefined;
  return isCAS(value) ? value : undefined;
}
 
/**
 * Network implementation for {@link resolveEnglishChemical}; see it for details.
 * @param term - A chemical name in English or a source language
 * @returns The match (empty object for none), or undefined on failure
 * @source
 */
async function resolveEnglishChemicalUncached(term: string): Promise<EnglishChemical | undefined> {
  const cleaned = term.trim();
  const search = await queryWikidata({
    action: 'query',
    list: 'search',
    srsearch: `${cleaned} haswbstatement:${CAS_PROPERTY}`,
    srlimit: String(wikidataConfig.englishSearchHits),
    srnamespace: '0',
  });
  if (search === undefined) {
    return undefined;
  }
  const hits =
    isPopulatedObject(search) && isPopulatedObject(search.query) ? search.query.search : [];
  const ids = (Array.isArray(hits) ? hits : [])
    .map((hit) => (isPopulatedObject(hit) ? hit.title : undefined))
    .filter((title): title is string => typeof title === 'string' && /^Q\d+$/.test(title));
  if (ids.length === 0) {
    return {};
  }
 
  const response = await queryWikidata({
    action: 'wbgetentities',
    ids: ids.join('|'),
    props: 'labels|aliases',
    languages: ['en', ...wikidataConfig.sourceLanguages].join('|'),
  });
  if (!isPopulatedObject(response) || !isPopulatedObject(response.entities)) {
    return undefined;
  }
  const match = pickMatchingEntity(response.entities, ids, cleaned.toLowerCase());
  if (match === undefined) {
    return {};
  }
  return { name: match.name, cas: await fetchCasOf(match.id) };
}
 
/**
 * Resolves a chemical name written in English or a configured source language (see
 * `wikidata.sourceLanguages` in config.json, e.g. Dutch `zoutzuur`) to its English name and CAS number. The term must
 * equal a Wikidata label or alias exactly, so an unrelated top search hit is never trusted.
 * Cached for `wikidata.ttlDays`; a definitive "no match" is cached as `{}`, a failed request
 * (undefined) is not.
 * @category Helpers
 * @param term - A chemical name, e.g. `zoutzuur` or `acetone`
 * @returns The English name and CAS, `{}` when nothing matches, or undefined if the lookup failed
 * @example
 * ```typescript
 * await resolveEnglishChemical("zoutzuur");
 * // Returns: { name: "hydrochloric acid", cas: "7647-01-0" }
 * await resolveEnglishChemical("not a chemical");
 * // Returns: {}
 * ```
 * @source
 */
export const resolveEnglishChemical: (term: string) => Promise<EnglishChemical | undefined> =
  withTtlCache(resolveEnglishChemicalUncached, {
    namespace: 'wikidataEnglish',
    ttlMs: TTL_MS,
    keyFromArgs: (term) => term.trim().toLowerCase(),
  });