mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Merge pull request #606 from maskaravivek/nearbyQuery
Fixed nearby query to show the read article link only if there's a wikipedia article
This commit is contained in:
commit
f1785114ac
6 changed files with 143 additions and 31 deletions
|
|
@ -6,7 +6,8 @@ SELECT
|
||||||
(SAMPLE(COALESCE(?class_label_preferred_language, ?class_label_any_language, "?")) as ?class_label)
|
(SAMPLE(COALESCE(?class_label_preferred_language, ?class_label_any_language, "?")) as ?class_label)
|
||||||
(SAMPLE(COALESCE(?icon0, ?icon1)) as ?icon)
|
(SAMPLE(COALESCE(?icon0, ?icon1)) as ?icon)
|
||||||
(SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)
|
(SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)
|
||||||
(SAMPLE(?sitelink) as ?sitelink)
|
?wikipediaArticle
|
||||||
|
?commonsArticle
|
||||||
WHERE {
|
WHERE {
|
||||||
# Around given location...
|
# Around given location...
|
||||||
SERVICE wikibase:around {
|
SERVICE wikibase:around {
|
||||||
|
|
@ -37,7 +38,18 @@ SELECT
|
||||||
OPTIONAL {
|
OPTIONAL {
|
||||||
?sitelink schema:about ?item .
|
?sitelink schema:about ?item .
|
||||||
?sitelink schema:inLanguage "en"
|
?sitelink schema:inLanguage "en"
|
||||||
}
|
}
|
||||||
|
OPTIONAL {
|
||||||
|
?wikipediaArticle schema:about ?item ;
|
||||||
|
schema:isPartOf <https://en.wikipedia.org/> .
|
||||||
|
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
|
||||||
|
}
|
||||||
|
|
||||||
|
OPTIONAL {
|
||||||
|
?commonsArticle schema:about ?item ;
|
||||||
|
schema:isPartOf <https://commons.wikimedia.org/> .
|
||||||
|
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GROUP BY ?item
|
GROUP BY ?item ?wikipediaArticle ?commonsArticle
|
||||||
|
|
@ -14,7 +14,6 @@ import butterknife.ButterKnife;
|
||||||
import butterknife.OnClick;
|
import butterknife.OnClick;
|
||||||
import butterknife.Unbinder;
|
import butterknife.Unbinder;
|
||||||
import fr.free.nrw.commons.R;
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.Utils;
|
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
import fr.free.nrw.commons.ui.widget.OverlayDialog;
|
import fr.free.nrw.commons.ui.widget.OverlayDialog;
|
||||||
import fr.free.nrw.commons.utils.DialogUtil;
|
import fr.free.nrw.commons.utils.DialogUtil;
|
||||||
|
|
@ -25,8 +24,7 @@ public class NearbyInfoDialog extends OverlayDialog {
|
||||||
private final static String ARG_DESC = "placeDesc";
|
private final static String ARG_DESC = "placeDesc";
|
||||||
private final static String ARG_LATITUDE = "latitude";
|
private final static String ARG_LATITUDE = "latitude";
|
||||||
private final static String ARG_LONGITUDE = "longitude";
|
private final static String ARG_LONGITUDE = "longitude";
|
||||||
private final static String ARG_ARTICLE_LINK = "articleLink";
|
private final static String ARG_SITE_LINK = "sitelink";
|
||||||
private final static String ARG_WIKI_DATA_LINK = "wikiDataLink";
|
|
||||||
|
|
||||||
@BindView(R.id.link_preview_title)
|
@BindView(R.id.link_preview_title)
|
||||||
TextView placeTitle;
|
TextView placeTitle;
|
||||||
|
|
@ -58,15 +56,10 @@ public class NearbyInfoDialog extends OverlayDialog {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getArticleLink(Bundle bundle) {
|
private void getArticleLink(Bundle bundle) {
|
||||||
String articleLink = bundle.getString(ARG_ARTICLE_LINK);
|
Sitelinks sitelinks = bundle.getParcelable(ARG_SITE_LINK);
|
||||||
articleLink = articleLink.replace("<", "").replace(">", "");
|
|
||||||
|
|
||||||
if (Utils.isNullOrWhiteSpace(articleLink) || articleLink == "\n") {
|
if (sitelinks.getWikipediaLink() != null) {
|
||||||
articleLink = bundle.getString(ARG_WIKI_DATA_LINK).replace("<", "").replace(">", "");
|
this.articleLink = sitelinks.getWikipediaLink();
|
||||||
}
|
|
||||||
|
|
||||||
if (!Utils.isNullOrWhiteSpace(articleLink) && articleLink != "\n") {
|
|
||||||
this.articleLink = Uri.parse(articleLink);
|
|
||||||
} else {
|
} else {
|
||||||
goToButton.setVisibility(View.GONE);
|
goToButton.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
@ -79,8 +72,7 @@ public class NearbyInfoDialog extends OverlayDialog {
|
||||||
bundle.putString(ARG_DESC, place.description);
|
bundle.putString(ARG_DESC, place.description);
|
||||||
bundle.putDouble(ARG_LATITUDE, place.location.latitude);
|
bundle.putDouble(ARG_LATITUDE, place.location.latitude);
|
||||||
bundle.putDouble(ARG_LONGITUDE, place.location.longitude);
|
bundle.putDouble(ARG_LONGITUDE, place.location.longitude);
|
||||||
bundle.putString(ARG_ARTICLE_LINK, place.siteLink.toString());
|
bundle.putParcelable(ARG_SITE_LINK, place.siteLinks);
|
||||||
bundle.putString(ARG_WIKI_DATA_LINK, place.wikiDataLink.toString());
|
|
||||||
mDialog.setArguments(bundle);
|
mDialog.setArguments(bundle);
|
||||||
DialogUtil.showSafely(fragmentActivity, mDialog);
|
DialogUtil.showSafely(fragmentActivity, mDialog);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,17 +70,19 @@ public class NearbyPlaces {
|
||||||
List<Place> places = new ArrayList<>();
|
List<Place> places = new ArrayList<>();
|
||||||
|
|
||||||
String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
|
String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
|
||||||
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
|
.replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius))
|
||||||
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
|
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
|
||||||
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
|
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
|
||||||
.replace("${LANG}", lang);
|
.replace("${LANG}", lang);
|
||||||
Timber.v(query);
|
|
||||||
|
Timber.d("Wikidata query "+ query);
|
||||||
|
|
||||||
// format as a URL
|
// format as a URL
|
||||||
String url = WIKIDATA_QUERY_URL.replace(
|
String url = WIKIDATA_QUERY_URL.replace(
|
||||||
"${QUERY}",
|
"${QUERY}",
|
||||||
URLEncoder.encode(query, "utf-8").replace("+", "%20")
|
URLEncoder.encode(query, "utf-8").replace("+", "%20")
|
||||||
);
|
);
|
||||||
|
|
||||||
Timber.d(url);
|
Timber.d(url);
|
||||||
URLConnection conn = new URL(url).openConnection();
|
URLConnection conn = new URL(url).openConnection();
|
||||||
conn.setRequestProperty("Accept", "text/tab-separated-values");
|
conn.setRequestProperty("Accept", "text/tab-separated-values");
|
||||||
|
|
@ -99,8 +101,9 @@ public class NearbyPlaces {
|
||||||
String point = fields[0];
|
String point = fields[0];
|
||||||
String name = Utils.stripLocalizedString(fields[2]);
|
String name = Utils.stripLocalizedString(fields[2]);
|
||||||
String type = Utils.stripLocalizedString(fields[4]);
|
String type = Utils.stripLocalizedString(fields[4]);
|
||||||
String sitelink = Utils.stripLocalizedString(fields[7]);
|
String wikipediaSitelink = Utils.stripLocalizedString(fields[7]);
|
||||||
String wikiDataLink = Utils.stripLocalizedString(fields[3]);
|
String commonsSitelink = Utils.stripLocalizedString(fields[8]);
|
||||||
|
String wikiDataLink = Utils.stripLocalizedString(fields[1]);
|
||||||
String icon = fields[5];
|
String icon = fields[5];
|
||||||
|
|
||||||
double latitude = 0;
|
double latitude = 0;
|
||||||
|
|
@ -123,8 +126,11 @@ public class NearbyPlaces {
|
||||||
type, // details
|
type, // details
|
||||||
Uri.parse(icon),
|
Uri.parse(icon),
|
||||||
new LatLng(latitude, longitude),
|
new LatLng(latitude, longitude),
|
||||||
Uri.parse(sitelink),
|
new Sitelinks.Builder()
|
||||||
Uri.parse(wikiDataLink)
|
.setWikipediaLink(wikipediaSitelink)
|
||||||
|
.setCommonsLink(commonsSitelink)
|
||||||
|
.setWikidataLink(wikiDataLink)
|
||||||
|
.build()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
|
|
@ -182,8 +188,7 @@ public class NearbyPlaces {
|
||||||
type, // details
|
type, // details
|
||||||
null,
|
null,
|
||||||
new LatLng(latitude, longitude),
|
new LatLng(latitude, longitude),
|
||||||
null,
|
new Sitelinks.Builder().build()
|
||||||
null
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
|
|
|
||||||
|
|
@ -16,19 +16,17 @@ public class Place {
|
||||||
public Bitmap image;
|
public Bitmap image;
|
||||||
public Bitmap secondaryImage;
|
public Bitmap secondaryImage;
|
||||||
public String distance;
|
public String distance;
|
||||||
public Uri siteLink;
|
public Sitelinks siteLinks;
|
||||||
public Uri wikiDataLink;
|
|
||||||
|
|
||||||
|
|
||||||
public Place(String name, String description, String longDescription,
|
public Place(String name, String description, String longDescription,
|
||||||
Uri secondaryImageUrl, LatLng location, Uri siteLink, Uri wikiDataLink) {
|
Uri secondaryImageUrl, LatLng location, Sitelinks siteLinks) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.longDescription = longDescription;
|
this.longDescription = longDescription;
|
||||||
this.secondaryImageUrl = secondaryImageUrl;
|
this.secondaryImageUrl = secondaryImageUrl;
|
||||||
this.location = location;
|
this.location = location;
|
||||||
this.siteLink = siteLink;
|
this.siteLinks = siteLinks;
|
||||||
this.wikiDataLink = wikiDataLink;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDistance(String distance) {
|
public void setDistance(String distance) {
|
||||||
|
|
|
||||||
103
app/src/main/java/fr/free/nrw/commons/nearby/Sitelinks.java
Normal file
103
app/src/main/java/fr/free/nrw/commons/nearby/Sitelinks.java
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.Utils;
|
||||||
|
|
||||||
|
public class Sitelinks implements Parcelable {
|
||||||
|
private final String wikipediaLink;
|
||||||
|
private final String commonsLink;
|
||||||
|
private final String wikidataLink;
|
||||||
|
|
||||||
|
|
||||||
|
protected Sitelinks(Parcel in) {
|
||||||
|
wikipediaLink = in.readString();
|
||||||
|
commonsLink = in.readString();
|
||||||
|
wikidataLink = in.readString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeString(wikipediaLink);
|
||||||
|
dest.writeString(commonsLink);
|
||||||
|
dest.writeString(wikidataLink);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Creator<Sitelinks> CREATOR = new Creator<Sitelinks>() {
|
||||||
|
@Override
|
||||||
|
public Sitelinks createFromParcel(Parcel in) {
|
||||||
|
return new Sitelinks(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Sitelinks[] newArray(int size) {
|
||||||
|
return new Sitelinks[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Uri getWikipediaLink() {
|
||||||
|
return sanitiseString(wikipediaLink);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Uri getCommonsLink() {
|
||||||
|
return sanitiseString(commonsLink);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Uri getWikidataLink() {
|
||||||
|
return sanitiseString(wikidataLink);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Uri sanitiseString(String stringUrl) {
|
||||||
|
stringUrl = stringUrl.replace("<", "").replace(">", "");
|
||||||
|
if (!Utils.isNullOrWhiteSpace(stringUrl) && stringUrl != null) {
|
||||||
|
return Uri.parse(stringUrl);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sitelinks(Sitelinks.Builder builder) {
|
||||||
|
this.wikidataLink = builder.wikidataLink;
|
||||||
|
this.wikipediaLink = builder.wikipediaLink;
|
||||||
|
this.commonsLink = builder.commonsLink;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private String wikidataLink;
|
||||||
|
private String commonsLink;
|
||||||
|
private String wikipediaLink;
|
||||||
|
|
||||||
|
public Builder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sitelinks.Builder setWikipediaLink(String link) {
|
||||||
|
this.wikipediaLink = link;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sitelinks.Builder setWikidataLink(String link) {
|
||||||
|
this.wikidataLink = link;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sitelinks.Builder setCommonsLink(@Nullable String link) {
|
||||||
|
this.commonsLink = link;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sitelinks build() {
|
||||||
|
return new Sitelinks(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,19 +6,21 @@ import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
public static String readFromFile(Context context, String fileName) {
|
public static String readFromFile(Context context, String fileName) {
|
||||||
String stringBuilder = "";
|
String stringBuilder = "";
|
||||||
BufferedReader reader = null;
|
BufferedReader reader = null;
|
||||||
try {
|
try {
|
||||||
reader = new BufferedReader(
|
reader = new BufferedReader(
|
||||||
new InputStreamReader(context.getAssets().open(fileName), "UTF-8"));
|
new InputStreamReader(context.getAssets().open(fileName)));
|
||||||
String mLine;
|
String mLine;
|
||||||
while ((mLine = reader.readLine()) != null) {
|
while ((mLine = reader.readLine()) != null) {
|
||||||
stringBuilder += mLine + "\n";
|
stringBuilder += mLine + "\n";
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
//log the exception
|
Timber.e("File not found exception", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (reader != null) {
|
if (reader != null) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue