Niedzielski has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/274047

Change subject: Use URL authority instead of host
......................................................................

Use URL authority instead of host

Bug: T120415
Depends-On: Iffe3cf1a0ebc75da22362706cc743b69ba8b9755
Change-Id: I23516ad1c2239f5fee6f2c0e7bd163dc220f8bb7
---
M app/build.gradle
M app/src/androidTest/java/org/wikipedia/test/SiteTests.java
M app/src/main/java/org/wikipedia/Site.java
M app/src/main/java/org/wikipedia/WikipediaApp.java
M app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java
M app/src/main/java/org/wikipedia/editing/EditTokenStorage.java
M app/src/main/java/org/wikipedia/history/HistoryEntryDatabaseTable.java
M app/src/main/java/org/wikipedia/page/LinkHandler.java
M app/src/main/java/org/wikipedia/page/PageActivity.java
M app/src/main/java/org/wikipedia/page/PageTitle.java
M app/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
M app/src/main/java/org/wikipedia/pageimages/PageImageDatabaseTable.java
M app/src/main/java/org/wikipedia/savedpages/SavedPageDatabaseTable.java
M app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
M 
app/src/main/java/org/wikipedia/useroption/dataclient/DefaultUserOptionDataClient.java
15 files changed, 59 insertions(+), 50 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/47/274047/1

diff --git a/app/build.gradle b/app/build.gradle
index 6e55052..092d832 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -157,7 +157,7 @@
 }
 
 dependencies {
-    compileSubprojectOrModule ':java-mwapi:json', 
'org.mediawiki.api:json:1.3.3', 'org.json', 'json'
+    compileSubprojectOrModule ':java-mwapi:json', 
'org.mediawiki.api:json:1.3.5-SNAPSHOT', 'org.json', 'json'
 
     // To keep the Maven Central dependencies up-to-date
     // use http://gradleplease.appspot.com/ or http://search.maven.org/.
diff --git a/app/src/androidTest/java/org/wikipedia/test/SiteTests.java 
b/app/src/androidTest/java/org/wikipedia/test/SiteTests.java
index 9bf47d9..eaf3780 100644
--- a/app/src/androidTest/java/org/wikipedia/test/SiteTests.java
+++ b/app/src/androidTest/java/org/wikipedia/test/SiteTests.java
@@ -15,15 +15,15 @@
 
     public void testNormalization() throws Exception {
         assertEquals(new Site("en.wikipedia.org"), new 
Site("en.m.wikipedia.org"));
-        assertEquals("bm.wikipedia.org", (new 
Site("bm.wikipedia.org")).host());
+        assertEquals("bm.wikipedia.org", new 
Site("bm.wikipedia.org").authority());
     }
 
     public void testIsSupportedSite() throws Exception {
-        assertTrue(Site.supportedHost("fr.wikipedia.org"));
-        assertTrue(Site.supportedHost("fr.m.wikipedia.org"));
-        assertTrue(Site.supportedHost("roa-rup.wikipedia.org"));
+        assertTrue(Site.supportedAuthority("fr.wikipedia.org"));
+        assertTrue(Site.supportedAuthority("fr.m.wikipedia.org"));
+        assertTrue(Site.supportedAuthority("roa-rup.wikipedia.org"));
 
-        assertFalse(Site.supportedHost("google.com"));
+        assertFalse(Site.supportedAuthority("google.com"));
     }
 
     public void testTitleForInternalLink() {
diff --git a/app/src/main/java/org/wikipedia/Site.java 
b/app/src/main/java/org/wikipedia/Site.java
index f757b4f..282cc4d 100644
--- a/app/src/main/java/org/wikipedia/Site.java
+++ b/app/src/main/java/org/wikipedia/Site.java
@@ -18,7 +18,7 @@
  * The base URL and Wikipedia language code for a wiki site. Examples:
  *
  * <ul>
- *     <lh>Name: scheme / host / language code</lh>
+ *     <lh>Name: scheme / authority / language code</lh>
  *     <li>English Wikipedia: HTTPS / en.wikipedia.org / en</li>
  *     <li>Chinese Wikipedia: HTTPS / zh.wikipedia.org / zh-hans or 
zh-hant</li>
  *     <li>Meta-Wiki: HTTPS / meta.wikimedia.org / (none)</li>
@@ -45,34 +45,34 @@
     @NonNull private final String languageCode; // possibly empty
 
     /**
-     * @return True if the host is supported by the app.
+     * @return True if the authority is supported by the app.
      */
-    public static boolean supportedHost(@NonNull String host) {
-        // endswith?
-        return host.matches("[a-z\\-]+\\.(m\\.)?" + 
Prefs.getMediaWikiBaseUri().getHost());
+    public static boolean supportedAuthority(@NonNull String authority) {
+        return authority.endsWith(Prefs.getMediaWikiBaseUri().getAuthority());
     }
 
     public static Site forLanguageCode(@NonNull String languageCode) {
         Uri uri = Prefs.getMediaWikiBaseUri();
         boolean secureSchema = uri.getScheme().equals("https");
-        return new Site(secureSchema, (languageCode.isEmpty() ? "" : 
(languageCodeToSubdomain(languageCode) + ".")) + uri.getHost(),
+        return new Site(secureSchema,
+                (languageCode.isEmpty() ? "" : 
(languageCodeToSubdomain(languageCode) + ".")) + uri.getAuthority(),
                 languageCode);
     }
 
     /** This method cannot resolve multi-dialect wikis like Simplified and 
Traditional Chinese. */
-    public Site(@NonNull String host) {
-        this(host, hostToLanguageCode(host));
+    public Site(@NonNull String authority) {
+        this(authority, authorityToLanguageCode(authority));
     }
 
-    public Site(@NonNull String host, @NonNull String languageCode) {
-        this(true, host, languageCode);
+    public Site(@NonNull String authority, @NonNull String languageCode) {
+        this(true, authority, languageCode);
     }
 
-    public Site(boolean secureScheme, @NonNull String host, @NonNull String 
languageCode) {
+    public Site(boolean secureScheme, @NonNull String authority, @NonNull 
String languageCode) {
         this(new Uri.Builder()
                 .scheme(secureScheme ? "https" : "http")
-                // TODO: verify no one is passing in mobile hosts and remove 
hostToDesktop().
-                .encodedAuthority(hostToDesktop(host))
+                // TODO: verify no one is passing in mobile authorities and 
remove authorityToDesktop().
+                .encodedAuthority(authorityToDesktop(authority))
                 .build(), languageCode);
     }
 
@@ -104,11 +104,16 @@
     }
 
     /**
-     * @return The complete wiki host host including language subdomain but 
not including scheme,
+     * @return The complete wiki authority including language subdomain but 
not including scheme,
      *         authentication, port, nor trailing slash.
      *
      * @see <a 
href='https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax'>URL 
syntax</a>
      */
+    @NonNull
+    public String authority() {
+        return uri.getAuthority();
+    }
+
     @NonNull
     public String host() {
         return uri.getHost();
@@ -135,8 +140,12 @@
         return host().replaceFirst("^" + subdomain + "\\.?", "$0m.");
     }
 
+    public int port() {
+        return uri.getPort();
+    }
+
     /**
-     * @return A hostless path for the segment including a leading "/".
+     * @return A path without an authority for the segment including a leading 
"/".
      */
     @NonNull
     public String path(@NonNull String segment) {
@@ -147,7 +156,7 @@
      * @return The canonical URL for segment.
      */
     public String url(@NonNull String segment) {
-        return scheme() + "://" + host() + path(segment);
+        return scheme() + "://" + authority() + path(segment);
     }
 
     /**
@@ -246,12 +255,12 @@
     }
 
     @NonNull
-    private static String hostToLanguageCode(@NonNull String host) {
-        return host.split("\\.")[0];
+    private static String authorityToLanguageCode(@NonNull String authority) {
+        return authority.split("\\.")[0];
     }
 
     @NonNull
-    private static String hostToDesktop(@NonNull String host) {
-        return host.replaceFirst("\\.m\\.", ".");
+    private static String authorityToDesktop(@NonNull String authority) {
+        return authority.replaceFirst("\\.m\\.", ".");
     }
 }
diff --git a/app/src/main/java/org/wikipedia/WikipediaApp.java 
b/app/src/main/java/org/wikipedia/WikipediaApp.java
index 4f350fe..1d9f9f6 100644
--- a/app/src/main/java/org/wikipedia/WikipediaApp.java
+++ b/app/src/main/java/org/wikipedia/WikipediaApp.java
@@ -222,16 +222,16 @@
     }
 
     public Api getAPIForSite(Site site, boolean mobile) {
-        String domain = mobile ? site.mobileHost() : site.host();
+        String host = mobile ? site.mobileHost() : site.host();
         String acceptLanguage = getAcceptLanguage(site);
         Map<String, String> customHeaders = buildCustomHeaders(acceptLanguage);
         Api api;
 
-        String cachedApiKey = domain + "-" + acceptLanguage;
+        String cachedApiKey = host + "-" + acceptLanguage;
         if (apis.containsKey(cachedApiKey)) {
             api = apis.get(cachedApiKey);
         } else {
-            api = new Api(domain, site.secureScheme(),
+            api = new Api(host, site.port(), site.secureScheme(),
                     site.path("api.php"), customHeaders);
             apis.put(cachedApiKey, api);
         }
diff --git a/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java 
b/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java
index ed90207..b9d978c 100644
--- a/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java
+++ b/app/src/main/java/org/wikipedia/dataclient/RestAdapterFactory.java
@@ -16,7 +16,7 @@
 
 public final class RestAdapterFactory {
     public static RestAdapter newInstance(@NonNull Site site) {
-        return newInstance(site, site.scheme() + "://" + site.host());
+        return newInstance(site, site.scheme() + "://" + site.authority());
     }
 
     public static RestAdapter newInstance(@NonNull final Site site, @NonNull 
String endpoint) {
diff --git a/app/src/main/java/org/wikipedia/editing/EditTokenStorage.java 
b/app/src/main/java/org/wikipedia/editing/EditTokenStorage.java
index 6792fd6..a926853 100644
--- a/app/src/main/java/org/wikipedia/editing/EditTokenStorage.java
+++ b/app/src/main/java/org/wikipedia/editing/EditTokenStorage.java
@@ -33,11 +33,11 @@
     }
 
     @Nullable public String token(@NonNull Site site) {
-        return tokenJar.get(site.host());
+        return tokenJar.get(site.authority());
     }
 
     public void token(@NonNull Site site, String token) {
-        updatePrefs(site.host(), token);
+        updatePrefs(site.authority(), token);
     }
 
     public void get(@NonNull final Site site, final TokenRetrievedCallback 
callback) {
diff --git 
a/app/src/main/java/org/wikipedia/history/HistoryEntryDatabaseTable.java 
b/app/src/main/java/org/wikipedia/history/HistoryEntryDatabaseTable.java
index d61675e..24e7ea2 100644
--- a/app/src/main/java/org/wikipedia/history/HistoryEntryDatabaseTable.java
+++ b/app/src/main/java/org/wikipedia/history/HistoryEntryDatabaseTable.java
@@ -41,7 +41,7 @@
     @Override
     protected ContentValues toContentValues(HistoryEntry obj) {
         ContentValues contentValues = new ContentValues();
-        contentValues.put(COL_SITE, obj.getTitle().getSite().host());
+        contentValues.put(COL_SITE, obj.getTitle().getSite().authority());
         contentValues.put(COL_LANG, obj.getTitle().getSite().languageCode());
         contentValues.put(COL_TITLE, obj.getTitle().getText());
         contentValues.put(COL_NAMESPACE, obj.getTitle().getNamespace());
@@ -88,7 +88,7 @@
     @Override
     protected String[] getUnfilteredPrimaryKeySelectionArgs(@NonNull 
HistoryEntry obj) {
         return new String[] {
-                obj.getTitle().getSite().host(),
+                obj.getTitle().getSite().authority(),
                 obj.getTitle().getSite().languageCode(),
                 obj.getTitle().getNamespace(),
                 obj.getTitle().getText()
diff --git a/app/src/main/java/org/wikipedia/page/LinkHandler.java 
b/app/src/main/java/org/wikipedia/page/LinkHandler.java
index 8882f94..553ed39 100644
--- a/app/src/main/java/org/wikipedia/page/LinkHandler.java
+++ b/app/src/main/java/org/wikipedia/page/LinkHandler.java
@@ -62,14 +62,14 @@
             Uri uri = Uri.parse(href);
             String authority = uri.getAuthority();
             // FIXME: Make this more complete, only to not handle URIs that 
contain unsupported actions
-            if (authority != null && Site.supportedHost(authority) && 
uri.getPath().startsWith("/wiki/")) {
+            if (authority != null && Site.supportedAuthority(authority) && 
uri.getPath().startsWith("/wiki/")) {
                 Site site = new Site(authority, getSite().languageCode());
                 PageTitle title = site.titleForUri(uri);
                 onInternalLinkClicked(title);
             } else {
                 // if it's a /w/ URI, turn it into a full URI and go external
                 if (href.startsWith("/w/")) {
-                    href = String.format("%1$s://%2$s", getSite().scheme(), 
getSite().host()) + href;
+                    href = String.format("%1$s://%2$s", getSite().scheme(), 
getSite().authority()) + href;
                 }
                 handleExternalLink(context, Uri.parse(href));
             }
diff --git a/app/src/main/java/org/wikipedia/page/PageActivity.java 
b/app/src/main/java/org/wikipedia/page/PageActivity.java
index dd2f9e6..5230a2f 100644
--- a/app/src/main/java/org/wikipedia/page/PageActivity.java
+++ b/app/src/main/java/org/wikipedia/page/PageActivity.java
@@ -592,7 +592,7 @@
         // Close the link preview, if one is open.
         hideLinkPreview();
 
-        app.putCrashReportProperty("api", title.getSite().host());
+        app.putCrashReportProperty("api", title.getSite().authority());
         app.putCrashReportProperty("title", title.toString());
 
         if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
diff --git a/app/src/main/java/org/wikipedia/page/PageTitle.java 
b/app/src/main/java/org/wikipedia/page/PageTitle.java
index 30ecfc8..c7c011e 100644
--- a/app/src/main/java/org/wikipedia/page/PageTitle.java
+++ b/app/src/main/java/org/wikipedia/page/PageTitle.java
@@ -176,7 +176,7 @@
             json.put("namespace", getNamespace());
             json.put("text", getText());
             json.put("fragment", getFragment());
-            json.put("site", site.host());
+            json.put("site", site.authority());
             return json;
         } catch (JSONException e) {
             throw new RuntimeException(e);
@@ -232,11 +232,11 @@
     }
 
     public String getCanonicalUri() {
-        return getUriForDomain(getSite().host());
+        return getUriForDomain(getSite().authority());
     }
 
     public String getMobileUri() {
-        return getUriForDomain(getSite().host());
+        return getUriForDomain(getSite().authority());
     }
 
     public String getUriForAction(String action) {
@@ -244,7 +244,7 @@
             return String.format(
                     "%1$s://%2$s/w/index.php?title=%3$s&action=%4$s",
                     getSite().scheme(),
-                    getSite().host(),
+                    getSite().authority(),
                     URLEncoder.encode(getPrefixedText(), "utf-8"),
                     action
             );
diff --git a/app/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java 
b/app/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
index 2ec5f66..e89a2ec 100644
--- a/app/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
+++ b/app/src/main/java/org/wikipedia/page/gallery/GalleryActivity.java
@@ -348,14 +348,14 @@
             } else {
                 Uri uri = Uri.parse(url);
                 String authority = uri.getAuthority();
-                if (authority != null && Site.supportedHost(authority)
+                if (authority != null && Site.supportedAuthority(authority)
                     && uri.getPath().startsWith("/wiki/")) {
                     PageTitle title = site.titleForUri(uri);
                     finishWithPageResult(title);
                 } else {
                     // if it's a /w/ URI, turn it into a full URI and go 
external
                     if (url.startsWith("/w/")) {
-                        url = String.format("%1$s://%2$s", site.scheme(), 
site.host()) + url;
+                        url = String.format("%1$s://%2$s", site.scheme(), 
site.authority()) + url;
                     }
                     handleExternalLink(GalleryActivity.this, Uri.parse(url));
                 }
diff --git 
a/app/src/main/java/org/wikipedia/pageimages/PageImageDatabaseTable.java 
b/app/src/main/java/org/wikipedia/pageimages/PageImageDatabaseTable.java
index 1ae22d7..659143e 100644
--- a/app/src/main/java/org/wikipedia/pageimages/PageImageDatabaseTable.java
+++ b/app/src/main/java/org/wikipedia/pageimages/PageImageDatabaseTable.java
@@ -39,7 +39,7 @@
     @Override
     protected ContentValues toContentValues(PageImage obj) {
         ContentValues contentValues = new ContentValues();
-        contentValues.put(COL_SITE, obj.getTitle().getSite().host());
+        contentValues.put(COL_SITE, obj.getTitle().getSite().authority());
         contentValues.put(COL_LANG, obj.getTitle().getSite().languageCode());
         contentValues.put(COL_NAMESPACE, obj.getTitle().getNamespace());
         contentValues.put(COL_TITLE, obj.getTitle().getPrefixedText());
@@ -110,7 +110,7 @@
     @Override
     protected String[] getUnfilteredPrimaryKeySelectionArgs(@NonNull PageImage 
obj) {
         return new String[] {
-                obj.getTitle().getSite().host(),
+                obj.getTitle().getSite().authority(),
                 obj.getTitle().getSite().languageCode(),
                 obj.getTitle().getNamespace(),
                 obj.getTitle().getText()
diff --git 
a/app/src/main/java/org/wikipedia/savedpages/SavedPageDatabaseTable.java 
b/app/src/main/java/org/wikipedia/savedpages/SavedPageDatabaseTable.java
index f3f697b..017c8ee 100644
--- a/app/src/main/java/org/wikipedia/savedpages/SavedPageDatabaseTable.java
+++ b/app/src/main/java/org/wikipedia/savedpages/SavedPageDatabaseTable.java
@@ -45,7 +45,7 @@
     @Override
     protected ContentValues toContentValues(SavedPage obj) {
         ContentValues contentValues = new ContentValues();
-        contentValues.put(COL_SITE, obj.getTitle().getSite().host());
+        contentValues.put(COL_SITE, obj.getTitle().getSite().authority());
         contentValues.put(COL_LANG, obj.getTitle().getSite().languageCode());
         contentValues.put(COL_TITLE, obj.getTitle().getText());
         contentValues.put(COL_NAMESPACE, obj.getTitle().getNamespace());
@@ -126,7 +126,7 @@
             json.put("namespace", page.getTitle().getNamespace());
             json.put("text", originalTitleText);
             json.put("fragment", page.getTitle().getFragment());
-            json.put("site", page.getTitle().getSite().host());
+            json.put("site", page.getTitle().getSite().authority());
             return StringUtil.md5string(json.toString());
         } catch (JSONException e) {
             throw new RuntimeException(e);
@@ -164,7 +164,7 @@
     @Override
     protected String[] getUnfilteredPrimaryKeySelectionArgs(@NonNull SavedPage 
obj) {
         return new String[] {
-                obj.getTitle().getSite().host(),
+                obj.getTitle().getSite().authority(),
                 obj.getTitle().getSite().languageCode(),
                 obj.getTitle().getNamespace(),
                 obj.getTitle().getText()
diff --git 
a/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java 
b/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
index 3c15364..de3ed01 100644
--- a/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
+++ b/app/src/main/java/org/wikipedia/server/restbase/RbPageEndpointsCache.java
@@ -29,7 +29,7 @@
 
     private RbPageService.RbEndpoints createRbService(Site site) {
         return RestAdapterFactory.newInstance(site,
-                String.format(Locale.ROOT, Prefs.getRestbaseUriFormat(), 
site.scheme(), site.host()))
+                String.format(Locale.ROOT, Prefs.getRestbaseUriFormat(), 
site.scheme(), site.authority()))
                 .create(RbPageService.RbEndpoints.class);
     }
 }
diff --git 
a/app/src/main/java/org/wikipedia/useroption/dataclient/DefaultUserOptionDataClient.java
 
b/app/src/main/java/org/wikipedia/useroption/dataclient/DefaultUserOptionDataClient.java
index baee739..dc1f918 100644
--- 
a/app/src/main/java/org/wikipedia/useroption/dataclient/DefaultUserOptionDataClient.java
+++ 
b/app/src/main/java/org/wikipedia/useroption/dataclient/DefaultUserOptionDataClient.java
@@ -53,7 +53,7 @@
 
         String token = app().getEditTokenStorage().token(site);
         if (token == null) {
-            throw RetrofitError.unexpectedError(site.host(), new 
RuntimeException("No token"));
+            throw RetrofitError.unexpectedError(site.authority(), new 
RuntimeException("No token"));
         }
         return token;
     }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I23516ad1c2239f5fee6f2c0e7bd163dc220f8bb7
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to