From ffb9af1f1c2fc35dcc8a636c77bd683229248d2c Mon Sep 17 00:00:00 2001 From: Sonal Yadav Date: Tue, 5 Aug 2025 09:43:40 +0530 Subject: [PATCH] Support both "label" and "itemLabel" for NearbyResultItem mapping (#6386) * Support both label and itemLabel for robust NearbyResultItem mapping. * fix code style * Add getOriginalLabel() for Wikidata edits to avoid fallback issues with itemLabel * Fix Wikidata edit failure by resetting hasInvalidLocation flag on upload confirmation --------- Co-authored-by: Sonal Yadav --- .../commons/nearby/model/NearbyResultItem.kt | 13 +++- .../mediaDetails/UploadMediaDetailFragment.kt | 1 + .../main/resources/queries/query_for_item.rq | 59 +++++++------------ 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyResultItem.kt b/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyResultItem.kt index c39d8901d..1d5d7bd80 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyResultItem.kt +++ b/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyResultItem.kt @@ -7,7 +7,8 @@ class NearbyResultItem( private val wikipediaArticle: ResultTuple?, private val commonsArticle: ResultTuple?, private val location: ResultTuple?, - private val label: ResultTuple?, + @field:SerializedName("label") private val label: ResultTuple?, + @field:SerializedName("itemLabel") private val itemLabel: ResultTuple?, @field:SerializedName("streetAddress") private val address: ResultTuple?, private val icon: ResultTuple?, @field:SerializedName("class") private val className: ResultTuple?, @@ -29,7 +30,15 @@ class NearbyResultItem( fun getLocation(): ResultTuple = location ?: ResultTuple() - fun getLabel(): ResultTuple = label ?: ResultTuple() + /** + * Returns label for display (pins, popup), using fallback to itemLabel if needed. + */ + fun getLabel(): ResultTuple = label ?: itemLabel ?: ResultTuple() + + /** + * Returns only the original label field, for Wikidata edits. + */ + fun getOriginalLabel(): ResultTuple = label ?: ResultTuple() fun getIcon(): ResultTuple = icon ?: ResultTuple() diff --git a/app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailFragment.kt b/app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailFragment.kt index 4a4c13ba7..43a9c3236 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailFragment.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailFragment.kt @@ -821,6 +821,7 @@ class UploadMediaDetailFragment : UploadBaseFragment(), UploadMediaDetailsContra { showProgress(false) uploadItem.imageQuality = IMAGE_OK + uploadItem.hasInvalidLocation = false // Reset invalid location flag when user confirms upload }, { presenterCallback!!.deletePictureAtIndex(index) diff --git a/app/src/main/resources/queries/query_for_item.rq b/app/src/main/resources/queries/query_for_item.rq index 2957b9b5b..fdc349d18 100644 --- a/app/src/main/resources/queries/query_for_item.rq +++ b/app/src/main/resources/queries/query_for_item.rq @@ -1,50 +1,32 @@ SELECT ?item - (SAMPLE(?label) AS ?label) - (SAMPLE(?class) AS ?class) - (SAMPLE(?description) AS ?description) - (SAMPLE(?classLabel) AS ?classLabel) - (SAMPLE(?pic) AS ?pic) - (SAMPLE(?destroyed) AS ?destroyed) - (SAMPLE(?endTime) AS ?endTime) - (SAMPLE(?wikipediaArticle) AS ?wikipediaArticle) - (SAMPLE(?commonsArticle) AS ?commonsArticle) - (SAMPLE(?commonsCategory) AS ?commonsCategory) - (SAMPLE(?dateOfOfficialClosure) AS ?dateOfOfficialClosure) - (SAMPLE(?pointInTime) AS ?pointInTime) + ?itemLabel + ?itemDescription + ?class + ?classLabel + ?pic + ?destroyed + ?endTime + ?wikipediaArticle + ?commonsArticle + ?commonsCategory + ?dateOfOfficialClosure + ?pointInTime WHERE { SERVICE { - values ?item { - ${ENTITY} - } + VALUES ?item {${ENTITY}} } - # Get the label in the preferred language of the user, or any other language if no label is available in that language. - OPTIONAL {?item rdfs:label ?itemLabelPreferredLanguage. FILTER (lang(?itemLabelPreferredLanguage) = "${LANG}")} - OPTIONAL {?item rdfs:label ?itemLabelAnyLanguage} - BIND(COALESCE(?itemLabelPreferredLanguage, ?itemLabelAnyLanguage, "?") as ?label) + # Get item label/class label/description in the preferred language of the user, or fallback. + SERVICE wikibase:label { bd:serviceParam wikibase:language "${LANG},en,aa,ab,ae,af,ak,am,an,ar,as,av,ay,az,ba,be,bg,bh,bi,bm,bn,bo,br,bs,ca,ce,ch,co,cr,cs,cu,cv,cy,da,de,dv,dz,ee,el,eo,es,et,eu,fa,ff,fi,fj,fo,fr,fy,ga,gd,gl,gn,gu,gv,ha,he,hi,ho,hr,ht,hu,hy,hz,ia,id,ie,ig,ii,ik,io,is,it,iu,ja,jv,ka,kg,ki,kj,kk,kl,km,kn,ko,kr,ks,ku,kv,kw,ky,la,lb,lg,li,ln,lo,lt,lu,lv,mg,mh,mi,mk,ml,mn,mo,mr,ms,mt,my,na,nb,nd,ne,ng,nl,nn,no,ny,oc,oj,om,or,os,pa,pi,pl,ps,pt,qu,rm,rn,ro,ru,rw,sa,sc,sd,se,sg,sh,si,sk,sl,sm,sn,so,sq,sr,ss,st,su,sv,sw,ta,te,tg,th,ti,tk,tl,tn,to,tr,ts,tt,tw,ty,ug,uk,ur,uz,ve,vi,vo,wa,wo,xh,yi,yo,za,zh,zu". } - # Get the description in the preferred language of the user, or any other language if no description is available in that language. - OPTIONAL {?item schema:description ?itemDescriptionPreferredLanguage. FILTER (lang(?itemDescriptionPreferredLanguage) = "${LANG}")} - OPTIONAL {?item schema:description ?itemDescriptionAnyLanguage} - BIND(COALESCE(?itemDescriptionPreferredLanguage, ?itemDescriptionAnyLanguage, "?") as ?description) + # Get class (such as forest or bridge) + OPTIONAL {?item p:P31/ps:P31 ?class} - # Get the class label in the preferred language of the user, or any other language if no label is available in that language. - OPTIONAL { - ?item p:P31/ps:P31 ?class. - OPTIONAL {?class rdfs:label ?classLabelPreferredLanguage. FILTER (lang(?classLabelPreferredLanguage) = "${LANG}")} - OPTIONAL {?class rdfs:label ?classLabelAnyLanguage} - BIND(COALESCE(?classLabelPreferredLanguage, ?classLabelAnyLanguage, "?") as ?classLabel) - } - - OPTIONAL { - ?item p:P31/ps:P31 ?class. - } - - # Get picture + # Get picture (items without a picture will be shown in red on the Nearby map) OPTIONAL {?item wdt:P18 ?pic} - # Get existence + # Get existence (whether an item still exists or not) OPTIONAL {?item wdt:P576 ?destroyed} OPTIONAL {?item wdt:P582 ?endTime} OPTIONAL {?item wdt:P3999 ?dateOfOfficialClosure} @@ -64,5 +46,4 @@ WHERE { ?commonsArticle schema:about ?item. ?commonsArticle schema:isPartOf . } -} -GROUP BY ?item \ No newline at end of file +} \ No newline at end of file