Changeset 32891 in osm for applications/editors/josm/plugins/wikipedia/src
- Timestamp:
- 2016-08-30T13:26:57+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia/src/org/wikipedia
- Files:
-
- 1 deleted
- 6 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;
Note:
See TracChangeset
for help on using the changeset viewer.