Commit 252efeb6 authored by Florian Schäfer's avatar Florian Schäfer

Add API query to get claims for a Wikdiata item

parent 9e15c0f5
package org.wikipedia.api;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.util.Objects;
public abstract class CustomDeserializer<T> extends StdDeserializer<T> {
private final ObjectMapper mapper;
protected CustomDeserializer(final ObjectMapper mapper) {
super((Class<?>) null);
this.mapper = Objects.requireNonNull(mapper);
}
@Override
public final T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return deserialize(p.getCodec().readTree(p), mapper);
}
public abstract T deserialize(final JsonNode node, final ObjectMapper mapper) throws JsonProcessingException;
}
......@@ -12,6 +12,7 @@ import org.wikipedia.api.ApiQuery;
import org.wikipedia.api.ApiUrl;
import org.wikipedia.api.SerializationSchema;
import org.wikipedia.api.wikidata_action.json.SitematrixResult;
import org.wikipedia.api.wikidata_action.json.WbgetclaimsResult;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
import org.wikipedia.tools.RegexUtil;
......@@ -87,6 +88,16 @@ public final class WikidataActionApiQuery<T> extends ApiQuery<T> {
);
}
public static WikidataActionApiQuery<WbgetclaimsResult> wbgetclaims(final String qId) {
if (!RegexUtil.isValidQId(qId)) {
throw new IllegalArgumentException("Invalid Q-ID: " + qId);
}
return new WikidataActionApiQuery<>(
FORMAT_PARAMS + "&action=wbgetclaims&props=&entity=" + qId,
WbgetclaimsResult.SCHEMA
);
}
@Override
public String getCacheKey() {
return getUrl().toString() + '?' + getQueryString();
......
......@@ -4,6 +4,7 @@ package org.wikipedia.tools;
import java.util.regex.Pattern;
public class RegexUtil {
private static final Pattern PROPERTY_ID_PATTERN = Pattern.compile("^P[1-9][0-9]*$");
private static final Pattern Q_ID_PATTERN = Pattern.compile("^Q[1-9][0-9]*$");
private static final Pattern SITE_ID_PATTERN = Pattern.compile("^[a-z][a-z][a-z]?wiki$");
public static final Pattern WIKIPEDIA_TAG_VALUE_PATTERN = Pattern.compile("([a-z][a-z][a-z]?):(.+)");
......@@ -14,6 +15,10 @@ public class RegexUtil {
// Private constructor to avoid instantiation
}
public static boolean isValidPropertyId(final String value) {
return value != null && PROPERTY_ID_PATTERN.matcher(value).matches();
}
public static boolean isValidQId(final String value) {
return value != null && Q_ID_PATTERN.matcher(value).matches();
}
......
package org.wikipedia.api.wikidata_action;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Test;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.json.WbgetclaimsResult;
import org.wikipedia.testutils.ResourceFileLoader;
public class WbgetclaimsTest extends AbstractWikidataActionApiTest {
@Test
public void test() throws IOException, URISyntaxException {
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetclaims/Q2.json"));
final WbgetclaimsResult result = ApiQueryClient.query(WikidataActionApiQuery.wbgetclaims("Q2"));
assertEquals(216, result.getClaims().size()); // Total number of claims
assertEquals(WbgetclaimsResult.Claim.RANK.PREFERRED, result.getClaims().stream().filter(it -> "Q2$9bbb058a-48ab-0a81-e88e-82bba6b2abe5".equals(it.getId())).findFirst().get().getRank());
assertEquals("P31", result.getClaims().stream().filter(it -> "Q2$50fad68d-4f91-f878-6f29-e655af54690e".equals(it.getId())).findFirst().get().getMainSnak().getProperty());
final Map<String, List<WbgetclaimsResult.Claim>> claimMap = result.getClaims().stream().collect(Collectors.groupingBy(it -> it.getMainSnak().getProperty()));
assertEquals(97, claimMap.keySet().size()); // Number of different properties for which claims exist
simpleRequestVerify("format=json&utf8=1&formatversion=1&action=wbgetclaims&props=&entity=Q2");
}
}
......@@ -44,9 +44,15 @@ public class WikidataActionApiQueryTest extends AbstractWikidataActionApiTest {
@Test
public void testApiName() {
assertEquals("Wikidata Action API", WikidataActionApiQuery.wbgetclaims("Q1").getApiName());
assertEquals("Wikidata Action API", WikidataActionApiQuery.sitematrix().getApiName());
}
@Test(expected = IllegalArgumentException.class)
public void testWbgetclaims_invalidQid() {
WikidataActionApiQuery.wbgetclaims("123");
}
@Test
public void testWbgetentitiesQuery() {
assertEquals(
......
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