Commit 6ac45e9a authored by Florian Schäfer's avatar Florian Schäfer

Add API query to retrieve all labels for a Wikidata item

parent 6089a4eb
......@@ -8,8 +8,8 @@ import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.api.ApiQuery;
import org.wikipedia.api.ApiUrl;
import org.wikipedia.api.SerializationSchema;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
import org.wikipedia.api.wikidata_action.json.SitematrixResult;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
import org.wikipedia.tools.RegexUtil;
public final class WikidataActionApiQuery<T> extends ApiQuery<T> {
......@@ -68,6 +68,13 @@ public final class WikidataActionApiQuery<T> extends ApiQuery<T> {
);
}
public static WikidataActionApiQuery<WbgetentitiesResult> wbgetentitiesLabels(final String qId) {
if (!RegexUtil.isValidQId(qId)) {
throw new IllegalArgumentException("Invalid Q-ID: " + qId);
}
return new WikidataActionApiQuery<>(FORMAT_PARAMS + "&action=wbgetentities&props=labels&ids=" + qId, WbgetentitiesResult.SCHEMA);
}
@Override
public String getCacheKey() {
return getUrl().toString() + '?' + getQuery();
......
......@@ -16,8 +16,10 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.wikipedia.api.SerializationSchema;
public final class WbgetentitiesResult {
......@@ -136,18 +138,23 @@ public final class WbgetentitiesResult {
private final String id;
private final String type;
private final Map<String, Sitelink> sitelinks = new HashMap<>();
private final Set<Label> labels = new HashSet<>();
@JsonCreator
public Entity(
@JsonProperty("id") final String id,
@JsonProperty("type") final String type,
@JsonProperty("sitelinks") final Map<String, Sitelink> sitelinks
@JsonProperty("sitelinks") final Map<String, Sitelink> sitelinks,
@JsonProperty("labels") final Map<String, Label> labels
) {
this.id = id;
this.type = type;
if (sitelinks != null) {
this.sitelinks.putAll(sitelinks);
}
if (labels != null) {
this.labels.addAll(labels.values());
}
}
public String getId() {
......@@ -158,6 +165,10 @@ public final class WbgetentitiesResult {
return type;
}
public Collection<Label> getLabels() {
return labels;
}
public Collection<Sitelink> getSitelinks() {
return Collections.unmodifiableCollection(sitelinks.values());
}
......@@ -167,6 +178,25 @@ public final class WbgetentitiesResult {
result.entities.put(key, this);
}
public static final class Label {
private final String language;
private final String value;
@JsonCreator
public Label(@JsonProperty("language") final String language, @JsonProperty("value") final String value) {
this.language = language;
this.value = value;
}
public String getLanguage() {
return language;
}
public String getValue() {
return value;
}
}
public static final class Sitelink {
private final String site;
private final String title;
......
{"entities":{"Q42":{"type":"item","id":"Q42","labels":{"fr":{"language":"fr","value":"Douglas Adams"},"ru":{"language":"ru","value":"\u0414\u0443\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"pl":{"language":"pl","value":"Douglas Adams"},"it":{"language":"it","value":"Douglas Adams"},"en-gb":{"language":"en-gb","value":"Douglas Adams"},"nb":{"language":"nb","value":"Douglas Adams"},"es":{"language":"es","value":"Douglas Adams"},"en-ca":{"language":"en-ca","value":"Douglas Adams"},"hr":{"language":"hr","value":"Douglas Adams"},"pt":{"language":"pt","value":"Douglas Adams"},"ko":{"language":"ko","value":"\ub354\uae00\ub7ec\uc2a4 \uc560\ub364\uc2a4"},"nl":{"language":"nl","value":"Douglas Adams"},"el":{"language":"el","value":"\u039d\u03c4\u03ac\u03b3\u03ba\u03bb\u03b1\u03c2 \u0386\u03bd\u03c4\u03b1\u03bc\u03c2"},"ar":{"language":"ar","value":"\u062f\u0648\u063a\u0644\u0627\u0633 \u0622\u062f\u0645\u0632"},"arz":{"language":"arz","value":"\u062f\u0648\u062c\u0644\u0627\u0633 \u0627\u062f\u0627\u0645\u0632"},"bar":{"language":"bar","value":"Douglas Adams"},"be":{"language":"be","value":"\u0414\u0443\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"bg":{"language":"bg","value":"\u0414\u044a\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"bs":{"language":"bs","value":"Douglas Adams"},"ca":{"language":"ca","value":"Douglas Adams"},"cs":{"language":"cs","value":"Douglas Adams"},"cy":{"language":"cy","value":"Douglas Adams"},"da":{"language":"da","value":"Douglas Adams"},"eo":{"language":"eo","value":"Douglas Adams"},"et":{"language":"et","value":"Douglas Adams"},"fa":{"language":"fa","value":"\u062f\u0627\u06af\u0644\u0627\u0633 \u0622\u062f\u0627\u0645\u0632"},"fi":{"language":"fi","value":"Douglas Adams"},"ga":{"language":"ga","value":"Douglas Adams"},"gl":{"language":"gl","value":"Douglas Adams"},"he":{"language":"he","value":"\u05d3\u05d0\u05d2\u05dc\u05e1 \u05d0\u05d3\u05de\u05e1"},"hu":{"language":"hu","value":"Douglas Adams"},"id":{"language":"id","value":"Douglas Adams"},"io":{"language":"io","value":"Douglas Adams"},"is":{"language":"is","value":"Douglas Adams"},"ja":{"language":"ja","value":"\u30c0\u30b0\u30e9\u30b9\u30fb\u30a2\u30c0\u30e0\u30ba"},"jv":{"language":"jv","value":"Douglas Adams"},"ka":{"language":"ka","value":"\u10d3\u10d0\u10d2\u10da\u10d0\u10e1 \u10d0\u10d3\u10d0\u10db\u10e1\u10d8"},"la":{"language":"la","value":"Duglassius Adams"},"lv":{"language":"lv","value":"Duglass Adamss"},"mk":{"language":"mk","value":"\u0414\u0430\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"mr":{"language":"mr","value":"\u0921\u0917\u094d\u0932\u0938 \u0905\u0945\u0921\u092e\u094d\u0938"},"nn":{"language":"nn","value":"Douglas Adams"},"ro":{"language":"ro","value":"Douglas Adams"},"sco":{"language":"sco","value":"Douglas Adams"},"sh":{"language":"sh","value":"Douglas Adams"},"sk":{"language":"sk","value":"Douglas Adams"},"sl":{"language":"sl","value":"Douglas Adams"},"sq":{"language":"sq","value":"Douglas Adams"},"sr":{"language":"sr","value":"\u0414\u0430\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"sv":{"language":"sv","value":"Douglas Adams"},"ta":{"language":"ta","value":"\u0b9f\u0b95\u0bcd\u0bb3\u0bb8\u0bcd \u0b86\u0b9f\u0bae\u0bcd\u0bb8\u0bcd"},"tr":{"language":"tr","value":"Douglas Adams"},"uk":{"language":"uk","value":"\u0414\u0443\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"vi":{"language":"vi","value":"Douglas Adams"},"zh":{"language":"zh","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9a\u5f53\u65af"},"zh-cn":{"language":"zh-cn","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9a\u5f53\u65af"},"zh-hans":{"language":"zh-hans","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9a\u5f53\u65af"},"zh-hant":{"language":"zh-hant","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9e\u7576\u65af"},"de-ch":{"language":"de-ch","value":"Douglas Adams"},"pt-br":{"language":"pt-br","value":"Douglas Adams"},"zh-sg":{"language":"zh-sg","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9a\u5f53\u65af"},"zh-my":{"language":"zh-my","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9a\u5f53\u65af"},"zh-hk":{"language":"zh-hk","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9e\u7576\u65af"},"zh-tw":{"language":"zh-tw","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9e\u7576\u65af"},"zh-mo":{"language":"zh-mo","value":"\u9053\u683c\u62c9\u65af\u00b7\u4e9e\u7576\u65af"},"war":{"language":"war","value":"Douglas Adams"},"be-tarask":{"language":"be-tarask","value":"\u0414\u0443\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0437"},"vep":{"language":"vep","value":"Adams Duglas"},"ur":{"language":"ur","value":"\u0688\u06af\u0644\u0633 \u0627\u06cc\u0688\u0645"},"oc":{"language":"oc","value":"Douglas Adams"},"af":{"language":"af","value":"Douglas Adams"},"an":{"language":"an","value":"Douglas Adams"},"br":{"language":"br","value":"Douglas Adams"},"eu":{"language":"eu","value":"Douglas Adams"},"lb":{"language":"lb","value":"Douglas Adams"},"lmo":{"language":"lmo","value":"Douglas Adams"},"lt":{"language":"lt","value":"Douglas Adams"},"nds":{"language":"nds","value":"Douglas Adams"},"nds-nl":{"language":"nds-nl","value":"Douglas Adams"},"pms":{"language":"pms","value":"Douglas Adams"},"vec":{"language":"vec","value":"Douglas Adams"},"wa":{"language":"wa","value":"Douglas Adams"},"sr-ec":{"language":"sr-ec","value":"\u0414\u0430\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"},"sr-el":{"language":"sr-el","value":"Daglas Adams"},"de":{"language":"de","value":"Douglas Adams"},"ckb":{"language":"ckb","value":"\u062f\u06d5\u06af\u0644\u0627\u0633 \u0626\u0627\u062f\u0645\u0632"},"fo":{"language":"fo","value":"Douglas Adams"},"kl":{"language":"kl","value":"Douglas Adams"},"gsw":{"language":"gsw","value":"Douglas Adams"},"te":{"language":"te","value":"\u0c21\u0c17\u0c4d\u0c32\u0c38\u0c4d \u0c06\u0c21\u0c2e\u0c4d\u0c38\u0c4d"},"si":{"language":"si","value":"\u0da9\u0d9c\u0dca\u0dbd\u0dc3\u0dca \u0d87\u0da9\u0db8\u0dca\u0dc3\u0dca"},"bn":{"language":"bn","value":"\u09a1\u0997\u09b2\u09be\u09b8 \u0985\u09cd\u09af\u09be\u09a1\u09be\u09ae\u09b8"},"hi":{"language":"hi","value":"\u0921\u0917\u094d\u0932\u0938 \u0905\u200d\u0921\u092e\u094d\u0938"},"rwr":{"language":"rwr","value":"\u0921\u0917\u094d\u0932\u0938 \u0905\u200d\u0921\u092e\u094d\u0938"},"mg":{"language":"mg","value":"Douglas Adams"},"ml":{"language":"ml","value":"\u0d21\u0d17\u0d4d\u0d32\u0d38\u0d4d \u0d06\u0d21\u0d02\u0d38\u0d4d"},"gu":{"language":"gu","value":"\u0aa1\u0a97\u0acd\u0ab2\u0abe\u0ab8 \u0a8f\u0aa1\u0aae\u0acd\u0ab8"},"hy":{"language":"hy","value":"\u0534\u0578\u0582\u0563\u056c\u0561\u057d \u0531\u0564\u0561\u0574\u057d"},"ast":{"language":"ast","value":"Douglas Adams"},"co":{"language":"co","value":"Douglas Adams"},"de-at":{"language":"de-at","value":"Douglas Adams"},"frp":{"language":"frp","value":"Douglas Adams"},"fur":{"language":"fur","value":"Douglas Adams"},"gd":{"language":"gd","value":"Douglas Adams"},"ia":{"language":"ia","value":"Douglas Adams"},"ie":{"language":"ie","value":"Douglas Adams"},"kg":{"language":"kg","value":"Douglas Adams"},"li":{"language":"li","value":"Douglas Adams"},"lij":{"language":"lij","value":"Douglas Adams"},"min":{"language":"min","value":"Douglas Adams"},"ms":{"language":"ms","value":"Douglas Adams"},"nap":{"language":"nap","value":"Douglas Adams"},"nrm":{"language":"nrm","value":"Douglas Adams"},"pcd":{"language":"pcd","value":"Douglas Adams"},"rm":{"language":"rm","value":"Douglas Adams"},"sc":{"language":"sc","value":"Douglas Adams"},"scn":{"language":"scn","value":"Douglas Adams"},"sw":{"language":"sw","value":"Douglas Adams"},"vls":{"language":"vls","value":"Douglas Adams"},"vo":{"language":"vo","value":"Douglas Adams"},"wo":{"language":"wo","value":"Douglas Adams"},"zu":{"language":"zu","value":"Douglas Adams"},"az":{"language":"az","value":"Duqlas Noel Adams"},"ak":{"language":"ak","value":"Doglas Adams"},"or":{"language":"or","value":"\u0b21\u0b17\u0b4d\u200c\u0b32\u0b3e\u0b38\u0b4d\u200c \u0b06\u0b26\u0b3e\u0b2e\u0b4d\u200c\u0b38"},"kn":{"language":"kn","value":"\u0ca1\u0c97\u0ccd\u0cb2\u0cb8\u0ccd \u0c86\u0ca1\u0cae\u0ccd\u0cb8\u0ccd"},"ne":{"language":"ne","value":"\u0921\u0917\u0932\u0938 \u090f\u0921\u092e\u094d\u0938"},"mrj":{"language":"mrj","value":"\u0410\u0434\u0430\u043c\u0441"},"th":{"language":"th","value":"\u0e14\u0e31\u0e4a\u0e01\u0e25\u0e32\u0e2a \u0e2d\u0e14\u0e31\u0e21\u0e2a\u0e4c"},"pa":{"language":"pa","value":"\u0a21\u0a17\u0a32\u0a38 \u0a10\u0a21\u0a2e\u0a1c\u0a3c"},"tcy":{"language":"tcy","value":"\u0ca1\u0cbe\u0c97\u0ccd\u0cb2\u0cb8\u0ccd \u0c86\u0ca1\u0cae\u0ccd\u0cb8\u0ccd"},"tl":{"language":"tl","value":"Douglas Adams"},"ext":{"language":"ext","value":"Douglas Adams"},"azb":{"language":"azb","value":"\u062f\u0627\u0642\u0644\u0627\u0633 \u0622\u062f\u0627\u0645\u0632"},"en":{"language":"en","value":"Douglas Adams"},"lfn":{"language":"lfn","value":"Douglas Adams"},"nan":{"language":"nan","value":"Douglas Adams"},"ky":{"language":"ky","value":"\u0414\u0443\u0433\u043b\u0430\u0441 \u0410\u0434\u0430\u043c\u0441"}}}},"success":1}
\ No newline at end of file
......@@ -151,6 +151,33 @@ public class WikidataActionApiQueryTest {
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=wbgetentities&props=sitelinks&sites=enwiki&sitefilter=enwiki&titles=United+States%7Cmissing-article%7CGreat+Britain%7CAnother+missing+article")));
}
@Test
public void testWikidataItemLabelQuery() throws IOException, URISyntaxException {
stubFor(post("/")
.withHeader("Accept", equalTo("application/json"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/labels_Q42.json"))
)
);
final WbgetentitiesResult result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels("Q42"));
assertEquals(1, result.getEntities().size());
assertEquals(138, result.getEntities().entrySet().iterator().next().getValue().getLabels().size());
assertEquals(0, result.getEntities().entrySet().iterator().next().getValue().getSitelinks().size());
assertEquals("Q42", result.getEntities().entrySet().iterator().next().getValue().getId());
assertEquals("item", result.getEntities().entrySet().iterator().next().getValue().getType());
assertEquals("Douglas Adams", result.getEntities().entrySet().iterator().next().getValue().getLabels().stream().filter(it -> "en".equals(it.getLanguage())).findAny().get().getValue());
assertEquals("Дуглас Адамс", result.getEntities().entrySet().iterator().next().getValue().getLabels().stream().filter(it -> "ru".equals(it.getLanguage())).findAny().get().getValue());
assertEquals("더글러스 애덤스", result.getEntities().entrySet().iterator().next().getValue().getLabels().stream().filter(it -> "ko".equals(it.getLanguage())).findAny().get().getValue());
assertEquals("ಡಾಗ್ಲಸ್ ಆಡಮ್ಸ್", result.getEntities().entrySet().iterator().next().getValue().getLabels().stream().filter(it -> "tcy".equals(it.getLanguage())).findAny().get().getValue());
verify(postRequestedFor(urlEqualTo("/")).withRequestBody(new EqualToPattern("format=json&utf8=1&formatversion=1&action=wbgetentities&props=labels&ids=Q42")));
}
/**
* Sets {@link WikidataActionApiQuery#defaultUrl} to the supplied URL
* @param url the new URL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment