Brion VIBBER has submitted this change and it was merged.

Change subject: Initial 'garbage collection' of unused cached images when 
cleaning history
......................................................................


Initial 'garbage collection' of unused cached images when cleaning history

Images linked from sections were saved into the database, but not deleted
automatically when their section was removed because they might be used
in multiple articles.

Adds a convenience method [image deleteIfUnused] to kill out the old images
if still unused after the cascading deletes remove the sections.

Change-Id: I6755ec0490e16d1d46fbb34e61c4d947858989a1
---
M wikipedia/Categories/Image+Convenience.h
M wikipedia/Categories/Image+Convenience.m
M wikipedia/View Controllers/History/HistoryViewController.m
M wikipedia/assets/abusefilter.css
M wikipedia/assets/preview.css
M wikipedia/assets/styles.css
6 files changed, 43 insertions(+), 8 deletions(-)

Approvals:
  Brion VIBBER: Verified; Looks good to me, approved



diff --git a/wikipedia/Categories/Image+Convenience.h 
b/wikipedia/Categories/Image+Convenience.h
index 4a40cef..b484dcb 100644
--- a/wikipedia/Categories/Image+Convenience.h
+++ b/wikipedia/Categories/Image+Convenience.h
@@ -8,4 +8,10 @@
 // Retrieves the highest resolution image in the core data store with same 
name as this image.
 -(Image 
*)getHighestResolutionImageWithSameNameUsingContext:(NSManagedObjectContext 
*)context;
 
+/**
+ * Check if this image is still in use, and if not remove it and its backing 
data.
+ * You'll still have to actually save updates on the managed object context!
+ */
+-(void)deleteIfUnused;
+
 @end
diff --git a/wikipedia/Categories/Image+Convenience.m 
b/wikipedia/Categories/Image+Convenience.m
index 48ab314..4109ac4 100644
--- a/wikipedia/Categories/Image+Convenience.m
+++ b/wikipedia/Categories/Image+Convenience.m
@@ -28,4 +28,14 @@
     return (images.count == 1) ? images[0] : self;
 }
 
+-(void)deleteIfUnused
+{
+    int count = (int)self.sectionImage.count;
+    NSLog(@"section image = %@ is referenced by %d sections", self.fileName, 
count);
+    if (count < 1) {
+        NSLog(@"deleting unused image %@", self.fileName);
+        [self.managedObjectContext deleteObject:self];
+    }
+}
+
 @end
diff --git a/wikipedia/View Controllers/History/HistoryViewController.m 
b/wikipedia/View Controllers/History/HistoryViewController.m
index 0aede61..ffdc30f 100644
--- a/wikipedia/View Controllers/History/HistoryViewController.m
+++ b/wikipedia/View Controllers/History/HistoryViewController.m
@@ -18,6 +18,7 @@
 #import "TopMenuContainerView.h"
 #import "TopMenuViewController.h"
 #import "UIViewController+StatusBarHeight.h"
+#import "Image+Convenience.h"
 
 #define HISTORY_THUMBNAIL_WIDTH 110
 #define HISTORY_RESULT_HEIGHT 66
@@ -201,7 +202,6 @@
             history.article.thumbnailImage.fileName
         );
         */
-
         if ([history.dateVisited isToday]) {
             [today addObject:history.objectID];
         }else if ([history.dateVisited isYesterday]) {
@@ -255,6 +255,7 @@
     //NSLog(@"GARBAGE COUNT = %lu", (unsigned long)garbage.count);
     //NSLog(@"GARBAGE = %@", garbage);
     if (garbage.count == 0) return;
+    NSMutableArray *imagesToCollect = [[NSMutableArray alloc] init];
 
     [articleDataContext_.mainContext performBlockAndWait:^(){
         for (NSManagedObjectID *historyID in garbage) {
@@ -271,16 +272,34 @@
 
             // Article deletes don't cascade to images (intentionally) so 
delete these manually.
             if (thumb) [articleDataContext_.mainContext deleteObject:thumb];
-
-//TODO: add code for deleting images which were only referenced by this article
+            
+            if (article) {
+                // Section images might be used in multiple articles, so list 
them up
+                // and kill them only after we've cleaned up the sections and 
can confirm
+                // they are no longer used...
+                for (Section *section in article.section) {
+                    for (SectionImage *sectionImage in section.sectionImage) {
+                        Image *image = sectionImage.image;
+                        [imagesToCollect addObject:image];
+                    }
+                }
+            }
 
             // Delete the article
             if (article) [articleDataContext_.mainContext 
deleteObject:article];
-
         }
+
         NSError *error = nil;
         [articleDataContext_.mainContext save:&error];
-        if (error) NSLog(@"GARBAGE error = %@", error);
+        if (error) NSLog(@"GARBAGE pass 1 error = %@", error);
+
+        // Now clean up the images that aren't used in remaining pages
+        for (Image *image in imagesToCollect) {
+            [image deleteIfUnused];
+        }
+        // and save again...
+        [articleDataContext_.mainContext save:&error];
+        if (error) NSLog(@"GARBAGE pass 2 error = %@", error);
 
     }];
 }
diff --git a/wikipedia/assets/abusefilter.css b/wikipedia/assets/abusefilter.css
index c8afb69..89b5850 100644
--- a/wikipedia/assets/abusefilter.css
+++ b/wikipedia/assets/abusefilter.css
@@ -190,7 +190,7 @@
   max-width: none !important;
 }
 .content .thumbcaption {
-  margin: 0;
+  margin: .5em 0 0;
   font-size: .8em;
   line-height: 1.5;
   padding: 0 !important;
diff --git a/wikipedia/assets/preview.css b/wikipedia/assets/preview.css
index 8415e02..d792df7 100644
--- a/wikipedia/assets/preview.css
+++ b/wikipedia/assets/preview.css
@@ -190,7 +190,7 @@
   max-width: none !important;
 }
 .content .thumbcaption {
-  margin: 0;
+  margin: .5em 0 0;
   font-size: .8em;
   line-height: 1.5;
   padding: 0 !important;
diff --git a/wikipedia/assets/styles.css b/wikipedia/assets/styles.css
index c8afb69..89b5850 100644
--- a/wikipedia/assets/styles.css
+++ b/wikipedia/assets/styles.css
@@ -190,7 +190,7 @@
   max-width: none !important;
 }
 .content .thumbcaption {
-  margin: 0;
+  margin: .5em 0 0;
   font-size: .8em;
   line-height: 1.5;
   padding: 0 !important;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I6755ec0490e16d1d46fbb34e61c4d947858989a1
Gerrit-PatchSet: 3
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mhurd <mh...@wikimedia.org>
Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org>
Gerrit-Reviewer: Mhurd <mh...@wikimedia.org>

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

Reply via email to