-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a simple darft for Wikidata APi requests
Task: #9
- Loading branch information
Showing
6 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/net/sourceforge/jwbf/mediawiki/contentRep/MediaWikiArticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/test/java/net/sourceforge/jwbf/mediawiki/wikidata/WikiDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
fer-rum
|
||
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); | ||
} | ||
|
||
|
||
} | ||
|
||
} |
How to transform this json to a org.wikidata.wdtk.datamodel.implementation.ClaimImpl ?