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

Show wikidata editor for non-local descriptions #2676

Merged
merged 6 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions WMF Framework/MWKArticle+Description.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

@objc public enum ArticleDescriptionSource: Int {
case none
case central
case local
}

extension MWKArticle {
public var descriptionSource: ArticleDescriptionSource {
let value = descriptionSourceNumber?.intValue ?? 0
return ArticleDescriptionSource(rawValue: value) ?? .none
}
}
4 changes: 4 additions & 0 deletions Wikipedia.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
7A3AD05B20ADB1DF00C92E04 /* WMFCurrentlyLoggedInUserFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0ED173A1E497AE7008B70AD /* WMFCurrentlyLoggedInUserFetcher.swift */; };
7A3AD05C20ADB1F500C92E04 /* WMFKeychainCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = B066F0D11E4F00B100A199F8 /* WMFKeychainCredentials.swift */; };
7A45AB8020AB2A4C006A92F5 /* Dictionary+Equality.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A45AB7F20AB2A4C006A92F5 /* Dictionary+Equality.swift */; };
7A4941E0216FE96400038D0F /* MWKArticle+Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4941DF216FE96400038D0F /* MWKArticle+Description.swift */; };
7A49A20121231510005C574C /* CollectionViewFooter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A49A20021231510005C574C /* CollectionViewFooter.swift */; };
7A49A20221231510005C574C /* CollectionViewFooter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A49A20021231510005C574C /* CollectionViewFooter.swift */; };
7A49A20321231510005C574C /* CollectionViewFooter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A49A20021231510005C574C /* CollectionViewFooter.swift */; };
Expand Down Expand Up @@ -3272,6 +3273,7 @@
7A35CB861FD82B6300AAF3B7 /* ReadingListDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadingListDetailViewController.swift; sourceTree = "<group>"; };
7A3AD05620ADAFEF00C92E04 /* WMFCaptcha.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WMFCaptcha.swift; path = "../../WMF Framework/WMFCaptcha.swift"; sourceTree = "<group>"; };
7A45AB7F20AB2A4C006A92F5 /* Dictionary+Equality.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+Equality.swift"; sourceTree = "<group>"; };
7A4941DF216FE96400038D0F /* MWKArticle+Description.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MWKArticle+Description.swift"; sourceTree = "<group>"; };
7A49A20021231510005C574C /* CollectionViewFooter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewFooter.swift; sourceTree = "<group>"; };
7A4ABA8520AA8966007AA405 /* UserHistoryFunnel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserHistoryFunnel.swift; sourceTree = "<group>"; };
7A4B333B2136EDED00C6C820 /* UnderlineButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnderlineButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5363,6 +5365,7 @@
children = (
B0E807B91C0CF04A0065EBC0 /* MWKArticle.h */,
B0E807BA1C0CF04A0065EBC0 /* MWKArticle.m */,
7A4941DF216FE96400038D0F /* MWKArticle+Description.swift */,
B0E807CD1C0CF04A0065EBC0 /* MWKSectionList.h */,
B0E807CE1C0CF04A0065EBC0 /* MWKSectionList.m */,
B0E807CF1C0CF04A0065EBC0 /* MWKSectionMetaData.h */,
Expand Down Expand Up @@ -10706,6 +10709,7 @@
D82C3A99213451100073EEAC /* DeviceInfo.swift in Sources */,
D80ACD291EA0DD0000DC3F20 /* FLAnimatedImage+SafeForSwift.m in Sources */,
D8FA18F21E1BDA35009675C3 /* UIImageView+WMFImageFetchingInternal.m in Sources */,
7A4941E0216FE96400038D0F /* MWKArticle+Description.swift in Sources */,
0E728D251DAEE2B50074EB4B /* WMFFeedContentFetcher.m in Sources */,
0E728D401DAEEC380074EB4B /* WMFRelatedSearchFetcher.m in Sources */,
D84C35F11F323CCA00895FA1 /* CollectionViewCell.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Wikipedia/Code/DescriptionEditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class DescriptionEditViewController: WMFScrollViewController, Themeable, UITextV
return
}

dataStore.wikidataDescriptionEditingController.publish(newWikidataDescription: descriptionToSave, for: articleURL) {error in
dataStore.wikidataDescriptionEditingController.publish(newWikidataDescription: descriptionToSave, from: article.descriptionSource, for: articleURL) {error in
let presentingVC = self.presentingViewController
DispatchQueue.main.async {
guard let error = error else {
Expand Down
1 change: 1 addition & 0 deletions Wikipedia/Code/MWKArticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static const NSInteger kMWKArticleSectionNone = -1;
@property (readonly, strong, nonatomic, nullable) MWKUser *lastmodifiedby; // required
@property (readonly, assign, nonatomic) int articleId; // required; -> 'id'
@property (readonly, strong, nonatomic, nullable) NSNumber *revisionId;
@property (readonly, strong, nonatomic, nullable) NSNumber *descriptionSourceNumber;

@property (copy, nonatomic, nullable) NSString *acceptLanguageRequestHeader;

Expand Down
16 changes: 15 additions & 1 deletion Wikipedia/Code/MWKArticle.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @interface MWKArticle ()
@property (readwrite, assign, nonatomic) BOOL editable; // required
@property (readwrite, assign, nonatomic, getter=isMain) BOOL main;
@property (readwrite, strong, nonatomic) NSNumber *revisionId;
@property (readwrite, strong, nonatomic) NSNumber *descriptionSourceNumber; // optional

@property (readwrite, nonatomic) NSInteger ns; //optional, defaults to 0

Expand Down Expand Up @@ -97,7 +98,7 @@ - (BOOL)isEqual:(id)object {
}

- (BOOL)isEqualToArticle:(MWKArticle *)other {
return WMF_EQUAL(self.url, isEqual:, other.url) && WMF_EQUAL(self.lastmodified, isEqualToDate:, other.lastmodified) && WMF_IS_EQUAL(self.lastmodifiedby, other.lastmodifiedby) && WMF_EQUAL(self.displaytitle, isEqualToString:, other.displaytitle) && WMF_EQUAL(self.protection, isEqual:, other.protection) && WMF_EQUAL(self.thumbnailURL, isEqualToString:, other.thumbnailURL) && WMF_EQUAL(self.imageURL, isEqualToString:, other.imageURL) && WMF_EQUAL(self.revisionId, isEqualToNumber:, other.revisionId) && self.articleId == other.articleId && self.languagecount == other.languagecount && self.isMain == other.isMain && self.sections.count == other.sections.count;
return WMF_EQUAL(self.url, isEqual:, other.url) && WMF_EQUAL(self.lastmodified, isEqualToDate:, other.lastmodified) && WMF_IS_EQUAL(self.lastmodifiedby, other.lastmodifiedby) && WMF_EQUAL(self.displaytitle, isEqualToString:, other.displaytitle) && WMF_EQUAL(self.protection, isEqual:, other.protection) && WMF_EQUAL(self.thumbnailURL, isEqualToString:, other.thumbnailURL) && WMF_EQUAL(self.imageURL, isEqualToString:, other.imageURL) && WMF_EQUAL(self.revisionId, isEqualToNumber:, other.revisionId) && self.articleId == other.articleId && self.languagecount == other.languagecount && self.isMain == other.isMain && self.sections.count == other.sections.count && self.descriptionSourceNumber == self.descriptionSourceNumber;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== in obj-c only compares the pointers, so this is true only if they're the exact same object, not the same number. Also, it's self. here on both sides instead of self. and other. It looks like WMF_EQUAL(self.descriptionSourceNumber, isEqualToNumber:, other.descriptionSourceNumber) handles nil cases and equality of the underlying numbers properly

}

- (BOOL)isDeeplyEqualToArticle:(MWKArticle *)article {
Expand Down Expand Up @@ -162,6 +163,7 @@ - (void)importMobileViewJSON:(NSDictionary *)dict {
self.lastmodifiedby = [self requiredUser:@"lastmodifiedby" dict:dict];
self.articleId = [[self requiredNumber:@"id" dict:dict] intValue];
self.languagecount = [[self requiredNumber:@"languagecount" dict:dict] intValue];
self.descriptionSourceNumber = [self descriptionSourceNumberFromStringValue:[self optionalString:@"descriptionsource" dict:dict]];

self.ns = [[self optionalNumber:@"ns" dict:dict] integerValue];

Expand Down Expand Up @@ -236,6 +238,18 @@ - (void)importMobileViewJSON:(NSDictionary *)dict {
[self importMediaJSON:self.media];
}

- (NSNumber *)descriptionSourceNumberFromStringValue:(NSString *)stringValue {
ArticleDescriptionSource source;
if ([stringValue isEqualToString:@"central"]) {
source = ArticleDescriptionSourceCentral;
} else if ([stringValue isEqualToString:@"local"]) {
source = ArticleDescriptionSourceLocal;
} else {
source = ArticleDescriptionSourceNone;
}
return [NSNumber numberWithInteger:source];
}

+ (NSInteger)articleImageWidth {
static dispatch_once_t onceToken;
static NSInteger articleImageWidth;
Expand Down
18 changes: 9 additions & 9 deletions Wikipedia/Code/WikidataDescriptionEditingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension WikidataAPIResult {
enum WikidataPublishingError: LocalizedError {
case invalidArticleURL
case apiResultNotParsedCorrectly
case blacklistedLanguage
case notEditable
case unknown
}

Expand Down Expand Up @@ -72,15 +72,14 @@ enum WikidataPublishingError: LocalizedError {
return blacklistedLanguages.contains(languageCode)
}

@objc(publishNewWikidataDescription:forArticleURL:completion:)
public func publish(newWikidataDescription: String, for articleURL: URL, completion: @escaping (Error?) -> Void) {
public func publish(newWikidataDescription: String, from source: ArticleDescriptionSource, for articleURL: URL, completion: @escaping (Error?) -> Void) {
guard let title = articleURL.wmf_title,
let language = articleURL.wmf_language,
let wiki = articleURL.wmf_wiki else {
completion(WikidataPublishingError.invalidArticleURL)
return
}
publish(newWikidataDescription: newWikidataDescription, forPageWithTitle: title, language: language, wiki: wiki, completion: completion)
publish(newWikidataDescription: newWikidataDescription, from: source, forPageWithTitle: title, language: language, wiki: wiki, completion: completion)
}

/// Publish new wikidata description.
Expand All @@ -91,10 +90,9 @@ enum WikidataPublishingError: LocalizedError {
/// - language: language code of the page's wiki, e.g., "en".
/// - wiki: wiki of the page to be updated, e.g., "enwiki"
/// - completion: completion block called when operation is completed.
private func publish(newWikidataDescription: String, forPageWithTitle title: String, language: String, wiki: String, completion: @escaping (Error?) -> Void) {
guard !isBlacklisted(language) else {
//DDLog("Attempting to publish a wikidata description in a blacklisted language; aborting")
completion(WikidataPublishingError.blacklistedLanguage)
private func publish(newWikidataDescription: String, from source: ArticleDescriptionSource, forPageWithTitle title: String, language: String, wiki: String, completion: @escaping (Error?) -> Void) {
guard !isBlacklisted(language) || source != .local else {
completion(WikidataPublishingError.notEditable)
return
}
let requestWithCSRFCompletion: (WikidataAPIResult?, URLResponse?, Error?) -> Void = { result, response, error in
Expand All @@ -117,6 +115,8 @@ public extension MWKArticle {
guard let dataStore = dataStore, let language = self.url.wmf_language else {
return false
}
return !dataStore.wikidataDescriptionEditingController.isBlacklisted(language)
let isLanguageBlacklisted = dataStore.wikidataDescriptionEditingController.isBlacklisted(language)
let isDescriptionFromLocalWiki = descriptionSource == .local
return !isLanguageBlacklisted || !isDescriptionFromLocalWiki
}
}