From e6c78411e8c715400cd85f4ef94e6fcef24de40b Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 23 Nov 2022 09:14:40 +0100 Subject: [PATCH] Fix fetching mids of multiple non-existent files. Closes #745. (#746) --- .../wikibaseapi/MediaInfoIdQueryAction.java | 2 +- .../wikibaseapi/WikibaseDataFetcherTest.java | 15 +++++++++++++++ .../test/resources/query-Not Found twice.json | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 wdtk-wikibaseapi/src/test/resources/query-Not Found twice.json 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 bf12c912c..eb19781c7 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 Map getMediaInfoIds(List fileNames) Map.Entry 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)); } } 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 87097117f..2846f5079 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 void testGetMediaInfoIdNotFound() throws IOException, MediaWikiApiErrorEx assertNull(result); } + @Test + public void testGetMediaInfoIdNotFoundTwice() throws IOException, MediaWikiApiErrorException { + Map 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 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 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 000000000..99e9350c6 --- /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