Changeset 33075 in osm for applications/editors/josm/plugins/wikipedia
- Timestamp:
- 2016-11-18T20:00:49+01:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/wikipedia
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java
r33074 r33075 44 44 public static final Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+"); 45 45 private static final XPath X_PATH = XPath.getInstance(); 46 47 private WikipediaApp() { 46 private final String wikipediaLang; 47 48 private WikipediaApp(final String wikipediaLang) { 49 this.wikipediaLang = wikipediaLang; 50 } 51 52 public static WikipediaApp forLanguage(final String wikipediaLang) { 53 return new WikipediaApp(wikipediaLang); 48 54 } 49 55 … … 56 62 } 57 63 58 public static String getSiteUrl(String wikipediaLang) {64 public String getSiteUrl() { 59 65 if ("wikidata".equals(wikipediaLang)) { 60 66 return "https://www.wikidata.org"; … … 64 70 } 65 71 66 public static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang,LatLon min, LatLon max) {72 public List<WikipediaEntry> getEntriesFromCoordinates(LatLon min, LatLon max) { 67 73 try { 68 74 // construct url 69 final String url = getSiteUrl( wikipediaLang) + "/w/api.php"75 final String url = getSiteUrl() + "/w/api.php" 70 76 + "?action=query" 71 77 + "&list=geosearch" … … 120 126 } 121 127 122 public static List<WikipediaEntry> getEntriesFromCategory(String wikipediaLang,String category, int depth) {128 public List<WikipediaEntry> getEntriesFromCategory(String category, int depth) { 123 129 try { 124 130 final String url = "https://tools.wmflabs.org/cats-php/" … … 145 151 } 146 152 147 public static void updateWIWOSMStatus(String wikipediaLang,List<WikipediaEntry> entries) {153 public void updateWIWOSMStatus(List<WikipediaEntry> entries) { 148 154 if (entries.size() > 20) { 149 partitionList(entries, 20).forEach(chunk -> updateWIWOSMStatus( wikipediaLang,chunk));155 partitionList(entries, 20).forEach(chunk -> updateWIWOSMStatus(chunk)); 150 156 return; 151 157 } … … 179 185 } 180 186 181 public static Stream<String> getWikipediaArticles(final String wikipediaLang,OsmPrimitive p) {187 public Stream<String> getWikipediaArticles(final OsmPrimitive p) { 182 188 if ("wikidata".equals(wikipediaLang)) { 183 189 return Stream.of(p.get("wikidata")).filter(Objects::nonNull); … … 194 200 * Returns a map mapping wikipedia articles to wikidata ids. 195 201 */ 196 public static Map<String, String> getWikidataForArticles(String wikipediaLang, List<String> articles) {202 public Map<String, String> getWikidataForArticles(Collection<String> articles) { 197 203 return articles.stream() 198 204 .distinct() … … 216 222 .values() 217 223 .stream() 218 .flatMap(chunk -> getWikidataForArticles0( wikipediaLang,chunk).entrySet().stream())224 .flatMap(chunk -> getWikidataForArticles0(chunk).entrySet().stream()) 219 225 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 220 226 } 221 227 222 private static Map<String, String> getWikidataForArticles0(String wikipediaLang,List<String> articles) {228 private Map<String, String> getWikidataForArticles0(List<String> articles) { 223 229 if (articles.isEmpty()) { 224 230 return Collections.emptyMap(); … … 249 255 } 250 256 251 public static List<String> getCategoriesForPrefix(final String wikipediaLang,final String prefix) {252 try { 253 final String url = getSiteUrl( wikipediaLang) + "/w/api.php"257 public List<String> getCategoriesForPrefix(final String prefix) { 258 try { 259 final String url = getSiteUrl() + "/w/api.php" 254 260 + "?action=query" 255 261 + "&list=prefixsearch" … … 334 340 } 335 341 336 public static Collection<WikipediaEntry> getInterwikiArticles(String wikipediaLang,String article) {337 try { 338 final String url = getSiteUrl( wikipediaLang) + "/w/api.php" +342 public Collection<WikipediaEntry> getInterwikiArticles(String article) { 343 try { 344 final String url = getSiteUrl() + "/w/api.php" + 339 345 "?action=query" + 340 346 "&prop=langlinks" + … … 356 362 } 357 363 358 public static LatLon getCoordinateForArticle(String wikipediaLang,String article) {359 try { 360 final String url = getSiteUrl( wikipediaLang) + "/w/api.php" +364 public LatLon getCoordinateForArticle(String article) { 365 try { 366 final String url = getSiteUrl() + "/w/api.php" + 361 367 "?action=query" + 362 368 "&prop=coordinates" + -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/actions/FetchWikidataAction.java
r32891 r33075 129 129 "Fetching {0} Wikidata ID for language ''{1}''", 130 130 "Fetching {0} Wikidata IDs for language ''{1}''", size, size, lang)); 131 final Map<String, String> wikidataByWikipedia = WikipediaApp. getWikidataForArticles(lang, new ArrayList<>(byArticle.keySet()));131 final Map<String, String> wikidataByWikipedia = WikipediaApp.forLanguage(lang).getWikidataForArticles(byArticle.keySet()); 132 132 ConditionalOptionPaneUtil.startBulkOperation(GuiUtils.PREF_OVERWRITE); 133 133 for (Map.Entry<String, Set<OsmPrimitive>> i : byArticle.entrySet()) { -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/actions/WikipediaAddNamesAction.java
r32891 r33075 31 31 final WikipediaEntry wp = WikipediaEntry.parseTag("wikipedia", getWikipediaValue()); 32 32 List<String[]> tags = new ArrayList<>(); 33 WikipediaApp. getInterwikiArticles(wp.lang,wp.article).stream()33 WikipediaApp.forLanguage(wp.lang).getInterwikiArticles(wp.article).stream() 34 34 .filter(this::useWikipediaLangArticle) 35 35 .map(i -> new String[]{"name:" + i.lang, i.article}) -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/data/WikipediaEntry.java
r32892 r33075 80 80 81 81 public String getBrowserUrl() { 82 return WikipediaApp. getSiteUrl(lang) + "/wiki/" + Utils.encodeUrl(article.replace(" ", "_"));82 return WikipediaApp.forLanguage(lang).getSiteUrl() + "/wiki/" + Utils.encodeUrl(article.replace(" ", "_")); 83 83 } 84 84 -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/gui/WikipediaCategorySearchDialog.java
r32887 r33075 58 58 final List<String> entries = query == null || query.isEmpty() 59 59 ? Collections.emptyList() 60 : WikipediaApp. getCategoriesForPrefix(WikipediaToggleDialog.wikipediaLang.get(),query);60 : WikipediaApp.forLanguage(WikipediaToggleDialog.wikipediaLang.get()).getCategoriesForPrefix(query); 61 61 GuiHelper.runInEDT(() -> lsResultModel.setItems(entries)); 62 62 }, 200, TimeUnit.MILLISECONDS); -
applications/editors/josm/plugins/wikipedia/src/org/wikipedia/gui/WikipediaToggleDialog.java
r33017 r33075 113 113 private void updateTitle() { 114 114 final String lang = getLanguageOfFirstItem(); 115 final String host = WikipediaApp. getSiteUrl(lang).split("/+")[1];115 final String host = WikipediaApp.forLanguage(lang).getSiteUrl().split("/+")[1]; 116 116 if (titleContext == null) { 117 117 setTitle(host); … … 155 155 @Override 156 156 List<WikipediaEntry> getEntries() { 157 return WikipediaApp. getEntriesFromCoordinates(158 wikidata ? "wikidata" : wikipediaLang.get(),min, max);157 return WikipediaApp.forLanguage(wikidata ? "wikidata" : wikipediaLang.get()) 158 .getEntriesFromCoordinates(min, max); 159 159 } 160 160 }.execute(); … … 175 175 publish(entries.toArray(new WikipediaEntry[entries.size()])); 176 176 WikipediaApp.partitionList(entries, 20).forEach(chunk -> { 177 WikipediaApp. updateWIWOSMStatus(chunk.get(0).lang,chunk);177 WikipediaApp.forLanguage(chunk.get(0).lang).updateWIWOSMStatus(chunk); 178 178 list.repaint(); 179 179 }); … … 217 217 @Override 218 218 List<WikipediaEntry> getEntries() { 219 return WikipediaApp. getEntriesFromCategory(220 wikipediaLang.get(),category, Main.pref.getInteger("wikipedia.depth", 3));219 return WikipediaApp.forLanguage(wikipediaLang.get()) 220 .getEntriesFromCategory(category, Main.pref.getInteger("wikipedia.depth", 3)); 221 221 } 222 222 }.execute(); … … 341 341 final LatLon latLon = entry.coordinate != null 342 342 ? entry.coordinate 343 : WikipediaApp. getCoordinateForArticle(entry.lang,entry.article);343 : WikipediaApp.forLanguage(entry.lang).getCoordinateForArticle(entry.article); 344 344 if (latLon == null) { 345 345 return; … … 354 354 if (Main.main != null && Main.getLayerManager().getEditDataSet() != null) { 355 355 Main.getLayerManager().getEditDataSet().allPrimitives().stream() 356 .flatMap(p -> WikipediaApp. getWikipediaArticles(language,p))356 .flatMap(p -> WikipediaApp.forLanguage(language).getWikipediaArticles(p)) 357 357 .forEach(articles::add); 358 358 } -
applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java
r33070 r33075 55 55 @Test 56 56 public void testGetInterwikiArticles1() { 57 final Collection<WikipediaEntry> iw = WikipediaApp. getInterwikiArticles("de","Österreich");57 final Collection<WikipediaEntry> iw = WikipediaApp.forLanguage("de").getInterwikiArticles("Österreich"); 58 58 assertThat(iw, hasItem(new WikipediaEntry("en", "Austria"))); 59 59 assertThat(iw, hasItem(new WikipediaEntry("no", "Østerrike"))); … … 63 63 @Test 64 64 public void testGetInterwikiArticles2() { 65 final Collection<WikipediaEntry> iw = WikipediaApp. getInterwikiArticles("en","Ampersand");65 final Collection<WikipediaEntry> iw = WikipediaApp.forLanguage("en").getInterwikiArticles("Ampersand"); 66 66 assertThat(iw, hasItem(new WikipediaEntry("fi", "&"))); 67 67 } … … 69 69 @Test 70 70 public void testGetCoordinates() throws Exception { 71 assertThat(WikipediaApp. getCoordinateForArticle("de","Marchreisenspitze"), is(new LatLon(47.1725, 11.30833333)));72 assertThat(WikipediaApp. getCoordinateForArticle("en","Austria"), is(new LatLon(47.33333333, 13.33333333)));73 assertThat(WikipediaApp. getCoordinateForArticle("en","Foobar2000"), nullValue());71 assertThat(WikipediaApp.forLanguage("de").getCoordinateForArticle("Marchreisenspitze"), is(new LatLon(47.1725, 11.30833333))); 72 assertThat(WikipediaApp.forLanguage("en").getCoordinateForArticle("Austria"), is(new LatLon(47.33333333, 13.33333333))); 73 assertThat(WikipediaApp.forLanguage("en").getCoordinateForArticle("Foobar2000"), nullValue()); 74 74 } 75 75 76 76 @Test 77 77 public void testFromCoordinates() throws Exception { 78 final List<WikipediaEntry> entries = WikipediaApp. getEntriesFromCoordinates("de",79 new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705));78 final List<WikipediaEntry> entries = WikipediaApp.forLanguage("de") 79 .getEntriesFromCoordinates(new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705)); 80 80 final long c = entries.stream() 81 81 .filter(entry -> "Reichstagsgebäude".equals(entry.article) && "de".equals(entry.lang)) … … 98 98 @Test 99 99 public void testFromCoordinatesWikidata() throws Exception { 100 final List<WikipediaEntry> entries = WikipediaApp. getEntriesFromCoordinates("wikidata",101 new LatLon(47.20, 11.30), new LatLon(47.22, 11.32));100 final List<WikipediaEntry> entries = WikipediaApp.forLanguage("wikidata") 101 .getEntriesFromCoordinates(new LatLon(47.20, 11.30), new LatLon(47.22, 11.32)); 102 102 final long c = entries.stream() 103 103 .map(WikidataEntry.class::cast) … … 109 109 @Test 110 110 public void testGetWikidataForArticles() throws Exception { 111 final Map<String, String> map = WikipediaApp. getWikidataForArticles("en",112 Arrays.asList("London", "Vienna", "Völs, Tyrol", "a-non-existing-article"));111 final Map<String, String> map = WikipediaApp.forLanguage("en") 112 .getWikidataForArticles(Arrays.asList("London", "Vienna", "Völs, Tyrol", "a-non-existing-article")); 113 113 assertThat(map.get("London"), is("Q84")); 114 114 assertThat(map.get("Vienna"), is("Q1741")); … … 120 120 @Test 121 121 public void testTicket13991() throws Exception { 122 final Map<String, String> map = WikipediaApp. getWikidataForArticles("en",123 Stream.iterate("London", x -> x).limit(100).collect(Collectors.toList()));122 final Map<String, String> map = WikipediaApp.forLanguage("en") 123 .getWikidataForArticles(Stream.iterate("London", x -> x).limit(100).collect(Collectors.toList())); 124 124 assertThat(map, is(Collections.singletonMap("London", "Q84"))); 125 125 final List<String> articles = IntStream.range(0, 200) 126 126 .mapToObj(i -> "a-non-existing-article-" + i) 127 127 .collect(Collectors.toList()); 128 assertTrue(WikipediaApp. getWikidataForArticles("en",articles).isEmpty());128 assertTrue(WikipediaApp.forLanguage("en").getWikidataForArticles(articles).isEmpty()); 129 129 } 130 130 … … 154 154 final WikipediaEntry entry2 = new WikipediaEntry("en", "London"); 155 155 final WikipediaEntry entry3 = new WikipediaEntry("en", "a-non-existing-article"); 156 WikipediaApp. updateWIWOSMStatus("en",Arrays.asList(entry1, entry2, entry3));156 WikipediaApp.forLanguage("en").updateWIWOSMStatus(Arrays.asList(entry1, entry2, entry3)); 157 157 assertThat(entry1.getWiwosmStatus(), is(true)); 158 158 assertThat(entry2.getWiwosmStatus(), is(true)); … … 162 162 @Test 163 163 public void testCategoriesForPrefix() throws Exception { 164 final List<String> categories = WikipediaApp. getCategoriesForPrefix("de","Gemeinde in Öster");164 final List<String> categories = WikipediaApp.forLanguage("de").getCategoriesForPrefix("Gemeinde in Öster"); 165 165 assertTrue(categories.contains("Gemeinde in Österreich")); 166 166 }
Note:
See TracChangeset
for help on using the changeset viewer.