Changeset 32625 in osm for applications/editors/josm


Ignore:
Timestamp:
2016-07-10T22:56:57+02:00 (8 years ago)
Author:
simon04
Message:

JOSM/wikipedia: add search Wikidata items - #josm12039

Location:
applications/editors/josm/plugins/wikipedia
Files:
2 added
4 edited

Legend:

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

    r32624 r32625  
    121121    }
    122122
     123    static List<WikidataEntry> getWikidataEntriesForQuery(final String language, final String query) {
     124        try {
     125            final String url = "https://www.wikidata.org/w/api.php" +
     126                    "?action=wbsearchentities" +
     127                    "&language=" + language +
     128                    "&strictlanguage=false" +
     129                    "&search=" + Utils.encodeUrl(query) +
     130                    "&limit=50" +
     131                    "&format=xml";
     132            final List<WikidataEntry> r = new ArrayList<>();
     133            try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     134                final Document xml = DOCUMENT_BUILDER.parse(in);
     135                final NodeList nodes = (NodeList) X_PATH.compile("//entity").evaluate(xml, XPathConstants.NODESET);
     136                final XPathExpression xpathId = X_PATH.compile("@id");
     137                final XPathExpression xpathLabel = X_PATH.compile("@label");
     138                for (int i = 0; i < nodes.getLength(); i++) {
     139                    final Node node = nodes.item(i);
     140                    final String id = (String) xpathId.evaluate(node, XPathConstants.STRING);
     141                    final String label = (String) xpathLabel.evaluate(node, XPathConstants.STRING);
     142                    r.add(new WikidataEntry(id, label, null));
     143                }
     144            }
     145            return r;
     146        } catch (Exception ex) {
     147            throw new RuntimeException(ex);
     148        }
     149    }
     150
    123151    static List<WikipediaEntry> getEntriesFromCategory(String wikipediaLang, String category, int depth) {
    124152        try {
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaPlugin.java

    r31857 r32625  
    1515        MainMenu.add(Main.main.menu.dataMenu, new WikipediaAddNamesAction());
    1616        MainMenu.add(Main.main.menu.dataMenu, new FetchWikidataAction());
     17        MainMenu.add(Main.main.menu.dataMenu, new WikidataItemSearchDialog.Action());
    1718    }
    1819
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaToggleDialog.java

    r32624 r32625  
    5555                new SideButton(new WikipediaLoadCategoryAction()),
    5656                new SideButton(new PasteWikipediaArticlesAction()),
    57                 new SideButton(new AddWikipediaTagAction()),
     57                new SideButton(new AddWikipediaTagAction(list)),
    5858                new SideButton(new WikipediaSettingsAction(), false)));
    5959        updateTitle();
     
    6161    /** A string describing the context (use-case) for determining the dialog title */
    6262    String titleContext = null;
    63     final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2));
     63    static final StringProperty wikipediaLang = new StringProperty("wikipedia.lang", LanguageInfo.getJOSMLocaleCode().substring(0, 2));
    6464    final Set<String> articles = new HashSet<>();
    6565    final DefaultListModel<WikipediaEntry> model = new DefaultListModel<>();
     
    287287    }
    288288
    289     class AddWikipediaTagAction extends AbstractAction {
    290 
    291         public AddWikipediaTagAction() {
     289    static class AddWikipediaTagAction extends AbstractAction {
     290
     291        private final JList<WikipediaEntry> list;
     292
     293        public AddWikipediaTagAction(JList<WikipediaEntry> list) {
    292294            super(tr("Add Tag"));
     295            this.list = list;
    293296            new ImageProvider("pastetags").getResource().attachImageIcon(this, true);
    294297            putValue(SHORT_DESCRIPTION, tr("Adds a ''wikipedia'' tag corresponding to this article to the selected objects"));
     
    297300        @Override
    298301        public void actionPerformed(ActionEvent e) {
    299             if (list.getSelectedValue() != null) {
    300                 Tag tag = ((WikipediaEntry) list.getSelectedValue()).createWikipediaTag();
    301                 if (tag != null) {
    302                     final Collection<OsmPrimitive> selected = Main.getLayerManager().getEditDataSet().getSelected();
    303                     if (!GuiUtils.confirmOverwrite(tag.getKey(), tag.getValue(), selected)) {
    304                         return;
    305                     }
    306                     ChangePropertyCommand cmd = new ChangePropertyCommand(
    307                             selected,
    308                             tag.getKey(), tag.getValue());
    309                     Main.main.undoRedo.add(cmd);
    310                     Main.worker.submit(new FetchWikidataAction.Fetcher(selected));
    311                 }
    312             }
     302            addTag(list.getSelectedValue());
     303        }
     304
     305        static void addTag(WikipediaEntry entry) {
     306            if (entry == null) {
     307                return;
     308            }
     309            addTag(entry.createWikipediaTag());
     310        }
     311
     312        static void addTag(Tag tag) {
     313            if (tag == null) {
     314                return;
     315            }
     316            final Collection<OsmPrimitive> selected = Main.getLayerManager().getEditDataSet().getSelected();
     317            if (!GuiUtils.confirmOverwrite(tag.getKey(), tag.getValue(), selected)) {
     318                return;
     319            }
     320            ChangePropertyCommand cmd = new ChangePropertyCommand(
     321                    selected,
     322                    tag.getKey(), tag.getValue());
     323            Main.main.undoRedo.add(cmd);
     324            Main.worker.submit(new FetchWikidataAction.Fetcher(selected));
    313325        }
    314326    }
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r32624 r32625  
    147147            }
    148148        }));
     149    }
     150
     151    @Test
     152    public void testForQuery() throws Exception {
     153        final List<WikipediaApp.WikidataEntry> entries = WikipediaApp.getWikidataEntriesForQuery("de", "Österreich");
     154        assertThat(entries.get(0).wikipediaArticle, is("Q40"));
     155        assertThat(entries.get(0).wikipediaLang, is("wikidata"));
     156        // assertThat(entries.get(0).label, is("Österreich"));
    149157    }
    150158
Note: See TracChangeset for help on using the changeset viewer.