Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor title functions #3232

Merged
merged 16 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/java/org/wikipedia/LongPressHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LongPressHandler(
wikiSite = this
}
}
title = wikiSite.titleForInternalLink(uri.path)
title = PageTitle.titleForInternalLink(uri.path, wikiSite)
referrer = callback.referrer
showPopupMenu(view, true)
}
Expand Down
16 changes: 0 additions & 16 deletions app/src/main/java/org/wikipedia/dataclient/WikiSite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.wikipedia.WikipediaApp
import org.wikipedia.json.UriSerializer
import org.wikipedia.language.AppLanguageLookUpTable
import org.wikipedia.language.LanguageUtil
import org.wikipedia.page.PageTitle
import org.wikipedia.util.UriUtil

/**
Expand Down Expand Up @@ -105,21 +104,6 @@ data class WikiSite(
return url() + path(segment)
}

// TODO: this method doesn't have much to do with WikiSite. Move to PageTitle?
fun titleForInternalLink(internalLink: String?): PageTitle {
// Strip the /wiki/ from the href
return PageTitle(UriUtil.removeInternalLinkPrefix(internalLink.orEmpty()), this)
}

// TODO: this method doesn't have much to do with WikiSite. Move to PageTitle?
fun titleForUri(uri: Uri): PageTitle {
var path = uri.path
if (!uri.fragment.isNullOrEmpty()) {
path += "#" + uri.fragment
}
return titleForInternalLink(path)
}

fun dbName(): String {
return subdomain().replace("-".toRegex(), "_") + "wiki"
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/wikipedia/gallery/GalleryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,14 @@ class GalleryActivity : BaseActivity(), LinkPreviewDialog.Callback, GalleryItemF
L.v("Link clicked was $urlStr")
var url = UriUtil.resolveProtocolRelativeUrl(urlStr)
if (url.startsWith("/wiki/")) {
val title = app.wikiSite.titleForInternalLink(url)
val title = PageTitle.titleForInternalLink(url, app.wikiSite)
showLinkPreview(title)
} else {
val uri = Uri.parse(url)
val authority = uri.authority
if (authority != null && WikiSite.supportedAuthority(authority) &&
uri.path != null && uri.path!!.startsWith("/wiki/")) {
val title = WikiSite(uri).titleForUri(uri)
val title = PageTitle.titleForUri(uri, WikiSite(uri))
showLinkPreview(title)
} else {
// if it's a /w/ URI, turn it into a full URI and go external
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object NotificationPresenter {
it.getPrimary()?.let { primary ->
if (NotificationCategory.EDIT_USER_TALK.id == n.category) {
val talkWiki = WikiSite(primary.url)
val talkTitle = talkWiki.titleForUri(Uri.parse(primary.url))
val talkTitle = PageTitle.titleForUri(Uri.parse(primary.url), talkWiki)
activityIntent = addIntentExtras(TalkTopicsActivity.newIntent(context, talkTitle, Constants.InvokeSource.NOTIFICATION), n.id, n.type)
addActionForTalkPage(context, builder, primary, n)
} else {
Expand Down Expand Up @@ -115,7 +115,7 @@ object NotificationPresenter {

private fun addActionForTalkPage(context: Context, builder: NotificationCompat.Builder, link: Notification.Link, n: Notification) {
val wiki = WikiSite(link.url)
val title = wiki.titleForUri(Uri.parse(link.url))
val title = PageTitle.titleForUri(Uri.parse(link.url), wiki)
val pendingIntent = PendingIntent.getActivity(context, 0,
addIntentExtras(TalkTopicsActivity.newIntent(context, title, Constants.InvokeSource.NOTIFICATION), n.id, n.type), PendingIntent.FLAG_UPDATE_CURRENT or DeviceUtil.pendingIntentFlags)
builder.addAction(0, StringUtil.fromHtml(link.label).toString(), pendingIntent)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/wikipedia/page/LinkHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ abstract class LinkHandler(protected val context: Context) : JSEventListener, Ur
val supportedAuthority = uri.authority?.run { WikiSite.supportedAuthority(this) } == true
when {
uri.path?.run { matches(("^${UriUtil.WIKI_REGEX}.*").toRegex()) } == true && supportedAuthority -> {
val newTitle = if (titleStr.isNullOrEmpty()) site.titleForInternalLink(uri.path) else PageTitle.withSeparateFragment(titleStr, uri.fragment, site)
val newTitle = if (titleStr.isNullOrEmpty()) {
PageTitle.titleForInternalLink(uri.path, site)
} else PageTitle.withSeparateFragment(titleStr, uri.fragment, site)
if (newTitle.isFilePage) {
onMediaLinkClicked(newTitle)
} else {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/wikipedia/page/PageActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class PageActivity : BaseActivity(), PageFragment.Callback, LinkPreviewDialog.Ca
}
uri?.let {
val wiki = WikiSite(it)
val title = wiki.titleForUri(it)
val title = PageTitle.titleForUri(it, wiki)
val historyEntry = HistoryEntry(title, if (intent.hasExtra(Constants.INTENT_EXTRA_NOTIFICATION_ID))
HistoryEntry.SOURCE_NOTIFICATION_SYSTEM else HistoryEntry.SOURCE_EXTERNAL_LINK)
// Populate the referrer with the externally-referring URL, e.g. an external Browser URL, if present.
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/wikipedia/page/PageTitle.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wikipedia.page

import android.net.Uri
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.SerialName
Expand Down Expand Up @@ -208,5 +209,18 @@ data class PageTitle(
PageTitle("$prefixedText#$fragment", wiki, null)
}
}

fun titleForInternalLink(internalLink: String?, wiki: WikiSite): PageTitle {
// Strip the /wiki/ from the href
return PageTitle(UriUtil.removeInternalLinkPrefix(internalLink.orEmpty()), wiki)
}

fun titleForUri(uri: Uri, wiki: WikiSite): PageTitle {
var path = uri.path
if (!uri.fragment.isNullOrEmpty()) {
path += "#" + uri.fragment
}
return titleForInternalLink(path, wiki)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ class SuggestedEditsCardsItemFragment : SuggestedEditsItemFragment() {
val page = response.query?.pages!![0]
if (page.imageInfo() != null) {
val imageInfo = page.imageInfo()!!
val title = if (imageInfo.commonsUrl.isEmpty()) page.title else Constants.commonsWikiSite.titleForUri(Uri.parse(imageInfo.commonsUrl)).prefixedText
val title = if (imageInfo.commonsUrl.isEmpty()) {
page.title
} else {
PageTitle.titleForUri(Uri.parse(imageInfo.commonsUrl), Constants.commonsWikiSite).prefixedText
}

sourceSummaryForEdit = PageSummaryForEdit(
title,
Expand Down Expand Up @@ -170,7 +174,11 @@ class SuggestedEditsCardsItemFragment : SuggestedEditsItemFragment() {
val page = response.query?.pages!![0]
if (page.imageInfo() != null) {
val imageInfo = page.imageInfo()!!
val title = if (imageInfo.commonsUrl.isEmpty()) page.title else Constants.commonsWikiSite.titleForUri(Uri.parse(imageInfo.commonsUrl)).prefixedText
val title = if (imageInfo.commonsUrl.isEmpty()) {
page.title
} else {
PageTitle.titleForUri(Uri.parse(imageInfo.commonsUrl), Constants.commonsWikiSite).prefixedText
}

sourceSummaryForEdit = PageSummaryForEdit(
title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.wikipedia.notifications.NotificationCategory
import org.wikipedia.notifications.NotificationLinkHandler
import org.wikipedia.notifications.NotificationListItemContainer
import org.wikipedia.notifications.db.Notification
import org.wikipedia.page.PageTitle
import org.wikipedia.talk.TalkTopicsActivity
import org.wikipedia.util.ResourceUtil
import org.wikipedia.util.StringUtil
Expand Down Expand Up @@ -72,7 +73,7 @@ class NotificationActionsOverflowView(context: Context) : FrameLayout(context) {
binding.overflowViewSecondary.visibility = View.VISIBLE

val uri = Uri.parse(secondary.first().url)
val pageTitle = WikiSite(uri).titleForUri(uri)
val pageTitle = PageTitle.titleForUri(uri, WikiSite(uri))
if (pageTitle.isUserPage) {
binding.overflowViewSecondaryTalk.visibility = View.VISIBLE
binding.overflowViewSecondaryTalk.text = context.getString(R.string.notifications_menu_user_talk_page, secondary.first().label)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.wikipedia.Constants
import org.wikipedia.R
import org.wikipedia.WikipediaApp
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.mwapi.MwParseResponse
import org.wikipedia.dataclient.page.PageSummary
import org.wikipedia.feed.aggregated.AggregatedFeedContent
Expand Down Expand Up @@ -108,8 +109,7 @@ class WidgetProviderFeaturedPage : AppWidgetProvider() {
text.getSpanEnd(span) - text.getSpanStart(span) <= 1) {
continue
}
val title = WikipediaApp.getInstance().wikiSite
.titleForInternalLink(UriUtil.decodeURL(span.url))
val title = PageTitle.titleForInternalLink(UriUtil.decodeURL(span.url), WikiSite(span.url))
if (!title.isFilePage && !title.isSpecial) {
titleText = title.displayText
break
Expand Down
12 changes: 6 additions & 6 deletions app/src/test/java/org/wikipedia/dataclient/WikiSiteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -265,27 +265,27 @@ class WikiSiteTest {
val wiki = WikiSite.forLanguageCode("en")
MatcherAssert.assertThat(
PageTitle("Main Page", wiki).prefixedText,
Matchers.`is`(wiki.titleForInternalLink(null).prefixedText)
Matchers.`is`(PageTitle.titleForInternalLink(null, wiki).prefixedText)
)
MatcherAssert.assertThat(
PageTitle("Main Page", wiki).prefixedText,
Matchers.`is`(wiki.titleForInternalLink("").prefixedText)
Matchers.`is`(PageTitle.titleForInternalLink("", wiki).prefixedText)
)
MatcherAssert.assertThat(
PageTitle("Main Page", wiki).prefixedText,
Matchers.`is`(wiki.titleForInternalLink("/wiki/").prefixedText)
Matchers.`is`(PageTitle.titleForInternalLink("/wiki/", wiki).prefixedText)
)
MatcherAssert.assertThat(
PageTitle("wiki", wiki).prefixedText,
Matchers.`is`(wiki.titleForInternalLink("wiki").prefixedText)
Matchers.`is`(PageTitle.titleForInternalLink("wiki", wiki).prefixedText)
)
MatcherAssert.assertThat(
PageTitle("wiki", wiki).prefixedText,
Matchers.`is`(wiki.titleForInternalLink("/wiki/wiki").prefixedText)
Matchers.`is`(PageTitle.titleForInternalLink("/wiki/wiki", wiki).prefixedText)
)
MatcherAssert.assertThat(
PageTitle("wiki/wiki", wiki).prefixedText,
Matchers.`is`(wiki.titleForInternalLink("/wiki/wiki/wiki").prefixedText)
Matchers.`is`(PageTitle.titleForInternalLink("/wiki/wiki/wiki", wiki).prefixedText)
)
}

Expand Down
38 changes: 20 additions & 18 deletions app/src/test/java/org/wikipedia/page/PageTitleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,40 @@
@Test public void testFromInternalLink() {
WikiSite enwiki = WikiSite.forLanguageCode("en");

assertThat(enwiki.titleForInternalLink("/wiki/India").getPrefixedText(), is("India"));
assertThat(enwiki.titleForInternalLink("/wiki/India").getNamespace(), emptyString());
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/India", enwiki).getPrefixedText(), is("India"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/India", enwiki).getNamespace(), emptyString());

assertThat(enwiki.titleForInternalLink("/wiki/Talk:India").getNamespace(), is("Talk"));
assertThat(enwiki.titleForInternalLink("/wiki/Talk:India").getText(), is("India"));
assertThat(enwiki.titleForInternalLink("/wiki/Talk:India").getFragment(), nullValue());
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India", enwiki).getNamespace(), is("Talk"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India", enwiki).getText(), is("India"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India", enwiki).getFragment(), nullValue());

assertThat(enwiki.titleForInternalLink("/wiki/Talk:India#").getNamespace(), is("Talk"));
assertThat(enwiki.titleForInternalLink("/wiki/Talk:India#").getText(), is("India"));
assertThat(enwiki.titleForInternalLink("/wiki/Talk:India#").getFragment(), nullValue());
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India#", enwiki).getNamespace(), is("Talk"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India#", enwiki).getText(), is("India"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India#", enwiki).getFragment(), nullValue());

assertThat(enwiki.titleForInternalLink("/wiki/Talk:India#History").getNamespace(), is("Talk"));
assertThat(enwiki.titleForInternalLink("/wiki/Talk:India#History").getText(), is("India"));
assertThat(enwiki.titleForInternalLink("/wiki/Talk:India#History").getFragment(), is("History"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India#History", enwiki).getNamespace(), is("Talk"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India#History", enwiki).getText(), is("India"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Talk:India#History", enwiki).getFragment(), is("History"));
}

@Test public void testCanonicalURL() {
WikiSite enwiki = WikiSite.forLanguageCode("en");

assertThat(enwiki.titleForInternalLink("/wiki/India").getUri(), is("https://en.wikipedia.org/wiki/India"));
assertThat(enwiki.titleForInternalLink("/wiki/India Gate").getUri(), is("https://en.wikipedia.org/wiki/India_Gate"));
assertThat(enwiki.titleForInternalLink("/wiki/India's Gate").getUri(), is("https://en.wikipedia.org/wiki/India%27s_Gate"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/India", enwiki).getUri(), is("https://en.wikipedia.org/wiki/India"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/India Gate", enwiki).getUri(), is("https://en.wikipedia.org/wiki/India_Gate"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/India's Gate", enwiki).getUri(), is("https://en.wikipedia.org/wiki/India%27s_Gate"));
}

@Test public void testVariants() {
WikiSite zhwiki = WikiSite.forLanguageCode("zh-hant");

assertThat(new PageTitle("Taiwan", WikiSite.forLanguageCode("en")).getUri(), is("https://en.wikipedia.org/wiki/Taiwan"));
assertThat(new PageTitle("Taiwan", WikiSite.forLanguageCode("zh")).getUri(), is("https://zh.wikipedia.org/zh/Taiwan"));
assertThat(new PageTitle("Taiwan", WikiSite.forLanguageCode("zh-hant")).getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
assertThat(new PageTitle("Taiwan", WikiSite.forLanguageCode("zh-hans")).getUri(), is("https://zh.wikipedia.org/zh-hans/Taiwan"));
assertThat(WikiSite.forLanguageCode("zh-hant").titleForInternalLink("/zh/Taiwan").getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
assertThat(WikiSite.forLanguageCode("zh-hant").titleForInternalLink("/zh-hant/Taiwan").getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
assertThat(WikiSite.forLanguageCode("zh-hant").titleForInternalLink("/wiki/Taiwan").getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
assertThat(PageTitle.Companion.titleForInternalLink("/zh/Taiwan", zhwiki).getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
assertThat(PageTitle.Companion.titleForInternalLink("/zh-hant/Taiwan", zhwiki).getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
assertThat(PageTitle.Companion.titleForInternalLink("/wiki/Taiwan", zhwiki).getUri(), is("https://zh.wikipedia.org/zh-hant/Taiwan"));
}

@Test public void testWikiSite() {
Expand All @@ -69,7 +71,7 @@
@Test public void testLangAsNamespace() {
Uri uri = Uri.parse("https://en.wikipedia.org/wiki/fr:Article");
WikiSite site = new WikiSite(uri);
PageTitle title = site.titleForUri(uri);
PageTitle title = PageTitle.Companion.titleForUri(uri, site);
assertThat(title.getWikiSite().authority(), is("fr.wikipedia.org"));
assertThat(title.getDisplayText(), is("Article"));
}
Expand Down