Fix p18 issue For an item with P18 item, do not add another one (#3527)

* Add p18value variable to contrib

* set place.pic to candidate contribution

* Add p18 value to contrib

* Passes p18 value to wikidata upload service

* Checks if pic parameter of the wikidata item is empty or not. If not, it does not overrides the existing image, it is just a regular commons upload.

* Make public var private

* Make current tests pass

* Add test case for p18 value is not empty

* Fix wrong log message

* Add nonnul annotation and fix method javadoc
This commit is contained in:
neslihanturan 2020-03-17 12:35:21 +02:00 committed by GitHub
parent bdb61dfda6
commit e5d5a7af92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 10 deletions

View file

@ -68,6 +68,7 @@ public class Contribution extends Media {
public String decimalCoords; public String decimalCoords;
public boolean isMultiple; public boolean isMultiple;
public String wikiDataEntityId; public String wikiDataEntityId;
private String p18Value;
public Uri contentProviderUri; public Uri contentProviderUri;
public String dateCreatedSource; public String dateCreatedSource;
@ -271,6 +272,19 @@ public class Contribution extends Media {
this.wikiDataEntityId = wikiDataEntityId; this.wikiDataEntityId = wikiDataEntityId;
} }
public String getP18Value() {
return p18Value;
}
/**
* When the corresponding image property of wiki entity is known as in case of nearby uploads,
* it can be set using the setter method
* @param p18Value p18 value, image property of the wikidata item
*/
public void setP18Value(String p18Value) {
this.p18Value = p18Value;
}
public void setContentProviderUri(Uri contentProviderUri) { public void setContentProviderUri(Uri contentProviderUri) {
this.contentProviderUri = contentProviderUri; this.contentProviderUri = contentProviderUri;
} }

View file

@ -197,6 +197,8 @@ public class UploadModel {
CommonsApplication.DEFAULT_EDIT_SUMMARY, item.gpsCoords.getCoords()); CommonsApplication.DEFAULT_EDIT_SUMMARY, item.gpsCoords.getCoords());
if (item.place != null) { if (item.place != null) {
contribution.setWikiDataEntityId(item.place.getWikiDataEntityId()); contribution.setWikiDataEntityId(item.place.getWikiDataEntityId());
// If item already has an image, we need to know it. We don't want to override existing image later
contribution.setP18Value(item.place.pic);
} }
if (null == selectedCategories) {//Just a fail safe, this should never be null if (null == selectedCategories) {//Just a fail safe, this should never be null
selectedCategories = new ArrayList<>(); selectedCategories = new ArrayList<>();

View file

@ -278,9 +278,10 @@ public class UploadService extends HandlerService<Contribution> {
showFailedNotification(contribution); showFailedNotification(contribution);
} else { } else {
String canonicalFilename = "File:" + uploadResult.getFilename(); String canonicalFilename = "File:" + uploadResult.getFilename();
Timber.d("Contribution upload success. Initiating Wikidata edit for entity id %s", Timber.d("Contribution upload success. Initiating Wikidata edit for"
contribution.getWikiDataEntityId()); + " entity id %s if necessary (if P18 is null). P18 value is %s",
wikidataEditService.createClaimWithLogging(contribution.getWikiDataEntityId(), canonicalFilename); contribution.getWikiDataEntityId(), contribution.getP18Value());
wikidataEditService.createClaimWithLogging(contribution.getWikiDataEntityId(), canonicalFilename, contribution.getP18Value());
contribution.setFilename(canonicalFilename); contribution.setFilename(canonicalFilename);
contribution.setImageUrl(uploadResult.getImageinfo().getOriginalUrl()); contribution.setImageUrl(uploadResult.getImageinfo().getOriginalUrl());
contribution.setState(Contribution.STATE_COMPLETED); contribution.setState(Contribution.STATE_COMPLETED);

View file

@ -3,6 +3,7 @@ package fr.free.nrw.commons.wikidata;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull;
import java.util.Locale; import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
@ -45,10 +46,11 @@ public class WikidataEditService {
/** /**
* Create a P18 claim and log the edit with custom tag * Create a P18 claim and log the edit with custom tag
* @param wikidataEntityId * @param wikidataEntityId a unique id of each Wikidata items
* @param fileName * @param fileName name of the file we will upload
* @param p18Value pic attribute of Wikidata item
*/ */
public void createClaimWithLogging(String wikidataEntityId, String fileName) { public void createClaimWithLogging(String wikidataEntityId, String fileName, @NonNull String p18Value) {
if (wikidataEntityId == null) { if (wikidataEntityId == null) {
Timber.d("Skipping creation of claim as Wikidata entity ID is null"); Timber.d("Skipping creation of claim as Wikidata entity ID is null");
return; return;
@ -64,6 +66,11 @@ public class WikidataEditService {
return; return;
} }
if (!p18Value.trim().isEmpty()) {
Timber.d("Skipping creation of claim as p18Value is not empty, we won't override existing image");
return;
}
editWikidataProperty(wikidataEntityId, fileName); editWikidataProperty(wikidataEntityId, fileName);
} }

View file

@ -34,13 +34,19 @@ class WikidataEditServiceTest {
@Test @Test
fun noClaimsWhenEntityIdIsNull() { fun noClaimsWhenEntityIdIsNull() {
wikidataEditService!!.createClaimWithLogging(null, "Test.jpg") wikidataEditService!!.createClaimWithLogging(null, "Test.jpg","")
verifyZeroInteractions(wikidataClient!!) verifyZeroInteractions(wikidataClient!!)
} }
@Test @Test
fun noClaimsWhenFileNameIsNull() { fun noClaimsWhenFileNameIsNull() {
wikidataEditService!!.createClaimWithLogging("Q1", null) wikidataEditService!!.createClaimWithLogging("Q1", null,"")
verifyZeroInteractions(wikidataClient!!)
}
@Test
fun noClaimsWhenP18IsNotEmpty() {
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg","Previous.jpg")
verifyZeroInteractions(wikidataClient!!) verifyZeroInteractions(wikidataClient!!)
} }
@ -48,7 +54,7 @@ class WikidataEditServiceTest {
fun noClaimsWhenLocationIsNotCorrect() { fun noClaimsWhenLocationIsNotCorrect() {
`when`(directKvStore!!.getBoolean("Picture_Has_Correct_Location", true)) `when`(directKvStore!!.getBoolean("Picture_Has_Correct_Location", true))
.thenReturn(false) .thenReturn(false)
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg") wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg","")
verifyZeroInteractions(wikidataClient!!) verifyZeroInteractions(wikidataClient!!)
} }
@ -60,7 +66,7 @@ class WikidataEditServiceTest {
.thenReturn(Observable.just(1L)) .thenReturn(Observable.just(1L))
`when`(wikidataClient!!.addEditTag(anyLong(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())) `when`(wikidataClient!!.addEditTag(anyLong(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(mock(AddEditTagResponse::class.java))) .thenReturn(Observable.just(mock(AddEditTagResponse::class.java)))
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg") wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg","")
verify(wikidataClient!!, times(1)) verify(wikidataClient!!, times(1))
.createClaim(ArgumentMatchers.anyString(), ArgumentMatchers.anyString()) .createClaim(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())
} }