mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-11-04 08:43:52 +01:00 
			
		
		
		
	Make P18 edits to corresponding wikidata entity on uploading from Nearby (#1495)
* Localisation updates from https://translatewiki.net. * Integrate API for displaying featured images (#1456) * Integrate API for displaying featured images * Add pagination and refactor code so that it can be reused for category images * Add license info to the images * Fix author view * Remove unused values * Fix minor issues with featured images * Fix null license url issue * Remove some log lines * Fix back navigation issue * fix tests * fix test inits * Gracefully handling various error situations * Added java docs * Update pull_request_template.md (#1476) * Update pull_request_template.md * Remove Javadocs mention * Added required/optional notes * resolves #1464 : MediaDataExtractor is making inefficient (redundant) server calls (#1496) * Open map of place where picture was taken (#1360) * Intent to map added * Merge conflicts resolved * Added the functionality to hide FAB incase of null coordinate * Merge Conflict resolved * Improve pr quality * Improve Quality * Added nested FAB animations * Nested FAB implemented * Improve Quality * Added up arrow * Javadocs Added * Add nearby tutorial (#1467) * Add dependency for MaterialShowcase * Add actionview class to get a reference to material showcase * Create a NearbyMaterialShowcaseSequence class * Apply sequence steps * Add first three steps of nearby showcase * Add sequence id constants to make sure they will be displayed only once * Add last step of sequence to explain plus fab * Create an object to prevent customize all sequences every time * Fix typo * Code cleanup * Add strings to strings.xml * Code cleanup * Revert irrelevant change * Revert irrelevant change * Remove showcaseview for recenter button * Use single showcaseView instead of sequence * Add single showcase view insted of sequence to be able to edit text style * Make sure it will be displayed only once * Cleanup * Update strings * Change dismiss text style * CONTRIBUTING: fix formatting of the gist of the guidelines (#1453) * CONTRIBUTING: fix formatting of the gist of the guidelines First level headings for a gist seems to be overkill. So, replace first level headings with an ordered-list which sounds more meaningful. * CONTRIBUTING: specify clearly that 'blame' is a feature of "Git" The contributing file specifies about the ability to know who wrote something without the need of @author javadoc tags but incorrectly attributes the feature to GitHub. Correctly attribute the feature to where it belongs, Git, and specify the name of the feature to help users easily take advantage of it. * Feature/switch to butterknife (#1494) * Implemented butterknife in MediaDetailFragment [issue #1491] * Implemented butterknife in MediaDetailPagerFragment [[issue #1491]] * post merge upstream master wip [[issue #1491]] * Localisation updates from https://translatewiki.net. * Bug fix #1504 (#1506) * Bug fix #1504 * Filtered messages with ConnectException [issue #1504] * A generalised message for exceptions in Nearby Activity [issue #1504] * Localisation updates from https://translatewiki.net. * Fix security exception crash while accessing network location provider (#1498) * Fix security exception crash while accessing network location provider * Added java docs * Localisation updates from https://translatewiki.net. * Log P18 edits to wikidata corresponding wikidata entity on uploading a nearby image * Added java docs * Fix test build * Refresh nearby * Refresh nearby list on successful edit * Java docs * Make authenticated wikidata edits * Updated toast message to show entity name that was edited
This commit is contained in:
		
							parent
							
								
									677f85a097
								
							
						
					
					
						commit
						4815e93fc9
					
				
					 21 changed files with 276 additions and 38 deletions
				
			
		| 
						 | 
				
			
			@ -8,7 +8,6 @@ import android.animation.AnimatorSet;
 | 
			
		|||
import android.animation.ObjectAnimator;
 | 
			
		||||
import android.content.ContentResolver;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.content.DialogInterface;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.content.SharedPreferences;
 | 
			
		||||
import android.content.pm.PackageManager;
 | 
			
		||||
| 
						 | 
				
			
			@ -69,14 +68,11 @@ import fr.free.nrw.commons.caching.CacheController;
 | 
			
		|||
import fr.free.nrw.commons.category.CategorizationFragment;
 | 
			
		||||
import fr.free.nrw.commons.category.OnCategoriesSaveHandler;
 | 
			
		||||
import fr.free.nrw.commons.contributions.Contribution;
 | 
			
		||||
import fr.free.nrw.commons.contributions.ContributionsActivity;
 | 
			
		||||
import fr.free.nrw.commons.modifications.CategoryModifier;
 | 
			
		||||
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
 | 
			
		||||
import fr.free.nrw.commons.modifications.ModifierSequence;
 | 
			
		||||
import fr.free.nrw.commons.modifications.ModifierSequenceDao;
 | 
			
		||||
import fr.free.nrw.commons.modifications.TemplateRemoveModifier;
 | 
			
		||||
 | 
			
		||||
import fr.free.nrw.commons.utils.ImageUtils;
 | 
			
		||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
 | 
			
		||||
import fr.free.nrw.commons.utils.ViewUtil;
 | 
			
		||||
import timber.log.Timber;
 | 
			
		||||
| 
						 | 
				
			
			@ -135,6 +131,7 @@ public class ShareActivity
 | 
			
		|||
 | 
			
		||||
    private String title;
 | 
			
		||||
    private String description;
 | 
			
		||||
    private String wikiDataEntityId;
 | 
			
		||||
    private Snackbar snackbar;
 | 
			
		||||
    private boolean duplicateCheckPassed = false;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -194,7 +191,7 @@ public class ShareActivity
 | 
			
		|||
            Timber.d("Cache the categories found");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        uploadController.startUpload(title, mediaUri, description, mimeType, source, decimalCoords, c -> {
 | 
			
		||||
        uploadController.startUpload(title, mediaUri, description, mimeType, source, decimalCoords, wikiDataEntityId, c -> {
 | 
			
		||||
            ShareActivity.this.contribution = c;
 | 
			
		||||
            showPostUpload();
 | 
			
		||||
        });
 | 
			
		||||
| 
						 | 
				
			
			@ -278,7 +275,10 @@ public class ShareActivity
 | 
			
		|||
            }
 | 
			
		||||
            if (intent.hasExtra("isDirectUpload")) {
 | 
			
		||||
                Timber.d("This was initiated by a direct upload from Nearby");
 | 
			
		||||
                isNearbyUpload = true;
 | 
			
		||||
                isNearbyUpload = intent.getBooleanExtra("isDirectUpload", false);
 | 
			
		||||
            }
 | 
			
		||||
            if (intent.hasExtra("wikiDataEntityId")) {
 | 
			
		||||
                wikiDataEntityId = intent.getStringExtra("wikiDataEntityId");
 | 
			
		||||
            }
 | 
			
		||||
            mimeType = intent.getType();
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -446,7 +446,6 @@ public class ShareActivity
 | 
			
		|||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onRequestPermissionsResult(int requestCode,
 | 
			
		||||
                                           @NonNull String[] permissions, @NonNull int[] grantResults) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -91,7 +91,7 @@ public class UploadController {
 | 
			
		|||
     * @param decimalCoords the coordinates in decimal. (e.g. "37.51136|-77.602615")
 | 
			
		||||
     * @param onComplete    the progress tracker
 | 
			
		||||
     */
 | 
			
		||||
    public void startUpload(String title, Uri mediaUri, String description, String mimeType, String source, String decimalCoords, ContributionUploadProgress onComplete) {
 | 
			
		||||
    public void startUpload(String title, Uri mediaUri, String description, String mimeType, String source, String decimalCoords, String wikiDataEntityId, ContributionUploadProgress onComplete) {
 | 
			
		||||
        Contribution contribution;
 | 
			
		||||
 | 
			
		||||
        //TODO: Modify this to include coords
 | 
			
		||||
| 
						 | 
				
			
			@ -101,6 +101,7 @@ public class UploadController {
 | 
			
		|||
 | 
			
		||||
        contribution.setTag("mimeType", mimeType);
 | 
			
		||||
        contribution.setSource(source);
 | 
			
		||||
        contribution.setWikiDataEntityId(wikiDataEntityId);
 | 
			
		||||
 | 
			
		||||
        //Calls the next overloaded method
 | 
			
		||||
        startUpload(contribution, onComplete);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,6 +18,7 @@ import java.io.FileNotFoundException;
 | 
			
		|||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.Locale;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.regex.Matcher;
 | 
			
		||||
import java.util.regex.Pattern;
 | 
			
		||||
| 
						 | 
				
			
			@ -36,6 +37,11 @@ import fr.free.nrw.commons.contributions.ContributionsContentProvider;
 | 
			
		|||
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
 | 
			
		||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
 | 
			
		||||
import fr.free.nrw.commons.mwapi.UploadResult;
 | 
			
		||||
import fr.free.nrw.commons.utils.ViewUtil;
 | 
			
		||||
import fr.free.nrw.commons.wikidata.WikidataEditListener;
 | 
			
		||||
import io.reactivex.Observable;
 | 
			
		||||
import io.reactivex.android.schedulers.AndroidSchedulers;
 | 
			
		||||
import io.reactivex.schedulers.Schedulers;
 | 
			
		||||
import timber.log.Timber;
 | 
			
		||||
 | 
			
		||||
public class UploadService extends HandlerService<Contribution> {
 | 
			
		||||
| 
						 | 
				
			
			@ -49,9 +55,11 @@ public class UploadService extends HandlerService<Contribution> {
 | 
			
		|||
    public static final String EXTRA_CAMPAIGN = EXTRA_PREFIX + ".campaign";
 | 
			
		||||
 | 
			
		||||
    @Inject MediaWikiApi mwApi;
 | 
			
		||||
    @Inject WikidataEditListener wikidataEditListener;
 | 
			
		||||
    @Inject SessionManager sessionManager;
 | 
			
		||||
    @Inject @Named("default_preferences") SharedPreferences prefs;
 | 
			
		||||
    @Inject ContributionDao contributionDao;
 | 
			
		||||
    @Inject @Named("direct_nearby_upload_prefs") SharedPreferences directPrefs;
 | 
			
		||||
 | 
			
		||||
    private NotificationManager notificationManager;
 | 
			
		||||
    private NotificationCompat.Builder curProgressNotification;
 | 
			
		||||
| 
						 | 
				
			
			@ -137,6 +145,7 @@ public class UploadService extends HandlerService<Contribution> {
 | 
			
		|||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void queue(int what, Contribution contribution) {
 | 
			
		||||
        Timber.d("Upload service queue has contribution with wiki data entity id as %s", contribution.getWikiDataEntityId());
 | 
			
		||||
        switch (what) {
 | 
			
		||||
            case ACTION_UPLOAD_FILE:
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -253,6 +262,7 @@ public class UploadService extends HandlerService<Contribution> {
 | 
			
		|||
            if (!resultStatus.equals("Success")) {
 | 
			
		||||
                showFailedNotification(contribution);
 | 
			
		||||
            } else {
 | 
			
		||||
                editWikidataProperty(contribution);
 | 
			
		||||
                contribution.setFilename(uploadResult.getCanonicalFilename());
 | 
			
		||||
                contribution.setImageUrl(uploadResult.getImageUrl());
 | 
			
		||||
                contribution.setState(Contribution.STATE_COMPLETED);
 | 
			
		||||
| 
						 | 
				
			
			@ -275,6 +285,48 @@ public class UploadService extends HandlerService<Contribution> {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Edits the wikidata entity by adding the P18 property to it.
 | 
			
		||||
     * Adding the P18 edit requires calling the wikidata API to create a claim against the entity
 | 
			
		||||
     * @param contribution
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressLint("CheckResult")
 | 
			
		||||
    private void editWikidataProperty(Contribution contribution) {
 | 
			
		||||
        Timber.d("Upload successful with wiki data entity id as %s", contribution.getWikiDataEntityId());
 | 
			
		||||
        Timber.d("Attempting to edit Wikidata property %s", contribution.getWikiDataEntityId());
 | 
			
		||||
        Observable.fromCallable(() -> mwApi.wikidatCreateClaim(contribution.getWikiDataEntityId(), "P18", "value", getFileName(contribution)))
 | 
			
		||||
                .subscribeOn(Schedulers.io())
 | 
			
		||||
                .observeOn(AndroidSchedulers.mainThread())
 | 
			
		||||
                .subscribe(result -> handleClaimResult(contribution, result), throwable -> {
 | 
			
		||||
                    Timber.e(throwable, "Error occurred while making claim");
 | 
			
		||||
                    ViewUtil.showLongToast(getBaseContext(), getResources().getString(R.string.wikidata_edit_failure));
 | 
			
		||||
                });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void handleClaimResult(Contribution contribution, Boolean result) {
 | 
			
		||||
        if(result) {
 | 
			
		||||
            wikidataEditListener.onSuccessfulWikidataEdit();
 | 
			
		||||
            String title = directPrefs.getString("Title", "");
 | 
			
		||||
            String successStringTemplate = getResources().getString(R.string.successful_wikidata_edit);
 | 
			
		||||
            String successMessage = String.format(Locale.getDefault(), successStringTemplate, title);
 | 
			
		||||
            ViewUtil.showLongToast(getBaseContext(), successMessage);
 | 
			
		||||
        } else {
 | 
			
		||||
            Timber.d("Unable to make wiki data edit for entity %s", contribution.getWikiDataEntityId());
 | 
			
		||||
            ViewUtil.showLongToast(getBaseContext(), getResources().getString(R.string.wikidata_edit_failure));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Formats and returns the filename as accepted by the wiki base API
 | 
			
		||||
     * https://www.mediawiki.org/wiki/Wikibase/API#wbcreateclaim
 | 
			
		||||
     * @param contribution
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    private String getFileName(Contribution contribution) {
 | 
			
		||||
        String filename = String.format("\"%s\"", contribution.getFilename().replace("File:", ""));
 | 
			
		||||
        return filename;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SuppressLint("StringFormatInvalid")
 | 
			
		||||
    private void showFailedNotification(Contribution contribution) {
 | 
			
		||||
        Notification failureNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue