Skip to content

Commit

Permalink
Move old CoreData stuff to separate project library
Browse files Browse the repository at this point in the history
Change-Id: Ife903169e3e1c592be6a9096ec8611b32c5fc1a1
  • Loading branch information
bvibber authored and montehurd committed Dec 23, 2014
1 parent 4b5a8da commit 479b666
Show file tree
Hide file tree
Showing 49 changed files with 480 additions and 321 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#import "NSManagedObjectContext+SimpleFetch.h"
#import "ArticleCoreDataObjects.h"
#import "SessionSingleton.h"
//#import "SessionSingleton.h"

@implementation NSManagedObjectContext (SimpleFetch)

Expand Down Expand Up @@ -46,7 +46,7 @@ -(NSManagedObjectID *)getArticleIDForTitle:(NSString *)title domain:(NSString *)
AND \
domain == %@",
title,
[SessionSingleton sharedInstance].site,
@"wikipedia.org",//[SessionSingleton sharedInstance].site,
domain
];

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import <CoreData/CoreData.h>

#import "Article+Convenience.h"
//#import "Article+Convenience.h"
#import "DiscoveryContext.h"
#import "Section.h"
#import "History.h"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
354 changes: 354 additions & 0 deletions OldDataSchema/OldDataSchema.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions OldDataSchema/OldDataSchema/OldDataSchema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// OldDataSchema.h
// OldDataSchema
//
// Created by Brion on 12/22/14.
// Copyright (c) 2014 Wikimedia Foundation. All rights reserved.
//

#import <Foundation/Foundation.h>


@class OldDataSchema;

@protocol OldDataSchemaDelegate

-(void)oldDataSchema:(OldDataSchema *)schema migrateArticle:(NSDictionary *)articleDict;
-(void)oldDataSchema:(OldDataSchema *)schema migrateHistoryEntry:(NSDictionary *)historyDict;
-(void)oldDataSchema:(OldDataSchema *)schema migrateSavedEntry:(NSDictionary *)savedDict;

@end


@interface OldDataSchema : NSObject

@property (weak) id<OldDataSchemaDelegate> delegate;

-(BOOL)exists;
-(void)migrateData;

@end
32 changes: 32 additions & 0 deletions OldDataSchema/OldDataSchema/OldDataSchema.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// OldDataSchema.m
// OldDataSchema
//
// Created by Brion on 12/22/14.
// Copyright (c) 2014 Wikimedia Foundation. All rights reserved.
//

#import "OldDataSchema.h"

#import "ArticleDataContextSingleton.h"
#import "ArticleCoreDataObjects.h"
#import "NSManagedObjectContext+SimpleFetch.h"

@implementation OldDataSchema {
ArticleDataContextSingleton *context;
}

-(BOOL)exists
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentRootPath = [documentPaths objectAtIndex:0];
NSString *filePath = [documentRootPath stringByAppendingPathComponent:@"articleData6.sqlite"];
return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
}

-(void)migrateData
{
// TODO
}

@end
152 changes: 44 additions & 108 deletions Wikipedia.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions wikipedia/Categories/Image+Convenience.h

This file was deleted.

41 changes: 0 additions & 41 deletions wikipedia/Categories/Image+Convenience.m

This file was deleted.

1 change: 0 additions & 1 deletion wikipedia/Categories/MWKSection+TOC.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) 2014 Wikimedia Foundation. Provided under MIT-style license; please copy and modify!

#import "MWKSection+TOC.h"
#import "Article.h"
#import "WikipediaAppUtils.h"
#import "SessionSingleton.h"
#import "NSString+FormattedAttributedString.h"
Expand Down
57 changes: 4 additions & 53 deletions wikipedia/Housekeeping/CoreDataHousekeeping.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

#import "CoreDataHousekeeping.h"
#import "NSDate-Utilities.h"
#import "ArticleDataContextSingleton.h"
#import "ArticleCoreDataObjects.h"

@interface CoreDataHousekeeping (){
NSManagedObjectContext *context_;
}

@end
Expand All @@ -18,13 +15,13 @@ - (instancetype)init
{
self = [super init];
if (self) {
context_ = [ArticleDataContextSingleton sharedInstance].mainContext;
}
return self;
}

-(void)performHouseKeeping
{
/*
[context_ performBlockAndWait:^(){
[self removeUnsavedUnhistoriedArticles];
Expand All @@ -43,10 +40,12 @@ -(void)performHouseKeeping
}
}];
*/
}

-(void)removeUnsavedUnhistoriedArticles
{
/*
// Removes articles which have neither saved nor history records.
// The user can remove items from both saved pages and history.
// If they've removed this article from both, not need to keep its
Expand All @@ -65,55 +64,7 @@ -(void)removeUnsavedUnhistoriedArticles
NSLog(@"removing article w/o history or save records = %@", article.title);
if (article) [context_ deleteObject:article];
}
}

-(void)removeUnsavedArticleSections
{
// Removes article sections for articles which are unsaved, but which still have a history record.

// This way the history items will still work if tapped - they will re-download their section data
// automatically because we also set "needsRefresh" to YES below. Also, by removing these sections,
// the "removeUnusedImages" should be able to clean up more images (unused images, that is,
// unreferenced by any sections, are removed by removeUnusedImages).

NSEntityDescription *entity = [NSEntityDescription entityForName: @"Article" inManagedObjectContext: context_];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// To-many query: http://stackoverflow.com/a/1195519
[request setPredicate:[NSPredicate predicateWithFormat:@"history.@count > 0 AND saved.@count == 0"]];

NSError *error = nil;
NSArray *articles = [context_ executeFetchRequest:request error:&error];
for (Article *article in articles) {
NSLog(@"removing sections from article w history w/o saved record = %@", article.title);
for (Section *section in article.section) {
if (section) [context_ deleteObject:section];
}
article.needsRefresh = @YES;
}
}

-(void)removeUnusedImages
{
// Remove core data Images which are not associated with either a SectionImage record or an Article record's
// "thumbnailImage" property.
NSEntityDescription *entity = [NSEntityDescription entityForName: @"Image" inManagedObjectContext: context_];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

// To-many query: http://stackoverflow.com/a/1195519 (the "@" part).
// Reminder that "article.@count" tells us if any article record "thumbnailImage" property is referencing
// this image.
[request setPredicate:[NSPredicate predicateWithFormat:@"sectionImage.@count == 0 AND article.@count == 0"]];

NSError *error = nil;
NSArray *images = [context_ executeFetchRequest:request error:&error];
for (Image *image in images) {
NSLog(@"unused image = %@", image.fileName);
if (image) [context_ deleteObject:image];
}
*/
}

@end
5 changes: 2 additions & 3 deletions wikipedia/Importer/ArticleImporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@

#import "ArticleImporter.h"

#import "ArticleDataContextSingleton.h"
#import "ArticleCoreDataObjects.h"
#import "SessionSingleton.h"
#import "NSManagedObjectContext+SimpleFetch.h"
#import "WikipediaAppUtils.h"
#import "SavedPagesFunnel.h"

@implementation ArticleImporter

-(void)importArticles:(NSArray *)articleDictionaries
{
/*
NSManagedObjectContext *context =
[ArticleDataContextSingleton sharedInstance].mainContext;
Expand Down Expand Up @@ -93,6 +91,7 @@ -(void)importArticles:(NSArray *)articleDictionaries
}
}];
*/
}

@end
12 changes: 11 additions & 1 deletion wikipedia/Networking/Fetchers/ArticleFetcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#import "ArticleFetcher.h"
#import "Defines.h"
#import "Section.h"
#import "QueuesSingleton.h"
#import "NSString+Extras.h"
#import "AFHTTPRequestOperationManager.h"
Expand Down Expand Up @@ -362,6 +361,8 @@ -(void)createImageRecordsForSection:(int)sectionId

-(void)associateThumbFromTempDirWithArticle
{
BOOL foundThumbInTempDir = NO;

// Map which search and nearby populates with title/thumb url mappings.
NSDictionary *map = [SessionSingleton sharedInstance].titleToTempDirThumbURLMap;
NSString *title = self.article.title.prefixedText;
Expand All @@ -386,10 +387,19 @@ -(void)associateThumbFromTempDirWithArticle
// Copy Search/Nearby thumb binary to core data store so it doesn't have to be re-downloaded.
MWKImage *image = [self.article importImageURL:thumbURL sectionId:MWK_SECTION_THUMBNAIL];
[self.article importImageData:data image:image mimeType:@"image"];
foundThumbInTempDir = YES;
}
}
}
}
if(!foundThumbInTempDir){
// If no image found in temp dir, use first article image.
MWKImageList *images = self.article.images;
if (images.count > 0) {
MWKImage *image = images[0];
self.article.thumbnailURL = image.sourceURL;
}
}
}

@end
57 changes: 0 additions & 57 deletions wikipedia/Networking/Fetchers/SearchResultFetcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#import "NSObject+Extras.h"
#import "NSString+Extras.h"
#import "WikipediaAppUtils.h"
#import "ArticleDataContextSingleton.h"
#import "ArticleCoreDataObjects.h"
#import "NSManagedObjectContext+SimpleFetch.h"

@interface SearchResultFetcher()

Expand Down Expand Up @@ -101,8 +98,6 @@ - (void)searchWithManager:(AFHTTPRequestOperationManager *)manager

// Set error condition so dependent ops don't even start and so the errorBlock below will fire.
error = [NSError errorWithDomain:@"Search Result Fetcher" code:SEARCH_RESULT_ERROR_NO_MATCHES userInfo:errorDict];
}else{
[self preparePlaceholderImageRecordsForSearchResults:self.searchResults];
}

[self finishWithError: error
Expand Down Expand Up @@ -238,58 +233,6 @@ -(NSString *)getSearchSuggestionFromResponse:(NSDictionary *)rawResponse
return output;
}

#pragma mark Core data Image record placeholder for thumbnail (so they get cached)

-(void)preparePlaceholderImageRecordsForSearchResults:(NSArray *)searchResults
{
// Prepare placeholder Image records.
[[ArticleDataContextSingleton sharedInstance].mainContext performBlockAndWait:^(){
for (NSDictionary *page in searchResults) {
// If url thumb found, prepare a core data Image object so URLCache
// will know this is an image to intercept.
NSDictionary *thumbData = page[@"thumbnail"];
if (thumbData) {
NSString *src = thumbData[@"source"];
NSNumber *height = thumbData[@"height"];
NSNumber *width = thumbData[@"width"];
if (src && height && width) {
[self insertPlaceHolderImageEntityIntoContext: [ArticleDataContextSingleton sharedInstance].mainContext
forImageWithUrl: src
width: width
height: height];
}
}
}
NSError *error = nil;
[[ArticleDataContextSingleton sharedInstance].mainContext save:&error];
}];
}

-(void)insertPlaceHolderImageEntityIntoContext: (NSManagedObjectContext *)context
forImageWithUrl: (NSString *)url
width: (NSNumber *)width
height: (NSNumber *)height
{
Image *existingImage = (Image *)[context getEntityForName: @"Image" withPredicateFormat:@"sourceUrl == %@", [url getUrlWithoutScheme]];
// If there's already an image record for this exact url, don't create another one!!!
if (!existingImage) {
Image *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
image.imageData = [NSEntityDescription insertNewObjectForEntityForName:@"ImageData" inManagedObjectContext:context];
image.imageData.data = [[NSData alloc] init];
image.dataSize = @(image.imageData.data.length);
image.fileName = [url lastPathComponent];
image.fileNameNoSizePrefix = [image.fileName getWikiImageFileNameWithoutSizePrefix];
image.extension = [url pathExtension];
image.imageDescription = nil;
image.sourceUrl = [url getUrlWithoutScheme];
image.dateRetrieved = [NSDate date];
image.dateLastAccessed = [NSDate date];
image.width = @(width.integerValue);
image.height = @(height.integerValue);
image.mimeType = [image.extension getImageMimeTypeForExtension];
}
}

/*
-(void)dealloc
{
Expand Down
Loading

0 comments on commit 479b666

Please sign in to comment.