Skip to content

Commit

Permalink
Added a simple darft for Wikidata APi requests
Browse files Browse the repository at this point in the history
Task: #9
  • Loading branch information
eldur committed Jun 11, 2014
1 parent 30946af commit 2069cf8
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-dumpfiles</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
<reporting>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public ApiRequestBuilder action(String action) {
return this;
}

/**
* @deprecated use json instead (you have to change response handling)
*/
@Deprecated
public ApiRequestBuilder formatXml() {
// TODO only json or xml
param("format", "xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.sourceforge.jwbf.core.actions.util.ProcessException;
import net.sourceforge.jwbf.core.bots.HttpBot;
import net.sourceforge.jwbf.core.bots.WikiBot;
import net.sourceforge.jwbf.core.contentRep.Article;
import net.sourceforge.jwbf.core.contentRep.SimpleArticle;
import net.sourceforge.jwbf.core.contentRep.Userinfo;
import net.sourceforge.jwbf.mediawiki.MediaWiki;
Expand All @@ -23,6 +22,7 @@
import net.sourceforge.jwbf.mediawiki.actions.meta.GetVersion;
import net.sourceforge.jwbf.mediawiki.actions.meta.Siteinfo;
import net.sourceforge.jwbf.mediawiki.contentRep.LoginData;
import net.sourceforge.jwbf.mediawiki.contentRep.MediaWikiArticle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -157,8 +157,8 @@ public void login(final String username, final String passwd) {
* @return a content representation of requested article, never null
* @see GetRevision
*/
public synchronized Article getArticle(final String name, final int properties) {
return new Article(this, readData(name, properties));
public synchronized MediaWikiArticle getArticle(final String name, final int properties) {
return new MediaWikiArticle(this, readData(name, properties));
}

/**
Expand All @@ -182,7 +182,7 @@ public SimpleArticle readData(String name) {
* @return a content representation of requested article, never null
* @see GetRevision
*/
public synchronized Article getArticle(final String name) {
public synchronized MediaWikiArticle getArticle(final String name) {
return getArticle(name, DEFAULT_READ_PROPERTIES);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.sourceforge.jwbf.mediawiki.contentRep;

import net.sourceforge.jwbf.core.bots.WikiBot;
import net.sourceforge.jwbf.core.contentRep.Article;
import net.sourceforge.jwbf.core.contentRep.SimpleArticle;

public class MediaWikiArticle extends Article {

public MediaWikiArticle(WikiBot bot, SimpleArticle sa) {
super(bot, sa);
}

public String getWikiDataEntity() {
return "Q1055";
}
}
13 changes: 7 additions & 6 deletions src/test/java/net/sourceforge/jwbf/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -87,13 +86,15 @@ public static void assumeReachable(URL url) {

}

public static void assumeReachable(String... urls) {
for (String url:urls) {
assumeReachable(url);
}
}

public static void assumeReachable(String url) {
try {
Assume.assumeFalse(url.trim().isEmpty());
assumeReachable(new URL(url));
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
assumeReachable(JWBF.newURL(url));
}

public static String anyWikiResponse(String filename) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package net.sourceforge.jwbf.mediawiki.wikidata;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.common.collect.ImmutableList;
import net.sourceforge.jwbf.TestHelper;
import net.sourceforge.jwbf.core.actions.Get;
import net.sourceforge.jwbf.core.actions.util.HttpAction;
import net.sourceforge.jwbf.mapper.JsonMapper;
import net.sourceforge.jwbf.mediawiki.ApiRequestBuilder;
import net.sourceforge.jwbf.mediawiki.actions.util.MWAction;
import net.sourceforge.jwbf.mediawiki.bots.MediaWikiBot;
import net.sourceforge.jwbf.mediawiki.contentRep.MediaWikiArticle;
import org.junit.Test;
import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;

public class WikiDataTest {

@Test
public void test() {
// GIVEN
// TODO do not work with a live system
String wikidataApiLiveUrl = "https://www.wikidata.org/w/api.php";
String enWikipediaLiveUrl = "http://en.wikipedia.org/w/api.php";
TestHelper.assumeReachable(wikidataApiLiveUrl, enWikipediaLiveUrl); // will skip the test if not
MediaWikiBot wikidataBot = new MediaWikiBot(wikidataApiLiveUrl);
MediaWikiBot enWikiBot = new MediaWikiBot(enWikipediaLiveUrl);


// WHEN
// http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Hamburg&format=jsonfm&
// prop=pageprops&ppprop=wikibase_item
MediaWikiArticle article = enWikiBot.getArticle("Hamburg");
String entity = article.getWikiDataEntity();
GetClaims getClaims = wikidataBot.getPerformedAction(new GetClaims(entity));

// THEN
assertEquals("Q1055", entity);
// TODO better assertions
assertEquals("must fail", getClaims.get().toString());
assertEquals("{\"claims\":", getClaims.getResult().substring(0, 10));
}

// TODO extract to file
private static class GetClaims extends MWAction {

private final Get getClaim;
private String result = "";

public GetClaims(String entity) {
getClaim = new ApiRequestBuilder() //
.action("wbgetclaims") //
.formatJson() //
.param("entity", entity) //
.buildGet();

}

@Override
public String processAllReturningText(String text) {
result = text;
return "doNotCallThis";
}

// TODO this have to be a complex type, not a string
public String getResult() {
return result;
}

public Claim get() {
return new JsonMapper().get(result, ClaimTransport.class).toClaim();
}

@Override
public HttpAction getNextMessage() {
return getClaim;
}

}

private static class ClaimTransport {

static DataObjectFactoryImpl dataObjectFactory = new DataObjectFactoryImpl();
private final String pId;
private final String qId;
private final String baseIri;
private final List<SnakGroup> qualifiers;

ClaimTransport(String pId, String qId, String baseIri, List<SnakGroup> qualifiers) {
this.pId = pId;
this.qId = qId;
this.baseIri = baseIri;
this.qualifiers = qualifiers;
}

@JsonCreator
private static ClaimTransport newClaimTransport(Map<String, Object> data) {

// {"claims":{"P31":[{"id":"q1055$536549ED-F2DD-4B8E-8C1D-B9212956DF3E","mainsnak":{"snaktype":"value",
// "property":"P31","datatype":"wikibase-item","datavalue":{"value":{"entity-type":"item","numeric-id":1221156},
// "type":"wikibase-entityid"}},"type":"statement","rank":"normal"}, ...

This comment has been minimized.

Copy link
@eldur

eldur Jun 11, 2014

Author Owner

How to transform this json to a org.wikidata.wdtk.datamodel.implementation.ClaimImpl ?

This comment has been minimized.

Copy link
@fer-rum

fer-rum Jun 11, 2014

You could use the JsonConverter class from wdtk-dumpfiles for that. Set it up with a DataObjectFactory implementation and a give base IRI and you should be good to go.

// assuming you have your JSON as String
String myJson = "{\"claims\":…";
// setting a base IRI, you probably have some already
String baseIRI = "org.wikidata";
// create a converter
JsonConverter myJsonConverter = new JsonConverter(baseIRI, new DataObjectFactoryImpl());
// get the appropriate repesentation in the wdtk-datamodel
ItemDocument myItem = myJsonConverter.convertToItemDocument(new JSONObject(myJson));
// get the claim
List<StatementGroup> statementGroups = myItem.getStatementGroups();
List<Claim> claims = new LinkedList<>():
for(StatementGroup group : statementGroups){
    for(Statement s : group.getStatements()){
        claims.add(s.getClaim());
    }
}
// now you got a liist of claims

A few side notes:

  • The JsonConverter employs really old org.json. I apologize about that. An update to jackson is planned but not for the nearest future.
  • There are two JsonConverter classes althpough in different packages. Apologies too, we are aware of that and intend to refactor them to more meaningful names.
  • The code I outlined is neither optimized nor beautyful nor anything, but it will hopefully give you the idea how it is supposed to work. I hacked it from memory so I might have messed up something. Please feel free to ask if something is screwed.
Map<String, List<Map<String, Object>>> claims = (Map<String, List<Map<String, Object>>>) data.get("claims");


List<SnakGroup> qualifiers = ImmutableList.of();
String pId = "P1";
String qId = "Q123";
String baseIri = "iri";

return new ClaimTransport(pId, qId, baseIri, qualifiers);
}

public Claim toClaim() {
PropertyIdValue propertiyId = dataObjectFactory.getPropertyIdValue(pId, baseIri);
Snak mainSnack = dataObjectFactory.getNoValueSnak(propertiyId);
EntityIdValue subject = dataObjectFactory.getItemIdValue(qId, baseIri);
return dataObjectFactory.getClaim(subject, mainSnack, qualifiers);
}


}

}

0 comments on commit 2069cf8

Please sign in to comment.