diff --git a/package-lock.json b/package-lock.json index e6f06be57..8137640bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -79,8 +79,7 @@ "turndown": "^7.1.3", "vite-node": "^0.28.3", "vitest": "^0.28.3", - "wikibase-sdk": "^7.14.0", - "wikidata-sdk": "^7.14.0", + "wikibase-sdk": "^10.1.1", "xml2js": "^0.5.0" }, "devDependencies": { @@ -20745,15 +20744,19 @@ } }, "node_modules/wikibase-sdk": { - "version": "7.15.0", - "license": "MIT", + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/wikibase-sdk/-/wikibase-sdk-10.1.1.tgz", + "integrity": "sha512-ePSfVhHeJ5ljAxde7O9ryzIBeNF54RB+LTn5naF+mUitDJeRkrgz2ZTxl89EMDzJeHUV4VnwVmLRWRcDqPA9Ng==", "engines": { - "node": ">= 6.4" + "node": ">= 12.0.0" } }, "node_modules/wikidata-sdk": { "version": "7.14.4", - "license": "MIT", + "resolved": "https://registry.npmjs.org/wikidata-sdk/-/wikidata-sdk-7.14.4.tgz", + "integrity": "sha512-UAFBXWLxEWvB0Pn/c+ekc1voU8o0zR7T3kRt9xHLyfy2OiV7W1htk2iErwJdfisBPDb2a35HUgf2x1+ZAdrM8A==", + "deprecated": "wikidata-sdk has been renamed wikibase-sdk", + "dev": true, "dependencies": { "wikibase-sdk": "^7.14.4" }, @@ -20761,6 +20764,15 @@ "node": ">= 6.4" } }, + "node_modules/wikidata-sdk/node_modules/wikibase-sdk": { + "version": "7.15.0", + "resolved": "https://registry.npmjs.org/wikibase-sdk/-/wikibase-sdk-7.15.0.tgz", + "integrity": "sha512-EZvOVz2Ezx1IsiSTlJ5XF1SLLudzWvtm7CV5DYKhO7CIX4EkB0Pc8seb8h6ZNEPRgYqnmrTx5aLsaIQW7GBe2w==", + "dev": true, + "engines": { + "node": ">= 6.4" + } + }, "node_modules/winston": { "version": "3.13.0", "license": "MIT", @@ -35251,12 +35263,25 @@ } }, "wikibase-sdk": { - "version": "7.15.0" + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/wikibase-sdk/-/wikibase-sdk-10.1.1.tgz", + "integrity": "sha512-ePSfVhHeJ5ljAxde7O9ryzIBeNF54RB+LTn5naF+mUitDJeRkrgz2ZTxl89EMDzJeHUV4VnwVmLRWRcDqPA9Ng==" }, "wikidata-sdk": { "version": "7.14.4", + "resolved": "https://registry.npmjs.org/wikidata-sdk/-/wikidata-sdk-7.14.4.tgz", + "integrity": "sha512-UAFBXWLxEWvB0Pn/c+ekc1voU8o0zR7T3kRt9xHLyfy2OiV7W1htk2iErwJdfisBPDb2a35HUgf2x1+ZAdrM8A==", + "dev": true, "requires": { "wikibase-sdk": "^7.14.4" + }, + "dependencies": { + "wikibase-sdk": { + "version": "7.15.0", + "resolved": "https://registry.npmjs.org/wikibase-sdk/-/wikibase-sdk-7.15.0.tgz", + "integrity": "sha512-EZvOVz2Ezx1IsiSTlJ5XF1SLLudzWvtm7CV5DYKhO7CIX4EkB0Pc8seb8h6ZNEPRgYqnmrTx5aLsaIQW7GBe2w==", + "dev": true + } } }, "winston": { diff --git a/package.json b/package.json index 4d8e670ff..404541dac 100644 --- a/package.json +++ b/package.json @@ -224,8 +224,7 @@ "turndown": "^7.1.3", "vite-node": "^0.28.3", "vitest": "^0.28.3", - "wikibase-sdk": "^7.14.0", - "wikidata-sdk": "^7.14.0", + "wikibase-sdk": "^10.1.1", "xml2js": "^0.5.0" }, "devDependencies": { diff --git a/scripts/fetchLanguages.ts b/scripts/fetchLanguages.ts index 1ecf6eb5e..25b51fa38 100644 --- a/scripts/fetchLanguages.ts +++ b/scripts/fetchLanguages.ts @@ -3,7 +3,6 @@ * Some meta-info (e.g. RTL) is exported too */ -import * as wds from "wikidata-sdk" import { Utils } from "../src/Utils" import ScriptUtils from "./ScriptUtils" import { existsSync, readFileSync, writeFileSync } from "fs" @@ -44,10 +43,10 @@ async function fetchRegularLanguages() { " ?lang wdt:P424 ?code. \n" + // Wikimedia language code seems to be close to the weblate entries ' SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } \n' + "} " - const url = wds.sparqlQuery(sparql) + const url = Wikidata.wds.sparqlQuery(sparql) // request the generated URL with your favorite HTTP request library - const result = await Utils.downloadJson(url, { "User-Agent": "MapComplete script" }) + const result = await Utils.downloadJson<{results: {bindings: any[]}}>(url, { "User-Agent": "MapComplete script" }) const bindings = result.results.bindings // Traditional chinese = 繁體中文 or 正體中文 @@ -97,9 +96,9 @@ async function fetchSpecial(id: number, code: string): Promise(url, { "User-Agent": "MapComplete script" }) const bindings = result.results.bindings bindings.forEach((binding) => (binding["code"] = { value: code })) return bindings @@ -133,9 +132,9 @@ async function getOfficialLanguagesPerCountry(): Promise> ?language wdt:P218 ?languageCode. SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } }` - const url = wds.sparqlQuery(sparql) + const url = Wikidata.wds.sparqlQuery(sparql) - const result = await Utils.downloadJson(url, { "User-Agent": "MapComplete script" }) + const result = await Utils.downloadJson<{results: {bindings: any[]}}>(url, { "User-Agent": "MapComplete script" }) const bindings: { countryCode: { value: string }; languageCode: { value: string } }[] = result.results.bindings for (const binding of bindings) { diff --git a/src/Logic/Web/Wikidata.ts b/src/Logic/Web/Wikidata.ts index c3ff77f9a..e4418515a 100644 --- a/src/Logic/Web/Wikidata.ts +++ b/src/Logic/Web/Wikidata.ts @@ -1,6 +1,6 @@ import { Utils } from "../../Utils" import { Store, UIEventSource } from "../UIEventSource" -import * as wds from "wikidata-sdk" +import { WBK} from "wikibase-sdk" export class WikidataResponse { public readonly id: string @@ -54,7 +54,8 @@ export class WikidataResponse { } static extractClaims(claimsJson: any): Map> { - const simplified = wds.simplify.claims(claimsJson, { + // @ts-ignore + const simplified = Wikidata.wds.simplify.claims(claimsJson, { timeConverter: "simple-day", }) @@ -127,6 +128,12 @@ interface SparqlResult { * Utility functions around wikidata */ export default class Wikidata { + + public static wds = WBK({ + instance: "https://wikidata.org", + sparqlEndpoint: "https://query.wikidata.org/bigdata/namespace/wdq/sparql" + }) + public static readonly neededUrls = [ "https://www.wikidata.org/", "https://wikidata.org/", @@ -204,7 +211,7 @@ export default class Wikidata { ${instanceOf} ${minusPhrases.join("\n ")} } ORDER BY ASC(?num) LIMIT ${options?.maxCount ?? 20}` - const url = wds.sparqlQuery(sparql) + const url = Wikidata. wds.sparqlQuery(sparql) const result = await Utils.downloadJson(url) /*The full uri of the wikidata-item*/ @@ -245,9 +252,9 @@ export default class Wikidata { lang + "&type=item&origin=*" + "&props=" // props= removes some unused values in the result - const response = await Utils.downloadJsonCached(url, 10000) + const response = await Utils.downloadJsonCached<{search: any[]}>(url, 10000) - const result: any[] = response.search + const result = response.search if (result.length < pageCount) { // No next page @@ -375,7 +382,7 @@ export default class Wikidata { /** * Build a SPARQL-query, return the result * - * @param keys: how variables are named. Every key not ending with 'Label' should appear in at least one statement + * @param keys how variables are named. Every key not ending with 'Label' should appear in at least one statement * @param statements * @constructor */ @@ -392,9 +399,9 @@ export default class Wikidata { statements.map((stmt) => (stmt.endsWith(".") ? stmt : stmt + ".")).join("\n") + ' SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }\n' + "}" - const url = wds.sparqlQuery(query) + const url = Wikidata.wds.sparqlQuery(query) const result = await Utils.downloadJsonCached(url, 24 * 60 * 60 * 1000) - return result.results.bindings + return result.results.bindings } private static _cache = new Map>()