1
+ package fr .free .nrw .commons .delete ;
2
+
3
+ import android .app .AlertDialog ;
4
+ import android .content .Context ;
5
+ import android .content .DialogInterface ;
6
+ import android .content .Intent ;
7
+ import android .os .AsyncTask ;
8
+ import java .text .SimpleDateFormat ;
9
+ import java .util .Calendar ;
10
+ import java .util .Locale ;
11
+
12
+ import javax .inject .Inject ;
13
+
14
+ import fr .free .nrw .commons .Media ;
15
+ import fr .free .nrw .commons .R ;
16
+ import fr .free .nrw .commons .auth .SessionManager ;
17
+ import fr .free .nrw .commons .di .ApplicationlessInjection ;
18
+ import fr .free .nrw .commons .mwapi .MediaWikiApi ;
19
+ import timber .log .Timber ;
20
+
21
+ import static android .support .v4 .content .ContextCompat .startActivity ;
22
+
23
+ public class DeleteTask extends AsyncTask <Void , Void , Integer > {
24
+
25
+ private static final int SUCCESS = 0 ;
26
+ private static final int FAILED = -1 ;
27
+ private static final int ALREADY_DELETED = -2 ;
28
+
29
+ @ Inject MediaWikiApi mwApi ;
30
+ @ Inject SessionManager sessionManager ;
31
+
32
+ private Context context ;
33
+ private Media media ;
34
+ private String reason ;
35
+
36
+ public DeleteTask (Context context , Media media , String reason ){
37
+ this .context = context ;
38
+ this .media = media ;
39
+ this .reason = reason ;
40
+ }
41
+
42
+ @ Override
43
+ protected void onPreExecute (){
44
+ ApplicationlessInjection
45
+ .getInstance (context .getApplicationContext ())
46
+ .getCommonsApplicationComponent ()
47
+ .inject (this );
48
+ }
49
+
50
+ @ Override
51
+ protected Integer doInBackground (Void ...voids ) {
52
+ String editToken ;
53
+ String authCookie ;
54
+ String summary = "Nominating " + media .getFilename () +" for deletion." ;
55
+
56
+ authCookie = sessionManager .getAuthCookie ();
57
+ mwApi .setAuthCookie (authCookie );
58
+
59
+ try {
60
+ if (mwApi .pageExists ("Commons:Deletion_requests/" +media .getFilename ())){
61
+ return ALREADY_DELETED ;
62
+ }
63
+ }
64
+ catch (Exception e ) {
65
+ Timber .d (e .getMessage ());
66
+ return FAILED ;
67
+ }
68
+
69
+ try {
70
+ editToken = mwApi .getEditToken ();
71
+ }
72
+ catch (Exception e ){
73
+ Timber .d (e .getMessage ());
74
+ return FAILED ;
75
+ }
76
+ if (editToken .equals ("+\\ " )) {
77
+ return FAILED ;
78
+ }
79
+
80
+ Calendar calendar = Calendar .getInstance ();
81
+ String fileDeleteString = "{{delete|reason=" + reason +
82
+ "|subpage=" +media .getFilename () +
83
+ "|day=" + calendar .get (Calendar .DAY_OF_MONTH ) +
84
+ "|month=" + calendar .getDisplayName (Calendar .MONTH ,Calendar .LONG , Locale .getDefault ()) +
85
+ "|year=" + calendar .get (Calendar .YEAR ) +
86
+ "}}" ;
87
+ try {
88
+ mwApi .prependEdit (editToken ,fileDeleteString +"\n " ,
89
+ media .getFilename (),summary );
90
+ }
91
+ catch (Exception e ) {
92
+ Timber .d (e .getMessage ());
93
+ return FAILED ;
94
+ }
95
+
96
+ String subpageString = "=== [[:" + media .getFilename () + "]] ===\n " +
97
+ reason +
98
+ " ~~~~" ;
99
+ try {
100
+ mwApi .edit (editToken ,subpageString +"\n " ,
101
+ "Commons:Deletion_requests/" +media .getFilename (),summary );
102
+ }
103
+ catch (Exception e ) {
104
+ Timber .d (e .getMessage ());
105
+ return FAILED ;
106
+ }
107
+
108
+ String logPageString = "\n {{Commons:Deletion requests/" + media .getFilename () +
109
+ "}}\n " ;
110
+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" );
111
+ String date = sdf .format (calendar .getTime ());
112
+ try {
113
+ mwApi .appendEdit (editToken ,logPageString +"\n " ,
114
+ "Commons:Deletion_requests/" +date ,summary );
115
+ }
116
+ catch (Exception e ) {
117
+ Timber .d (e .getMessage ());
118
+ return FAILED ;
119
+ }
120
+
121
+ String userPageString = "\n {{subst:idw|" + media .getFilename () +
122
+ "}} ~~~~" ;
123
+ try {
124
+ mwApi .appendEdit (editToken ,userPageString +"\n " ,
125
+ "User_Talk:" +sessionManager .getCurrentAccount ().name ,summary );
126
+ }
127
+ catch (Exception e ) {
128
+ Timber .d (e .getMessage ());
129
+ return FAILED ;
130
+ }
131
+ return SUCCESS ;
132
+ }
133
+
134
+ @ Override
135
+ protected void onPostExecute (Integer result ) {
136
+ String message = "" ;
137
+ String title = "" ;
138
+ switch (result ){
139
+ case SUCCESS :
140
+ title = "Success" ;
141
+ message = "Successfully nominated " + media .getDisplayTitle () + " deletion.\n " +
142
+ "Check the webpage for more details" ;
143
+ break ;
144
+ case FAILED :
145
+ title = "Failed" ;
146
+ message = "Could not request deletion. Something went wrong." ;
147
+ break ;
148
+ case ALREADY_DELETED :
149
+ title = "Already Nominated" ;
150
+ message = media .getDisplayTitle () + " has already been nominated for deletion.\n " +
151
+ "Check the webpage for more details" ;
152
+ break ;
153
+ }
154
+ AlertDialog alert ;
155
+ AlertDialog .Builder builder = new AlertDialog .Builder (context );
156
+ builder .setTitle (title );
157
+ builder .setMessage (message );
158
+ builder .setCancelable (true );
159
+ builder .setPositiveButton (
160
+ R .string .ok ,
161
+ new DialogInterface .OnClickListener () {
162
+ public void onClick (DialogInterface dialog , int id ) {}
163
+ });
164
+ builder .setNeutralButton (R .string .view_browser ,
165
+ new DialogInterface .OnClickListener () {
166
+ public void onClick (DialogInterface dialog , int id ) {
167
+ Intent browserIntent = new Intent (Intent .ACTION_VIEW , media .getFilePageTitle ().getMobileUri ());
168
+ startActivity (context ,browserIntent ,null );
169
+ }
170
+ });
171
+ alert = builder .create ();
172
+ alert .show ();
173
+ }
174
+ }
0 commit comments