Revert "Wmhack2018 (#1536)" (#1539)

This reverts commit 01cb9ccd70.
This commit is contained in:
neslihanturan 2018-05-19 23:02:29 +03:00 committed by GitHub
parent 01cb9ccd70
commit 32d36944fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 45 additions and 1086 deletions

View file

@ -38,14 +38,12 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.Random;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.PageTitle;
import fr.free.nrw.commons.category.CategoryImageUtils;
import fr.free.nrw.commons.category.QueryContinue;
import fr.free.nrw.commons.media.RecentChangesImageUtils;
import fr.free.nrw.commons.notification.Notification;
import fr.free.nrw.commons.notification.NotificationUtils;
import in.yuvi.http.fluent.Http;
@ -62,14 +60,6 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
private String wikiMediaToolforgeUrl = "https://tools.wmflabs.org/";
private static final String THUMB_SIZE = "640";
// Give up if no random recent image found after 5 tries
private static final int MAX_RANDOM_TRIES = 5;
// Random image request is for some time in the past 30 days
private static final int RANDOM_SECONDS = 60 * 60 * 24 * 30;
// Assuming MW always gives me UTC
private static final SimpleDateFormat isoFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
private static final String FILE_NAMESPACE = "6";
private AbstractHttpClient httpClient;
private MWApi api;
private Context context;
@ -233,19 +223,6 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
.getString("/api/query/pages/page/@_idx")) != -1;
}
@Override
public boolean thank(String editToken, String revision) throws IOException {
ApiResult res = api.action("thank")
.param("rev", revision)
.param("token", editToken)
.param("source", getUserAgent())
.post();
String r = res.getString("/api/result/@success");
// Does this correctly check the success/failure?
// The docs https://www.mediawiki.org/wiki/Extension:Thanks seems unclear about that.
return r.equals("success");
}
@Override
@Nullable
public String edit(String editToken, String processedPageContent, String filename, String summary) throws IOException {
@ -458,20 +435,6 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
.getString("/api/query/pages/page/revisions/rev");
}
@Override
@Nullable
public String firstRevisionOfFile(String filename) throws IOException {
String res = api.action("query")
.param("prop", "revisions")
.param("rvprop", "timestamp|ids")
.param("titles", filename)
.param("rvdir", "newer")
.param("rvlimit", "1")
.get()
.getString("/api/query/pages/page/revisions/rev/@revid");
return res;
}
@Override
@NonNull
public List<Notification> getNotifications() {
@ -653,59 +616,11 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
}
private Date parseMWDate(String mwDate) {
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH); // Assuming MW always gives me UTC
try {
return isoFormat.parse(mwDate);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
private String formatMWDate(Date date) {
return isoFormat.format(date);
}
public Media getRecentRandomImage() throws IOException {
Media media = null;
int tries = 0;
Random r = new Random();
while (media == null && tries < MAX_RANDOM_TRIES) {
Date now = new Date();
Date startDate = new Date(now.getTime() - r.nextInt(RANDOM_SECONDS) * 1000L);
ApiResult apiResult = null;
try {
MWApi.RequestBuilder requestBuilder = api.action("query")
.param("list", "recentchanges")
.param("rcstart", formatMWDate(startDate))
.param("rcnamespace", FILE_NAMESPACE)
.param("rcprop", "title|ids")
.param("rctype", "new|log")
.param("rctoponly", "1");
apiResult = requestBuilder.get();
} catch (IOException e) {
Timber.e("Failed to obtain recent random", e);
}
if (apiResult != null) {
ApiResult recentChangesNode = apiResult.getNode("/api/query/recentchanges");
if (recentChangesNode != null
&& recentChangesNode.getDocument() != null
&& recentChangesNode.getDocument().getChildNodes() != null
&& recentChangesNode.getDocument().getChildNodes().getLength() > 0) {
NodeList childNodes = recentChangesNode.getDocument().getChildNodes();
String imageTitle = RecentChangesImageUtils.findImageInRecentChanges(childNodes);
if (imageTitle != null) {
boolean deletionStatus = pageExists("Commons:Deletion_requests/" + imageTitle);
if (!deletionStatus) {
// strip File: prefix
imageTitle = imageTitle.replace("File:", "");
media = new Media(imageTitle);
}
}
}
}
tries++;
}
return media;
}
}

View file

@ -75,14 +75,7 @@ public interface MediaWikiApi {
@NonNull
Single<Integer> getUploadCount(String userName);
boolean thank(String editToken, String revision) throws IOException;
String firstRevisionOfFile(String filename) throws IOException;
interface ProgressListener {
void onProgress(long transferred, long total);
}
@Nullable
Media getRecentRandomImage() throws IOException;
}