2021-09-29 23:56:59 +02:00
|
|
|
import ImageProvider, {ProvidedImage} from "./ImageProvider";
|
|
|
|
import BaseUIElement from "../../UI/BaseUIElement";
|
|
|
|
import Svg from "../../Svg";
|
|
|
|
import {WikimediaImageProvider} from "./WikimediaImageProvider";
|
2021-10-02 22:31:16 +02:00
|
|
|
import Wikidata from "../Web/Wikidata";
|
2021-09-29 23:56:59 +02:00
|
|
|
|
|
|
|
export class WikidataImageProvider extends ImageProvider {
|
|
|
|
|
|
|
|
public static readonly singleton = new WikidataImageProvider()
|
|
|
|
public readonly defaultKeyPrefixes = ["wikidata"]
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
public SourceIcon(backlinkSource?: string): BaseUIElement {
|
|
|
|
throw Svg.wikidata_svg();
|
2021-09-29 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async ExtractUrls(key: string, value: string): Promise<Promise<ProvidedImage>[]> {
|
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) {
|
2021-10-03 01:38:57 +02:00
|
|
|
return []
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
const allImages: Promise<ProvidedImage>[] = []
|
2021-09-29 23:56:59 +02:00
|
|
|
// 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 = await WikimediaImageProvider.singleton.ExtractUrls(undefined, img)
|
2021-09-29 23:56:59 +02:00
|
|
|
allImages.push(...promises)
|
|
|
|
}
|
2021-10-06 02:30:23 +02:00
|
|
|
// 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
|
2021-10-06 02:30:23 +02:00
|
|
|
}
|
|
|
|
const promises = await 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
|
2021-10-06 02:30:23 +02:00
|
|
|
if (commons !== undefined && (commons.startsWith("Category:") || commons.startsWith("File:"))) {
|
2021-11-07 16:34:51 +01:00
|
|
|
const promises = await WikimediaImageProvider.singleton.ExtractUrls(undefined, commons)
|
2021-09-29 23:56:59 +02:00
|
|
|
allImages.push(...promises)
|
|
|
|
}
|
|
|
|
return allImages
|
|
|
|
}
|
|
|
|
|
2022-06-13 00:51:53 +02:00
|
|
|
public DownloadAttribution(url: string): Promise<any> {
|
2021-11-07 16:34:51 +01:00
|
|
|
throw new Error("Method not implemented; shouldn't be needed!");
|
|
|
|
}
|
|
|
|
|
2021-09-29 23:56:59 +02:00
|
|
|
}
|