Check status before nominating for deletion

This commit is contained in:
Suchit Kar 2018-03-18 01:36:30 +05:30
parent c303525508
commit 9a27f9c332
5 changed files with 120 additions and 75 deletions

View file

@ -1,10 +1,10 @@
package fr.free.nrw.commons.delete; package fr.free.nrw.commons.delete;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.widget.Button;
import android.widget.Toast;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
@ -12,14 +12,19 @@ import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
import fr.free.nrw.commons.Media; import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.di.ApplicationlessInjection; import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.mwapi.MediaWikiApi;
import timber.log.Timber; import timber.log.Timber;
import static android.view.View.GONE; import static android.support.v4.content.ContextCompat.startActivity;
public class DeleteTask extends AsyncTask<Void, Void, Boolean> { public class DeleteTask extends AsyncTask<Void, Void, Integer> {
private static final int SUCCESS = 0;
private static final int FAILED = -1;
private static final int ALREADY_DELETED = -2;
@Inject MediaWikiApi mwApi; @Inject MediaWikiApi mwApi;
@Inject SessionManager sessionManager; @Inject SessionManager sessionManager;
@ -43,7 +48,7 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
} }
@Override @Override
protected Boolean doInBackground(Void ...voids) { protected Integer doInBackground(Void ...voids) {
String editToken; String editToken;
String authCookie; String authCookie;
String summary = "Nominating " + media.getFilename() +" for deletion."; String summary = "Nominating " + media.getFilename() +" for deletion.";
@ -51,15 +56,25 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
authCookie = sessionManager.getAuthCookie(); authCookie = sessionManager.getAuthCookie();
mwApi.setAuthCookie(authCookie); mwApi.setAuthCookie(authCookie);
try{
if (mwApi.pageExists("Commons:Deletion_requests/"+media.getFilename())){
return ALREADY_DELETED;
}
}
catch (Exception e) {
Timber.d(e.getMessage());
return FAILED;
}
try { try {
editToken = mwApi.getEditToken(); editToken = mwApi.getEditToken();
} }
catch (Exception e){ catch (Exception e){
Timber.d(e.getMessage()); Timber.d(e.getMessage());
return false; return FAILED;
} }
if (editToken.equals("+\\")) { if (editToken.equals("+\\")) {
return false; return FAILED;
} }
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
@ -75,7 +90,7 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
} }
catch (Exception e) { catch (Exception e) {
Timber.d(e.getMessage()); Timber.d(e.getMessage());
return false; return FAILED;
} }
String subpageString = "=== [[:" + media.getFilename() + "]] ===\n" + String subpageString = "=== [[:" + media.getFilename() + "]] ===\n" +
@ -83,11 +98,11 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
" ~~~~"; " ~~~~";
try{ try{
mwApi.edit(editToken,subpageString+"\n", mwApi.edit(editToken,subpageString+"\n",
"Commons:Deletion requests/"+media.getFilename(),summary); "Commons:Deletion_requests/"+media.getFilename(),summary);
} }
catch (Exception e) { catch (Exception e) {
Timber.d(e.getMessage()); Timber.d(e.getMessage());
return false; return FAILED;
} }
String logPageString = "\n{{Commons:Deletion requests/" + media.getFilename() + String logPageString = "\n{{Commons:Deletion requests/" + media.getFilename() +
@ -96,11 +111,11 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
String date = sdf.format(calendar.getTime()); String date = sdf.format(calendar.getTime());
try{ try{
mwApi.appendEdit(editToken,logPageString+"\n", mwApi.appendEdit(editToken,logPageString+"\n",
"Commons:Deletion requests/"+date,summary); "Commons:Deletion_requests/"+date,summary);
} }
catch (Exception e) { catch (Exception e) {
Timber.d(e.getMessage()); Timber.d(e.getMessage());
return false; return FAILED;
} }
String userPageString = "\n{{subst:idw|" + media.getFilename() + String userPageString = "\n{{subst:idw|" + media.getFilename() +
@ -111,20 +126,49 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
} }
catch (Exception e) { catch (Exception e) {
Timber.d(e.getMessage()); Timber.d(e.getMessage());
return false; return FAILED;
} }
return true; return SUCCESS;
} }
@Override @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Integer result) {
String toastText = ""; String message = "";
if (result) { String title = "";
toastText = "Successfully requested deletion."; switch (result){
case SUCCESS:
title = "Success";
message = "Successfully nominated " + media.getDisplayTitle() + " deletion.\n" +
"Check the webpage for more details";
break;
case FAILED:
title = "Failed";
message = "Could not request deletion. Something went wrong.";
break;
case ALREADY_DELETED:
title = "Already Nominated";
message = media.getDisplayTitle() + " has already been nominated for deletion.\n" +
"Check the webpage for more details";
break;
} }
else{ AlertDialog alert;
toastText = "Could not request deletion."; AlertDialog.Builder builder = new AlertDialog.Builder(context);
} builder.setTitle(title);
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show(); builder.setMessage(message);
builder.setCancelable(true);
builder.setPositiveButton(
R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {}
});
builder.setNeutralButton(R.string.view_browser,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, media.getFilePageTitle().getMobileUri());
startActivity(context,browserIntent,null);
}
});
alert = builder.create();
alert.show();
} }
} }

View file

@ -15,6 +15,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -298,58 +299,48 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment {
} }
if (delete.getVisibility()==View.VISIBLE){ if (delete.getVisibility()==View.VISIBLE){
delete.setOnClickListener(v -> { delete.setOnClickListener(v -> {
AlertDialog alert; AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); alert.setMessage("Why should this file be deleted?");
builder.setMessage("Sure you want to delete?"); final EditText input = new EditText(getActivity());
builder.setCancelable(true); alert.setView(input);
builder.setPositiveButton( input.requestFocus();
R.string.yes, alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) {
public void onClick(DialogInterface dialog, int id) { String reason = input.getText().toString();
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); DeleteTask deleteTask = new DeleteTask(getActivity(), media, reason);
alert.setTitle("Why should this file be deleted?"); deleteTask.execute();
final EditText input = new EditText(getActivity()); }
alert.setView(input); });
alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
String reason = input.getText().toString(); }
DeleteTask deleteTask = new DeleteTask(getActivity(), media, reason); });
deleteTask.execute(); AlertDialog d = alert.create();
} input.addTextChangedListener(new TextWatcher() {
}); private void handleText() {
alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
public void onClick(DialogInterface dialog, int whichButton) {} if (input.getText().length() == 0) {
}); okButton.setEnabled(false);
AlertDialog d = alert.create(); } else {
input.addTextChangedListener(new TextWatcher() { okButton.setEnabled(true);
private void handleText() { }
final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE); }
if(input.getText().length() == 0) {
okButton.setEnabled(false); @Override
} else { public void afterTextChanged(Editable arg0) {
okButton.setEnabled(true); handleText();
} }
}
@Override @Override
public void afterTextChanged(Editable arg0) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
handleText(); }
}
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {
@Override }
public void onTextChanged(CharSequence s, int start, int before, int count) {} });
}); d.show();
d.show(); d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
});
builder.setNegativeButton(
R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {}
});
alert = builder.create();
alert.show();
}); });
} }
} }

View file

@ -205,6 +205,13 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
.getNodes("/api/query/pages/page/imageinfo").size() > 0; .getNodes("/api/query/pages/page/imageinfo").size() > 0;
} }
@Override
public boolean pageExists(String pageName) throws IOException {
return Double.parseDouble( api.action("query")
.param("titles", pageName)
.get()
.getString("/api/query/pages/page/@_idx")) != -1;
}
@Override @Override
@Nullable @Nullable

View file

@ -28,6 +28,8 @@ public interface MediaWikiApi {
boolean fileExistsWithName(String fileName) throws IOException; boolean fileExistsWithName(String fileName) throws IOException;
boolean pageExists(String pageName) throws IOException;
String findThumbnailByFilename(String filename) throws IOException; String findThumbnailByFilename(String filename) throws IOException;
boolean logEvents(LogBuilder[] logBuilders); boolean logEvents(LogBuilder[] logBuilders);

View file

@ -231,6 +231,7 @@
<string name="no_web_browser">No web browser found to open URL</string> <string name="no_web_browser">No web browser found to open URL</string>
<string name="null_url">Error! URL not found</string> <string name="null_url">Error! URL not found</string>
<string name="nominate_deletion">Nominate for Deletion</string> <string name="nominate_deletion">Nominate for Deletion</string>
<string name="view_browser">View in Browser</string>
<string name="nearby_location_has_not_changed">Location has not changed.</string> <string name="nearby_location_has_not_changed">Location has not changed.</string>
<string name="nearby_location_not_available">Location not available.</string> <string name="nearby_location_not_available">Location not available.</string>