Convert WikidataEditService to kotlin

This commit is contained in:
Paul Hawke 2024-11-30 23:02:23 -06:00
parent 4dac09a3b3
commit 67653f198c
3 changed files with 254 additions and 273 deletions

View file

@ -496,14 +496,14 @@ class UploadWorker(
withContext(Dispatchers.Main) {
wikidataEditService.handleImageClaimResult(
contribution.wikidataPlace,
contribution.wikidataPlace!!,
revisionID,
)
}
} else {
withContext(Dispatchers.Main) {
wikidataEditService.handleImageClaimResult(
contribution.wikidataPlace,
contribution.wikidataPlace!!,
null,
)
}

View file

@ -1,271 +0,0 @@
package fr.free.nrw.commons.wikidata;
import static fr.free.nrw.commons.media.MediaClientKt.PAGE_ID_PREFIX;
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.Nullable;
import com.google.gson.Gson;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.Contribution;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.upload.UploadResult;
import fr.free.nrw.commons.upload.WikidataItem;
import fr.free.nrw.commons.upload.WikidataPlace;
import fr.free.nrw.commons.utils.ConfigUtils;
import fr.free.nrw.commons.utils.ViewUtil;
import fr.free.nrw.commons.wikidata.model.DataValue;
import fr.free.nrw.commons.wikidata.model.DataValue.ValueString;
import fr.free.nrw.commons.wikidata.model.EditClaim;
import fr.free.nrw.commons.wikidata.model.RemoveClaim;
import fr.free.nrw.commons.wikidata.model.SnakPartial;
import fr.free.nrw.commons.wikidata.model.StatementPartial;
import fr.free.nrw.commons.wikidata.model.WikiBaseMonolingualTextValue;
import fr.free.nrw.commons.wikidata.mwapi.MwPostResponse;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import timber.log.Timber;
/**
* This class is meant to handle the Wikidata edits made through the app It will talk with MediaWiki
* Apis to make the necessary calls, log the edits and fire listeners on successful edits
*/
@Singleton
public class WikidataEditService {
public static final String COMMONS_APP_TAG = "wikimedia-commons-app";
private final Context context;
private final WikidataEditListener wikidataEditListener;
private final JsonKvStore directKvStore;
private final WikiBaseClient wikiBaseClient;
private final WikidataClient wikidataClient;
private final Gson gson;
@Inject
public WikidataEditService(final Context context,
final WikidataEditListener wikidataEditListener,
@Named("default_preferences") final JsonKvStore directKvStore,
final WikiBaseClient wikiBaseClient,
final WikidataClient wikidataClient, final Gson gson) {
this.context = context;
this.wikidataEditListener = wikidataEditListener;
this.directKvStore = directKvStore;
this.wikiBaseClient = wikiBaseClient;
this.wikidataClient = wikidataClient;
this.gson = gson;
}
/**
* Edits the wikibase entity by adding DEPICTS property. Adding DEPICTS property requires call
* to the wikibase API to set tag against the entity.
*/
@SuppressLint("CheckResult")
private Observable<Boolean> addDepictsProperty(
final String fileEntityId,
final List<String> depictedItems
) {
final EditClaim data = editClaim(
ConfigUtils.isBetaFlavour() ? Collections.singletonList("Q10")
// Wikipedia:Sandbox (Q10)
: depictedItems
);
return wikiBaseClient.postEditEntity(PAGE_ID_PREFIX + fileEntityId, gson.toJson(data))
.doOnNext(success -> {
if (success) {
Timber.d("DEPICTS property was set successfully for %s", fileEntityId);
} else {
Timber.d("Unable to set DEPICTS property for %s", fileEntityId);
}
})
.doOnError(throwable -> {
Timber.e(throwable, "Error occurred while setting DEPICTS property");
ViewUtil.showLongToast(context, throwable.toString());
})
.subscribeOn(Schedulers.io());
}
/**
* Takes depicts ID as a parameter and create a uploadable data with the Id
* and send the data for POST operation
*
* @param fileEntityId ID of the file
* @param depictedItems IDs of the selected depict item
* @return Observable<Boolean>
*/
@SuppressLint("CheckResult")
public Observable<Boolean> updateDepictsProperty(
final String fileEntityId,
final List<String> depictedItems
) {
final String entityId = PAGE_ID_PREFIX + fileEntityId;
final List<String> claimIds = getDepictionsClaimIds(entityId);
final RemoveClaim data = removeClaim( /* Please consider removeClaim scenario for BetaDebug */
ConfigUtils.isBetaFlavour() ? Collections.singletonList("Q10")
// Wikipedia:Sandbox (Q10)
: claimIds
);
return wikiBaseClient.postDeleteClaims(entityId, gson.toJson(data))
.doOnError(throwable -> {
Timber.e(throwable, "Error occurred while removing existing claims for DEPICTS property");
ViewUtil.showLongToast(context, context.getString(R.string.wikidata_edit_failure));
}).switchMap(success-> {
if(success) {
Timber.d("DEPICTS property was deleted successfully");
return addDepictsProperty(fileEntityId, depictedItems);
} else {
Timber.d("Unable to delete DEPICTS property");
return Observable.empty();
}
});
}
@SuppressLint("CheckResult")
private List<String> getDepictionsClaimIds(final String entityId) {
return wikiBaseClient.getClaimIdsByProperty(entityId, WikidataProperties.DEPICTS.getPropertyName())
.subscribeOn(Schedulers.io())
.blockingFirst();
}
private EditClaim editClaim(final List<String> entityIds) {
return EditClaim.from(entityIds, WikidataProperties.DEPICTS.getPropertyName());
}
private RemoveClaim removeClaim(final List<String> claimIds) {
return RemoveClaim.from(claimIds);
}
/**
* Show a success toast when the edit is made successfully
*/
private void showSuccessToast(final String wikiItemName) {
final String successStringTemplate = context.getString(R.string.successful_wikidata_edit);
final String successMessage = String
.format(Locale.getDefault(), successStringTemplate, wikiItemName);
ViewUtil.showLongToast(context, successMessage);
}
/**
* Adds label to Wikidata using the fileEntityId and the edit token, obtained from
* csrfTokenClient
*
* @param fileEntityId
* @return
*/
@SuppressLint("CheckResult")
private Observable<Boolean> addCaption(final long fileEntityId, final String languageCode,
final String captionValue) {
return wikiBaseClient.addLabelsToWikidata(fileEntityId, languageCode, captionValue)
.doOnNext(mwPostResponse -> onAddCaptionResponse(fileEntityId, mwPostResponse))
.doOnError(throwable -> {
Timber.e(throwable, "Error occurred while setting Captions");
ViewUtil.showLongToast(context, context.getString(R.string.wikidata_edit_failure));
})
.map(mwPostResponse -> mwPostResponse != null);
}
private void onAddCaptionResponse(Long fileEntityId, MwPostResponse response) {
if (response != null) {
Timber.d("Caption successfully set, revision id = %s", response);
} else {
Timber.d("Error occurred while setting Captions, fileEntityId = %s", fileEntityId);
}
}
public Long createClaim(@Nullable final WikidataPlace wikidataPlace, final String fileName,
final Map<String, String> captions) {
if (!(directKvStore.getBoolean("Picture_Has_Correct_Location", true))) {
Timber
.d("Image location and nearby place location mismatched, so Wikidata item won't be edited");
return null;
}
return addImageAndMediaLegends(wikidataPlace, fileName, captions);
}
public Long addImageAndMediaLegends(final WikidataItem wikidataItem, final String fileName,
final Map<String, String> captions) {
final SnakPartial p18 = new SnakPartial("value",
WikidataProperties.IMAGE.getPropertyName(),
new ValueString(fileName.replace("File:", "")));
final List<SnakPartial> snaks = new ArrayList<>();
for (final Map.Entry<String, String> entry : captions.entrySet()) {
snaks.add(new SnakPartial("value",
WikidataProperties.MEDIA_LEGENDS.getPropertyName(), new DataValue.MonoLingualText(
new WikiBaseMonolingualTextValue(entry.getValue(), entry.getKey()))));
}
final String id = wikidataItem.getId() + "$" + UUID.randomUUID().toString();
final StatementPartial claim = new StatementPartial(p18, "statement", "normal", id,
Collections.singletonMap(WikidataProperties.MEDIA_LEGENDS.getPropertyName(), snaks),
Arrays.asList(WikidataProperties.MEDIA_LEGENDS.getPropertyName()));
return wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle();
}
public void handleImageClaimResult(final WikidataItem wikidataItem, final Long revisionId) {
if (revisionId != null) {
if (wikidataEditListener != null) {
wikidataEditListener.onSuccessfulWikidataEdit();
}
showSuccessToast(wikidataItem.getName());
} else {
Timber.d("Unable to make wiki data edit for entity %s", wikidataItem);
ViewUtil.showLongToast(context, context.getString(R.string.wikidata_edit_failure));
}
}
public Observable<Boolean> addDepictionsAndCaptions(
final UploadResult uploadResult,
final Contribution contribution
) {
return wikiBaseClient.getFileEntityId(uploadResult)
.doOnError(throwable -> {
Timber
.e(throwable, "Error occurred while getting EntityID to set DEPICTS property");
ViewUtil.showLongToast(context, context.getString(R.string.wikidata_edit_failure));
})
.switchMap(fileEntityId -> {
if (fileEntityId != null) {
Timber.d("EntityId for image was received successfully: %s", fileEntityId);
return Observable.concat(
depictionEdits(contribution, fileEntityId),
captionEdits(contribution, fileEntityId)
);
} else {
Timber.d("Error acquiring EntityId for image: %s", uploadResult);
return Observable.empty();
}
}
);
}
private Observable<Boolean> captionEdits(Contribution contribution, Long fileEntityId) {
return Observable.fromIterable(contribution.getMedia().getCaptions().entrySet())
.concatMap(entry -> addCaption(fileEntityId, entry.getKey(), entry.getValue()));
}
private Observable<Boolean> depictionEdits(Contribution contribution, Long fileEntityId) {
final List<String> depictIDs = new ArrayList<>();
for (final WikidataItem wikidataItem :
contribution.getDepictedItems()) {
depictIDs.add(wikidataItem.getId());
}
return addDepictsProperty(fileEntityId.toString(), depictIDs);
}
}

View file

@ -0,0 +1,252 @@
package fr.free.nrw.commons.wikidata
import android.annotation.SuppressLint
import android.content.Context
import com.google.gson.Gson
import fr.free.nrw.commons.R
import fr.free.nrw.commons.contributions.Contribution
import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.media.PAGE_ID_PREFIX
import fr.free.nrw.commons.upload.UploadResult
import fr.free.nrw.commons.upload.WikidataItem
import fr.free.nrw.commons.upload.WikidataPlace
import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
import fr.free.nrw.commons.utils.ViewUtil.showLongToast
import fr.free.nrw.commons.wikidata.WikidataProperties.DEPICTS
import fr.free.nrw.commons.wikidata.WikidataProperties.IMAGE
import fr.free.nrw.commons.wikidata.WikidataProperties.MEDIA_LEGENDS
import fr.free.nrw.commons.wikidata.model.DataValue.MonoLingualText
import fr.free.nrw.commons.wikidata.model.DataValue.ValueString
import fr.free.nrw.commons.wikidata.model.EditClaim
import fr.free.nrw.commons.wikidata.model.RemoveClaim
import fr.free.nrw.commons.wikidata.model.SnakPartial
import fr.free.nrw.commons.wikidata.model.StatementPartial
import fr.free.nrw.commons.wikidata.model.WikiBaseMonolingualTextValue
import fr.free.nrw.commons.wikidata.mwapi.MwPostResponse
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import java.util.Arrays
import java.util.Collections
import java.util.Locale
import java.util.Objects
import java.util.UUID
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton
/**
* This class is meant to handle the Wikidata edits made through the app It will talk with MediaWiki
* Apis to make the necessary calls, log the edits and fire listeners on successful edits
*/
@Singleton
class WikidataEditService @Inject constructor(
private val context: Context,
private val wikidataEditListener: WikidataEditListener?,
@param:Named("default_preferences") private val directKvStore: JsonKvStore,
private val wikiBaseClient: WikiBaseClient,
private val wikidataClient: WikidataClient, private val gson: Gson
) {
@SuppressLint("CheckResult")
private fun addDepictsProperty(
fileEntityId: String,
depictedItems: List<String>
): Observable<Boolean> {
val data = EditClaim.from(
if (isBetaFlavour) listOf("Q10") else depictedItems, DEPICTS.propertyName
)
return wikiBaseClient.postEditEntity(PAGE_ID_PREFIX + fileEntityId, gson.toJson(data))
.doOnNext { success: Boolean ->
if (success) {
Timber.d("DEPICTS property was set successfully for %s", fileEntityId)
} else {
Timber.d("Unable to set DEPICTS property for %s", fileEntityId)
}
}
.doOnError { throwable: Throwable ->
Timber.e(throwable, "Error occurred while setting DEPICTS property")
showLongToast(context, throwable.toString())
}
.subscribeOn(Schedulers.io())
}
@SuppressLint("CheckResult")
fun updateDepictsProperty(
fileEntityId: String?,
depictedItems: List<String>
): Observable<Boolean> {
val entityId: String = PAGE_ID_PREFIX + fileEntityId
val claimIds = getDepictionsClaimIds(entityId)
/* Please consider removeClaim scenario for BetaDebug */
val data = RemoveClaim.from(if (isBetaFlavour) listOf("Q10") else claimIds)
return wikiBaseClient.postDeleteClaims(entityId, gson.toJson(data))
.doOnError { throwable: Throwable? ->
Timber.e(
throwable,
"Error occurred while removing existing claims for DEPICTS property"
)
showLongToast(
context,
context.getString(R.string.wikidata_edit_failure)
)
}.switchMap { success: Boolean ->
if (success) {
Timber.d("DEPICTS property was deleted successfully")
return@switchMap addDepictsProperty(fileEntityId!!, depictedItems)
} else {
Timber.d("Unable to delete DEPICTS property")
return@switchMap Observable.empty<Boolean>()
}
}
}
@SuppressLint("CheckResult")
private fun getDepictionsClaimIds(entityId: String): List<String> {
return wikiBaseClient.getClaimIdsByProperty(entityId, DEPICTS.propertyName)
.subscribeOn(Schedulers.io())
.blockingFirst()
}
private fun showSuccessToast(wikiItemName: String) {
val successStringTemplate = context.getString(R.string.successful_wikidata_edit)
val successMessage = String.format(Locale.getDefault(), successStringTemplate, wikiItemName)
showLongToast(context, successMessage)
}
@SuppressLint("CheckResult")
private fun addCaption(
fileEntityId: Long, languageCode: String,
captionValue: String
): Observable<Boolean> {
return wikiBaseClient.addLabelsToWikidata(fileEntityId, languageCode, captionValue)
.doOnNext { mwPostResponse: MwPostResponse? ->
onAddCaptionResponse(
fileEntityId,
mwPostResponse
)
}
.doOnError { throwable: Throwable? ->
Timber.e(throwable, "Error occurred while setting Captions")
showLongToast(
context,
context.getString(R.string.wikidata_edit_failure)
)
}
.map(Objects::nonNull)
}
private fun onAddCaptionResponse(fileEntityId: Long, response: MwPostResponse?) {
if (response != null) {
Timber.d("Caption successfully set, revision id = %s", response)
} else {
Timber.d("Error occurred while setting Captions, fileEntityId = %s", fileEntityId)
}
}
fun createClaim(
wikidataPlace: WikidataPlace?, fileName: String,
captions: Map<String, String>
): Long? {
if (!(directKvStore.getBoolean("Picture_Has_Correct_Location", true))) {
Timber.d(
"Image location and nearby place location mismatched, so Wikidata item won't be edited"
)
return null
}
return addImageAndMediaLegends(wikidataPlace!!, fileName, captions)
}
fun addImageAndMediaLegends(
wikidataItem: WikidataItem, fileName: String,
captions: Map<String, String>
): Long {
val p18 = SnakPartial(
"value",
IMAGE.propertyName,
ValueString(fileName.replace("File:", ""))
)
val snaks: MutableList<SnakPartial> = ArrayList()
for ((key, value) in captions) {
snaks.add(
SnakPartial(
"value",
MEDIA_LEGENDS.propertyName, MonoLingualText(
WikiBaseMonolingualTextValue(value!!, key!!)
)
)
)
}
val id = wikidataItem.id + "$" + UUID.randomUUID().toString()
val claim = StatementPartial(
p18, "statement", "normal", id, Collections.singletonMap<String, List<SnakPartial>>(
MEDIA_LEGENDS.propertyName, snaks
), Arrays.asList(MEDIA_LEGENDS.propertyName)
)
return wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle()
}
fun handleImageClaimResult(wikidataItem: WikidataItem, revisionId: Long?) {
if (revisionId != null) {
wikidataEditListener?.onSuccessfulWikidataEdit()
showSuccessToast(wikidataItem.name)
} else {
Timber.d("Unable to make wiki data edit for entity %s", wikidataItem)
showLongToast(context, context.getString(R.string.wikidata_edit_failure))
}
}
fun addDepictionsAndCaptions(
uploadResult: UploadResult,
contribution: Contribution
): Observable<Boolean> {
return wikiBaseClient.getFileEntityId(uploadResult)
.doOnError { throwable: Throwable? ->
Timber.e(
throwable,
"Error occurred while getting EntityID to set DEPICTS property"
)
showLongToast(
context,
context.getString(R.string.wikidata_edit_failure)
)
}
.switchMap { fileEntityId: Long? ->
if (fileEntityId != null) {
Timber.d("EntityId for image was received successfully: %s", fileEntityId)
return@switchMap Observable.concat<Boolean>(
depictionEdits(contribution, fileEntityId),
captionEdits(contribution, fileEntityId)
)
} else {
Timber.d("Error acquiring EntityId for image: %s", uploadResult)
return@switchMap Observable.empty<Boolean>()
}
}
}
private fun captionEdits(contribution: Contribution, fileEntityId: Long): Observable<Boolean> {
return Observable.fromIterable(contribution.media.captions.entries)
.concatMap { addCaption(fileEntityId, it.key, it.value) }
}
private fun depictionEdits(
contribution: Contribution,
fileEntityId: Long
): Observable<Boolean> = addDepictsProperty(fileEntityId.toString(), buildList {
for ((_, _, _, _, _, _, id) in contribution.depictedItems) {
add(id)
}
})
companion object {
const val COMMONS_APP_TAG: String = "wikimedia-commons-app"
}
}