Changeset 32887 in osm for applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
- Timestamp:
- 2016-08-30T10:52:38+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r32829 r32887 12 12 import java.util.Collection; 13 13 import java.util.Collections; 14 import java.util.Comparator;15 14 import java.util.HashMap; 16 15 import java.util.List; … … 19 18 import java.util.Objects; 20 19 import java.util.TreeMap; 21 import java.util.regex.Matcher;22 20 import java.util.regex.Pattern; 23 21 import java.util.stream.Collectors; … … 31 29 import org.openstreetmap.josm.data.coor.LatLon; 32 30 import org.openstreetmap.josm.data.osm.OsmPrimitive; 33 import org.openstreetmap.josm.data.osm.Tag;34 31 import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils; 35 import org.openstreetmap.josm.tools.AlphanumComparator;36 import org.openstreetmap.josm.tools.CheckParameterUtil;37 32 import org.openstreetmap.josm.tools.HttpClient; 38 33 import org.openstreetmap.josm.tools.Utils; 39 34 import org.w3c.dom.Document; 40 35 import org.w3c.dom.Node; 36 import org.wikipedia.data.WikidataEntry; 37 import org.wikipedia.data.WikipediaEntry; 38 import org.wikipedia.data.WikipediaLangArticle; 39 import org.wikipedia.tools.XPath; 41 40 42 41 public final class WikipediaApp { … … 56 55 } 57 56 58 static String getSiteUrl(String wikipediaLang) { 57 public static String getSiteUrl(String wikipediaLang) { 59 58 if ("wikidata".equals(wikipediaLang)) { 60 59 return "https://www.wikidata.org"; … … 64 63 } 65 64 66 static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang, LatLon min, LatLon max) { 65 public static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang, LatLon min, LatLon max) { 67 66 try { 68 67 // construct url … … 99 98 } 100 99 101 static List<WikidataEntry> getWikidataEntriesForQuery(final String languageForQuery, final String query, final Locale localeForLabels) { 100 public static List<WikidataEntry> getWikidataEntriesForQuery(final String languageForQuery, final String query, final Locale localeForLabels) { 102 101 try { 103 102 final String url = "https://www.wikidata.org/w/api.php" + … … 120 119 } 121 120 122 static List<WikipediaEntry> getEntriesFromCategory(String wikipediaLang, String category, int depth) { 121 public static List<WikipediaEntry> getEntriesFromCategory(String wikipediaLang, String category, int depth) { 123 122 try { 124 123 final String url = "https://tools.wmflabs.org/cats-php/" … … 138 137 } 139 138 140 static List<WikipediaEntry> getEntriesFromClipboard(final String wikipediaLang) { 139 public static List<WikipediaEntry> getEntriesFromClipboard(final String wikipediaLang) { 141 140 return Pattern.compile("[\\n\\r]+") 142 141 .splitAsStream(ClipboardUtils.getClipboardStringContent()) … … 145 144 } 146 145 147 static void updateWIWOSMStatus(String wikipediaLang, List<WikipediaEntry> entries) { 146 public static void updateWIWOSMStatus(String wikipediaLang, List<WikipediaEntry> entries) { 148 147 if (entries.size() > 20) { 149 148 partitionList(entries, 20).forEach(chunk -> updateWIWOSMStatus(wikipediaLang, chunk)); … … 179 178 } 180 179 181 static Stream<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) { 180 public static Stream<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) { 182 181 if ("wikidata".equals(wikipediaLang)) { 183 182 return Stream.of(p.get("wikidata")).filter(Objects::nonNull); … … 194 193 * Returns a map mapping wikipedia articles to wikidata ids. 195 194 */ 196 static Map<String, String> getWikidataForArticles(String wikipediaLang, List<String> articles) { 195 public static Map<String, String> getWikidataForArticles(String wikipediaLang, List<String> articles) { 197 196 if (articles.size() > 50) { 198 197 return partitionList(articles, 50).stream() … … 225 224 } 226 225 227 static List<String> getCategoriesForPrefix(final String wikipediaLang, final String prefix) { 226 public static List<String> getCategoriesForPrefix(final String wikipediaLang, final String prefix) { 228 227 try { 229 228 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" … … 247 246 } 248 247 249 static String getLabelForWikidata(String wikidataId, Locale locale, String 248 public static String getLabelForWikidata(String wikidataId, Locale locale, String... preferredLanguage) { 250 249 try { 251 250 final List<WikidataEntry> entry = Collections.singletonList(new WikidataEntry(wikidataId, null, null, null)); … … 310 309 } 311 310 312 static Collection<WikipediaLangArticle> getInterwikiArticles(String wikipediaLang, String article) { 311 public static Collection<WikipediaLangArticle> getInterwikiArticles(String wikipediaLang, String article) { 313 312 try { 314 313 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" + … … 332 331 } 333 332 334 static LatLon getCoordinateForArticle(String wikipediaLang, String article) { 333 public static LatLon getCoordinateForArticle(String wikipediaLang, String article) { 335 334 try { 336 335 final String url = getSiteUrl(wikipediaLang) + "/w/api.php" + … … 351 350 throw new RuntimeException(ex); 352 351 } 353 }354 355 static class WikipediaLangArticle {356 357 final String lang, article;358 359 public WikipediaLangArticle(String lang, String article) {360 this.lang = lang;361 this.article = article;362 }363 364 public static WikipediaLangArticle parseFromUrl(String url) {365 if (url == null) {366 return null;367 }368 // decode URL for nicer value369 url = Utils.decodeUrl(url);370 // extract Wikipedia language and371 final Matcher m = Pattern.compile("(https?:)?//(\\w*)\\.wikipedia\\.org/wiki/(.*)").matcher(url);372 if (!m.matches()) {373 return null;374 }375 return new WikipediaLangArticle(m.group(2), m.group(3));376 }377 378 public static WikipediaLangArticle parseTag(String key, String value) {379 if (value == null) {380 return null;381 } else if (value.startsWith("http")) {382 //wikipedia=http...383 return parseFromUrl(value);384 } else if (value.contains(":")) {385 //wikipedia=[lang]:[article]386 //wikipedia:[lang]=[lang]:[article]387 final String[] item = Utils.decodeUrl(value).split(":", 2);388 final String article = item[1].replace("_", " ");389 return new WikipediaLangArticle(item[0], article);390 } else if (key.startsWith("wikipedia:")) {391 //wikipedia:[lang]=[lang]:[article]392 //wikipedia:[lang]=[article]393 final String lang = key.split(":", 2)[1];394 final String[] item = Utils.decodeUrl(value).split(":", 2);395 final String article = item[item.length == 2 ? 1 : 0].replace("_", " ");396 return new WikipediaLangArticle(lang, article);397 } else {398 return null;399 }400 }401 402 @Override403 public String toString() {404 return lang + ":" + article;405 }406 407 @Override408 public boolean equals(Object o) {409 if (this == o) return true;410 if (o == null || getClass() != o.getClass()) return false;411 final WikipediaLangArticle that = (WikipediaLangArticle) o;412 return Objects.equals(lang, that.lang) &&413 Objects.equals(article, that.article);414 }415 416 @Override417 public int hashCode() {418 return Objects.hash(lang, article);419 }420 }421 422 static class WikipediaEntry implements Comparable<WikipediaEntry> {423 424 final String label;425 final String wikipediaLang, wikipediaArticle;426 final LatLon coordinate;427 private Boolean wiwosmStatus;428 429 WikipediaEntry(String wikipediaLang, String wikipediaArticle) {430 this(wikipediaLang, wikipediaArticle, null, null);431 }432 433 WikipediaEntry(String wikipediaLang, String wikipediaArticle, String label, LatLon coordinate) {434 this.label = label;435 this.wikipediaLang = wikipediaLang;436 this.wikipediaArticle = wikipediaArticle;437 this.coordinate = coordinate;438 }439 440 protected Tag createWikipediaTag() {441 return new Tag("wikipedia", wikipediaLang + ":" + wikipediaArticle);442 }443 444 public void setWiwosmStatus(Boolean wiwosmStatus) {445 this.wiwosmStatus = wiwosmStatus;446 }447 448 public Boolean getWiwosmStatus() {449 return wiwosmStatus;450 }451 452 public String getBrowserUrl() {453 return getSiteUrl(wikipediaLang) + "/wiki/" + Utils.encodeUrl(wikipediaArticle.replace(" ", "_"));454 }455 456 public String getLabelText() {457 return wikipediaArticle;458 }459 460 @Override461 public String toString() {462 return wikipediaArticle;463 }464 465 @Override466 public int compareTo(WikipediaEntry o) {467 return Comparator468 .<WikipediaEntry, String>comparing(x -> x.label, AlphanumComparator.getInstance())469 .thenComparing(x -> x.wikipediaArticle, AlphanumComparator.getInstance())470 .compare(this, o);471 }472 }473 474 static class WikidataEntry extends WikipediaEntry {475 476 final String description;477 478 WikidataEntry(String id, String label, LatLon coordinate, String description) {479 super("wikidata", id, label, coordinate);480 this.description = description;481 ensureValidWikidataId(id);482 }483 484 @Override485 protected Tag createWikipediaTag() {486 return new Tag("wikidata", wikipediaArticle);487 }488 489 @Override490 public String getLabelText() {491 final String descriptionInParen = description == null ? "" : (" (" + description + ")");492 return getLabelText(label, wikipediaArticle + descriptionInParen);493 }494 495 static String getLabelText(String bold, String gray) {496 return Utils.escapeReservedCharactersHTML(bold) + " <span color='gray'>" + Utils.escapeReservedCharactersHTML(gray) + "</span>";497 }498 }499 500 static void ensureValidWikidataId(String id) {501 CheckParameterUtil.ensureThat(WIKIDATA_PATTERN.matcher(id).matches(), "Invalid Wikidata ID given: " + id);502 352 } 503 353
Note:
See TracChangeset
for help on using the changeset viewer.