jenkins-bot has submitted this change and it was merged.

Change subject: Add support for loading link preview content via RESTBase
......................................................................


Add support for loading link preview content via RESTBase

Obtains link preview content from a RESTBase lead section request, if the
article is on English Wikipedia and RESTBase content loading is enabled.

A forthcoming patch will add a feature flag to push this to a small
portion of our beta users.

Also eliminates some unused variables and adds a null check to both
versions of *PageLead.getDescription().

Bug: T111126
Change-Id: I7bb5335639f720fe2fb62422ff2a3cafa93fadfc
---
M app/src/main/java/org/wikipedia/page/JsonPageLoadStrategy.java
M app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewContents.java
M app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewDialog.java
M app/src/main/java/org/wikipedia/server/mwapi/MwPageLead.java
M app/src/main/java/org/wikipedia/server/restbase/RbPageLead.java
M app/src/main/java/org/wikipedia/server/restbase/RbPageService.java
A app/src/main/java/org/wikipedia/util/PageLoadUtil.java
7 files changed, 117 insertions(+), 36 deletions(-)

Approvals:
  Sniedzielski: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/app/src/main/java/org/wikipedia/page/JsonPageLoadStrategy.java 
b/app/src/main/java/org/wikipedia/page/JsonPageLoadStrategy.java
index 0f5d38c..572383f 100644
--- a/app/src/main/java/org/wikipedia/page/JsonPageLoadStrategy.java
+++ b/app/src/main/java/org/wikipedia/page/JsonPageLoadStrategy.java
@@ -12,8 +12,6 @@
 import org.wikipedia.page.bottomcontent.BottomContentHandler;
 import org.wikipedia.page.bottomcontent.BottomContentInterface;
 import org.wikipedia.page.leadimages.LeadImagesHandler;
-import org.wikipedia.server.PageService;
-import org.wikipedia.server.PageServiceFactory;
 import org.wikipedia.server.PageLead;
 import org.wikipedia.server.PageRemaining;
 import org.wikipedia.server.ServiceError;
@@ -22,6 +20,7 @@
 import org.wikipedia.savedpages.LoadSavedPageTask;
 import org.wikipedia.search.SearchBarHideHandler;
 import org.wikipedia.util.DimenUtil;
+import org.wikipedia.util.PageLoadUtil;
 import org.wikipedia.views.ObservableWebView;
 import org.wikipedia.views.SwipeRefreshLayoutWithScroll;
 
@@ -34,7 +33,6 @@
 import retrofit.client.Response;
 
 import android.content.Intent;
-import android.content.res.Resources;
 import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.annotation.VisibleForTesting;
@@ -616,8 +614,11 @@
 
     @VisibleForTesting
     protected void loadLeadSection(final int startSequenceNum) {
-        getApiService().pageLead(model.getTitle().getPrefixedText(), 
calculateLeadImageWidth(),
-                !app.isImageDownloadEnabled(), new PageLead.Callback() {
+        PageLoadUtil.getApiService(model.getTitle().getSite()).pageLead(
+                model.getTitle().getPrefixedText(),
+                PageLoadUtil.calculateLeadImageWidth(),
+                !app.isImageDownloadEnabled(),
+                new PageLead.Callback() {
                     @Override
                     public void success(PageLead pageLead, Response response) {
                         Log.v(TAG, response.getUrl());
@@ -699,14 +700,11 @@
         }).execute();
     }
 
-    private int calculateLeadImageWidth() {
-        Resources res = app.getResources();
-        return (int) (res.getDimension(R.dimen.leadImageWidth) / 
res.getDisplayMetrics().density);
-    }
-
     private void loadRemainingSections(final int startSequenceNum) {
-        getApiService().pageRemaining(model.getTitle().getPrefixedText(),
-                !app.isImageDownloadEnabled(), new PageRemaining.Callback() {
+        PageLoadUtil.getApiService(model.getTitle().getSite()).pageRemaining(
+                model.getTitle().getPrefixedText(),
+                !app.isImageDownloadEnabled(),
+                new PageRemaining.Callback() {
                     @Override
                     public void success(PageRemaining pageRemaining, Response 
response) {
                         Log.v(TAG, response.getUrl());
@@ -740,10 +738,6 @@
             return;
         }
         fragment.commonSectionFetchOnCatch(caught);
-    }
-
-    private PageService getApiService() {
-        return PageServiceFactory.create(model.getTitle().getSite());
     }
 
     /**
diff --git 
a/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewContents.java 
b/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewContents.java
index 340d1ea..e417562 100755
--- a/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewContents.java
+++ b/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewContents.java
@@ -1,5 +1,7 @@
 package org.wikipedia.page.linkpreview;
 
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.text.TextUtils;
 
 import org.wikipedia.page.PageTitle;
@@ -7,6 +9,7 @@
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.wikipedia.Utils;
+import org.wikipedia.server.restbase.RbPageLead;
 
 import java.text.BreakIterator;
 import java.util.ArrayList;
@@ -21,17 +24,12 @@
         return title;
     }
 
-    private final String leadImageName;
-    public String getLeadImageName() {
-        return leadImageName;
-    }
-
     private final String extract;
     public String getExtract() {
         return extract;
     }
 
-    public LinkPreviewContents(JSONObject json, Site site) throws 
JSONException {
+    public LinkPreviewContents(@NonNull JSONObject json, @NonNull Site site) 
throws JSONException {
         title = new PageTitle(json.getString("title"), site);
         extract = 
makeStringFromSentences(getSentences(removeParens(json.optString("extract")), 
site), EXTRACT_MAX_SENTENCES);
         if (json.has("thumbnail")) {
@@ -40,7 +38,20 @@
         if (json.has("terms") && 
json.getJSONObject("terms").has("description")) {
             
title.setDescription(Utils.capitalizeFirstChar(json.getJSONObject("terms").getJSONArray("description").optString(0)));
         }
-        leadImageName = json.has("pageimage") ? "File:" + 
json.optString("pageimage") : null;
+    }
+
+    public LinkPreviewContents(@NonNull RbPageLead pageLead, @NonNull Site 
site) {
+        title = new PageTitle(pageLead.getDisplayTitle(), site);
+        extract = pageLead.getExtract();
+        title.setThumbUrl(pageLead.getLeadImageUrl());
+        title.setDescription(pageLead.getDescription());
+    }
+
+    private LinkPreviewContents(@NonNull PageTitle title, @Nullable String 
extract, @Nullable String thumbUrl, @Nullable String description) {
+        this.title = title;
+        this.extract = extract;
+        this.title.setThumbUrl(thumbUrl);
+        this.title.setDescription(description);
     }
 
     /**
diff --git 
a/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewDialog.java 
b/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewDialog.java
index b594d2f..1f09103 100755
--- a/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewDialog.java
+++ b/app/src/main/java/org/wikipedia/page/linkpreview/LinkPreviewDialog.java
@@ -13,8 +13,13 @@
 import org.wikipedia.page.gallery.GalleryCollection;
 import org.wikipedia.page.gallery.GalleryCollectionFetchTask;
 import org.wikipedia.page.gallery.GalleryThumbnailScrollView;
+import org.wikipedia.server.PageLead;
+import org.wikipedia.server.PageServiceFactory;
+import org.wikipedia.server.restbase.RbPageLead;
+import org.wikipedia.settings.Prefs;
 import org.wikipedia.util.ApiUtil;
 import org.wikipedia.util.FeedbackUtil;
+import org.wikipedia.util.PageLoadUtil;
 import org.wikipedia.views.ViewUtil;
 
 import android.content.DialogInterface;
@@ -31,6 +36,9 @@
 import android.widget.TextView;
 
 import java.util.Map;
+
+import retrofit.RetrofitError;
+import retrofit.client.Response;
 
 public class LinkPreviewDialog extends SwipeableBottomDialog implements 
DialogInterface.OnDismissListener {
     private static final String TAG = "LinkPreviewDialog";
@@ -85,6 +93,7 @@
     @Override
     protected View inflateDialogView(LayoutInflater inflater, ViewGroup 
container) {
         WikipediaApp app = WikipediaApp.getInstance();
+        boolean shouldLoadImages = app.isImageDownloadEnabled();
         pageTitle = getArguments().getParcelable("title");
         entrySource = getArguments().getInt("entrySource");
 
@@ -107,7 +116,7 @@
         extractText = (TextView) 
rootView.findViewById(R.id.link_preview_extract);
 
         thumbnailGallery = (GalleryThumbnailScrollView) 
rootView.findViewById(R.id.link_preview_thumbnail_gallery);
-        if (app.isImageDownloadEnabled()) {
+        if (shouldLoadImages) {
             new GalleryThumbnailFetchTask(pageTitle).execute();
             thumbnailGallery.setGalleryViewListener(galleryViewListener);
         }
@@ -129,7 +138,12 @@
         progressBar.setVisibility(View.VISIBLE);
 
         // and kick off the task to load all the things...
-        new LinkPreviewFetchTask(app.getAPIForSite(pageTitle.getSite()), 
pageTitle).execute();
+        // Use RESTBase if the user is in the sample group
+        if (pageTitle.getSite().getLanguageCode().equalsIgnoreCase("en") && 
Prefs.forceRestbaseUsage()) {
+            loadContentWithRestBase(shouldLoadImages);
+        } else {
+            loadContentWithMwapi();
+        }
 
         funnel = new LinkPreviewFunnel(app);
         funnel.logLinkClick();
@@ -139,10 +153,6 @@
 
     public interface OnNavigateListener {
         void onNavigate(PageTitle title);
-    }
-
-    public void setOnNavigateListener(OnNavigateListener listener) {
-        onNavigateListener = listener;
     }
 
     public void goToLinkedPage() {
@@ -169,6 +179,42 @@
             funnel.logCancel();
         }
     }
+
+    private void loadContentWithMwapi() {
+        Log.v(TAG, "Loading link preview with MWAPI");
+        new 
LinkPreviewMwapiFetchTask(WikipediaApp.getInstance().getAPIForSite(pageTitle.getSite()),
 pageTitle).execute();
+    }
+
+    private void loadContentWithRestBase(boolean shouldLoadImages) {
+        Log.v(TAG, "Loading link preview with RESTBase");
+        PageServiceFactory.create(pageTitle.getSite()).pageLead(
+                pageTitle.getPrefixedText(),
+                PageLoadUtil.calculateLeadImageWidth(),
+                !shouldLoadImages,
+                linkPreviewOnLoadCallback);
+    }
+
+    private PageLead.Callback linkPreviewOnLoadCallback = new 
PageLead.Callback() {
+        @Override
+        public void success(PageLead pageLead, Response response) {
+            Log.v(TAG, response.getUrl());
+            progressBar.setVisibility(View.GONE);
+            if (pageLead.getLeadSectionContent() != null) {
+                contents = new LinkPreviewContents((RbPageLead) pageLead, 
pageTitle.getSite());
+                layoutPreview();
+            } else {
+                FeedbackUtil.showMessage(getActivity(), 
R.string.error_network_error);
+                dismiss();
+            }
+        }
+
+        @Override
+        public void failure(RetrofitError error) {
+            Log.e(TAG, "Link preview fetch error: " + error);
+            // Fall back to MWAPI
+            loadContentWithMwapi();
+        }
+    };
 
     private PageActivity getPageActivity() {
         return (PageActivity) getActivity();
@@ -204,8 +250,8 @@
         }
     }
 
-    private class LinkPreviewFetchTask extends PreviewFetchTask {
-        public LinkPreviewFetchTask(Api api, PageTitle title) {
+    private class LinkPreviewMwapiFetchTask extends PreviewFetchTask {
+        public LinkPreviewMwapiFetchTask(Api api, PageTitle title) {
             super(api, title);
         }
 
diff --git a/app/src/main/java/org/wikipedia/server/mwapi/MwPageLead.java 
b/app/src/main/java/org/wikipedia/server/mwapi/MwPageLead.java
index fb64a59..709f0e3 100644
--- a/app/src/main/java/org/wikipedia/server/mwapi/MwPageLead.java
+++ b/app/src/main/java/org/wikipedia/server/mwapi/MwPageLead.java
@@ -141,7 +141,7 @@
 
         @Nullable
         public String getDescription() {
-            return Utils.capitalizeFirstChar(description);
+            return description != null ? 
Utils.capitalizeFirstChar(description) : null;
         }
 
         @Nullable
diff --git a/app/src/main/java/org/wikipedia/server/restbase/RbPageLead.java 
b/app/src/main/java/org/wikipedia/server/restbase/RbPageLead.java
index dbfdb8e..2122c62 100644
--- a/app/src/main/java/org/wikipedia/server/restbase/RbPageLead.java
+++ b/app/src/main/java/org/wikipedia/server/restbase/RbPageLead.java
@@ -33,6 +33,7 @@
     @Expose @Nullable private String displaytitle;
     @Expose @Nullable private String redirected;
     @Expose @Nullable private String normalizedtitle;
+    @Expose @Nullable private String extract;
     @Expose private int languagecount;
     @Expose private boolean editable;
     @Expose private boolean mainpage;
@@ -88,8 +89,6 @@
         }
     }
 
-
-
     /** Converter */
     public PageProperties toPageProperties() {
         return new PageProperties(this);
@@ -106,6 +105,11 @@
     @Nullable
     public String getLastModified() {
         return lastmodified;
+    }
+
+    @Nullable
+    public String getExtract() {
+        return extract;
     }
 
     public int getLanguageCount() {
@@ -129,7 +133,7 @@
 
     @Nullable
     public String getDescription() {
-        return Utils.capitalizeFirstChar(description);
+        return description != null ? Utils.capitalizeFirstChar(description) : 
null;
     }
 
     @Nullable
diff --git a/app/src/main/java/org/wikipedia/server/restbase/RbPageService.java 
b/app/src/main/java/org/wikipedia/server/restbase/RbPageService.java
index 4962d86..1e2da51 100644
--- a/app/src/main/java/org/wikipedia/server/restbase/RbPageService.java
+++ b/app/src/main/java/org/wikipedia/server/restbase/RbPageService.java
@@ -77,7 +77,7 @@
 
     /**
      * Optional boolean Retrofit parameter.
-     * We don't want to send the query parameter at all when it's false since 
the presence of the
+     * We don't want to send the query parameter at all when it's false since 
the presence of the parameter
      * alone is enough to trigger the truthy behavior.
      */
     private Boolean optional(boolean param) {
diff --git a/app/src/main/java/org/wikipedia/util/PageLoadUtil.java 
b/app/src/main/java/org/wikipedia/util/PageLoadUtil.java
new file mode 100644
index 0000000..82aa1ed
--- /dev/null
+++ b/app/src/main/java/org/wikipedia/util/PageLoadUtil.java
@@ -0,0 +1,26 @@
+package org.wikipedia.util;
+
+import android.content.res.Resources;
+import android.support.annotation.NonNull;
+
+import org.wikipedia.R;
+import org.wikipedia.Site;
+import org.wikipedia.WikipediaApp;
+import org.wikipedia.server.PageService;
+import org.wikipedia.server.PageServiceFactory;
+
+public final class PageLoadUtil {
+
+    @NonNull
+    public static PageService getApiService(Site site) {
+        return PageServiceFactory.create(site);
+    }
+
+    // TODO: use getResources().getDimensionPixelSize()?  Define 
leadImageWidth with px, not dp?
+    public static int calculateLeadImageWidth() {
+        Resources res = WikipediaApp.getInstance().getResources();
+        return (int) (res.getDimension(R.dimen.leadImageWidth) / 
res.getDisplayMetrics().density);
+    }
+
+    private PageLoadUtil() { }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/238823
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7bb5335639f720fe2fb62422ff2a3cafa93fadfc
Gerrit-PatchSet: 3
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Dbrant <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Niedzielski <[email protected]>
Gerrit-Reviewer: Sniedzielski <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to