Changeset 32891 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2016-08-30T13:26:57+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 2 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32888 r32891 36 36 import org.wikipedia.data.WikidataEntry; 37 37 import org.wikipedia.data.WikipediaEntry; 38 import org.wikipedia.data.WikipediaLangArticle;39 38 import org.wikipedia.tools.XPath; 40 39 … … 153 152 final String url = "https://tools.wmflabs.org/wiwosm/osmjson/getGeoJSON.php?action=check&lang=" + wikipediaLang; 154 153 try { 155 final String articles = entries.stream().map(i -> i. wikipediaArticle).collect(Collectors.joining(","));154 final String articles = entries.stream().map(i -> i.article).collect(Collectors.joining(",")); 156 155 final String requestBody = "articles=" + Utils.encodeUrl(articles); 157 156 try (final BufferedReader reader = HttpClient.create(new URL(url), "POST").setReasonForRequest("Wikipedia") … … 174 173 } 175 174 for (WikipediaEntry i : entries) { 176 i.setWiwosmStatus(status.get(i. wikipediaArticle));175 i.setWiwosmStatus(status.get(i.article)); 177 176 } 178 177 } … … 184 183 return Stream 185 184 .of("wikipedia", "wikipedia:" + wikipediaLang) 186 .map(key -> Wikipedia LangArticle.parseTag(key, p.get(key)))185 .map(key -> WikipediaEntry.parseTag(key, p.get(key))) 187 186 .filter(Objects::nonNull) 188 187 .filter(wp -> wikipediaLang.equals(wp.lang)) … … 267 266 "?action=wbgetentities" + 268 267 "&props=labels|descriptions" + 269 "&ids=" + entries.stream().map(x -> x. wikipediaArticle).collect(Collectors.joining("|")) +268 "&ids=" + entries.stream().map(x -> x.article).collect(Collectors.joining("|")) + 270 269 "&format=xml"; 271 270 final Collection<String> languages = new ArrayList<>(); … … 281 280 final Document xml = newDocumentBuilder().parse(in); 282 281 for (final WikipediaEntry entry : entries) { 283 final Node entity = X_PATH.evaluateNode("//entity[@id='" + entry. wikipediaArticle + "']", xml);282 final Node entity = X_PATH.evaluateNode("//entity[@id='" + entry.article + "']", xml); 284 283 if (entity == null) { 285 284 continue; 286 285 } 287 286 r.add(new WikidataEntry( 288 entry. wikipediaArticle,287 entry.article, 289 288 getFirstField(languages, "label", entity), 290 289 entry.coordinate, … … 309 308 } 310 309 311 public static Collection<Wikipedia LangArticle> getInterwikiArticles(String wikipediaLang, String article) {310 public static Collection<WikipediaEntry> getInterwikiArticles(String wikipediaLang, String article) { 312 311 try { 313 312 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" + … … 323 322 final String lang = X_PATH.evaluateString("@lang", node); 324 323 final String name = node.getTextContent(); 325 return new Wikipedia LangArticle(lang, name);324 return new WikipediaEntry(lang, name); 326 325 }).collect(Collectors.toList()); 327 326 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/actions/FetchWikidataAction.java
r32888 r32891 29 29 import org.openstreetmap.josm.tools.Utils; 30 30 import org.wikipedia.WikipediaApp; 31 import org.wikipedia.data.Wikipedia LangArticle;31 import org.wikipedia.data.WikipediaEntry; 32 32 import org.wikipedia.gui.GuiUtils; 33 33 … … 52 52 private boolean canceled = false; 53 53 private final List<Command> commands = new ArrayList<>(); 54 private final Collection<Wikipedia LangArticle> notFound = new ArrayList<>();54 private final Collection<WikipediaEntry> notFound = new ArrayList<>(); 55 55 56 56 public Fetcher(Collection<? extends OsmPrimitive> selection) { … … 85 85 final Map<String, PrimitivesWithWikipedia> r = new HashMap<>(); 86 86 for (final OsmPrimitive i : selection) { 87 final Wikipedia LangArticle tag = WikipediaLangArticle.parseTag("wikipedia", i.get("wikipedia"));87 final WikipediaEntry tag = WikipediaEntry.parseTag("wikipedia", i.get("wikipedia")); 88 88 if (tag != null) { 89 89 if (!r.containsKey(tag.lang)) { … … 114 114 final MultiMap<String, OsmPrimitive> byArticle = new MultiMap<>(); 115 115 final List<Command> commands = new ArrayList<>(); 116 final List<Wikipedia LangArticle> notFound = new ArrayList<>();116 final List<WikipediaEntry> notFound = new ArrayList<>(); 117 117 118 118 PrimitivesWithWikipedia(String lang) { … … 139 139 } 140 140 } else { 141 final Wikipedia LangArticle article = new WikipediaLangArticle(lang, wikipedia);141 final WikipediaEntry article = new WikipediaEntry(lang, wikipedia); 142 142 Main.warn(tr("No Wikidata ID found for: {0}", article)); 143 143 notFound.add(article); … … 154 154 } 155 155 156 List<Wikipedia LangArticle> getNotFound() {156 List<WikipediaEntry> getNotFound() { 157 157 return notFound; 158 158 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/actions/WikipediaAddNamesAction.java
r32888 r32891 17 17 import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog; 18 18 import org.wikipedia.WikipediaApp; 19 import org.wikipedia.data.Wikipedia LangArticle;19 import org.wikipedia.data.WikipediaEntry; 20 20 21 21 public class WikipediaAddNamesAction extends JosmAction { … … 29 29 @Override 30 30 public void actionPerformed(ActionEvent e) { 31 final Wikipedia LangArticle wp = WikipediaLangArticle.parseTag("wikipedia", getWikipediaValue());31 final WikipediaEntry wp = WikipediaEntry.parseTag("wikipedia", getWikipediaValue()); 32 32 List<String[]> tags = new ArrayList<>(); 33 33 WikipediaApp.getInterwikiArticles(wp.lang, wp.article).stream() … … 41 41 } 42 42 43 private boolean useWikipediaLangArticle(Wikipedia LangArticlei) {43 private boolean useWikipediaLangArticle(WikipediaEntry i) { 44 44 return (!Main.pref.getBoolean("wikipedia.filter-iso-languages", true) 45 45 || Arrays.asList(Locale.getISOLanguages()).contains(i.lang)) -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/data/WikidataEntry.java
r32888 r32891 20 20 @Override 21 21 public Tag createWikipediaTag() { 22 return new Tag("wikidata", wikipediaArticle);22 return new Tag("wikidata", article); 23 23 } 24 24 … … 26 26 public String getLabelText() { 27 27 final String descriptionInParen = description == null ? "" : (" (" + description + ")"); 28 return getLabelText(label, wikipediaArticle + descriptionInParen);28 return getLabelText(label, article + descriptionInParen); 29 29 } 30 30 -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/data/WikipediaEntry.java
r32888 r32891 9 9 10 10 import java.util.Comparator; 11 import java.util.Objects; 12 import java.util.regex.Matcher; 13 import java.util.regex.Pattern; 11 14 12 15 public class WikipediaEntry implements Comparable<WikipediaEntry> { 13 16 17 public final String lang; 18 public final String article; 14 19 public final String label; 15 public final String wikipediaLang, wikipediaArticle;16 20 public final LatLon coordinate; 17 21 private Boolean wiwosmStatus; 18 22 19 public WikipediaEntry(String wikipediaLang, String wikipediaArticle) {20 this( wikipediaLang, wikipediaArticle, null, null);23 public WikipediaEntry(String lang, String article) { 24 this(lang, article, null, null); 21 25 } 22 26 23 public WikipediaEntry(String wikipediaLang, String wikipediaArticle, String label, LatLon coordinate) { 27 public WikipediaEntry(String lang, String article, String label, LatLon coordinate) { 28 this.lang = lang; 29 this.article = article; 24 30 this.label = label; 25 this.wikipediaLang = wikipediaLang;26 this.wikipediaArticle = wikipediaArticle;27 31 this.coordinate = coordinate; 28 32 } 29 33 34 static WikipediaEntry parseFromUrl(String url) { 35 if (url == null) { 36 return null; 37 } 38 // decode URL for nicer value 39 url = Utils.decodeUrl(url); 40 // extract Wikipedia language and 41 final Matcher m = Pattern.compile("(https?:)?//(\\w*)\\.wikipedia\\.org/wiki/(.*)").matcher(url); 42 if (!m.matches()) { 43 return null; 44 } 45 return new WikipediaEntry(m.group(2), m.group(3)); 46 } 47 48 public static WikipediaEntry parseTag(String key, String value) { 49 if (value == null) { 50 return null; 51 } else if (value.startsWith("http")) { 52 //wikipedia=http... 53 return WikipediaEntry.parseFromUrl(value); 54 } else if (value.contains(":")) { 55 //wikipedia=[lang]:[article] 56 //wikipedia:[lang]=[lang]:[article] 57 final String[] item = Utils.decodeUrl(value).split(":", 2); 58 final String article = item[1].replace("_", " "); 59 return new WikipediaEntry(item[0], article); 60 } else if (key.startsWith("wikipedia:")) { 61 //wikipedia:[lang]=[lang]:[article] 62 //wikipedia:[lang]=[article] 63 final String lang = key.split(":", 2)[1]; 64 final String[] item = Utils.decodeUrl(value).split(":", 2); 65 final String article = item[item.length == 2 ? 1 : 0].replace("_", " "); 66 return new WikipediaEntry(lang, article); 67 } else { 68 return null; 69 } 70 } 71 30 72 public Tag createWikipediaTag() { 31 return new Tag("wikipedia", wikipediaLang + ":" + wikipediaArticle);73 return new Tag("wikipedia", lang + ":" + article); 32 74 } 33 75 … … 41 83 42 84 public String getBrowserUrl() { 43 return WikipediaApp.getSiteUrl( wikipediaLang) + "/wiki/" + Utils.encodeUrl(wikipediaArticle.replace(" ", "_"));85 return WikipediaApp.getSiteUrl(lang) + "/wiki/" + Utils.encodeUrl(article.replace(" ", "_")); 44 86 } 45 87 46 88 public String getLabelText() { 47 return wikipediaArticle;89 return article; 48 90 } 49 91 50 92 @Override 51 93 public String toString() { 52 return wikipediaArticle;94 return article; 53 95 } 54 96 … … 57 99 return Comparator 58 100 .<WikipediaEntry, String>comparing(x -> x.label, AlphanumComparator.getInstance()) 59 .thenComparing(x -> x. wikipediaArticle, AlphanumComparator.getInstance())101 .thenComparing(x -> x.article, AlphanumComparator.getInstance()) 60 102 .compare(this, o); 61 103 } 104 105 @Override 106 public boolean equals(Object o) { 107 if (this == o) return true; 108 if (o == null || getClass() != o.getClass()) return false; 109 final WikipediaEntry that = (WikipediaEntry) o; 110 return Objects.equals(lang, that.lang) && 111 Objects.equals(article, that.article); 112 } 113 114 @Override 115 public int hashCode() { 116 return Objects.hash(lang, article); 117 } 62 118 } -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/gui/WikipediaToggleDialog.java
r32888 r32891 79 79 Main.map.mapView.zoomTo(bbox); 80 80 } 81 final String search = Optional.ofNullable(entry.label).orElse(entry. wikipediaArticle).replaceAll("\\(.*\\)", "");81 final String search = Optional.ofNullable(entry.label).orElse(entry.article).replaceAll("\\(.*\\)", ""); 82 82 SearchAction.search(search, SearchAction.SearchMode.replace); 83 83 } … … 95 95 label.setIcon(ImageProvider.getIfAvailable("misc", "grey_check")); 96 96 label.setToolTipText(/* I18n: WIWOSM server already links Wikipedia article to object/s */ tr("Available via WIWOSM server")); 97 } else if (articles.contains(entry. wikipediaArticle)) {97 } else if (articles.contains(entry.article)) { 98 98 label.setIcon(ImageProvider.getIfAvailable("misc", "green_check")); 99 99 label.setToolTipText(/* I18n: object/s from dataset contain link to Wikipedia article */ tr("Available in local dataset")); … … 124 124 private String getLanguageOfFirstItem() { 125 125 try { 126 return list.getModel().getElementAt(0). wikipediaLang;126 return list.getModel().getElementAt(0).lang; 127 127 } catch (ArrayIndexOutOfBoundsException ignore) { 128 128 return wikipediaLang.get(); … … 176 176 publish(entries.toArray(new WikipediaEntry[entries.size()])); 177 177 WikipediaApp.partitionList(entries, 20).forEach(chunk -> { 178 WikipediaApp.updateWIWOSMStatus(chunk.get(0). wikipediaLang, chunk);178 WikipediaApp.updateWIWOSMStatus(chunk.get(0).lang, chunk); 179 179 list.repaint(); 180 180 }); … … 342 342 final LatLon latLon = entry.coordinate != null 343 343 ? entry.coordinate 344 : WikipediaApp.getCoordinateForArticle(entry. wikipediaLang, entry.wikipediaArticle);344 : WikipediaApp.getCoordinateForArticle(entry.lang, entry.article); 345 345 if (latLon == null) { 346 346 return; -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r32890 r32891 7 7 import org.wikipedia.data.WikidataEntry; 8 8 import org.wikipedia.data.WikipediaEntry; 9 import org.wikipedia.data.WikipediaLangArticle;10 9 11 10 import java.util.Arrays; … … 52 51 @Test 53 52 public void testGetInterwikiArticles1() { 54 final Collection<Wikipedia LangArticle> iw = WikipediaApp.getInterwikiArticles("de", "Österreich");55 assertThat(iw, hasItem(new Wikipedia LangArticle("en", "Austria")));56 assertThat(iw, hasItem(new Wikipedia LangArticle("no", "Østerrike")));57 assertThat(iw, hasItem(new Wikipedia LangArticle("ko", "오스트리아")));53 final Collection<WikipediaEntry> iw = WikipediaApp.getInterwikiArticles("de", "Österreich"); 54 assertThat(iw, hasItem(new WikipediaEntry("en", "Austria"))); 55 assertThat(iw, hasItem(new WikipediaEntry("no", "Østerrike"))); 56 assertThat(iw, hasItem(new WikipediaEntry("ko", "오스트리아"))); 58 57 } 59 58 60 59 @Test 61 60 public void testGetInterwikiArticles2() { 62 final Collection<Wikipedia LangArticle> iw = WikipediaApp.getInterwikiArticles("en", "Ampersand");63 assertThat(iw, hasItem(new Wikipedia LangArticle("fi", "&")));61 final Collection<WikipediaEntry> iw = WikipediaApp.getInterwikiArticles("en", "Ampersand"); 62 assertThat(iw, hasItem(new WikipediaEntry("fi", "&"))); 64 63 } 65 64 … … 76 75 new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705)); 77 76 final long c = entries.stream() 78 .filter(entry -> "Reichstagsgebäude".equals(entry. wikipediaArticle) && "de".equals(entry.wikipediaLang))77 .filter(entry -> "Reichstagsgebäude".equals(entry.article) && "de".equals(entry.lang)) 79 78 .count(); 80 79 assertEquals(1, c); … … 85 84 final List<WikidataEntry> de = WikipediaApp.getWikidataEntriesForQuery("de", "Österreich", Locale.GERMAN); 86 85 final List<WikidataEntry> en = WikipediaApp.getWikidataEntriesForQuery("de", "Österreich", Locale.ENGLISH); 87 assertThat(de.get(0). wikipediaArticle, is("Q40"));88 assertThat(de.get(0). wikipediaLang, is("wikidata"));86 assertThat(de.get(0).article, is("Q40")); 87 assertThat(de.get(0).lang, is("wikidata")); 89 88 assertThat(de.get(0).label, is("Österreich")); 90 89 assertThat(de.get(0).description, is("Staat in Mitteleuropa")); … … 98 97 new LatLon(47.20, 11.30), new LatLon(47.22, 11.32)); 99 98 final long c = entries.stream() 100 .filter(entry -> "Q865406".equals(entry. wikipediaArticle) && "wikidata".equals(entry.wikipediaLang) && "Birgitzer Alm".equals(entry.label))99 .filter(entry -> "Q865406".equals(entry.article) && "wikidata".equals(entry.lang) && "Birgitzer Alm".equals(entry.label)) 101 100 .count(); 102 101 assertEquals(1, c); -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/data/WikipediaEntryTest.java
r32890 r32891 5 5 6 6 import static org.hamcrest.CoreMatchers.is; 7 import static org.hamcrest.CoreMatchers.nullValue; 7 8 import static org.junit.Assert.*; 8 9 9 10 public class WikipediaEntryTest { 11 12 @Test 13 public void testParseFromUrl1() { 14 final WikipediaEntry actual = WikipediaEntry.parseFromUrl("https://de.wikipedia.org/wiki/Österreich"); 15 assertThat(actual.article, is("Österreich")); 16 assertThat(actual.lang, is("de")); 17 } 18 19 @Test 20 public void testParseFromUrl2() { 21 final WikipediaEntry actual = WikipediaEntry.parseFromUrl("http://de.wikipedia.org/wiki/%C3%96sterreich"); 22 assertThat(actual.article, is("Österreich")); 23 assertThat(actual.lang, is("de")); 24 } 25 26 @Test 27 public void testParseFromUrl3() { 28 final WikipediaEntry actual = WikipediaEntry.parseFromUrl("http://de.wikipedia.org/wiki/Sternheim_%26_Emanuel"); 29 assertThat(actual.article, is("Sternheim_&_Emanuel")); 30 assertThat(actual.lang, is("de")); 31 } 32 33 @Test 34 public void testParseFromUrl4() { 35 final WikipediaEntry actual = WikipediaEntry.parseFromUrl("//de.wikipedia.org/wiki/Reichstagsgeb%C3%A4ude"); 36 assertThat(actual.article, is("Reichstagsgebäude")); 37 assertThat(actual.lang, is("de")); 38 } 39 40 @Test 41 public void testParseFromTag0() { 42 final WikipediaEntry actual = WikipediaEntry.parseTag("wikipedia", "Österreich"); 43 assertThat(actual, nullValue()); 44 } 45 46 @Test 47 public void testParseFromTag1() { 48 final WikipediaEntry actual = WikipediaEntry.parseTag("wikipedia", "de:Österreich"); 49 assertThat(actual.article, is("Österreich")); 50 assertThat(actual.lang, is("de")); 51 } 52 53 @Test 54 public void testParseFromTag2() { 55 final WikipediaEntry actual = WikipediaEntry.parseTag("wikipedia:de", "Österreich"); 56 assertThat(actual.article, is("Österreich")); 57 assertThat(actual.lang, is("de")); 58 } 59 60 @Test 61 public void testParseFromTag3() { 62 final WikipediaEntry actual = WikipediaEntry.parseTag("wikipedia:de", "de:Österreich"); 63 assertThat(actual.article, is("Österreich")); 64 assertThat(actual.lang, is("de")); 65 } 66 67 @Test 68 public void testParseFromTag4() { 69 final WikipediaEntry actual = WikipediaEntry.parseTag("wikipedia", "https://de.wikipedia.org/wiki/Österreich"); 70 assertThat(actual.article, is("Österreich")); 71 assertThat(actual.lang, is("de")); 72 } 10 73 11 74 @Test
Note:
See TracChangeset
for help on using the changeset viewer.