Bgerstle has uploaded a new change for review.

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

Change subject: fix issue causing large images to not be rerouted to WMF cache
......................................................................

fix issue causing large images to not be rerouted to WMF cache

- increases memory & disk capacity to 64MB. in practice we don't use
  much of this since we reroute images to our own storage mechanism.
  however, we could eventually bloat the cache with JSON or other
  non-rerouted data.
- fixes an assertion where tapping an article image which isn't in the
  cache would fail to be found by "selectedImage" logic before
  presenting the gallery
- Adds some (currently disabled) logs to URLCache to ease debugging

Change-Id: I7b604206e54876a266aa5bd8a1396c45d2077095
---
M MediaWikiKit/MediaWikiKit/MWKImage.m
M MediaWikiKit/MediaWikiKit/MWKImageList.h
M wikipedia/AppDelegate.m
M wikipedia/View Controllers/WebView/WebViewController.m
M wikipedia/Web Image Interception/URLCache.m
5 files changed, 34 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia 
refs/changes/94/191894/2

diff --git a/MediaWikiKit/MediaWikiKit/MWKImage.m 
b/MediaWikiKit/MediaWikiKit/MWKImage.m
index 34876f3..e6805a1 100644
--- a/MediaWikiKit/MediaWikiKit/MWKImage.m
+++ b/MediaWikiKit/MediaWikiKit/MWKImage.m
@@ -141,7 +141,8 @@
     _dateRetrieved = [[NSDate alloc] init];
     _dateLastAccessed = [[NSDate alloc] init];
     _mimeType = [self getImageMimeTypeForExtension:self.extension];
-    
+
+#warning TODO(bgerstle): get width & height info w/o expensively inflating the 
image data and then throwing it away
     UIImage *img = [UIImage imageWithData:data scale:1.0];
     _width = [NSNumber numberWithInt:img.size.width];
     _height = [NSNumber numberWithInt:img.size.height];
diff --git a/MediaWikiKit/MediaWikiKit/MWKImageList.h 
b/MediaWikiKit/MediaWikiKit/MWKImageList.h
index e373bce..9e8691d 100644
--- a/MediaWikiKit/MediaWikiKit/MWKImageList.h
+++ b/MediaWikiKit/MediaWikiKit/MWKImageList.h
@@ -52,10 +52,20 @@
 /**
  * Searches the receiver for a cached image variant matching @c sourceURL.
  * @return An @c MWKImage object or @c nil if no matching variant is found.
+ * @see -largestImageVariantForURL:cachedOnly:
  */
 - (MWKImage*)largestImageVariantForURL:(NSString*)sourceURL;
 
 /**
+ * Find an image with the specified URL, optionally requiring it to be stored 
in the cache.
+ * @param imageURL      The @c sourceURL of the image to be retrieved.
+ * @param cachedOnly    Whether or not matches must also be cached.
+ * @return A @c MWKImage object where @c sourceURL matches @c imageURL. If @c 
cachedOnly is @c YES, the object will also
+ *         be cached. Otherwise @c nil if no matching, cached (if specified) 
entries are found. 
+ */
+- (MWKImage*)largestImageVariantForURL:(NSString *)imageURL 
cachedOnly:(BOOL)cachedOnly;
+
+/**
  * Reduce the receiver by removing all but the largest variants of the 
contained images, preserving order.
  * @warning In an effort to make this method more reliable, it should always 
return an image for each entry in the list,
  *          but some of the returned images might not be cached.
diff --git a/wikipedia/AppDelegate.m b/wikipedia/AppDelegate.m
index 45b3431..42a7c2c 100644
--- a/wikipedia/AppDelegate.m
+++ b/wikipedia/AppDelegate.m
@@ -18,8 +18,8 @@
      Set the shared url cache to our custom NSURLCache which re-routes images 
to
      our article cache.
      */
-    URLCache *urlCache = [[URLCache alloc] 
initWithMemoryCapacity:MegabytesToBytes(8)
-                                                     
diskCapacity:MegabytesToBytes(25)
+    URLCache *urlCache = [[URLCache alloc] 
initWithMemoryCapacity:MegabytesToBytes(64)
+                                                     
diskCapacity:MegabytesToBytes(64)
                                                          diskPath:nil];
     [NSURLCache setSharedURLCache:urlCache];
     
diff --git a/wikipedia/View Controllers/WebView/WebViewController.m 
b/wikipedia/View Controllers/WebView/WebViewController.m
index 5f17c4e..a9581aa 100644
--- a/wikipedia/View Controllers/WebView/WebViewController.m
+++ b/wikipedia/View Controllers/WebView/WebViewController.m
@@ -916,7 +916,8 @@
 
         NSString *selectedImageURL = payload[@"url"];
         NSCParameterAssert(selectedImageURL.length);
-        MWKImage *selectedImage = [strSelf->session.article.images 
largestImageVariantForURL:selectedImageURL];
+        MWKImage *selectedImage = [strSelf->session.article.images 
largestImageVariantForURL:selectedImageURL
+                                                                               
   cachedOnly:NO];
         NSCParameterAssert(selectedImage);
         [strSelf presentGalleryForArticle:strSelf->session.article 
showingImage:selectedImage];
     }];
diff --git a/wikipedia/Web Image Interception/URLCache.m b/wikipedia/Web Image 
Interception/URLCache.m
index bac164e..8f83674 100644
--- a/wikipedia/Web Image Interception/URLCache.m
+++ b/wikipedia/Web Image Interception/URLCache.m
@@ -8,6 +8,12 @@
 #import "NSString+Extras.h"
 #import "SessionSingleton.h"
 
+#if 0
+#define URLCacheLog(...) NSLog(__VA_ARGS__)
+#else
+#define URLCacheLog(...)
+#endif
+
 @interface URLCache ()
 
 @property (readonly) MWKArticle *article;
@@ -54,14 +60,16 @@
 - (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse 
forRequest:(NSURLRequest *)request
 {
     if (![self isMIMETypeRerouted:cachedResponse.response.MIMEType]) {
+        URLCacheLog(@"Storing cached response for %@ due to MIMEType", 
request);
         [super storeCachedResponse:cachedResponse forRequest:request];
         if ([[request URL].host hasSuffix:@".m.wikipedia.org"] &&
             [cachedResponse.response.MIMEType 
rangeOfString:@"application/json"].location != NSNotFound) {
             // NSData *data = cachedResponse.data;
             // NSString *newStr = [[NSString alloc] initWithData:data
             //                                          
encoding:NSUTF8StringEncoding];
-            // NSLog(@"%@", newStr);
+            // URLCacheLog(@"%@", newStr);
             dispatch_async(dispatch_get_main_queue(), ^(){
+                URLCacheLog(@"Processing zero headers for cached repsonse from 
%@", request);
                 [self processZeroHeaders:cachedResponse.response];
             });
         }
@@ -69,6 +77,7 @@
     }
     if (![self isURLRerouted:request.URL]) {
         // Only cache stuff from upload.wikimedia.org
+        URLCacheLog(@"%@ is not being cached since it's not from 
upload.wikimedia.org", request);
         return;
     }
 
@@ -85,6 +94,7 @@
     
     if (!image) {
         // If an Image object wasn't pre-created by 
:createSectionImageRecordsForSectionHtml:onContext:" then don't try to cache.
+        URLCacheLog(@"Storing cached response without image record for %@", 
request);
         [super storeCachedResponse:cachedResponse forRequest:request];
         return;
     }
@@ -110,10 +120,11 @@
     //imageDataToUse = self.debuggingPlaceHolderImageData;
 
     @try {
+        URLCacheLog(@"Rerouting cached response to WMF data store for %@", 
request);
         [self.article importImageData:imageDataToUse image:image];
     }
     @catch (NSException *e) {
-        NSLog(@"Failure to save cached image data: %@", e);
+        URLCacheLog(@"Failure to save cached image data: %@", e);
         return;
     }
     
@@ -129,7 +140,7 @@
 }
 
 - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
-    //NSLog(@"Default Cache.db usage:\n\tcurrentDiskUsage: %lu\n\tdiskCapacity 
= %lu\n\tcurrentMemoryUsage = %lu\n\tmemoryCapacity = %lu", (unsigned 
long)self.currentDiskUsage, (unsigned long)self.diskCapacity, (unsigned 
long)self.currentMemoryUsage, (unsigned long)self.memoryCapacity);
+    //URLCacheLog(@"Default Cache.db usage:\n\tcurrentDiskUsage: 
%lu\n\tdiskCapacity = %lu\n\tcurrentMemoryUsage = %lu\n\tmemoryCapacity = %lu", 
(unsigned long)self.currentDiskUsage, (unsigned long)self.diskCapacity, 
(unsigned long)self.currentMemoryUsage, (unsigned long)self.memoryCapacity);
 
     if (![self isMIMETypeRerouted:[request.URL.pathExtension 
getImageMimeTypeForExtension]]) {
         return [super cachedResponseForRequest:request];
@@ -148,7 +159,7 @@
     NSURL *requestURL = [request.URL copy];
     
     __block NSCachedURLResponse *cachedResponse = nil;
-    //NSLog(@"[NSThread isMainThread] = %d", [NSThread isMainThread]);
+    //URLCacheLog(@"[NSThread isMainThread] = %d", [NSThread isMainThread]);
     NSString *imageURL = requestURL.absoluteString;
     
     // Strip "http:" or "https:"
@@ -160,19 +171,19 @@
     // If a core data Image was found, but its data length is zero, the Image 
record was probably
     // created when the section html was parsed to create sectionImage 
records, in which case
     // a request needs to actually be made, so set cachedResponse to nil so 
this happens.
-    // NSLog(@"imageFromDB.data = %@", imageFromDB.data);
+    // URLCacheLog(@"imageFromDB.data = %@", imageFromDB.data);
     if (imageFromDB && !imageFromDB.dateRetrieved) {
         cachedResponse = nil;
     }else if (imageFromDB) {
         NSData *imageData = [self.article.dataStore 
imageDataWithImage:imageFromDB];
-        //NSLog(@"CACHED IMAGE FOUND!!!!!! requestURL = %@", imageURL);
+        //URLCacheLog(@"CACHED IMAGE FOUND!!!!!! requestURL = %@", imageURL);
         NSURLResponse *response = [[NSURLResponse alloc] 
initWithURL:requestURL MIMEType:imageFromDB.mimeType 
expectedContentLength:imageData.length textEncodingName:nil];
         cachedResponse = [[NSCachedURLResponse alloc] 
initWithResponse:response data:imageData];
     }
 
     if (cachedResponse) return cachedResponse;
 
-    //NSLog(@"CACHED IMAGE NOT FOUND!!!!! request.URL = %@", imageURL);
+    //URLCacheLog(@"CACHED IMAGE NOT FOUND!!!!! request.URL = %@", imageURL);
     return [super cachedResponseForRequest:request];
 }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7b604206e54876a266aa5bd8a1396c45d2077095
Gerrit-PatchSet: 2
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Bgerstle <bgers...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to