Refactoring to not expose the ApiResult and Apache HTTP implementation

This commit is contained in:
Paul Hawke 2017-07-04 14:43:25 -05:00
parent 599e7bb453
commit d64c1b51cc
14 changed files with 386 additions and 200 deletions

View file

@ -1,6 +1,5 @@
package fr.free.nrw.commons;
import org.mediawiki.api.ApiResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -21,6 +20,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.mwapi.MediaResult;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import timber.log.Timber;
@ -64,20 +64,14 @@ public class MediaDataExtractor {
}
MediaWikiApi api = CommonsApplication.getInstance().getMWApi();
processResult(api.fetchMediaByFilename(filename));
fetched = true;
}
private void processResult(ApiResult result) throws IOException {
String wikiSource = result.getString("/api/query/pages/page/revisions/rev");
String parseTreeXmlSource = result.getString("/api/query/pages/page/revisions/rev/@parsetree");
MediaResult result = api.fetchMediaByFilename(filename);
// In-page category links are extracted from source, as XML doesn't cover [[links]]
extractCategories(wikiSource);
extractCategories(result.getWikiSource());
// Description template info is extracted from preprocessor XML
processWikiParseTree(parseTreeXmlSource);
processWikiParseTree(result.getParseTreeXmlSource());
fetched = true;
}
/**

View file

@ -134,7 +134,7 @@ public class CategorizationFragment extends Fragment {
//Override onPostExecute to access the results of async API call
titleCategoriesSub = new TitleCategories(title) {
@Override
protected void onPostExecute(ArrayList<String> result) {
protected void onPostExecute(List<String> result) {
super.onPostExecute(result);
Timber.d("Results in onPostExecute: %s", result);
titleCatItems.addAll(result);
@ -277,8 +277,8 @@ public class CategorizationFragment extends Fragment {
prefixUpdaterSub = new PrefixUpdater(this) {
@Override
protected ArrayList<String> doInBackground(Void... voids) {
ArrayList<String> result = new ArrayList<>();
protected List<String> doInBackground(Void... voids) {
List<String> result = new ArrayList<>();
try {
result = super.doInBackground();
latch.await();
@ -291,7 +291,7 @@ public class CategorizationFragment extends Fragment {
}
@Override
protected void onPostExecute(ArrayList<String> result) {
protected void onPostExecute(List<String> result) {
super.onPostExecute(result);
results.addAll(result);
@ -309,7 +309,7 @@ public class CategorizationFragment extends Fragment {
methodAUpdaterSub = new MethodAUpdater(this) {
@Override
protected void onPostExecute(ArrayList<String> result) {
protected void onPostExecute(List<String> result) {
results.clear();
super.onPostExecute(result);

View file

@ -3,12 +3,11 @@ package fr.free.nrw.commons.category;
import android.os.AsyncTask;
import android.view.View;
import org.mediawiki.api.ApiResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
@ -19,12 +18,12 @@ import timber.log.Timber;
* the keyword typed in by the user. The 'srsearch' action-specific parameter is used for this
* purpose. This class should be subclassed in CategorizationFragment.java to aggregate the results.
*/
public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
class MethodAUpdater extends AsyncTask<Void, Void, List<String>> {
private String filter;
CategorizationFragment catFragment;
private CategorizationFragment catFragment;
public MethodAUpdater(CategorizationFragment catFragment) {
MethodAUpdater(CategorizationFragment catFragment) {
this.catFragment = catFragment;
}
@ -42,10 +41,11 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
* Remove categories that contain a year in them (starting with 19__ or 20__), except for this year
* and previous year
* Rationale: https://github.com/commons-app/apps-android-commons/issues/47
*
* @param items Unfiltered list of categories
* @return Filtered category list
*/
private ArrayList<String> filterYears(ArrayList<String> items) {
private List<String> filterYears(List<String> items) {
Iterator<String> iterator;
@ -60,12 +60,12 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
Timber.d("Previous year: %s", prevYearInString);
//Copy to Iterator to prevent ConcurrentModificationException when removing item
for(iterator = items.iterator(); iterator.hasNext();) {
for (iterator = items.iterator(); iterator.hasNext(); ) {
String s = iterator.next();
//Check if s contains a 4-digit word anywhere within the string (.* is wildcard)
//And that s does not equal the current year or previous year
if(s.matches(".*(19|20)\\d{2}.*") && !s.contains(yearInString) && !s.contains(prevYearInString)) {
if (s.matches(".*(19|20)\\d{2}.*") && !s.contains(yearInString) && !s.contains(prevYearInString)) {
Timber.d("Filtering out year %s", s);
iterator.remove();
}
@ -76,30 +76,22 @@ public class MethodAUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
}
@Override
protected ArrayList<String> doInBackground(Void... voids) {
protected List<String> doInBackground(Void... voids) {
//otherwise if user has typed something in that isn't in cache, search API for matching categories
MediaWikiApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
ArrayList<String> categories = new ArrayList<>();
List<String> categories = new ArrayList<>();
//URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
try {
result = api.searchCategories(CategorizationFragment.SEARCH_CATS_LIMIT, filter);
Timber.d("Method A URL filter %s", result);
categories = api.searchCategories(CategorizationFragment.SEARCH_CATS_LIMIT, filter);
Timber.d("Method A URL filter %s", categories);
} catch (IOException e) {
Timber.e(e, "IO Exception: ");
//Return empty arraylist
return categories;
}
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
for(ApiResult categoryNode: categoryNodes) {
String cat = categoryNode.getDocument().getTextContent();
String catString = cat.replace("Category:", "");
categories.add(catString);
}
Timber.d("Found categories from Method A search, waiting for filter");
return new ArrayList<>(filterYears(categories));
}

View file

@ -4,12 +4,11 @@ import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import org.mediawiki.api.ApiResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
@ -21,7 +20,7 @@ import timber.log.Timber;
* for this purpose. This class should be subclassed in CategorizationFragment.java to aggregate
* the results.
*/
public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
public class PrefixUpdater extends AsyncTask<Void, Void, List<String>> {
private String filter;
private CategorizationFragment catFragment;
@ -44,10 +43,11 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
* Remove categories that contain a year in them (starting with 19__ or 20__), except for this year
* and previous year
* Rationale: https://github.com/commons-app/apps-android-commons/issues/47
*
* @param items Unfiltered list of categories
* @return Filtered category list
*/
private ArrayList<String> filterYears(ArrayList<String> items) {
private List<String> filterYears(List<String> items) {
Iterator<String> iterator;
@ -62,12 +62,12 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
Timber.d("Previous year: %s", prevYearInString);
//Copy to Iterator to prevent ConcurrentModificationException when removing item
for(iterator = items.iterator(); iterator.hasNext();) {
for (iterator = items.iterator(); iterator.hasNext(); ) {
String s = iterator.next();
//Check if s contains a 4-digit word anywhere within the string (.* is wildcard)
//And that s does not equal the current year or previous year
if(s.matches(".*(19|20)\\d{2}.*") && !s.contains(yearInString) && !s.contains(prevYearInString)) {
if (s.matches(".*(19|20)\\d{2}.*") && !s.contains(yearInString) && !s.contains(prevYearInString)) {
Timber.d("Filtering out year %s", s);
iterator.remove();
}
@ -78,16 +78,16 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
}
@Override
protected ArrayList<String> doInBackground(Void... voids) {
protected List<String> doInBackground(Void... voids) {
//If user hasn't typed anything in yet, get GPS and recent items
if(TextUtils.isEmpty(filter)) {
if (TextUtils.isEmpty(filter)) {
ArrayList<String> mergedItems = new ArrayList<>(catFragment.mergeItems());
Timber.d("Merged items, waiting for filter");
return new ArrayList<>(filterYears(mergedItems));
}
//if user types in something that is in cache, return cached category
if(catFragment.categoriesCache.containsKey(filter)) {
if (catFragment.categoriesCache.containsKey(filter)) {
ArrayList<String> cachedItems = new ArrayList<>(catFragment.categoriesCache.get(filter));
Timber.d("Found cache items, waiting for filter");
return new ArrayList<>(filterYears(cachedItems));
@ -96,22 +96,16 @@ public class PrefixUpdater extends AsyncTask<Void, Void, ArrayList<String>> {
//otherwise if user has typed something in that isn't in cache, search API for matching categories
//URL: https://commons.wikimedia.org/w/api.php?action=query&list=allcategories&acprefix=filter&aclimit=25
MediaWikiApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
ArrayList<String> categories = new ArrayList<>();
List<String> categories = new ArrayList<>();
try {
result = api.allCategories(CategorizationFragment.SEARCH_CATS_LIMIT, this.filter);
Timber.d("Prefix URL filter %s", result);
categories = api.allCategories(CategorizationFragment.SEARCH_CATS_LIMIT, this.filter);
Timber.d("Prefix URL filter %s", categories);
} catch (IOException e) {
Timber.e(e, "IO Exception: ");
//Return empty arraylist
return categories;
}
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/allcategories/c");
for(ApiResult categoryNode: categoryNodes) {
categories.add(categoryNode.getDocument().getTextContent());
}
Timber.d("Found categories from Prefix search, waiting for filter");
return new ArrayList<>(filterYears(categories));
}

View file

@ -2,10 +2,9 @@ package fr.free.nrw.commons.category;
import android.os.AsyncTask;
import org.mediawiki.api.ApiResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
@ -16,13 +15,13 @@ import timber.log.Timber;
* the title entered in previous screen. The 'srsearch' action-specific parameter is used for this
* purpose. This class should be subclassed in CategorizationFragment.java to add the results to recent and GPS cats.
*/
public class TitleCategories extends AsyncTask<Void, Void, ArrayList<String>> {
class TitleCategories extends AsyncTask<Void, Void, List<String>> {
private final static int SEARCH_CATS_LIMIT = 25;
private String title;
public TitleCategories(String title) {
TitleCategories(String title) {
this.title = title;
}
@ -32,32 +31,23 @@ public class TitleCategories extends AsyncTask<Void, Void, ArrayList<String>> {
}
@Override
protected ArrayList<String> doInBackground(Void... voids) {
protected List<String> doInBackground(Void... voids) {
MediaWikiApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
ArrayList<String> items = new ArrayList<>();
List<String> titleCategories = new ArrayList<>();
//URL https://commons.wikimedia.org/w/api.php?action=query&format=xml&list=search&srwhat=text&srenablerewrites=1&srnamespace=14&srlimit=10&srsearch=
try {
result = api.searchTitles(SEARCH_CATS_LIMIT, this.title);
Timber.d("Searching for cats for title: %s", result);
titleCategories = api.searchTitles(SEARCH_CATS_LIMIT, this.title);
} catch (IOException e) {
Timber.e(e, "IO Exception: ");
//Return empty arraylist
return items;
return titleCategories;
}
ArrayList<ApiResult> categoryNodes = result.getNodes("/api/query/search/p/@title");
for(ApiResult categoryNode: categoryNodes) {
String cat = categoryNode.getDocument().getTextContent();
String catString = cat.replace("Category:", "");
items.add(catString);
}
Timber.d("Title cat query results: %s", titleCategories);
Timber.d("Title cat query results: %s", items);
return items;
return titleCategories;
}
}

View file

@ -12,14 +12,14 @@ import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import org.mediawiki.api.ApiResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.mwapi.LogEventResult;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import timber.log.Timber;
@ -65,7 +65,7 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
SharedPreferences prefs = this.getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
String lastModified = prefs.getString("lastSyncTimestamp", "");
Date curTime = new Date();
ApiResult result;
LogEventResult result;
Boolean done = false;
String queryContinue = null;
while(!done) {
@ -81,22 +81,21 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
}
Timber.d("Last modified at %s", lastModified);
ArrayList<ApiResult> uploads = result.getNodes("/api/query/logevents/item");
Timber.d("%d results!", uploads.size());
List<LogEventResult.LogEvent> logEvents = result.getLogEvents();
Timber.d("%d results!", logEvents.size());
ArrayList<ContentValues> imageValues = new ArrayList<>();
for(ApiResult image: uploads) {
String pageId = image.getString("@pageid");
if (pageId.equals("0")) {
for (LogEventResult.LogEvent image : logEvents) {
if (image.isDeleted()) {
// means that this upload was deleted.
continue;
}
String filename = image.getString("@title");
String filename = image.getFilename();
if(fileExists(contentProviderClient, filename)) {
Timber.d("Skipping %s", filename);
continue;
}
String thumbUrl = Utils.makeThumbBaseUrl(filename);
Date dateUpdated = Utils.parseMWDate(image.getString("@timestamp"));
Date dateUpdated = image.getDateUpdated();
Contribution contrib = new Contribution(null, thumbUrl, filename, "", -1, dateUpdated, dateUpdated, user, "", "");
contrib.setState(Contribution.STATE_COMPLETED);
imageValues.add(contrib.toContentValues());
@ -118,7 +117,8 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
throw new RuntimeException(e);
}
}
queryContinue = result.getString("/api/query-continue/logevents/@lestart");
queryContinue = result.getQueryContinue();
if(TextUtils.isEmpty(queryContinue)) {
done = true;
}

View file

@ -12,8 +12,6 @@ import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import org.mediawiki.api.ApiResult;
import java.io.IOException;
import fr.free.nrw.commons.CommonsApplication;
@ -41,14 +39,14 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
}
// Exit early if nothing to do
if(allModifications == null || allModifications.getCount() == 0) {
if (allModifications == null || allModifications.getCount() == 0) {
Timber.d("No modifications to perform");
return;
}
String authCookie;
try {
authCookie = AccountManager.get(getContext()).blockingGetAuthToken(account, "", false);
authCookie = AccountManager.get(getContext()).blockingGetAuthToken(account, "", false);
} catch (OperationCanceledException | AuthenticatorException e) {
throw new RuntimeException(e);
} catch (IOException e) {
@ -56,7 +54,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
return;
}
if(Utils.isNullOrWhiteSpace(authCookie)) {
if (Utils.isNullOrWhiteSpace(authCookie)) {
Timber.d("Could not authenticate :(");
return;
}
@ -65,7 +63,6 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
api.setAuthCookie(authCookie);
String editToken;
ApiResult requestResult, responseResult;
try {
editToken = api.getEditToken();
} catch (IOException e) {
@ -81,7 +78,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
try {
contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
while(!allModifications.isAfterLast()) {
while (!allModifications.isAfterLast()) {
ModifierSequence sequence = ModifierSequence.fromCursor(allModifications);
sequence.setContentProviderClient(contentProviderClient);
Contribution contrib;
@ -95,32 +92,31 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
contributionCursor.moveToFirst();
contrib = Contribution.fromCursor(contributionCursor);
if(contrib.getState() == Contribution.STATE_COMPLETED) {
if (contrib.getState() == Contribution.STATE_COMPLETED) {
String pageContent;
try {
requestResult = api.revisionsByFilename(contrib.getFilename());
pageContent = api.revisionsByFilename(contrib.getFilename());
} catch (IOException e) {
Timber.d("Network fuckup on modifications sync!");
continue;
}
Timber.d("Page content is %s", Utils.getStringFromDOM(requestResult.getDocument()));
String pageContent = requestResult.getString("/api/query/pages/page/revisions/rev");
String processedPageContent = sequence.executeModifications(contrib.getFilename(), pageContent);
Timber.d("Page content is %s", pageContent);
String processedPageContent = sequence.executeModifications(contrib.getFilename(), pageContent);
String editResult;
try {
responseResult = api.edit(editToken, processedPageContent, contrib.getFilename(), sequence.getEditSummary());
editResult = api.edit(editToken, processedPageContent, contrib.getFilename(), sequence.getEditSummary());
} catch (IOException e) {
Timber.d("Network fuckup on modifications sync!");
continue;
}
Timber.d("Response is %s", Utils.getStringFromDOM(responseResult.getDocument()));
Timber.d("Response is %s", editResult);
String result = responseResult.getString("/api/edit/@result");
if(!result.equals("Success")) {
if (!editResult.equals("Success")) {
// FIXME: Log this somewhere else
Timber.d("Non success result! %s", result);
Timber.d("Non success result! %s", editResult);
} else {
sequence.delete();
}
@ -128,7 +124,7 @@ public class ModificationsSyncAdapter extends AbstractThreadedSyncAdapter {
allModifications.moveToNext();
}
} finally {
if(contributionsClient != null) {
if (contributionsClient != null) {
contributionsClient.release();
}
}

View file

@ -1,6 +1,8 @@
package fr.free.nrw.commons.mwapi;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.apache.http.HttpResponse;
@ -15,33 +17,30 @@ import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.mediawiki.api.ApiResult;
import org.mediawiki.api.MWApi;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.Utils;
import in.yuvi.http.fluent.Http;
import timber.log.Timber;
/**
* @author Addshore
*/
public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implements MediaWikiApi {
public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
private static final String THUMB_SIZE = "640";
private static AbstractHttpClient httpClient;
private AbstractHttpClient httpClient;
private MWApi api;
public ApacheHttpClientMediaWikiApi(String apiURL) {
super(apiURL, getHttpClient());
}
private static AbstractHttpClient getHttpClient() {
if (httpClient == null) {
httpClient = newHttpClient();
}
return httpClient;
}
private static AbstractHttpClient newHttpClient() {
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
@ -49,10 +48,10 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
params.setParameter(CoreProtocolPNames.USER_AGENT, "Commons/" + BuildConfig.VERSION_NAME + " (https://mediawiki.org/wiki/Apps/Commons) Android/" + Build.VERSION.RELEASE);
return new DefaultHttpClient(cm, params);
httpClient = new DefaultHttpClient(cm, params);
api = new MWApi(apiURL, httpClient);
}
/**
* @param username String
* @param password String
@ -60,7 +59,7 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
* @throws IOException On api request IO issue
*/
public String login(String username, String password) throws IOException {
return getErrorCodeToReturn(action("clientlogin")
return getErrorCodeToReturn(api.action("clientlogin")
.param("rememberMe", "1")
.param("username", username)
.param("password", password)
@ -77,7 +76,7 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
* @throws IOException On api request IO issue
*/
public String login(String username, String password, String twoFactorCode) throws IOException {
return getErrorCodeToReturn(action("clientlogin")
return getErrorCodeToReturn(api.action("clientlogin")
.param("rememberMe", "1")
.param("username", username)
.param("password", password)
@ -88,7 +87,7 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
}
private String getLoginToken() throws IOException {
return this.action("query")
return api.action("query")
.param("action", "query")
.param("meta", "tokens")
.param("type", "login")
@ -106,7 +105,7 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
private String getErrorCodeToReturn(ApiResult loginApiResult) {
String status = loginApiResult.getString("/api/clientlogin/@status");
if (status.equals("PASS")) {
this.isLoggedIn = true;
api.isLoggedIn = true;
return status;
} else if (status.equals("FAIL")) {
return loginApiResult.getString("/api/clientlogin/@messagecode");
@ -122,10 +121,29 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
return "genericerror-" + status;
}
// Moved / consolidated methods
@Override
public String getAuthCookie() {
return api.getAuthCookie();
}
@Override
public void setAuthCookie(String authCookie) {
api.setAuthCookie(authCookie);
}
@Override
public boolean validateLogin() throws IOException {
return api.validateLogin();
}
@Override
public String getEditToken() throws IOException {
return api.getEditToken();
}
@Override
public boolean fileExistsWithName(String fileName) throws IOException {
return action("query")
return api.action("query")
.param("prop", "imageinfo")
.param("titles", "File:" + fileName)
.get()
@ -133,18 +151,20 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
}
@Override
public ApiResult edit(String editToken, String processedPageContent, String filename, String summary) throws IOException {
return action("edit")
@Nullable
public String edit(String editToken, String processedPageContent, String filename, String summary) throws IOException {
return api.action("edit")
.param("title", filename)
.param("token", editToken)
.param("text", processedPageContent)
.param("summary", summary)
.post();
.post()
.getString("/api/edit/@result");
}
@Override
public String findThumbnailByFilename(String filename) throws IOException {
return action("query")
return api.action("query")
.param("format", "xml")
.param("prop", "imageinfo")
.param("iiprop", "url")
@ -155,52 +175,101 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
}
@Override
public ApiResult fetchMediaByFilename(String filename) throws IOException {
return action("query")
@NonNull
public MediaResult fetchMediaByFilename(String filename) throws IOException {
ApiResult apiResult = api.action("query")
.param("prop", "revisions")
.param("titles", filename)
.param("rvprop", "content")
.param("rvlimit", 1)
.param("rvgeneratexml", 1)
.get();
return new MediaResult(
apiResult.getString("/api/query/pages/page/revisions/rev"),
apiResult.getString("/api/query/pages/page/revisions/rev/@parsetree"));
}
@Override
public ApiResult searchCategories(int searchCatsLimit, String filterValue) throws IOException {
return action("query")
@NonNull
public List<String> searchCategories(int searchCatsLimit, String filterValue) throws IOException {
List<ApiResult> categoryNodes = api.action("query")
.param("format", "xml")
.param("list", "search")
.param("srwhat", "text")
.param("srnamespace", "14")
.param("srlimit", searchCatsLimit)
.param("srsearch", filterValue)
.get();
.get()
.getNodes("/api/query/search/p/@title");
if (categoryNodes == null) {
return Collections.emptyList();
}
List<String> categories = new ArrayList<>();
for (ApiResult categoryNode : categoryNodes) {
String cat = categoryNode.getDocument().getTextContent();
String catString = cat.replace("Category:", "");
categories.add(catString);
}
return categories;
}
@Override
public ApiResult allCategories(int searchCatsLimit, String filterValue) throws IOException {
return action("query")
@NonNull
public List<String> allCategories(int searchCatsLimit, String filterValue) throws IOException {
ArrayList<ApiResult> categoryNodes = api.action("query")
.param("list", "allcategories")
.param("acprefix", filterValue)
.param("aclimit", searchCatsLimit)
.get();
.get()
.getNodes("/api/query/allcategories/c");
if (categoryNodes == null) {
return Collections.emptyList();
}
List<String> categories = new ArrayList<>();
for (ApiResult categoryNode : categoryNodes) {
categories.add(categoryNode.getDocument().getTextContent());
}
return categories;
}
@Override
public ApiResult searchTitles(int searchCatsLimit, String title) throws IOException {
return action("query")
@NonNull
public List<String> searchTitles(int searchCatsLimit, String title) throws IOException {
ArrayList<ApiResult> categoryNodes = api.action("query")
.param("format", "xml")
.param("list", "search")
.param("srwhat", "text")
.param("srnamespace", "14")
.param("srlimit", searchCatsLimit)
.param("srsearch", title)
.get();
.get()
.getNodes("/api/query/search/p/@title");
if (categoryNodes == null) {
return Collections.emptyList();
}
List<String> titleCategories = new ArrayList<>();
for (ApiResult categoryNode : categoryNodes) {
String cat = categoryNode.getDocument().getTextContent();
String catString = cat.replace("Category:", "");
titleCategories.add(catString);
}
return titleCategories;
}
@Override
public ApiResult logEvents(String user, String lastModified, String queryContinue, int limit) throws IOException {
org.mediawiki.api.MWApi.RequestBuilder builder = action("query")
@NonNull
public LogEventResult logEvents(String user, String lastModified, String queryContinue, int limit) throws IOException {
org.mediawiki.api.MWApi.RequestBuilder builder = api.action("query")
.param("list", "logevents")
.param("letype", "upload")
.param("leprop", "title|timestamp|ids")
@ -212,25 +281,47 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
if (!TextUtils.isEmpty(queryContinue)) {
builder.param("lestart", queryContinue);
}
return builder.get();
ApiResult result = builder.get();
return new LogEventResult(
getLogEventsFromResult(result),
result.getString("/api/query-continue/logevents/@lestart"));
}
@NonNull
private ArrayList<LogEventResult.LogEvent> getLogEventsFromResult(ApiResult result) {
ArrayList<ApiResult> uploads = result.getNodes("/api/query/logevents/item");
Timber.d("%d results!", uploads.size());
ArrayList<LogEventResult.LogEvent> logEvents = new ArrayList<>();
for (ApiResult image : uploads) {
logEvents.add(new LogEventResult.LogEvent(
image.getString("@pageid"),
image.getString("@title"),
Utils.parseMWDate(image.getString("@timestamp")))
);
}
return logEvents;
}
@Override
public ApiResult revisionsByFilename(String filename) throws IOException {
return action("query")
@Nullable
public String revisionsByFilename(String filename) throws IOException {
return api.action("query")
.param("prop", "revisions")
.param("rvprop", "timestamp|content")
.param("titles", filename)
.get();
.get()
.getString("/api/query/pages/page/revisions/rev");
}
@Override
public ApiResult existingFile(String fileSha1) throws IOException {
return action("query")
public boolean existingFile(String fileSha1) throws IOException {
return api.action("query")
.param("format", "xml")
.param("list", "allimages")
.param("aisha1", fileSha1)
.get();
.get()
.getNodes("/api/query/allimages/img").size() > 0;
}
@Override
@ -255,4 +346,25 @@ public class ApacheHttpClientMediaWikiApi extends org.mediawiki.api.MWApi implem
return allSuccess;
}
@Override
@NonNull
public UploadResult uploadFile(String filename, InputStream file, long dataLength, String pageContents, String editSummary, final ProgressListener progressListener) throws IOException {
ApiResult result = api.upload(filename, file, dataLength, pageContents, editSummary, new in.yuvi.http.fluent.ProgressListener() {
@Override
public void onProgress(long transferred, long total) {
progressListener.onProgress(transferred, total);
}
});
String resultStatus = result.getString("/api/upload/@result");
if (!resultStatus.equals("Success")) {
String errorCode = result.getString("/api/error/@code");
return new UploadResult(resultStatus, errorCode);
} else {
Date dateUploaded = Utils.parseMWDate(result.getString("/api/upload/imageinfo/@timestamp"));
String canonicalFilename = "File:" + result.getString("/api/upload/@filename").replace("_", " "); // Title vs Filename
String imageUrl = result.getString("/api/upload/imageinfo/@url");
return new UploadResult(dateUploaded, canonicalFilename, imageUrl);
}
}
}

View file

@ -0,0 +1,51 @@
package fr.free.nrw.commons.mwapi;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Date;
import java.util.List;
public class LogEventResult {
private final List<LogEvent> logEvents;
private final String queryContinue;
LogEventResult(@NonNull List<LogEvent> logEvents, String queryContinue) {
this.logEvents = logEvents;
this.queryContinue = queryContinue;
}
@NonNull
public List<LogEvent> getLogEvents() {
return logEvents;
}
@Nullable
public String getQueryContinue() {
return queryContinue;
}
public static class LogEvent {
private final String pageId;
private final String filename;
private final Date dateUpdated;
LogEvent(String pageId, String filename, Date dateUpdated) {
this.pageId = pageId;
this.filename = filename;
this.dateUpdated = dateUpdated;
}
public boolean isDeleted() {
return pageId.equals("0");
}
public String getFilename() {
return filename;
}
public Date getDateUpdated() {
return dateUpdated;
}
}
}

View file

@ -0,0 +1,19 @@
package fr.free.nrw.commons.mwapi;
public class MediaResult {
private final String wikiSource;
private final String parseTreeXmlSource;
MediaResult(String wikiSource, String parseTreeXmlSource) {
this.wikiSource = wikiSource;
this.parseTreeXmlSource = parseTreeXmlSource;
}
public String getWikiSource() {
return wikiSource;
}
public String getParseTreeXmlSource() {
return parseTreeXmlSource;
}
}

View file

@ -1,11 +1,11 @@
package fr.free.nrw.commons.mwapi;
import org.mediawiki.api.ApiResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import in.yuvi.http.fluent.ProgressListener;
import java.util.List;
public interface MediaWikiApi {
String getAuthCookie();
@ -20,27 +20,39 @@ public interface MediaWikiApi {
String getEditToken() throws IOException;
ApiResult upload(String filename, InputStream file, long dataLength, String pageContents, String editSummary, ProgressListener progressListener) throws IOException;
boolean fileExistsWithName(String fileName) throws IOException;
ApiResult edit(String editToken, String processedPageContent, String filename, String summary) throws IOException;
String findThumbnailByFilename(String filename) throws IOException;
ApiResult fetchMediaByFilename(String filename) throws IOException;
ApiResult searchCategories(int searchCatsLimit, String filterValue) throws IOException;
ApiResult allCategories(int searchCatsLimit, String filter) throws IOException;
ApiResult searchTitles(int searchCatsLimit, String title) throws IOException;
ApiResult logEvents(String user, String lastModified, String queryContinue, int limit) throws IOException;
ApiResult revisionsByFilename(String filename) throws IOException;
ApiResult existingFile(String fileSha1) throws IOException;
boolean logEvents(LogBuilder[] logBuilders);
@NonNull
UploadResult uploadFile(String filename, InputStream file, long dataLength, String pageContents, String editSummary, ProgressListener progressListener) throws IOException;
@Nullable
String edit(String editToken, String processedPageContent, String filename, String summary) throws IOException;
@NonNull
MediaResult fetchMediaByFilename(String filename) throws IOException;
@NonNull
List<String> searchCategories(int searchCatsLimit, String filterValue) throws IOException;
@NonNull
List<String> allCategories(int searchCatsLimit, String filter) throws IOException;
@NonNull
List<String> searchTitles(int searchCatsLimit, String title) throws IOException;
@Nullable
String revisionsByFilename(String filename) throws IOException;
boolean existingFile(String fileSha1) throws IOException;
@NonNull
LogEventResult logEvents(String user, String lastModified, String queryContinue, int limit) throws IOException;
interface ProgressListener {
void onProgress(long transferred, long total);
}
}

View file

@ -0,0 +1,42 @@
package fr.free.nrw.commons.mwapi;
import java.util.Date;
public class UploadResult {
private String errorCode;
private String resultStatus;
private Date dateUploaded;
private String imageUrl;
private String canonicalFilename;
UploadResult(String resultStatus, String errorCode) {
this.resultStatus = resultStatus;
this.errorCode = errorCode;
}
UploadResult(Date dateUploaded, String canonicalFilename, String imageUrl) {
this.dateUploaded = dateUploaded;
this.canonicalFilename = canonicalFilename;
this.imageUrl = imageUrl;
}
public Date getDateUploaded() {
return dateUploaded;
}
public String getImageUrl() {
return imageUrl;
}
public String getCanonicalFilename() {
return canonicalFilename;
}
public String getErrorCode() {
return errorCode;
}
public String getResultStatus() {
return resultStatus;
}
}

View file

@ -6,10 +6,7 @@ import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import org.mediawiki.api.ApiResult;
import java.io.IOException;
import java.util.ArrayList;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
@ -50,23 +47,17 @@ public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
MediaWikiApi api = CommonsApplication.getInstance().getMWApi();
ApiResult result;
// https://commons.wikimedia.org/w/api.php?action=query&list=allimages&format=xml&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba
boolean fileExists;
try {
String fileSha1 = this.fileSha1;
result = api.existingFile(fileSha1);
Timber.d("Searching Commons API for existing file: %s", result);
fileExists = api.existingFile(fileSha1);
} catch (IOException e) {
Timber.e(e, "IO Exception: ");
return false;
}
ArrayList<ApiResult> resultNodes = result.getNodes("/api/query/allimages/img");
Timber.d("Result nodes: %s", resultNodes);
boolean fileExists = !resultNodes.isEmpty();
Timber.d("File already exists in Commons: %s", fileExists);
return fileExists;
}

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons.upload;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
@ -13,12 +14,9 @@ import android.support.v4.app.NotificationCompat;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import org.mediawiki.api.ApiResult;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
@ -34,7 +32,7 @@ import fr.free.nrw.commons.contributions.ContributionsContentProvider;
import fr.free.nrw.commons.modifications.ModificationsContentProvider;
import fr.free.nrw.commons.mwapi.EventLog;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import in.yuvi.http.fluent.ProgressListener;
import fr.free.nrw.commons.mwapi.UploadResult;
import timber.log.Timber;
public class UploadService extends HandlerService<Contribution> {
@ -69,7 +67,7 @@ public class UploadService extends HandlerService<Contribution> {
super("UploadService");
}
private class NotificationUpdateProgressListener implements ProgressListener {
private class NotificationUpdateProgressListener implements MediaWikiApi.ProgressListener {
String notificationTag;
boolean notificationTitleChanged;
@ -180,10 +178,10 @@ public class UploadService extends HandlerService<Contribution> {
return START_REDELIVER_INTENT;
}
@SuppressLint("StringFormatInvalid")
private void uploadContribution(Contribution contribution) {
MediaWikiApi api = app.getMWApi();
ApiResult result;
InputStream file = null;
String notificationTag = contribution.getLocalUri().toString();
@ -240,32 +238,27 @@ public class UploadService extends HandlerService<Contribution> {
getString(R.string.upload_progress_notification_title_finishing, contribution.getDisplayTitle()),
contribution
);
result = api.upload(filename, file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
UploadResult uploadResult = api.uploadFile(filename, file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
Timber.d("Response is %s", Utils.getStringFromDOM(result.getDocument()));
Timber.d("Response is %s", uploadResult.toString());
curProgressNotification = null;
String resultStatus = result.getString("/api/upload/@result");
String resultStatus = uploadResult.getResultStatus();
if(!resultStatus.equals("Success")) {
String errorCode = result.getString("/api/error/@code");
showFailedNotification(contribution);
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT)
.param("username", app.getCurrentAccount().name)
.param("source", contribution.getSource())
.param("multiple", contribution.getMultiple())
.param("result", errorCode)
.param("result", uploadResult.getErrorCode())
.param("filename", contribution.getFilename())
.log();
} else {
Date dateUploaded = null;
dateUploaded = Utils.parseMWDate(result.getString("/api/upload/imageinfo/@timestamp"));
String canonicalFilename = "File:" + result.getString("/api/upload/@filename").replace("_", " "); // Title vs Filename
String imageUrl = result.getString("/api/upload/imageinfo/@url");
contribution.setFilename(canonicalFilename);
contribution.setImageUrl(imageUrl);
contribution.setFilename(uploadResult.getCanonicalFilename());
contribution.setImageUrl(uploadResult.getImageUrl());
contribution.setState(Contribution.STATE_COMPLETED);
contribution.setDateUploaded(dateUploaded);
contribution.setDateUploaded(uploadResult.getDateUploaded());
contribution.save();
EventLog.schema(CommonsApplication.EVENT_UPLOAD_ATTEMPT)
@ -279,7 +272,6 @@ public class UploadService extends HandlerService<Contribution> {
} catch(IOException e) {
Timber.d("I have a network fuckup");
showFailedNotification(contribution);
return;
} finally {
if ( filename != null ) {
unfinishedUploads.remove(filename);
@ -293,8 +285,9 @@ public class UploadService extends HandlerService<Contribution> {
}
}
@SuppressLint("StringFormatInvalid")
private void showFailedNotification(Contribution contribution) {
Notification failureNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
Notification failureNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ContributionsActivity.class), 0))