Commit 3f2fba90 authored by Florian Schäfer's avatar Florian Schäfer

Show also aliases and descriptions, additionally to labels

parent 5a2f8ce1
Pipeline #27739370 passed with stages
in 6 minutes and 51 seconds
......@@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.openstreetmap.josm.tools.HttpClient;
......@@ -115,15 +116,15 @@ public final class WikidataActionApiQuery<T> extends ApiQuery<T> {
);
}
public static WikidataActionApiQuery<Map<String, WbgetentitiesResult.Entity.Label>> wbgetentitiesLabels(final String qId) {
public static WikidataActionApiQuery<Optional<WbgetentitiesResult.Entity>> wbgetentitiesLabels(final String qId) {
if (!RegexUtil.isValidQId(qId)) {
throw new IllegalArgumentException("Invalid Q-ID: " + qId);
}
return new WikidataActionApiQuery<>(
FORMAT_PARAMS + "&action=wbgetentities&props=labels&ids=" + qId,
FORMAT_PARAMS + "&action=wbgetentities&props=labels|descriptions|aliases&ids=" + qId,
WbgetentitiesResult.SCHEMA,
TimeUnit.MINUTES.toMillis(10),
result -> result.getEntities().values().stream().findFirst().map(WbgetentitiesResult.Entity::getLabels).orElse(new HashMap<>())
result -> result.getEntities().values().stream().findFirst()
);
}
......
......@@ -18,7 +18,6 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import org.wikipedia.api.SerializationSchema;
import org.wikipedia.tools.RegexUtil;
......
......@@ -18,6 +18,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.wikipedia.api.SerializationSchema;
public final class WbgetentitiesResult {
......@@ -135,23 +136,38 @@ public final class WbgetentitiesResult {
public static final class Entity implements AbstractEntity {
private final String id;
private final String type;
private final Map<String, Collection<String>> aliases;
private final Map<String, Sitelink> sitelinks = new HashMap<>();
private final Map<String, Label> labels = new HashMap<>();
private final Map<String, String> descriptions;
private final Map<String, String> labels;
@JsonCreator
public Entity(
@JsonProperty("id") final String id,
@JsonProperty("type") final String type,
@JsonProperty("sitelinks") final Map<String, Sitelink> sitelinks,
@JsonProperty("labels") final Map<String, Label> labels
@JsonProperty("aliases") final Map<String, Collection<Label>> aliases,
@JsonProperty("descriptions") final Map<String, Label> descriptions,
@JsonProperty("labels") final Map<String, Label> labels,
@JsonProperty("sitelinks") final Map<String, Sitelink> sitelinks
) {
this.id = id;
this.type = type;
if (sitelinks != null) {
this.sitelinks.putAll(sitelinks);
}
this.aliases = new HashMap<>(aliases == null ? 0 : aliases.size());
this.descriptions = new HashMap<>(descriptions == null ? 0 : descriptions.size());
this.labels = new HashMap<>(labels == null ? 0 : labels.size());
if (aliases != null) {
aliases.forEach((lang, alias) -> this.aliases.put(lang, alias.stream().map(Label::getValue).collect(Collectors.toList())));
}
if (descriptions != null) {
descriptions.values().forEach(description -> this.descriptions.put(description.getLangCode(), description.getValue()));
}
if (labels != null) {
labels.values().forEach(label -> this.labels.put(label.getLangCode(), label));
labels.values().forEach(label -> this.labels.put(label.getLangCode(), label.getValue()));
}
}
......@@ -163,7 +179,15 @@ public final class WbgetentitiesResult {
return type;
}
public Map<String, Label> getLabels() {
public Map<String, Collection<String>> getAliases() {
return Collections.unmodifiableMap(aliases);
}
public Map<String, String> getDescriptions() {
return Collections.unmodifiableMap(descriptions);
}
public Map<String, String> getLabels() {
return Collections.unmodifiableMap(labels);
}
......
......@@ -4,7 +4,6 @@ import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.openstreetmap.josm.gui.Notification;
......
......@@ -3,11 +3,15 @@ package org.wikipedia.gui;
import java.awt.BorderLayout;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
......@@ -38,7 +42,7 @@ class WikidataInfoLabelPanel extends ProgressJPanel {
private static class LabelTableModel extends AbstractTableModel {
private final WikidataInfoLabelPanel parent;
private String qIdBeingDownloaded;
private final List<Pair<WbgetentitiesResult.Entity.Label, String>> valueMap = new ArrayList<>();
private final List<TableRow> rows = new ArrayList<>();
LabelTableModel(final WikidataInfoLabelPanel parent) {
this.parent = parent;
......@@ -50,26 +54,37 @@ class WikidataInfoLabelPanel extends ProgressJPanel {
new Thread(() -> {
try {
parent.showProgress(I18n.tr("Download labels for {0}…", qId));
valueMap.clear();
rows.clear();
parent.table.revalidate();
parent.revalidate();
parent.repaint();
final Map<String, WbgetentitiesResult.Entity.Label> newValues = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels(qId));
final Optional<WbgetentitiesResult.Entity> currentEntity = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels(qId));
final Map<String, String> languages = new HashMap<>();
try {
languages.putAll(ApiQueryClient.query(WikidataActionApiQuery.queryLanguages()));
} catch (IOException e) {
Logging.warn("Could not download language names! Only the language codes are displayed.", e);
}
synchronized (valueMap) {
synchronized (rows) {
if (qIdBeingDownloaded != null && qIdBeingDownloaded.equals(qId)) {
valueMap.clear();
valueMap.addAll(
newValues.values().stream()
.map(it -> Pair.create(it, languages.containsKey(it.getLangCode()) ? languages.get(it.getLangCode()) : it.getLangCode()))
.sorted(Comparator.comparing(it -> it.a.getLangCode()))
.collect(Collectors.toList())
);
rows.clear();
currentEntity.ifPresent(entity -> {
final Map<String, String> labels = entity.getLabels();
final Map<String, String> descriptions = entity.getDescriptions();
final Map<String, Collection<String>> aliases = entity.getAliases();
final Set<String> langCodes = new HashSet<>(labels.keySet());
langCodes.addAll(descriptions.keySet());
langCodes.addAll(aliases.keySet());
langCodes.stream().sorted().forEach(langCode -> {
this.rows.add(new TableRow(
langCode,
languages.get(langCode),
labels.get(langCode),
descriptions.get(langCode),
aliases.get(langCode)
));
});
});
parent.table.revalidate();
}
}
......@@ -82,21 +97,23 @@ class WikidataInfoLabelPanel extends ProgressJPanel {
@Override
public int getRowCount() {
return valueMap.size();
return rows.size();
}
@Override
public int getColumnCount() {
return 3;
return 5;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0: return valueMap.get(rowIndex).a.getLangCode();
case 1: return valueMap.get(rowIndex).b;
case 2:
default: return valueMap.get(rowIndex).a.getValue();
case 0: return rows.get(rowIndex).langCode;
case 1: return rows.get(rowIndex).language;
case 2: return rows.get(rowIndex).label;
case 3: return rows.get(rowIndex).description;
case 4:
default: return String.join(", ", rows.get(rowIndex).aliases);
}
}
......@@ -105,14 +122,32 @@ class WikidataInfoLabelPanel extends ProgressJPanel {
switch (column) {
case 0: return I18n.tr("language code");
case 1: return I18n.tr("language");
case 2:
default: return I18n.tr("label");
case 2: return I18n.tr("label");
case 3: return I18n.tr("description");
case 4:
default: return I18n.tr("aliases");
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 3;
return columnIndex >= 3;
}
private static class TableRow {
private final String langCode;
private final String language;
private final String label;
private final String description;
private final Collection<String> aliases;
TableRow(final String langCode, final String language, final String label, final String description, final Collection<String> aliases) {
this.langCode = Objects.requireNonNull(langCode);
this.language = language == null ? langCode : language;
this.label = label == null ? "" : label;
this.description = description == null ? "" : description;
this.aliases = aliases == null ? new ArrayList<>() : aliases;
}
}
}
}
package org.wikipedia.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Comparator;
import java.util.Optional;
......@@ -18,12 +16,12 @@ import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.OpenBrowser;
import org.openstreetmap.josm.tools.Utils;
import org.wikipedia.WikipediaPlugin;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.WikidataActionApiQuery;
import org.wikipedia.api.wikidata_action.json.SitematrixResult;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
import org.wikipedia.data.WikipediaSite;
public class WikidataInfoSitelinkPanel extends ProgressJPanel {
......@@ -61,7 +59,7 @@ public class WikidataInfoSitelinkPanel extends ProgressJPanel {
linkButton.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
final String uri = s.getUrl() + "/w/index.php?title=" + URLEncoder.encode(sitelink.getTitle());
final String uri = s.getUrl() + "/w/index.php?title=" + Utils.encodeUrl(sitelink.getTitle());
final String error = OpenBrowser.displayUrl(uri);
if (error != null) {
new Notification(I18n.tr("Can't open website {0} in browser! Error message: {1}", uri, error))
......
......@@ -6,15 +6,12 @@ import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
......@@ -24,11 +21,8 @@ import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
import org.openstreetmap.josm.data.osm.event.DataSetListener;
import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.OpenBrowser;
import org.wikipedia.WikipediaPlugin;
import org.wikipedia.data.WikidataEntry;
import org.wikipedia.data.WikipediaEntry;
import org.wikipedia.tools.OsmPrimitiveUtil;
......
......@@ -10,6 +10,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.junit.Test;
import org.wikipedia.api.ApiQueryClient;
import org.wikipedia.api.wikidata_action.json.WbgetentitiesResult;
......@@ -121,15 +122,15 @@ public class WikidataActionApiQueryTest extends WikidataActionApiTestAbstract {
public void testWikidataItemLabelQuery() throws IOException, URISyntaxException {
simpleJsonStub(ResourceFileLoader.getResourceBytes(WikidataActionApiQueryTest.class, "response/wbgetentities/labels_Q42.json"));
final Map<String, WbgetentitiesResult.Entity.Label> result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels("Q42"));
assertEquals(138, result.size());
final Optional<WbgetentitiesResult.Entity> result = ApiQueryClient.query(WikidataActionApiQuery.wbgetentitiesLabels("Q42"));
assertEquals(138, result.get().getLabels().size());
assertEquals("Douglas Adams", result.get("en").getValue());
assertEquals("Дуглас Адамс", result.get("ru").getValue());
assertEquals("더글러스 애덤스", result.get("ko").getValue());
assertEquals("ಡಾಗ್ಲಸ್ ಆಡಮ್ಸ್", result.get("tcy").getValue());
assertEquals("Douglas Adams", result.get().getLabels().get("en"));
assertEquals("Дуглас Адамс", result.get().getLabels().get("ru"));
assertEquals("더글러스 애덤스", result.get().getLabels().get("ko"));
assertEquals("ಡಾಗ್ಲಸ್ ಆಡಮ್ಸ್", result.get().getLabels().get("tcy"));
simpleRequestVerify("format=json&utf8=1&formatversion=1&action=wbgetentities&props=labels&ids=Q42");
simpleRequestVerify("format=json&utf8=1&formatversion=1&action=wbgetentities&props=labels|descriptions|aliases&ids=Q42");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment