Repository: Wikidata/Wikidata-Toolkit (java)

Issue:
Incorrect behavior when converting multiple non-existent filenames to mids
When giving multiple non-existent filenames to convert to Mids, `WikibaseDataFetcher.getMediaInfoIdsByFileName` does not return the expected value because the API returns a payload of the form:
```json
{"batchcomplete":"",
  "query":{
    "pages":{
         "-1":{"ns":6,"title":"File:DoesNotExist.png","missing":""},
         "-2":{"ns":6,"title":"File:DoesNotExistEither.jpg","missing":""}
}}}
```

The code assumes that non-existent filenames always get `-1` as key, but the second gets `-2` so code returns a "mid" of `M-2`.

Pull request description:
Fix fetching mids of multiple non-existent files.
Closes #745.

I would release this immediately after merge as this is the source of an important bug in OpenRefine.

Patch:
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/MediaInfoIdQueryAction.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/MediaInfoIdQueryAction.java
index bf12c912..eb19781c 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/MediaInfoIdQueryAction.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/MediaInfoIdQueryAction.java
@@ -100,7 +100,7 @@ public class MediaInfoIdQueryAction {
 			Map.Entry<String, JsonNode> page = iterator.next();
 			String pageId = page.getKey();
 			String title = page.getValue().get("title").textValue();
-			if (!"-1".equals(pageId)) { // "-1" means not found
+			if (!pageId.startsWith("-")) { // negative keys such as "-1", "-2", ... mean not found
 				midMap.put(title, Datamodel.makeMediaInfoIdValue("M" + pageId, siteIri));
 			}
 		}


Test patch:
diff --git a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataFetcherTest.java b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataFetcherTest.java
index 87097117..2846f507 100644
--- a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataFetcherTest.java
+++ b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WikibaseDataFetcherTest.java
@@ -267,6 +267,21 @@ public class WikibaseDataFetcherTest {
 		assertNull(result);
 	}
 
+	@Test
+	public void testGetMediaInfoIdNotFoundTwice() throws IOException, MediaWikiApiErrorException {
+		Map<String, String> parameters = new HashMap<>();
+		parameters.put("action", "query");
+		parameters.put("format", "json");
+		parameters.put("titles", "File:Not Found|File:Not Found Either");
+		con.setWebResourceFromPath(parameters, getClass(),
+				"/query-Not Found twice.json", CompressionType.NONE);
+
+		Map<String, MediaInfoIdValue> result = wdf.getMediaInfoIdsByFileName("Not Found", "Not Found Either");
+		assertEquals(result.size(), 2);
+		assertNull(result.get("Not Found"));
+		assertNull(result.get("Not Found Either"));
+	}
+
 	@Test
 	public void testWbGetVirtualMediaInfoEntityFromTitle() throws IOException, MediaWikiApiErrorException {
 		Map<String, String> parameters = new HashMap<>();
diff --git a/wdtk-wikibaseapi/src/test/resources/query-Not Found twice.json b/wdtk-wikibaseapi/src/test/resources/query-Not Found twice.json
new file mode 100644
index 00000000..99e9350c
--- /dev/null
+++ b/wdtk-wikibaseapi/src/test/resources/query-Not Found twice.json
@@ -0,0 +1,17 @@
+{
+  "batchcomplete": "",
+  "query": {
+    "pages": {
+      "-1": {
+        "ns": 6,
+        "title": "File:Not Found",
+        "missing": ""
+      },
+      "-2": {
+        "ns": 6,
+        "title": "File:Not Found Either",
+        "missing": ""
+      }
+    }
+  }
+}
\ No newline at end of file