Repository: Wikidata/Wikidata-Toolkit (java)

Issue:
IndexOutOfBoundsException when fetching a MediaInfoDocument for a page where structured data has never been edited
Consider a Wikimedia Commons file whose structured data has never been edited. It still has a pageid, so we can still construct its Mid, but fetching it with `wbgetentities` will return it as missing:

https://commons.wikimedia.org/w/api.php?action=wbgetentities&ids=M91629437
```
{"entities":
   {"M91629437":
      {"id":"M91629437","missing":""}
   }
}
```

When performing this query with Wikidata-Toolkit (via `WikibaseDataFetcher.getEntityDocuments()` one currently obtains this:

```
Exception in thread "Thread-3" java.lang.IndexOutOfBoundsException: Index: 0
	at java.base/java.util.Collections$EmptyList.get(Collections.java:4483)
	at org.wikidata.wdtk.wikibaseapi.WbGetEntitiesAction.wbGetEntities(WbGetEntitiesAction.java:239)
	at org.wikidata.wdtk.wikibaseapi.WbGetEntitiesAction.wbGetEntities(WbGetEntitiesAction.java:108)
	at org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher.getEntityDocumentMap(WikibaseDataFetcher.java:404)
	at org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher.getEntityDocuments(WikibaseDataFetcher.java:187)
```

Pull request description:
Fix fetching of non-existent Mids on Commons.
Closes #643.

Patch:
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesAction.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesAction.java
index 4378f416..3ccd4339 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesAction.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesAction.java
@@ -236,7 +236,14 @@ public class WbGetEntitiesAction {
 					EntityIdValue entityIdValue = EntityIdValueImpl.fromId(entityNode.get("id").asText(), siteIri);
 					if(entityIdValue instanceof MediaInfoIdValue) {
 						//TODO: bad hack, it would be much nicer if the API would return the page title
-						result.put(titlesList.get(i), Datamodel.makeMediaInfoDocument((MediaInfoIdValue) entityIdValue));
+                                                MediaInfoDocument emptyDocument = Datamodel.makeMediaInfoDocument((MediaInfoIdValue) entityIdValue);
+                                                String key = null;
+                                                if (titles != null) {
+                                                        key = titlesList.get(i);
+                                                } else {
+                                                        key = entityIdValue.getId();
+                                                }
+                                                result.put(key, emptyDocument);
 					}
 				} catch (IllegalArgumentException e) {
 					throw new MalformedResponseException(


Test patch:
diff --git a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesActionTest.java b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesActionTest.java
index 1a1dcd63..4702cc4c 100644
--- a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesActionTest.java
+++ b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesActionTest.java
@@ -61,6 +61,13 @@ public class WbGetEntitiesActionTest {
 		params.put("sitefilter", "enwiki");
 		this.con.setWebResourceFromPath(params, getClass(),
 				"/wbgetentities-Q6-Q42-P31.json", CompressionType.NONE);
+		
+		params.clear();
+		params.put("action", "wbgetentities");
+		params.put("format", "json");
+		params.put("ids", "M91629437");
+		this.con.setWebResourceFromPath(params, getClass(),
+				"/wbgetentities-missing-mid.json", CompressionType.NONE);
 
 		this.action = new WbGetEntitiesAction(this.con, Datamodel.SITE_WIKIDATA);
 
@@ -142,5 +149,11 @@ public class WbGetEntitiesActionTest {
 	public void testNoTitlesOrIds() throws MediaWikiApiErrorException, IOException {
 		assertThrows(IllegalArgumentException.class, () -> action.wbGetEntities(null, "enwiki", null, null, null, null));
 	}
+	
+	// for https://github.com/Wikidata/Wikidata-Toolkit/issues/643
+	@Test
+	public void testMissingMid() throws MediaWikiApiErrorException, IOException {
+		action.wbGetEntities("M91629437", null, null, null, null, null);
+	}
 
 }
diff --git a/wdtk-wikibaseapi/src/test/resources/wbgetentities-missing-mid.json b/wdtk-wikibaseapi/src/test/resources/wbgetentities-missing-mid.json
new file mode 100644
index 00000000..7b0a823a
--- /dev/null
+++ b/wdtk-wikibaseapi/src/test/resources/wbgetentities-missing-mid.json
@@ -0,0 +1,5 @@
+{"entities":
+   {"M91629437":
+      {"id":"M91629437","missing":""}
+   }
+}