Changeset 18046 in josm


Ignore:
Timestamp:
2021-07-17T14:42:03+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21126 - make sure URL generated for searching Wikimedia Commons images can be converted to URI, needed for HTTP/2

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Mediawiki.java

    r17880 r18046  
    9595    public void searchGeoImages(Bounds bounds, BiConsumer<String, LatLon> imageConsumer)
    9696            throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
    97         final URL url = new URL(baseUrl +
    98                 "?format=xml" +
    99                 "&action=query" +
    100                 "&list=geosearch" +
    101                 "&gsnamespace=6" +
    102                 "&gslimit=500" +
    103                 "&gsprop=type|name" +
    104                 "&gsbbox=" + bounds.getMaxLat() + "|" + bounds.getMinLon() + "|" + bounds.getMinLat() + "|" + bounds.getMaxLon());
     97        final URL url = new URL(getGeoImagesUrl(baseUrl, bounds));
    10598        final Document document = getDocument(url);
    10699        final XPath xPath = XPathFactory.newInstance().newXPath();
     
    113106            imageConsumer.accept(title, new LatLon(lat, lon));
    114107        }
     108    }
     109
     110    /**
     111     * Returns the URL for searching geolocated images in given bounds.
     112     * @param baseUrl The wiki base URL
     113     * @param bounds the bounds of the search area
     114     * @return the URL for searching geolocated images in given bounds
     115     * @since 18046
     116     */
     117    public static String getGeoImagesUrl(String baseUrl, Bounds bounds) {
     118        String sep = Utils.encodeUrl("|");
     119        return baseUrl +
     120                "?format=xml" +
     121                "&action=query" +
     122                "&list=geosearch" +
     123                "&gsnamespace=6" +
     124                "&gslimit=500" +
     125                "&gsprop=type" + sep + "name" +
     126                "&gsbbox=" + bounds.getMaxLat() + sep + bounds.getMinLon() + sep + bounds.getMinLat() + sep + bounds.getMaxLon();
    115127    }
    116128
  • trunk/test/unit/org/openstreetmap/josm/tools/MediawikiTest.java

    r17275 r18046  
    44import static org.junit.jupiter.api.Assertions.assertEquals;
    55
     6import java.net.URI;
     7import java.net.URL;
     8
    69import org.junit.jupiter.api.Test;
     10import org.openstreetmap.josm.data.Bounds;
    711
    812/**
     
    2125                Mediawiki.getImageUrl("https://upload.wikimedia.org/wikipedia/commons/", "OpenJDK_logo.svg"));
    2226    }
     27
     28    /**
     29     * Test of {@link Mediawiki#getGeoImagesUrl}
     30     * @throws Exception never
     31     */
     32    @Test
     33    void testGeoImagesUrl() throws Exception {
     34        // See https://josm.openstreetmap.de/ticket/21126
     35        // Checks that URL can be converted to URI, needed for HTTP/2
     36        assertEquals(new URI("https://commons.wikimedia.org/w/api.php?format=xml&action=query&list=geosearch&gsnamespace=6&gslimit=500&gsprop=type%7Cname&gsbbox=48.8623665%7C2.3913497%7C48.8600879%7C2.3967605"),
     37                new URL(Mediawiki.getGeoImagesUrl("https://commons.wikimedia.org/w/api.php", new Bounds(48.8600879, 2.3913497, 48.8623665, 2.3967605))).toURI());
     38    }
    2339}
Note: See TracChangeset for help on using the changeset viewer.