MapComplete/src/Logic/ImageProviders/WikidataImageProvider.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
3 KiB
TypeScript
Raw Normal View History

import ImageProvider, { PanoramaView, ProvidedImage } from "./ImageProvider"
import BaseUIElement from "../../UI/BaseUIElement"
import { WikimediaImageProvider } from "./WikimediaImageProvider"
2021-10-02 22:31:16 +02:00
import Wikidata from "../Web/Wikidata"
import SvelteUIElement from "../../UI/Base/SvelteUIElement"
import * as Wikidata_icon from "../../assets/svg/Wikidata.svelte"
import { Utils } from "../../Utils"
import { Feature, Point } from "geojson"
export class WikidataImageProvider extends ImageProvider {
public static readonly singleton = new WikidataImageProvider()
public readonly defaultKeyPrefixes = ["wikidata"]
public readonly name = "Wikidata"
2024-10-19 14:44:55 +02:00
private static readonly keyBlacklist: ReadonlySet<string> = new Set([
"mapillary",
2025-04-15 18:18:44 +02:00
...Utils.Times((i) => "mapillary:" + i, 10),
2024-10-19 14:44:55 +02:00
])
private constructor() {
super()
}
public apiUrls(): string[] {
return Wikidata.neededUrls
}
2023-12-02 03:12:34 +01:00
public SourceIcon(): BaseUIElement {
return new SvelteUIElement(Wikidata_icon)
}
2024-10-19 14:44:55 +02:00
public async ExtractUrls(key: string, value: string): Promise<ProvidedImage[]> {
if (WikidataImageProvider.keyBlacklist.has(key)) {
return undefined
}
2021-10-03 01:38:57 +02:00
const entity = await Wikidata.LoadWikidataEntryAsync(value)
2021-11-07 16:34:51 +01:00
if (entity === undefined) {
return undefined
2021-10-03 01:38:57 +02:00
}
2021-11-07 16:34:51 +01:00
const allImages: Promise<ProvidedImage[]>[] = []
// P18 is the claim 'depicted in this image'
2021-10-02 22:31:16 +02:00
for (const img of Array.from(entity.claims.get("P18") ?? [])) {
const promises = WikimediaImageProvider.singleton.ExtractUrls(undefined, img)
allImages.push(promises)
}
// P373 is 'commons category'
for (let cat of Array.from(entity.claims.get("P373") ?? [])) {
2021-11-07 16:34:51 +01:00
if (!cat.startsWith("Category:")) {
cat = "Category:" + cat
}
const promises = WikimediaImageProvider.singleton.ExtractUrls(undefined, cat)
allImages.push(promises)
}
2021-11-07 16:34:51 +01:00
2021-10-03 01:38:57 +02:00
const commons = entity.commons
if (
commons !== undefined &&
(commons.startsWith("Category:") || commons.startsWith("File:"))
) {
const promises = WikimediaImageProvider.singleton.ExtractUrls(undefined, commons)
allImages.push(promises)
}
const resolved = await Promise.all(Utils.NoNull(allImages))
2025-06-18 21:40:01 +02:00
const flattened = resolved.flatMap((x) => x)
if (flattened.length === 1) {
flattened[0].originalAttribute = { key, value }
}
return flattened
}
2025-04-09 17:10:33 +02:00
public DownloadAttribution(): Promise<undefined> {
2021-11-07 16:34:51 +01:00
throw new Error("Method not implemented; shouldn't be needed!")
}
public getPanoramaInfo(image: { id: string }): Promise<Feature<Point, PanoramaView>> {
return undefined
}
2025-05-03 23:48:35 +02:00
visitUrl(
image: Partial<ProvidedImage>,
location?: { lon: number; lat: number }
): string | undefined {
return undefined
}
}