Search in sources :

Example 1 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class MethodAUpdater method doInBackground.

@Override
protected ArrayList<String> doInBackground(Void... voids) {
    //otherwise if user has typed something in that isn't in cache, search API for matching categories
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    ArrayList<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.action("query").param("format", "xml").param("list", "search").param("srwhat", "text").param("srnamespace", "14").param("srlimit", catFragment.SEARCH_CATS_LIMIT).param("srsearch", filter).get();
        Timber.d("Method A URL filter %s", result);
    } 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));
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 2 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class PrefixUpdater method doInBackground.

@Override
protected ArrayList<String> doInBackground(Void... voids) {
    //If user hasn't typed anything in yet, get GPS and recent items
    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)) {
        ArrayList<String> cachedItems = new ArrayList<>(catFragment.categoriesCache.get(filter));
        Timber.d("Found cache items, waiting for filter");
        return new ArrayList<>(filterYears(cachedItems));
    }
    //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
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    ArrayList<String> categories = new ArrayList<>();
    try {
        result = api.action("query").param("list", "allcategories").param("acprefix", filter).param("aclimit", catFragment.SEARCH_CATS_LIMIT).get();
        Timber.d("Prefix URL filter %s", result);
    } 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));
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class TitleCategories method doInBackground.

@Override
protected ArrayList<String> doInBackground(Void... voids) {
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    ArrayList<String> items = 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.action("query").param("format", "xml").param("list", "search").param("srwhat", "text").param("srnamespace", "14").param("srlimit", SEARCH_CATS_LIMIT).param("srsearch", title).get();
        Timber.d("Searching for cats for title: %s", result);
    } catch (IOException e) {
        Timber.e(e, "IO Exception: ");
        //Return empty arraylist
        return items;
    }
    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", items);
    return items;
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 4 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class ContributionsSyncAdapter method onPerformSync.

@Override
public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
    // This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
    String user = account.name;
    MWApi api = CommonsApplication.getInstance().getMWApi();
    SharedPreferences prefs = this.getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
    String lastModified = prefs.getString("lastSyncTimestamp", "");
    Date curTime = new Date();
    ApiResult result;
    Boolean done = false;
    String queryContinue = null;
    while (!done) {
        try {
            MWApi.RequestBuilder builder = api.action("query").param("list", "logevents").param("letype", "upload").param("leprop", "title|timestamp|ids").param("leuser", user).param("lelimit", getLimit());
            if (!TextUtils.isEmpty(lastModified)) {
                builder.param("leend", lastModified);
            }
            if (!TextUtils.isEmpty(queryContinue)) {
                builder.param("lestart", queryContinue);
            }
            result = builder.get();
        } catch (IOException e) {
            // There isn't really much we can do, eh?
            // FIXME: Perhaps add EventLogging?
            // Not sure if this does anything. Shitty docs
            syncResult.stats.numIoExceptions += 1;
            Timber.d("Syncing failed due to %s", e);
            return;
        }
        Timber.d("Last modified at %s", lastModified);
        ArrayList<ApiResult> uploads = result.getNodes("/api/query/logevents/item");
        Timber.d("%d results!", uploads.size());
        ArrayList<ContentValues> imageValues = new ArrayList<>();
        for (ApiResult image : uploads) {
            String pageId = image.getString("@pageid");
            if (pageId.equals("0")) {
                // means that this upload was deleted.
                continue;
            }
            String filename = image.getString("@title");
            if (fileExists(contentProviderClient, filename)) {
                Timber.d("Skipping %s", filename);
                continue;
            }
            String thumbUrl = Utils.makeThumbBaseUrl(filename);
            Date dateUpdated = Utils.parseMWDate(image.getString("@timestamp"));
            Contribution contrib = new Contribution(null, thumbUrl, filename, "", -1, dateUpdated, dateUpdated, user, "", "");
            contrib.setState(Contribution.STATE_COMPLETED);
            imageValues.add(contrib.toContentValues());
            if (imageValues.size() % COMMIT_THRESHOLD == 0) {
                try {
                    contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues.toArray(new ContentValues[] {}));
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
                imageValues.clear();
            }
        }
        if (imageValues.size() != 0) {
            try {
                contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues.toArray(new ContentValues[] {}));
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }
        queryContinue = result.getString("/api/query-continue/logevents/@lestart");
        if (TextUtils.isEmpty(queryContinue)) {
            done = true;
        }
    }
    prefs.edit().putString("lastSyncTimestamp", Utils.toMWDate(curTime)).apply();
    Timber.d("Oh hai, everyone! Look, a kitty!");
}
Also used : ContentValues(android.content.ContentValues) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Date(java.util.Date) ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) RemoteException(android.os.RemoteException)

Example 5 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class WikiAccountAuthenticator method getAuthCookie.

private String getAuthCookie(String username, String password) throws IOException {
    MWApi api = CommonsApplication.getInstance().getMWApi();
    //TODO add 2fa support here
    String result = api.login(username, password);
    if (result.equals("PASS")) {
        return api.getAuthCookie();
    } else {
        return null;
    }
}
Also used : MWApi(fr.free.nrw.commons.MWApi)

Aggregations

MWApi (fr.free.nrw.commons.MWApi)7 IOException (java.io.IOException)6 ApiResult (org.mediawiki.api.ApiResult)6 ArrayList (java.util.ArrayList)4 RemoteException (android.os.RemoteException)2 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentValues (android.content.ContentValues)1 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 Contribution (fr.free.nrw.commons.contributions.Contribution)1 Date (java.util.Date)1