Wikidata / Wikidata-Toolkit

Java library to interact with Wikibase
https://www.mediawiki.org/wiki/Wikidata_Toolkit
Apache License 2.0
375 stars 100 forks source link

WbGetEntitiesAction cannot search media-info for titles containing "-" #777

Closed don-vip closed 1 year ago

don-vip commented 1 year ago

This code fails to retrieve the media-info for this file:

@Test
void testGetMediaInfoDocument() throws Exception {
    assertNotNull(WikibaseDataFetcher.getWikimediaCommonsDataFetcher().getEntityDocumentByTitle(
            "commonswiki", 
            "File:IAU_2006_General_Assembly-_Result_of_the_IAU_Resolution_Votes_(iau0603d).jpg"));
}

It's because of this split, line 169:

List<String> titlesList = titles == null ? Collections.emptyList() : Arrays.asList(titles.split("-"));

As the file name contains a dash, the map key does not match the full title.

don-vip commented 1 year ago

As a workaround I use this hack:

    static MediaInfoDocument getMediaInfoDocument(String filename) throws MediaWikiApiErrorException, IOException {
        // workaround to https://github.com/Wikidata/Wikidata-Toolkit/issues/777
        String title = "File:" + filename;
        if (fetcher.getEntityDocumentsByTitle("commonswiki", title)
                .get(title.split("-")[0]) instanceof MediaInfoDocument doc) {
            return doc;
        } else {
            throw new IllegalStateException("No commons mediaInfo found for filename " + filename);
        }
    }
wetneb commented 1 year ago

Thanks @don-vip! What a honor to have a JOSM developer in this modest repository! :)

This titles.split("-") should just be titles.split("|"). Titles cannot contain the | character and this is the one that is used to encode lists of parameters in this API. See https://commons.wikimedia.org/w/api.php?action=help&modules=wbgetentities

don-vip commented 1 year ago

Hi @wetneb, thank you! You're welcome, thank you for the quick fix 👍