Mhurd has submitted this change and it was merged. Change subject: associate images using URLs ......................................................................
associate images using URLs - Use source URLs to determine whether image info corresponds to an image - Lazily calculate and cache the regex output used to calculate filename from URL - some other minor things (fix for unit tests) edit: use @synthesize instead of explicit ivar Bug: T89914 Change-Id: I862704957b81ab39646368851028e48931ae67d2 --- M MediaWikiKit/MediaWikiKit/MWKImage.m M MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.h M MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.m M MediaWikiKit/MediaWikiKit/MWKImageInfo.h M MediaWikiKit/MediaWikiKit/MWKImageInfo.m M MediaWikiKit/MediaWikiKitTests/MWKImageListTests.m M Wikipedia.xcodeproj/project.pbxproj M wikipedia/Base.lproj/Main_iPhone.storyboard M wikipedia/View Controllers/Image Gallery/WMFImageGalleryViewController.m A wikipedia/mw-utils/WMFImageURLParsing.h A wikipedia/mw-utils/WMFImageURLParsing.m 11 files changed, 75 insertions(+), 56 deletions(-) Approvals: Mhurd: Verified; Looks good to me, approved diff --git a/MediaWikiKit/MediaWikiKit/MWKImage.m b/MediaWikiKit/MediaWikiKit/MWKImage.m index 34876f3..2f41f69 100644 --- a/MediaWikiKit/MediaWikiKit/MWKImage.m +++ b/MediaWikiKit/MediaWikiKit/MWKImage.m @@ -9,6 +9,13 @@ #import "UIKit/UIKit.h" #import "WikipediaAppUtils.h" #import "MediaWikiKit.h" +#import "WMFImageURLParsing.h" + +@interface MWKImage () +{ + NSString* _fileNameNoSizePrefix; +} +@end @implementation MWKImage @@ -17,6 +24,8 @@ self = [super initWithSite:article.site]; if (self) { _article = article; + + // fileNameNoSizePrefix is lazily derived from this property, so be careful if _sourceURL needs to be re-set _sourceURL = [url copy]; _dateLastAccessed = nil; @@ -72,16 +81,7 @@ +(NSString *)fileNameNoSizePrefix:(NSString *)sourceURL { - if (!sourceURL) { return nil; } - - NSString *fileName = [sourceURL lastPathComponent]; - NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"^\\d+px-(.*)$" options:0 error:nil]; - NSArray *matches = [re matchesInString:fileName options:0 range:NSMakeRange(0, [fileName length])]; - if ([matches count]) { - return [fileName substringWithRange:[matches[0] rangeAtIndex:1]]; - } else { - return fileName; - } + return WMFParseImageNameFromSourceURL(sourceURL); } + (NSString*)canonicalFilenameFromSourceURL:(NSString *)sourceURL @@ -101,9 +101,12 @@ } } --(NSString *)fileNameNoSizePrefix +-(NSString*)fileNameNoSizePrefix { - return [MWKImage fileNameNoSizePrefix:self.sourceURL]; + if (!_fileNameNoSizePrefix) { + _fileNameNoSizePrefix = [MWKImage fileNameNoSizePrefix:self.sourceURL]; + } + return _fileNameNoSizePrefix; } -(id)dataExport diff --git a/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.h b/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.h index a8d6d2c..19ed0e6 100644 --- a/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.h +++ b/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.h @@ -9,11 +9,7 @@ #import "MWKImageInfo.h" #import "MWKImage.h" -FOUNDATION_EXPORT NSString* const MWKImageAssociationKeyPath; - @interface MWKImageInfo (MWKImageComparison) - -- (id)imageAssociationValue; - (BOOL)isAssociatedWithImage:(MWKImage*)image; @@ -21,7 +17,7 @@ @interface MWKImage (MWKImageInfoComparison) -- (id)infoAssociationValue; +@property (nonatomic, readonly) id infoAssociationValue; - (BOOL)isAssociatedWithInfo:(MWKImageInfo*)info; diff --git a/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.m b/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.m index 7d08be2..6e6e540 100644 --- a/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.m +++ b/MediaWikiKit/MediaWikiKit/MWKImageInfo+MWKImageComparison.m @@ -10,18 +10,11 @@ #import "MWKImage.h" #import "WikipediaAppUtils.h" -NSString* const MWKImageAssociationKeyPath = @"canonicalFilename"; - @implementation MWKImageInfo (MWKImageComparison) - -- (id)imageAssociationValue -{ - return self.canonicalFilename; -} - (BOOL)isAssociatedWithImage:(MWKImage *)image { - return [self.canonicalFilename isEqualToString:image.canonicalFilename]; + return [self.imageAssociationValue isEqual:image.infoAssociationValue]; } @end @@ -30,7 +23,7 @@ - (id)infoAssociationValue { - return self.canonicalFilename; + return self.fileNameNoSizePrefix; } - (BOOL)isAssociatedWithInfo:(MWKImageInfo *)info diff --git a/MediaWikiKit/MediaWikiKit/MWKImageInfo.h b/MediaWikiKit/MediaWikiKit/MWKImageInfo.h index 6f88475..1942037 100644 --- a/MediaWikiKit/MediaWikiKit/MWKImageInfo.h +++ b/MediaWikiKit/MediaWikiKit/MWKImageInfo.h @@ -34,6 +34,9 @@ /// Name of the entity owning this image. @property (nonatomic, readonly, copy) NSString *owner; +/// Value which can be used to associate the receiver with a @c MWKImage. +@property (nonatomic, readonly) id imageAssociationValue; + /// Factory method for creating an instance from the output of @c exportData. + (instancetype)imageInfoWithExportedData:(NSDictionary*)exportedData; @@ -45,8 +48,5 @@ imageURL:(NSURL*)imageURL imageThumbURL:(NSURL*)imageThumbURL owner:(NSString*)owner; - -/// Name of the canonical file associated with the receiver, without the "File:" prefix. -- (NSString*)canonicalFilename; @end diff --git a/MediaWikiKit/MediaWikiKit/MWKImageInfo.m b/MediaWikiKit/MediaWikiKit/MWKImageInfo.m index 818623c..a7caa71 100644 --- a/MediaWikiKit/MediaWikiKit/MWKImageInfo.m +++ b/MediaWikiKit/MediaWikiKit/MWKImageInfo.m @@ -1,16 +1,9 @@ -// // MWKImageMetadata.m -// MediaWikiKit -// -// Created by Brion on 1/16/15. -// Copyright (c) 2015 Wikimedia Foundation. All rights reserved. -// - #import "MediaWikiKit.h" #import "NSMutableDictionary+WMFMaybeSet.h" #import "WikipediaAppUtils.h" +#import "WMFImageURLParsing.h" // !!!: don't change key constants w/o writing conversion code to pull values from the old keys - // Model Version 1.0.0 NSString* const mWKImageInfoModelVersionKey = @"modelVersion"; NSUInteger const MWKImageInfoModelVersion_1 = 1; @@ -25,6 +18,7 @@ NSString* const MWKImageInfoLicenseKey = @"license"; @implementation MWKImageInfo +@synthesize imageAssociationValue=_imageAssociationValue; - (instancetype)initWithCanonicalPageTitle:(NSString *)canonicalPageTitle canonicalFileURL:(NSURL*)canonicalFileURL @@ -97,13 +91,13 @@ - (BOOL)isEqualToGalleryItem:(MWKImageInfo*)other { return WMF_EQUAL(self.canonicalPageTitle, isEqualToString:, other.canonicalPageTitle) - && WMF_IS_EQUAL(self.canonicalFileURL, other.canonicalFileURL) - && WMF_EQUAL(self.imageDescription, isEqualToString:, other.imageDescription) - && WMF_EQUAL(self.license, isEqualToLicense:, other.license) - && WMF_IS_EQUAL(self.filePageURL, other.filePageURL) - && WMF_IS_EQUAL(self.imageURL, other.imageURL) - && WMF_IS_EQUAL(self.imageThumbURL, other.imageThumbURL) - && WMF_EQUAL(self.owner, isEqualToString:, other.owner); + && WMF_IS_EQUAL(self.canonicalFileURL, other.canonicalFileURL) + && WMF_EQUAL(self.imageDescription, isEqualToString:, other.imageDescription) + && WMF_EQUAL(self.license, isEqualToLicense:, other.license) + && WMF_IS_EQUAL(self.filePageURL, other.filePageURL) + && WMF_IS_EQUAL(self.imageURL, other.imageURL) + && WMF_IS_EQUAL(self.imageThumbURL, other.imageThumbURL) + && WMF_EQUAL(self.owner, isEqualToString:, other.owner); } - (NSUInteger)hash @@ -118,14 +112,12 @@ #pragma mark - Calculated properties -- (NSString*)canonicalFilename +- (id)imageAssociationValue { - if (self.canonicalPageTitle.length > 5) { - // string after "File:" prefix - return [self.canonicalPageTitle substringFromIndex:5]; - } else { - return nil; + if (!_imageAssociationValue) { + _imageAssociationValue = WMFParseImageNameFromSourceURL(self.imageURL); } + return _imageAssociationValue; } @end diff --git a/MediaWikiKit/MediaWikiKitTests/MWKImageListTests.m b/MediaWikiKit/MediaWikiKitTests/MWKImageListTests.m index 0427a6d..f4be8fd 100644 --- a/MediaWikiKit/MediaWikiKitTests/MWKImageListTests.m +++ b/MediaWikiKit/MediaWikiKitTests/MWKImageListTests.m @@ -46,7 +46,7 @@ // create article w/ mock section to prevent crashing due to image import side effects MWKArticle *article = [[MWKArticle alloc] initWithTitle:nil dataStore:tmpDataStore]; - [article.sections addSection:mock([MWKSection class])]; + [article.sections setSections:mock([MWKSection class])]; NSArray *dummySourceURLs = [@[@"10px-a.jpg", @"10px-b.jpg", @"100px-a.jpg", @"10px-c.jpg"] bk_map:^id(id obj) { return [MWKDataStoreValidImageSitePrefix stringByAppendingString:obj]; diff --git a/Wikipedia.xcodeproj/project.pbxproj b/Wikipedia.xcodeproj/project.pbxproj index 5a8776f..4229507 100644 --- a/Wikipedia.xcodeproj/project.pbxproj +++ b/Wikipedia.xcodeproj/project.pbxproj @@ -216,6 +216,8 @@ BC50C3841A83C96D006DC7AF /* WMFNetworkUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = BC50C37E1A83C784006DC7AF /* WMFNetworkUtilities.m */; }; BC50C3871A83CBDA006DC7AF /* MWKImageInfoResponseSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = BC50C3861A83CBDA006DC7AF /* MWKImageInfoResponseSerializer.m */; }; BC6FEAE11A9B7EFD00A1D890 /* WMFCodingStyle.m in Sources */ = {isa = PBXBuildFile; fileRef = BC6FEAE01A9B7EFD00A1D890 /* WMFCodingStyle.m */; }; + BC7DFCD61AA4E5FE000035C3 /* WMFImageURLParsing.m in Sources */ = {isa = PBXBuildFile; fileRef = BC7DFCD51AA4E5FE000035C3 /* WMFImageURLParsing.m */; }; + BC7DFCD71AA4E5FE000035C3 /* WMFImageURLParsing.m in Sources */ = {isa = PBXBuildFile; fileRef = BC7DFCD51AA4E5FE000035C3 /* WMFImageURLParsing.m */; }; BC86B9361A92966B00B4C039 /* AFHTTPRequestOperationManager+UniqueRequests.m in Sources */ = {isa = PBXBuildFile; fileRef = BC86B9351A92966B00B4C039 /* AFHTTPRequestOperationManager+UniqueRequests.m */; }; BC86B93D1A929CC500B4C039 /* UICollectionViewFlowLayout+NSCopying.m in Sources */ = {isa = PBXBuildFile; fileRef = BC86B93C1A929CC500B4C039 /* UICollectionViewFlowLayout+NSCopying.m */; }; BC86B9401A929D7900B4C039 /* UICollectionViewFlowLayout+WMFItemSizeThatFits.m in Sources */ = {isa = PBXBuildFile; fileRef = BC86B93F1A929D7900B4C039 /* UICollectionViewFlowLayout+WMFItemSizeThatFits.m */; }; @@ -734,6 +736,8 @@ BC50C3861A83CBDA006DC7AF /* MWKImageInfoResponseSerializer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MWKImageInfoResponseSerializer.m; path = Serializers/MWKImageInfoResponseSerializer.m; sourceTree = "<group>"; }; BC6FEAE01A9B7EFD00A1D890 /* WMFCodingStyle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WMFCodingStyle.m; sourceTree = "<group>"; }; BC7DFCCB1AA4BA8A000035C3 /* WMFCodingStyle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WMFCodingStyle.h; sourceTree = "<group>"; }; + BC7DFCD41AA4E5FE000035C3 /* WMFImageURLParsing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WMFImageURLParsing.h; sourceTree = "<group>"; }; + BC7DFCD51AA4E5FE000035C3 /* WMFImageURLParsing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WMFImageURLParsing.m; sourceTree = "<group>"; }; BC8309961A7BF935003FC5C7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; BC8309C81A7C370B003FC5C7 /* NSArray+PredicateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSArray+PredicateTests.m"; path = "Utilities/NSArray+PredicateTests.m"; sourceTree = "<group>"; }; BC86B9341A92966B00B4C039 /* AFHTTPRequestOperationManager+UniqueRequests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AFHTTPRequestOperationManager+UniqueRequests.h"; sourceTree = "<group>"; }; @@ -2157,6 +2161,8 @@ children = ( C9180EC218AED30C006C1DCA /* WikipediaAppUtils.h */, C9180EC318AED30C006C1DCA /* WikipediaAppUtils.m */, + BC7DFCD41AA4E5FE000035C3 /* WMFImageURLParsing.h */, + BC7DFCD51AA4E5FE000035C3 /* WMFImageURLParsing.m */, ); path = "mw-utils"; sourceTree = "<group>"; @@ -2799,6 +2805,7 @@ BC42735E1A7C73B300068882 /* NSArray+PredicateTests.m in Sources */, BCB669F01A83F71C00C7B1FE /* MWKSavedPageList.m in Sources */, BCB669FD1A84158200C7B1FE /* CircularBitwiseRotationTests.m in Sources */, + BC7DFCD71AA4E5FE000035C3 /* WMFImageURLParsing.m in Sources */, BCB669D31A83F6D300C7B1FE /* MWKTestCase.m in Sources */, BCB669E51A83F6D300C7B1FE /* MWKImageStorageTests.m in Sources */, BCB669F51A83F71C00C7B1FE /* MWKImageInfo.m in Sources */, @@ -2949,6 +2956,8 @@ 0487048419F8262600B7D307 /* CaptchaResetter.m in Sources */, D407E6411A51DBDA00CCC8B1 /* SchemaConverter.m in Sources */, BCB669A71A83F6C400C7B1FE /* MWKSiteDataObject.m in Sources */, + 043BFC7A1A932BA50068B932 /* LegalFooterViewController.m in Sources */, + BC7DFCD61AA4E5FE000035C3 /* WMFImageURLParsing.m in Sources */, 04821CD119895EDC007558F6 /* ReferenceGradientView.m in Sources */, 0460F8DC19B0F932001BC59B /* CenteredPathView.m in Sources */, 0472BC18193AD88C00C40BDA /* MWKSection+DisplayHtml.m in Sources */, diff --git a/wikipedia/Base.lproj/Main_iPhone.storyboard b/wikipedia/Base.lproj/Main_iPhone.storyboard index 748d234..3fd063c 100644 --- a/wikipedia/Base.lproj/Main_iPhone.storyboard +++ b/wikipedia/Base.lproj/Main_iPhone.storyboard @@ -913,7 +913,7 @@ <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="z4O-h7-mfv"> <rect key="frame" x="0.0" y="0.0" width="320" height="568"/> <subviews> - <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="m9e-S5-3FD" userLabel="Empty marker"> + <view contentMode="scaleToFill" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="m9e-S5-3FD" userLabel="Empty marker"> <rect key="frame" x="0.0" y="165" width="320" height="238"/> <subviews> <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="SavedPagesEmpty" translatesAutoresizingMaskIntoConstraints="NO" id="Nki-Th-pdU"> @@ -1336,7 +1336,7 @@ <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="HLl-qz-Bgx"> <rect key="frame" x="0.0" y="0.0" width="320" height="568"/> <subviews> - <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Yc2-71-IOD" userLabel="Empty marker"> + <view contentMode="scaleToFill" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Yc2-71-IOD" userLabel="Empty marker"> <rect key="frame" x="0.0" y="200" width="320" height="168"/> <subviews> <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="RecentPagesEmpty" translatesAutoresizingMaskIntoConstraints="NO" id="o1U-9i-KeG"> diff --git a/wikipedia/View Controllers/Image Gallery/WMFImageGalleryViewController.m b/wikipedia/View Controllers/Image Gallery/WMFImageGalleryViewController.m index e3b0930..c1ae0b7 100644 --- a/wikipedia/View Controllers/Image Gallery/WMFImageGalleryViewController.m +++ b/wikipedia/View Controllers/Image Gallery/WMFImageGalleryViewController.m @@ -42,6 +42,13 @@ #define ImgGalleryLog(...) #endif +NSDictionary* WMFIndexImageInfo(NSArray* imageInfo) +{ + return [imageInfo bk_index:^id<NSCopying>(MWKImageInfo* info) { + return info.imageAssociationValue ?: [NSNull null]; + }]; +} + @interface WMFImageGalleryViewController () <UIGestureRecognizerDelegate, UICollectionViewDelegateFlowLayout> { @@ -144,8 +151,7 @@ - (NSDictionary*)indexedImageInfo { if (!_indexedImageInfo) { - _indexedImageInfo = [[self.dataStore imageInfoForArticle:self.article] - bk_indexWithKeypath:MWKImageAssociationKeyPath]; + _indexedImageInfo = WMFIndexImageInfo([self.dataStore imageInfoForArticle:self.article]); } return _indexedImageInfo; } @@ -219,7 +225,7 @@ - (void)updateImageInfo:(NSArray*)imageInfo { - _indexedImageInfo = [imageInfo bk_indexWithKeypath:MWKImageAssociationKeyPath]; + _indexedImageInfo = WMFIndexImageInfo(imageInfo); [self.collectionView reloadItemsAtIndexPaths:self.collectionView.indexPathsForVisibleItems]; } diff --git a/wikipedia/mw-utils/WMFImageURLParsing.h b/wikipedia/mw-utils/WMFImageURLParsing.h new file mode 100644 index 0000000..7d2036e --- /dev/null +++ b/wikipedia/mw-utils/WMFImageURLParsing.h @@ -0,0 +1,5 @@ +#import <Foundation/Foundation.h> + +FOUNDATION_EXPORT NSString* WMFParseImageNameFromSourceURL(NSURL* sourceURL) __attribute__((overloadable)); + +FOUNDATION_EXPORT NSString* WMFParseImageNameFromSourceURL(NSString* sourceURL) __attribute__((overloadable)); \ No newline at end of file diff --git a/wikipedia/mw-utils/WMFImageURLParsing.m b/wikipedia/mw-utils/WMFImageURLParsing.m new file mode 100644 index 0000000..d56ccf8 --- /dev/null +++ b/wikipedia/mw-utils/WMFImageURLParsing.m @@ -0,0 +1,15 @@ +#import "WMFImageURLParsing.h" + +NSString* WMFParseImageNameFromSourceURL(NSURL* sourceURL) __attribute__((overloadable)) +{ + return WMFParseImageNameFromSourceURL(sourceURL.absoluteString); +} + +NSString* WMFParseImageNameFromSourceURL(NSString* sourceURL) __attribute__((overloadable)) +{ + if (!sourceURL) { return nil; } + NSString *fileName = [sourceURL lastPathComponent]; + NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"^\\d+px-(.*)$" options:0 error:nil]; + NSArray *matches = [re matchesInString:fileName options:0 range:NSMakeRange(0, [fileName length])]; + return matches.count ? [fileName substringWithRange:[matches[0] rangeAtIndex:1]] : fileName; +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/193863 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I862704957b81ab39646368851028e48931ae67d2 Gerrit-PatchSet: 4 Gerrit-Project: apps/ios/wikipedia Gerrit-Branch: master Gerrit-Owner: Bgerstle <bgers...@wikimedia.org> Gerrit-Reviewer: Bgerstle <bgers...@wikimedia.org> Gerrit-Reviewer: Dr0ptp4kt <ab...@wikimedia.org> Gerrit-Reviewer: Fjalapeno <cfl...@wikimedia.org> Gerrit-Reviewer: Mhurd <mh...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits