Ignore:
Timestamp:
2015-12-22T21:35:16+01:00 (9 years ago)
Author:
simon04
Message:

JOSM/wikipedia: Add FetchWikidataAction - see #josm10148

This action allows to fetch Wikidata IDs using wikipedia tags.

The icon (license: public domain) is taken from
https://commons.wikimedia.org/wiki/File:Wikidata-logo-without-paddings.svg

Location:
applications/editors/josm/plugins/wikipedia/src/org/wikipedia
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r31851 r31852  
    1616import java.util.Objects;
    1717import java.util.Scanner;
     18import java.util.TreeMap;
    1819import java.util.regex.Matcher;
    1920import java.util.regex.Pattern;
     
    3233import org.openstreetmap.josm.tools.Utils.Function;
    3334import org.w3c.dom.Document;
     35import org.w3c.dom.Node;
    3436import org.w3c.dom.NodeList;
    3537
     
    164166    }
    165167
     168    /**
     169     * Returns a map mapping wikipedia articles to wikidata ids.
     170     */
     171    static Map<String, String> getWikidataForArticles(String wikipediaLang, Collection<String> articles) {
     172        try {
     173            final String url = "https://www.wikidata.org/w/api.php" +
     174                    "?action=wbgetentities" +
     175                    "&props=sitelinks" +
     176                    "&sites=" + wikipediaLang + "wiki" +
     177                    "&sitefilter=" + wikipediaLang + "wiki" +
     178                    "&format=xml" +
     179                    "&titles=" + Utils.join("|", Utils.transform(articles, new Function<String, String>() {
     180                @Override
     181                public String apply(String x) {
     182                    return Utils.encodeUrl(x);
     183                }
     184            }));
     185            Main.info("Wikipedia: GET " + url);
     186            final Map<String, String> r = new TreeMap<>();
     187            try (final InputStream in = Utils.openURL(new URL(url))) {
     188                final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
     189                final NodeList nodes = (NodeList) XPathFactory.newInstance().newXPath().compile("//entity").evaluate(xml, XPathConstants.NODESET);
     190                for (int i = 0; i < nodes.getLength(); i++) {
     191                    final Node node = nodes.item(i);
     192                    final String wikidata = (String) XPathFactory.newInstance().newXPath().compile("./@id").evaluate(node, XPathConstants.STRING);
     193                    final String wikipedia = (String) XPathFactory.newInstance().newXPath().compile("./sitelinks/sitelink/@title").evaluate(node, XPathConstants.STRING);
     194                    r.put(wikipedia, wikidata);
     195                }
     196            }
     197            return r;
     198        } catch (Exception ex) {
     199            throw new RuntimeException(ex);
     200        }
     201    }
     202
    166203    static Collection<WikipediaLangArticle> getInterwikiArticles(String wikipediaLang, String article) {
    167204        try {
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaPlugin.java

    r29778 r31852  
    1414        new WikipediaCopyTemplate();
    1515        MainMenu.add(Main.main.menu.dataMenu, new WikipediaAddNamesAction());
     16        MainMenu.add(Main.main.menu.dataMenu, new FetchWikidataAction());
    1617    }
    1718
Note: See TracChangeset for help on using the changeset viewer.