From 14f08ae1c9685d4f1de6ffb30070a6adf370397a Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Mon, 16 Nov 2020 22:36:28 -0600 Subject: [PATCH 1/2] Aaald - attempt to navigate to particular talk page topic using fragment --- .../ArticleAsLivingDocViewModels.swift | 31 +++++++++++++------ .../SignificantEventsModels.swift | 5 ++- WMF Framework/String+LinkParsing.swift | 6 ++++ ...ivingDocLargeEventCollectionViewCell.swift | 12 ++++++- .../ArticleAsLivingDocViewController.swift | 15 ++++++--- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift b/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift index e959d537aae..595e3f95822 100644 --- a/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift +++ b/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift @@ -571,7 +571,7 @@ public extension ArticleAsLivingDocViewModel { public enum ButtonsToDisplay { case thankAndViewChanges(userId: UInt, revisionId: UInt) - case viewDiscussion(sectionName: String) + case viewDiscussion(sectionName: String?) } public let typedEvent: SignificantEvents.TypedEvent @@ -599,7 +599,13 @@ public extension ArticleAsLivingDocViewModel { case .newTalkPageTopic(let newTalkPageTopic): self.userId = newTalkPageTopic.userId userGroups = newTalkPageTopic.userGroups - self.buttonsToDisplay = .viewDiscussion(sectionName: newTalkPageTopic.section) + + if let talkPageSection = newTalkPageTopic.section { + self.buttonsToDisplay = .viewDiscussion(sectionName: Self.sectionTitleWithEqualSignsStripped(originalTitle: talkPageSection)) + } else { + self.buttonsToDisplay = .viewDiscussion(sectionName: nil) + } + case .large(let largeChange): self.userId = largeChange.userId userGroups = largeChange.userGroups @@ -637,7 +643,6 @@ public extension ArticleAsLivingDocViewModel { revId = 0 parentId = 0 } - } public static func == (lhs: ArticleAsLivingDocViewModel.Event.Large, rhs: ArticleAsLivingDocViewModel.Event.Large) -> Bool { @@ -905,17 +910,23 @@ public extension ArticleAsLivingDocViewModel.Event.Large { } //strip == signs from all section titles - let finalSet = set.map { (section) -> String in - if let match = section.range(of: "(?<==)[^=]+", options: .regularExpression) { - return String(section[match]) - } - - return section - } + let finalSet = set.map { Self.sectionTitleWithEqualSignsStripped(originalTitle: $0) } return Set(finalSet) } + private static func sectionTitleWithEqualSignsStripped(originalTitle: String) -> String { + var loopTitle = originalTitle + let regex = "\\s*[=]{2,}\\s*" + var maybeMatch = loopTitle.range(of: regex, options: .regularExpression) + while let match = maybeMatch { + loopTitle.removeSubrange(match) + maybeMatch = loopTitle.range(of: regex, options: .regularExpression) + } + + return loopTitle + } + private func localizedStringFromSections(sections: [String]) -> String? { var localizedString: String switch sections.count { diff --git a/WMF Framework/Significant Events Endpoint/SignificantEventsModels.swift b/WMF Framework/Significant Events Endpoint/SignificantEventsModels.swift index 8c24730cb8c..8ab38877372 100644 --- a/WMF Framework/Significant Events Endpoint/SignificantEventsModels.swift +++ b/WMF Framework/Significant Events Endpoint/SignificantEventsModels.swift @@ -231,7 +231,7 @@ public extension SignificantEvents { public let timestampString: String public let user: String public let userId: UInt - public let section: String + public let section: String? public let snippet: String public let userGroups: [String]? public let userEditCount: UInt? @@ -242,7 +242,6 @@ public extension SignificantEvents { let timestampString = untypedEvent.timestampString, let user = untypedEvent.user, let userId = untypedEvent.userId, - let section = untypedEvent.section, let snippet = untypedEvent.snippet else { return nil } @@ -253,7 +252,7 @@ public extension SignificantEvents { self.timestampString = timestampString self.user = user self.userId = userId - self.section = section + self.section = untypedEvent.section self.snippet = snippet self.userGroups = untypedEvent.userGroups self.userEditCount = untypedEvent.userEditCount diff --git a/WMF Framework/String+LinkParsing.swift b/WMF Framework/String+LinkParsing.swift index 4b69a958ade..ca187047597 100644 --- a/WMF Framework/String+LinkParsing.swift +++ b/WMF Framework/String+LinkParsing.swift @@ -50,6 +50,12 @@ public extension String { return replacingOccurrences(of: " ", with: "_").precomposedStringWithCanonicalMapping } + var asTalkPageFragment: String? { + let denormalizedName = replacingOccurrences(of: " ", with: "_") + let unlinkedName = denormalizedName.replacingOccurrences(of: "[[", with: "").replacingOccurrences(of: "]]", with: "") + return unlinkedName.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.wmf_encodeURIComponentAllowed()) + } + //assumes string is already normalized var googleFormPercentEncodedPageTitle: String? { return googleFormPageTitle?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) diff --git a/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocLargeEventCollectionViewCell.swift b/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocLargeEventCollectionViewCell.swift index 75adad25927..97c0d0277c6 100644 --- a/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocLargeEventCollectionViewCell.swift +++ b/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocLargeEventCollectionViewCell.swift @@ -345,7 +345,17 @@ class ArticleAsLivingDocLargeEventCollectionViewCell: CollectionViewCell { ArticleAsLivingDocFunnel.shared.logModalViewDiscussionButtonTapped(position: loggingPosition) } - articleDelegate?.showTalkPage() + guard let largeEvent = largeEvent else { + return + } + + switch largeEvent.buttonsToDisplay { + case .viewDiscussion(let sectionName): + articleDelegate?.showTalkPageWithSectionName(sectionName) + default: + assertionFailure("Unexpected button type") + articleDelegate?.showTalkPageWithSectionName(nil) + } } } diff --git a/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocViewController.swift b/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocViewController.swift index a373a2f9e8c..2aa3d46ff2d 100644 --- a/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocViewController.swift +++ b/Wikipedia/Code/Article as a Living Document/ArticleAsLivingDocViewController.swift @@ -10,7 +10,6 @@ protocol ArticleAsLivingDocViewControllerDelegate: class { func fetchNextPage(nextRvStartId: UInt, theme: Theme) func showEditHistory() func handleLink(with href: String) - func showTalkPage() func livingDocViewWillAppear() func livingDocViewWillPush() } @@ -18,7 +17,7 @@ protocol ArticleAsLivingDocViewControllerDelegate: class { protocol ArticleDetailsShowing: class { func goToHistory() func goToDiff(revisionId: UInt, parentId: UInt, diffType: DiffContainerViewModel.DiffType) - func showTalkPage() + func showTalkPageWithSectionName(_ sectionName: String?) func thankButtonTapped(for revisionID: Int, isUserAnonymous: Bool, livingDocLoggingValues: ArticleAsLivingDocLoggingValues) } @@ -442,11 +441,19 @@ extension ArticleAsLivingDocViewController: ArticleAsLivingDocHorizontallyScroll @available(iOS 13.0, *) extension ArticleAsLivingDocViewController: ArticleDetailsShowing { - func showTalkPage() { - guard let talkPageURL = delegate?.articleURL.articleTalkPage else { + func showTalkPageWithSectionName(_ sectionName: String?) { + + var maybeTalkPageURL = delegate?.articleURL.articleTalkPage + if let convertedSectionName = sectionName?.asTalkPageFragment, + let talkPageURL = maybeTalkPageURL { + maybeTalkPageURL = URL(string: talkPageURL.absoluteString + "#" + convertedSectionName) + } + + guard let talkPageURL = maybeTalkPageURL else { showGenericError() return } + navigate(to: talkPageURL) } From fc8a1715982dde9c4fa92b30babe5d87f02f50f3 Mon Sep 17 00:00:00 2001 From: Toni Sevener Date: Wed, 18 Nov 2020 11:30:19 -0600 Subject: [PATCH 2/2] improve section title regex --- .../ArticleAsLivingDocViewModels.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift b/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift index 595e3f95822..fee3187f56e 100644 --- a/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift +++ b/WMF Framework/Significant Events Endpoint/ArticleAsLivingDocViewModels.swift @@ -601,7 +601,7 @@ public extension ArticleAsLivingDocViewModel { userGroups = newTalkPageTopic.userGroups if let talkPageSection = newTalkPageTopic.section { - self.buttonsToDisplay = .viewDiscussion(sectionName: Self.sectionTitleWithEqualSignsStripped(originalTitle: talkPageSection)) + self.buttonsToDisplay = .viewDiscussion(sectionName: Self.sectionTitleWithWikitextStripped(originalTitle: talkPageSection)) } else { self.buttonsToDisplay = .viewDiscussion(sectionName: nil) } @@ -910,14 +910,16 @@ public extension ArticleAsLivingDocViewModel.Event.Large { } //strip == signs from all section titles - let finalSet = set.map { Self.sectionTitleWithEqualSignsStripped(originalTitle: $0) } + let finalSet = set.map { Self.sectionTitleWithWikitextStripped(originalTitle: $0) } return Set(finalSet) } - private static func sectionTitleWithEqualSignsStripped(originalTitle: String) -> String { + //remove one or more equal signs and zero or more spaces on either side of the title text + private static func sectionTitleWithWikitextStripped(originalTitle: String) -> String { var loopTitle = originalTitle - let regex = "\\s*[=]{2,}\\s*" + + let regex = "^=+\\s*|\\s*=+$" var maybeMatch = loopTitle.range(of: regex, options: .regularExpression) while let match = maybeMatch { loopTitle.removeSubrange(match)