Commit eb7c7bfa authored by Simon Legner's avatar Simon Legner

JOSM/wikipedia: Refactoring (wikipediaLang as field)

git-svn-id: https://svn.openstreetmap.org/applications/editors/josm/plugins/wikipedia@33075 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
parent 2d9325d6
......@@ -43,8 +43,14 @@ public final class WikipediaApp {
public static final Pattern WIKIDATA_PATTERN = Pattern.compile("Q\\d+");
private static final XPath X_PATH = XPath.getInstance();
private final String wikipediaLang;
private WikipediaApp() {
private WikipediaApp(final String wikipediaLang) {
this.wikipediaLang = wikipediaLang;
}
public static WikipediaApp forLanguage(final String wikipediaLang) {
return new WikipediaApp(wikipediaLang);
}
static String getMediawikiLocale(Locale locale) {
......@@ -55,7 +61,7 @@ public final class WikipediaApp {
}
}
public static String getSiteUrl(String wikipediaLang) {
public String getSiteUrl() {
if ("wikidata".equals(wikipediaLang)) {
return "https://www.wikidata.org";
} else {
......@@ -63,10 +69,10 @@ public final class WikipediaApp {
}
}
public static List<WikipediaEntry> getEntriesFromCoordinates(String wikipediaLang, LatLon min, LatLon max) {
public List<WikipediaEntry> getEntriesFromCoordinates(LatLon min, LatLon max) {
try {
// construct url
final String url = getSiteUrl(wikipediaLang) + "/w/api.php"
final String url = getSiteUrl() + "/w/api.php"
+ "?action=query"
+ "&list=geosearch"
+ "&format=xml"
......@@ -119,7 +125,7 @@ public final class WikipediaApp {
}
}
public static List<WikipediaEntry> getEntriesFromCategory(String wikipediaLang, String category, int depth) {
public List<WikipediaEntry> getEntriesFromCategory(String category, int depth) {
try {
final String url = "https://tools.wmflabs.org/cats-php/"
+ "?lang=" + wikipediaLang
......@@ -144,9 +150,9 @@ public final class WikipediaApp {
.collect(Collectors.toList());
}
public static void updateWIWOSMStatus(String wikipediaLang, List<WikipediaEntry> entries) {
public void updateWIWOSMStatus(List<WikipediaEntry> entries) {
if (entries.size() > 20) {
partitionList(entries, 20).forEach(chunk -> updateWIWOSMStatus(wikipediaLang, chunk));
partitionList(entries, 20).forEach(chunk -> updateWIWOSMStatus(chunk));
return;
}
Map<String, Boolean> status = new HashMap<>();
......@@ -178,7 +184,7 @@ public final class WikipediaApp {
}
}
public static Stream<String> getWikipediaArticles(final String wikipediaLang, OsmPrimitive p) {
public Stream<String> getWikipediaArticles(final OsmPrimitive p) {
if ("wikidata".equals(wikipediaLang)) {
return Stream.of(p.get("wikidata")).filter(Objects::nonNull);
}
......@@ -193,7 +199,7 @@ public final class WikipediaApp {
/**
* Returns a map mapping wikipedia articles to wikidata ids.
*/
public static Map<String, String> getWikidataForArticles(String wikipediaLang, List<String> articles) {
public Map<String, String> getWikidataForArticles(Collection<String> articles) {
return articles.stream()
.distinct()
.collect(Collectors.groupingBy(new Function<String, Integer>() {
......@@ -215,11 +221,11 @@ public final class WikipediaApp {
}))
.values()
.stream()
.flatMap(chunk -> getWikidataForArticles0(wikipediaLang, chunk).entrySet().stream())
.flatMap(chunk -> getWikidataForArticles0(chunk).entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private static Map<String, String> getWikidataForArticles0(String wikipediaLang, List<String> articles) {
private Map<String, String> getWikidataForArticles0(List<String> articles) {
if (articles.isEmpty()) {
return Collections.emptyMap();
}
......@@ -248,9 +254,9 @@ public final class WikipediaApp {
}
}
public static List<String> getCategoriesForPrefix(final String wikipediaLang, final String prefix) {
public List<String> getCategoriesForPrefix(final String prefix) {
try {
final String url = getSiteUrl(wikipediaLang) + "/w/api.php"
final String url = getSiteUrl() + "/w/api.php"
+ "?action=query"
+ "&list=prefixsearch"
+ "&format=xml"
......@@ -333,9 +339,9 @@ public final class WikipediaApp {
.orElse(null);
}
public static Collection<WikipediaEntry> getInterwikiArticles(String wikipediaLang, String article) {
public Collection<WikipediaEntry> getInterwikiArticles(String article) {
try {
final String url = getSiteUrl(wikipediaLang) + "/w/api.php" +
final String url = getSiteUrl() + "/w/api.php" +
"?action=query" +
"&prop=langlinks" +
"&titles=" + Utils.encodeUrl(article) +
......@@ -355,9 +361,9 @@ public final class WikipediaApp {
}
}
public static LatLon getCoordinateForArticle(String wikipediaLang, String article) {
public LatLon getCoordinateForArticle(String article) {
try {
final String url = getSiteUrl(wikipediaLang) + "/w/api.php" +
final String url = getSiteUrl() + "/w/api.php" +
"?action=query" +
"&prop=coordinates" +
"&titles=" + Utils.encodeUrl(article) +
......
......@@ -128,7 +128,7 @@ public class FetchWikidataAction extends JosmAction {
monitor.beginTask(trn(
"Fetching {0} Wikidata ID for language ''{1}''",
"Fetching {0} Wikidata IDs for language ''{1}''", size, size, lang));
final Map<String, String> wikidataByWikipedia = WikipediaApp.getWikidataForArticles(lang, new ArrayList<>(byArticle.keySet()));
final Map<String, String> wikidataByWikipedia = WikipediaApp.forLanguage(lang).getWikidataForArticles(byArticle.keySet());
ConditionalOptionPaneUtil.startBulkOperation(GuiUtils.PREF_OVERWRITE);
for (Map.Entry<String, Set<OsmPrimitive>> i : byArticle.entrySet()) {
final String wikipedia = i.getKey();
......
......@@ -30,7 +30,7 @@ public class WikipediaAddNamesAction extends JosmAction {
public void actionPerformed(ActionEvent e) {
final WikipediaEntry wp = WikipediaEntry.parseTag("wikipedia", getWikipediaValue());
List<String[]> tags = new ArrayList<>();
WikipediaApp.getInterwikiArticles(wp.lang, wp.article).stream()
WikipediaApp.forLanguage(wp.lang).getInterwikiArticles(wp.article).stream()
.filter(this::useWikipediaLangArticle)
.map(i -> new String[]{"name:" + i.lang, i.article})
.forEach(tags::add);
......
......@@ -79,7 +79,7 @@ public class WikipediaEntry implements Comparable<WikipediaEntry> {
}
public String getBrowserUrl() {
return WikipediaApp.getSiteUrl(lang) + "/wiki/" + Utils.encodeUrl(article.replace(" ", "_"));
return WikipediaApp.forLanguage(lang).getSiteUrl() + "/wiki/" + Utils.encodeUrl(article.replace(" ", "_"));
}
public String getLabelText() {
......
......@@ -57,7 +57,7 @@ final class WikipediaCategorySearchDialog extends ExtendedDialog {
debouncer.debounce(getClass(), () -> {
final List<String> entries = query == null || query.isEmpty()
? Collections.emptyList()
: WikipediaApp.getCategoriesForPrefix(WikipediaToggleDialog.wikipediaLang.get(), query);
: WikipediaApp.forLanguage(WikipediaToggleDialog.wikipediaLang.get()).getCategoriesForPrefix(query);
GuiHelper.runInEDT(() -> lsResultModel.setItems(entries));
}, 200, TimeUnit.MILLISECONDS);
}
......
......@@ -112,7 +112,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
private void updateTitle() {
final String lang = getLanguageOfFirstItem();
final String host = WikipediaApp.getSiteUrl(lang).split("/+")[1];
final String host = WikipediaApp.forLanguage(lang).getSiteUrl().split("/+")[1];
if (titleContext == null) {
setTitle(host);
} else {
......@@ -154,8 +154,8 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
@Override
List<WikipediaEntry> getEntries() {
return WikipediaApp.getEntriesFromCoordinates(
wikidata ? "wikidata" : wikipediaLang.get(), min, max);
return WikipediaApp.forLanguage(wikidata ? "wikidata" : wikipediaLang.get())
.getEntriesFromCoordinates(min, max);
}
}.execute();
} catch (Exception ex) {
......@@ -174,7 +174,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
entries.sort(null);
publish(entries.toArray(new WikipediaEntry[entries.size()]));
WikipediaApp.partitionList(entries, 20).forEach(chunk -> {
WikipediaApp.updateWIWOSMStatus(chunk.get(0).lang, chunk);
WikipediaApp.forLanguage(chunk.get(0).lang).updateWIWOSMStatus(chunk);
list.repaint();
});
return null;
......@@ -216,8 +216,8 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
new UpdateWikipediaArticlesSwingWorker() {
@Override
List<WikipediaEntry> getEntries() {
return WikipediaApp.getEntriesFromCategory(
wikipediaLang.get(), category, Main.pref.getInteger("wikipedia.depth", 3));
return WikipediaApp.forLanguage(wikipediaLang.get())
.getEntriesFromCategory(category, Main.pref.getInteger("wikipedia.depth", 3));
}
}.execute();
}
......@@ -340,7 +340,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
}
final LatLon latLon = entry.coordinate != null
? entry.coordinate
: WikipediaApp.getCoordinateForArticle(entry.lang, entry.article);
: WikipediaApp.forLanguage(entry.lang).getCoordinateForArticle(entry.article);
if (latLon == null) {
return;
}
......@@ -353,7 +353,7 @@ public class WikipediaToggleDialog extends ToggleDialog implements ActiveLayerCh
articles.clear();
if (Main.main != null && Main.getLayerManager().getEditDataSet() != null) {
Main.getLayerManager().getEditDataSet().allPrimitives().stream()
.flatMap(p -> WikipediaApp.getWikipediaArticles(language, p))
.flatMap(p -> WikipediaApp.forLanguage(language).getWikipediaArticles(p))
.forEach(articles::add);
}
}
......
......@@ -54,7 +54,7 @@ public class WikipediaAppTest {
@Test
public void testGetInterwikiArticles1() {
final Collection<WikipediaEntry> iw = WikipediaApp.getInterwikiArticles("de", "Österreich");
final Collection<WikipediaEntry> iw = WikipediaApp.forLanguage("de").getInterwikiArticles("Österreich");
assertThat(iw, hasItem(new WikipediaEntry("en", "Austria")));
assertThat(iw, hasItem(new WikipediaEntry("no", "Østerrike")));
assertThat(iw, hasItem(new WikipediaEntry("ko", "오스트리아")));
......@@ -62,21 +62,21 @@ public class WikipediaAppTest {
@Test
public void testGetInterwikiArticles2() {
final Collection<WikipediaEntry> iw = WikipediaApp.getInterwikiArticles("en", "Ampersand");
final Collection<WikipediaEntry> iw = WikipediaApp.forLanguage("en").getInterwikiArticles("Ampersand");
assertThat(iw, hasItem(new WikipediaEntry("fi", "&")));
}
@Test
public void testGetCoordinates() throws Exception {
assertThat(WikipediaApp.getCoordinateForArticle("de", "Marchreisenspitze"), is(new LatLon(47.1725, 11.30833333)));
assertThat(WikipediaApp.getCoordinateForArticle("en", "Austria"), is(new LatLon(47.33333333, 13.33333333)));
assertThat(WikipediaApp.getCoordinateForArticle("en", "Foobar2000"), nullValue());
assertThat(WikipediaApp.forLanguage("de").getCoordinateForArticle("Marchreisenspitze"), is(new LatLon(47.1725, 11.30833333)));
assertThat(WikipediaApp.forLanguage("en").getCoordinateForArticle("Austria"), is(new LatLon(47.33333333, 13.33333333)));
assertThat(WikipediaApp.forLanguage("en").getCoordinateForArticle("Foobar2000"), nullValue());
}
@Test
public void testFromCoordinates() throws Exception {
final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("de",
new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705));
final List<WikipediaEntry> entries = WikipediaApp.forLanguage("de")
.getEntriesFromCoordinates(new LatLon(52.5179786, 13.3753321), new LatLon(52.5192215, 13.3768705));
final long c = entries.stream()
.filter(entry -> "Reichstagsgebäude".equals(entry.article) && "de".equals(entry.lang))
.count();
......@@ -97,8 +97,8 @@ public class WikipediaAppTest {
@Test
public void testFromCoordinatesWikidata() throws Exception {
final List<WikipediaEntry> entries = WikipediaApp.getEntriesFromCoordinates("wikidata",
new LatLon(47.20, 11.30), new LatLon(47.22, 11.32));
final List<WikipediaEntry> entries = WikipediaApp.forLanguage("wikidata")
.getEntriesFromCoordinates(new LatLon(47.20, 11.30), new LatLon(47.22, 11.32));
final long c = entries.stream()
.map(WikidataEntry.class::cast)
.filter(entry -> "Q865406".equals(entry.article) && "wikidata".equals(entry.lang) && "Birgitzer Alm".equals(entry.label))
......@@ -108,8 +108,8 @@ public class WikipediaAppTest {
@Test
public void testGetWikidataForArticles() throws Exception {
final Map<String, String> map = WikipediaApp.getWikidataForArticles("en",
Arrays.asList("London", "Vienna", "Völs, Tyrol", "a-non-existing-article"));
final Map<String, String> map = WikipediaApp.forLanguage("en")
.getWikidataForArticles(Arrays.asList("London", "Vienna", "Völs, Tyrol", "a-non-existing-article"));
assertThat(map.get("London"), is("Q84"));
assertThat(map.get("Vienna"), is("Q1741"));
assertThat(map.get("Völs, Tyrol"), is("Q278250"));
......@@ -119,13 +119,13 @@ public class WikipediaAppTest {
@Test
public void testTicket13991() throws Exception {
final Map<String, String> map = WikipediaApp.getWikidataForArticles("en",
Stream.iterate("London", x -> x).limit(100).collect(Collectors.toList()));
final Map<String, String> map = WikipediaApp.forLanguage("en")
.getWikidataForArticles(Stream.iterate("London", x -> x).limit(100).collect(Collectors.toList()));
assertThat(map, is(Collections.singletonMap("London", "Q84")));
final List<String> articles = IntStream.range(0, 200)
.mapToObj(i -> "a-non-existing-article-" + i)
.collect(Collectors.toList());
assertTrue(WikipediaApp.getWikidataForArticles("en", articles).isEmpty());
assertTrue(WikipediaApp.forLanguage("en").getWikidataForArticles(articles).isEmpty());
}
@Test
......@@ -153,7 +153,7 @@ public class WikipediaAppTest {
final WikipediaEntry entry1 = new WikipediaEntry("en", "Vienna");
final WikipediaEntry entry2 = new WikipediaEntry("en", "London");
final WikipediaEntry entry3 = new WikipediaEntry("en", "a-non-existing-article");
WikipediaApp.updateWIWOSMStatus("en", Arrays.asList(entry1, entry2, entry3));
WikipediaApp.forLanguage("en").updateWIWOSMStatus(Arrays.asList(entry1, entry2, entry3));
assertThat(entry1.getWiwosmStatus(), is(true));
assertThat(entry2.getWiwosmStatus(), is(true));
assertThat(entry3.getWiwosmStatus(), is(false));
......@@ -161,7 +161,7 @@ public class WikipediaAppTest {
@Test
public void testCategoriesForPrefix() throws Exception {
final List<String> categories = WikipediaApp.getCategoriesForPrefix("de", "Gemeinde in Öster");
final List<String> categories = WikipediaApp.forLanguage("de").getCategoriesForPrefix("Gemeinde in Öster");
assertTrue(categories.contains("Gemeinde in Österreich"));
}
}
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