Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Infer Wikimedia Commons category from Wikidata to show photos 12303 #18141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions OsmAnd-java/src/main/java/net/osmand/wiki/WikiCoreHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class WikiCoreHelper {
private static final Log LOG = PlatformUtil.getLog(WikiCoreHelper.class);
private static final String WIKIMEDIA_API_ENDPOINT = "https://commons.wikimedia.org/w/api.php";
private static final String WIKIDATA_API_ENDPOINT = "https://www.wikidata.org/w/api.php";
private static final String WIKIDATA_ACTION = "?action=wbgetclaims&property=P18&entity=";
private static final String WIKIDATA_GET_CLAIMS_P18 = "?action=wbgetclaims&property=P18&entity=";
private static final String WIKIDATA_GET_CLAIMS_P373 = "?action=wbgetclaims&property=P373&entity=";
private static final String WIKIMEDIA_ACTION = "?action=query&list=categorymembers&cmtitle=";
private static final String CM_LIMIT = "&cmlimit=500";
private static final String FORMAT_JSON = "&format=json";
Expand Down Expand Up @@ -60,17 +61,33 @@ public static List<WikiImage> getWikimediaImageList(String wikiMediaTagContent,

public static List<WikiImage> getWikidataImageList(String wikidataId) {
List<WikiImage> wikiImages = new ArrayList<>();
String url = WIKIDATA_API_ENDPOINT + WIKIDATA_ACTION + wikidataId + FORMAT_JSON;
getImageList(wikidataId, wikiImages);
getCategoryImageList(wikidataId, wikiImages);
return wikiImages;
}

private static void getImageList(String wikidataId, List<WikiImage> wikiImages) {
String url = WIKIDATA_API_ENDPOINT + WIKIDATA_GET_CLAIMS_P18 + wikidataId + FORMAT_JSON;
WikidataResponse response = sendWikipediaApiRequest(url, WikidataResponse.class);
if (response != null && response.claims != null && response.claims.p18 != null) {
for (P18 p18 : response.claims.p18) {
for (Property p18 : response.claims.p18) {
String imageFileName = p18.mainsnak.datavalue.value;
if (imageFileName != null) {
wikiImages.add(getImageData(imageFileName));
}
}
}
return wikiImages;
}

private static void getCategoryImageList(String wikidataId, List<WikiImage> wikiImages) {
String url = WIKIDATA_API_ENDPOINT + WIKIDATA_GET_CLAIMS_P373 + wikidataId + FORMAT_JSON;
WikidataResponse response = sendWikipediaApiRequest(url, WikidataResponse.class);
if (response != null && response.claims != null && response.claims.p373 != null) {
for (Property p373 : response.claims.p373) {
String imageFileName = WIKIMEDIA_CATEGORY + p373.mainsnak.datavalue.value;
getWikimediaImageList(imageFileName, wikiImages);
}
}
}

public static WikiImage getImageData(String imageFileName) {
Expand Down Expand Up @@ -113,12 +130,15 @@ public static class WikidataResponse {
}

public static class Claims {
@SerializedName("P18")
@SerializedName("P18") // image https://www.wikidata.org/wiki/Property:P18
@Expose
private final List<Property> p18 = null;
@SerializedName("P373") // Commons category https://www.wikidata.org/wiki/Property:P373
@Expose
private final List<P18> p18 = null;
private final List<Property> p373 = null;
}

public static class P18 {
public static class Property {
@SerializedName("mainsnak")
@Expose
private Mainsnak mainsnak;
Expand Down