Search in sources :

Example 1 with MwQueryPage

use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.

the class DisambigListAdapter method fetchDescriptions.

/**
 * Start getting Wikidata descriptions (directly from the current Wikipedia wiki).
 */
private void fetchDescriptions() {
    final List<PageTitle> titleList = new ArrayList<>();
    for (DisambigResult r : items) {
        titleList.add(r.getTitle());
    }
    if (titleList.isEmpty()) {
        return;
    }
    new DescriptionClient().request(wiki, titleList, new DescriptionClient.Callback() {

        @Override
        public void success(@NonNull Call<MwQueryResponse> call, @NonNull List<MwQueryPage> results) {
            for (MwQueryPage page : results) {
                PageTitle pageTitle = new PageTitle(null, page.title(), wiki);
                for (PageTitle title : titleList) {
                    if (title.getPrefixedText().equals(pageTitle.getPrefixedText()) || title.getDisplayText().equals(pageTitle.getDisplayText())) {
                        title.setDescription(page.description());
                        break;
                    }
                }
            }
            notifyDataSetChanged();
        }

        @Override
        public void failure(@NonNull Call<MwQueryResponse> call, @NonNull Throwable caught) {
        // descriptions are expendable
        }
    });
}
Also used : DescriptionClient(org.wikipedia.wikidata.DescriptionClient) MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) ArrayList(java.util.ArrayList) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse)

Example 2 with MwQueryPage

use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.

the class DescriptionClientTest method testRequestSuccess.

@Test
public void testRequestSuccess() throws Throwable {
    enqueueFromFile("reading_list_page_info.json");
    DescriptionClient.Callback cb = mock(DescriptionClient.Callback.class);
    Call<MwQueryResponse> call = request(cb);
    server().takeRequest();
    ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
    verify(cb).success(eq(call), captor.capture());
    List<MwQueryPage> result = captor.getValue();
    MwQueryPage biden = result.get(0);
    MwQueryPage obama = result.get(1);
    assertThat(biden.title(), is("Joe Biden"));
    assertThat(biden.description(), is("47th Vice President of the United States"));
    assertThat(obama.title(), is("Barack Obama"));
    assertThat(obama.description(), is("44th President of the United States of America"));
}
Also used : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) List(java.util.List) Test(org.junit.Test) MockWebServerTest(org.wikipedia.test.MockWebServerTest)

Example 3 with MwQueryPage

use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.

the class ShareHandler method shareSnippet.

private void shareSnippet(@NonNull CharSequence input) {
    final String selectedText = StringUtil.sanitizeText(input.toString());
    final PageTitle title = fragment.getTitle();
    final String leadImageNameText = fragment.getPage().getPageProperties().getLeadImageName() != null ? fragment.getPage().getPageProperties().getLeadImageName() : "";
    final PageTitle imageTitle = new PageTitle(Namespace.FILE.toLegacyString(), leadImageNameText, title.getWikiSite());
    disposables.add(ServiceFactory.get(title.getWikiSite()).getImageExtMetadata(imageTitle.getPrefixedText()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(response -> {
        // noinspection ConstantConditions
        MwQueryPage page = response.query().pages().get(0);
        return page.imageInfo() != null && page.imageInfo().getMetadata() != null ? new ImageLicense(page.imageInfo().getMetadata()) : new ImageLicense();
    }).subscribe(imageLicense -> {
        final Bitmap snippetBitmap = SnippetImage.getSnippetImage(fragment.requireContext(), fragment.getLeadImageBitmap(), title.getDisplayText(), fragment.getPage().isMainPage() ? "" : StringUtils.capitalize(title.getDescription()), selectedText, imageLicense);
        fragment.showBottomSheet(new PreviewDialog(fragment.getContext(), snippetBitmap, title, selectedText, funnel));
    }, caught -> {
        // If we failed to get license info for the lead image, just share the text
        PreviewDialog.shareAsText(fragment.requireContext(), title, selectedText, funnel);
        L.e("Error fetching image license info for " + title.getDisplayText(), caught);
    }));
}
Also used : Context(android.content.Context) ActionMode(android.view.ActionMode) Arrays(java.util.Arrays) NoDimBottomSheetDialog(org.wikipedia.page.NoDimBottomSheetDialog) ImageView(android.widget.ImageView) PageProperties(org.wikipedia.page.PageProperties) NonNull(android.support.annotation.NonNull) StringUtils(org.apache.commons.lang3.StringUtils) ShareUtil(org.wikipedia.util.ShareUtil) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Locale(java.util.Locale) WikipediaApp(org.wikipedia.WikipediaApp) StringUtil(org.wikipedia.util.StringUtil) WiktionaryDialog(org.wikipedia.wiktionary.WiktionaryDialog) Menu(android.view.Menu) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) ImageLicense(org.wikipedia.gallery.ImageLicense) ActivityUtil(org.wikipedia.activity.ActivityUtil) ServiceFactory(org.wikipedia.dataclient.ServiceFactory) R(org.wikipedia.R) LayoutInflater(android.view.LayoutInflater) MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) PageTitle(org.wikipedia.page.PageTitle) CommunicationBridge(org.wikipedia.bridge.CommunicationBridge) Namespace(org.wikipedia.page.Namespace) UriUtil(org.wikipedia.util.UriUtil) Prefs(org.wikipedia.settings.Prefs) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) ShareMode(org.wikipedia.analytics.ShareAFactFunnel.ShareMode) ShareAFactFunnel(org.wikipedia.analytics.ShareAFactFunnel) PageFragment(org.wikipedia.page.PageFragment) Bitmap(android.graphics.Bitmap) Page(org.wikipedia.page.Page) L(org.wikipedia.util.log.L) Nullable(android.support.annotation.Nullable) FeedbackUtil(org.wikipedia.util.FeedbackUtil) ImageLicense(org.wikipedia.gallery.ImageLicense) Bitmap(android.graphics.Bitmap) MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) PageTitle(org.wikipedia.page.PageTitle)

Example 4 with MwQueryPage

use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.

the class ReadingListPageInfoClientTest method testRequestSuccess.

@Test
@SuppressWarnings("checkstyle:magicnumber")
public void testRequestSuccess() throws Throwable {
    enqueueFromFile("reading_list_page_info.json");
    ReadingListPageInfoClient.Callback cb = mock(ReadingListPageInfoClient.Callback.class);
    Call<MwQueryResponse> call = request(cb);
    server().takeRequest();
    ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
    verify(cb).success(eq(call), captor.capture());
    List<MwQueryPage> result = captor.getValue();
    MwQueryPage biden = result.get(0);
    MwQueryPage obama = result.get(1);
    assertThat(biden.title(), is("Joe Biden"));
    assertThat(biden.description(), is("47th Vice President of the United States"));
    assertThat(biden.thumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Official_portrait_of_Vice_President_Joe_Biden.jpg/255px-Official_portrait_of_Vice_President_Joe_Biden.jpg"));
    assertThat(obama.title(), is("Barack Obama"));
    assertThat(obama.description(), is("44th President of the United States of America"));
    assertThat(obama.thumbUrl(), is("https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/256px-President_Barack_Obama.jpg"));
}
Also used : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) List(java.util.List) Test(org.junit.Test) MockWebServerTest(org.wikipedia.test.MockWebServerTest)

Example 5 with MwQueryPage

use of org.wikipedia.dataclient.mwapi.MwQueryPage in project apps-android-wikipedia by wikimedia.

the class ImageLicenseFetchClient method request.

@VisibleForTesting
Call<MwQueryResponse> request(@NonNull Service service, @NonNull final PageTitle title, @NonNull final Callback cb) {
    Call<MwQueryResponse> call = service.request(title.toString());
    call.enqueue(new retrofit2.Callback<MwQueryResponse>() {

        @Override
        public void onResponse(Call<MwQueryResponse> call, Response<MwQueryResponse> response) {
            if (response.body().success()) {
                // noinspection ConstantConditions
                MwQueryPage page = response.body().query().pages().get(0);
                cb.success(call, page.imageInfo() != null && page.imageInfo().getMetadata() != null ? new ImageLicense(page.imageInfo().getMetadata()) : new ImageLicense());
            } else if (response.body().hasError()) {
                // noinspection ConstantConditions
                cb.failure(call, new MwException(response.body().getError()));
            } else {
                cb.failure(call, new IOException("An unknown error occurred."));
            }
        }

        @Override
        public void onFailure(Call<MwQueryResponse> call, Throwable t) {
            cb.failure(call, t);
        }
    });
    return call;
}
Also used : MwQueryPage(org.wikipedia.dataclient.mwapi.MwQueryPage) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) MwException(org.wikipedia.dataclient.mwapi.MwException) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

MwQueryPage (org.wikipedia.dataclient.mwapi.MwQueryPage)7 MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)5 PageTitle (org.wikipedia.page.PageTitle)3 VisibleForTesting (android.support.annotation.VisibleForTesting)2 ArrayMap (android.support.v4.util.ArrayMap)2 IOException (java.io.IOException)2 List (java.util.List)2 Test (org.junit.Test)2 MwException (org.wikipedia.dataclient.mwapi.MwException)2 MockWebServerTest (org.wikipedia.test.MockWebServerTest)2 Context (android.content.Context)1 Bitmap (android.graphics.Bitmap)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 ActionMode (android.view.ActionMode)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ImageView (android.widget.ImageView)1