Skip to content

Commit e5d5a7a

Browse files
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
1 parent bdb61df commit e5d5a7a

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/Contribution.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public Contribution[] newArray(int i) {
6868
public String decimalCoords;
6969
public boolean isMultiple;
7070
public String wikiDataEntityId;
71+
private String p18Value;
7172
public Uri contentProviderUri;
7273
public String dateCreatedSource;
7374

@@ -271,6 +272,19 @@ public void setWikiDataEntityId(String wikiDataEntityId) {
271272
this.wikiDataEntityId = wikiDataEntityId;
272273
}
273274

275+
public String getP18Value() {
276+
return p18Value;
277+
}
278+
279+
/**
280+
* When the corresponding image property of wiki entity is known as in case of nearby uploads,
281+
* it can be set using the setter method
282+
* @param p18Value p18 value, image property of the wikidata item
283+
*/
284+
public void setP18Value(String p18Value) {
285+
this.p18Value = p18Value;
286+
}
287+
274288
public void setContentProviderUri(Uri contentProviderUri) {
275289
this.contentProviderUri = contentProviderUri;
276290
}

app/src/main/java/fr/free/nrw/commons/upload/UploadModel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public Observable<Contribution> buildContributions() {
197197
CommonsApplication.DEFAULT_EDIT_SUMMARY, item.gpsCoords.getCoords());
198198
if (item.place != null) {
199199
contribution.setWikiDataEntityId(item.place.getWikiDataEntityId());
200+
// If item already has an image, we need to know it. We don't want to override existing image later
201+
contribution.setP18Value(item.place.pic);
200202
}
201203
if (null == selectedCategories) {//Just a fail safe, this should never be null
202204
selectedCategories = new ArrayList<>();

app/src/main/java/fr/free/nrw/commons/upload/UploadService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,10 @@ private void uploadContribution(Contribution contribution) {
278278
showFailedNotification(contribution);
279279
} else {
280280
String canonicalFilename = "File:" + uploadResult.getFilename();
281-
Timber.d("Contribution upload success. Initiating Wikidata edit for entity id %s",
282-
contribution.getWikiDataEntityId());
283-
wikidataEditService.createClaimWithLogging(contribution.getWikiDataEntityId(), canonicalFilename);
281+
Timber.d("Contribution upload success. Initiating Wikidata edit for"
282+
+ " entity id %s if necessary (if P18 is null). P18 value is %s",
283+
contribution.getWikiDataEntityId(), contribution.getP18Value());
284+
wikidataEditService.createClaimWithLogging(contribution.getWikiDataEntityId(), canonicalFilename, contribution.getP18Value());
284285
contribution.setFilename(canonicalFilename);
285286
contribution.setImageUrl(uploadResult.getImageinfo().getOriginalUrl());
286287
contribution.setState(Contribution.STATE_COMPLETED);

app/src/main/java/fr/free/nrw/commons/wikidata/WikidataEditService.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55

6+
import androidx.annotation.NonNull;
67
import java.util.Locale;
78

89
import javax.inject.Inject;
@@ -45,10 +46,11 @@ public class WikidataEditService {
4546

4647
/**
4748
* Create a P18 claim and log the edit with custom tag
48-
* @param wikidataEntityId
49-
* @param fileName
49+
* @param wikidataEntityId a unique id of each Wikidata items
50+
* @param fileName name of the file we will upload
51+
* @param p18Value pic attribute of Wikidata item
5052
*/
51-
public void createClaimWithLogging(String wikidataEntityId, String fileName) {
53+
public void createClaimWithLogging(String wikidataEntityId, String fileName, @NonNull String p18Value) {
5254
if (wikidataEntityId == null) {
5355
Timber.d("Skipping creation of claim as Wikidata entity ID is null");
5456
return;
@@ -64,6 +66,11 @@ public void createClaimWithLogging(String wikidataEntityId, String fileName) {
6466
return;
6567
}
6668

69+
if (!p18Value.trim().isEmpty()) {
70+
Timber.d("Skipping creation of claim as p18Value is not empty, we won't override existing image");
71+
return;
72+
}
73+
6774
editWikidataProperty(wikidataEntityId, fileName);
6875
}
6976

app/src/test/kotlin/fr/free/nrw/commons/wikidata/WikidataEditServiceTest.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,27 @@ class WikidataEditServiceTest {
3434

3535
@Test
3636
fun noClaimsWhenEntityIdIsNull() {
37-
wikidataEditService!!.createClaimWithLogging(null, "Test.jpg")
37+
wikidataEditService!!.createClaimWithLogging(null, "Test.jpg","")
3838
verifyZeroInteractions(wikidataClient!!)
3939
}
4040

4141
@Test
4242
fun noClaimsWhenFileNameIsNull() {
43-
wikidataEditService!!.createClaimWithLogging("Q1", null)
43+
wikidataEditService!!.createClaimWithLogging("Q1", null,"")
44+
verifyZeroInteractions(wikidataClient!!)
45+
}
46+
47+
@Test
48+
fun noClaimsWhenP18IsNotEmpty() {
49+
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg","Previous.jpg")
4450
verifyZeroInteractions(wikidataClient!!)
4551
}
4652

4753
@Test
4854
fun noClaimsWhenLocationIsNotCorrect() {
4955
`when`(directKvStore!!.getBoolean("Picture_Has_Correct_Location", true))
5056
.thenReturn(false)
51-
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg")
57+
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg","")
5258
verifyZeroInteractions(wikidataClient!!)
5359
}
5460

@@ -60,7 +66,7 @@ class WikidataEditServiceTest {
6066
.thenReturn(Observable.just(1L))
6167
`when`(wikidataClient!!.addEditTag(anyLong(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
6268
.thenReturn(Observable.just(mock(AddEditTagResponse::class.java)))
63-
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg")
69+
wikidataEditService!!.createClaimWithLogging("Q1", "Test.jpg","")
6470
verify(wikidataClient!!, times(1))
6571
.createClaim(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())
6672
}

0 commit comments

Comments
 (0)