This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from atfinke/3.1
3.1 Released
- Loading branch information
Showing
71 changed files
with
937 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>BannedURLFragments</key> | ||
<array> | ||
<string>/wiki/File</string> | ||
<string>org/wiki/Wikipedia:</string> | ||
<string>org/wiki/Special</string> | ||
<string>org/wiki/Portal:</string> | ||
<string>/File:</string> | ||
<string>#/</string> | ||
</array> | ||
<key>BaseURLString</key> | ||
<string>https://en.m.wikipedia.org/wiki</string> | ||
<key>PageTitleCharactersToRemove</key> | ||
<integer>0</integer> | ||
<key>PageTitleStringToReplace</key> | ||
<string> - Wikipedia</string> | ||
<key>QuickRace</key> | ||
<false/> | ||
<key>RandomURLString</key> | ||
<string>https://en.m.wikipedia.org/wiki/Special:Random</string> | ||
<key>Version</key> | ||
<integer>1</integer> | ||
<key>WhatLinksHereURLString</key> | ||
<string>https://en.m.wikipedia.org/w/index.php?title=Special:WhatLinksHere</string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// | ||
// WKRConstants.swift | ||
// WKRKit | ||
// | ||
// Created by Andrew Finke on 8/5/17. | ||
// Copyright © 2017 Andrew Finke. All rights reserved. | ||
// | ||
|
||
import CloudKit | ||
import Foundation | ||
|
||
public class WKRKitConstants { | ||
|
||
public let version: Int | ||
public static var current = WKRKitConstants() | ||
|
||
internal let quickRace: Bool | ||
|
||
internal let pageTitleStringToReplace: String | ||
internal let pageTitleCharactersToRemove: Int | ||
|
||
internal let baseURLString: String | ||
internal let randomURLString: String | ||
internal let whatLinksHereURLString: String | ||
|
||
internal let bannedURLFragments: [String] | ||
|
||
init() { | ||
//swiftlint:disable:next line_length | ||
guard let documentsConstantsURL = FileManager.default.documentsDirectory?.appendingPathComponent("WKRKitConstants.plist"), | ||
let documentsConstants = NSDictionary(contentsOf: documentsConstantsURL) as? [String:Any] else { | ||
fatalError() | ||
} | ||
|
||
guard let version = documentsConstants["Version"] as? Int else { | ||
fatalError("WKRKitConstants: No Version value") | ||
} | ||
guard let quickRace = documentsConstants["QuickRace"] as? Bool else { | ||
fatalError("WKRKitConstants: No QuickRace value") | ||
} | ||
guard let pageTitleStringToReplace = documentsConstants["PageTitleStringToReplace"] as? String else { | ||
fatalError("WKRKitConstants: No PageTitleStringToReplace value") | ||
} | ||
guard let pageTitleCharactersToRemove = documentsConstants["PageTitleCharactersToRemove"] as? Int else { | ||
fatalError("WKRKitConstants: No PageTitleCharactersToRemove value") | ||
} | ||
guard let baseURLString = documentsConstants["BaseURLString"] as? String else { | ||
fatalError("WKRKitConstants: No BaseURLString value") | ||
} | ||
guard let randomURLString = documentsConstants["RandomURLString"] as? String else { | ||
fatalError("WKRKitConstants: No RandomURLString value") | ||
} | ||
guard let whatLinksHereURLString = documentsConstants["WhatLinksHereURLString"] as? String else { | ||
fatalError("WKRKitConstants: No WhatLinksHereURLString value") | ||
} | ||
guard let bannedURLFragments = documentsConstants["BannedURLFragments"] as? [String] else { | ||
fatalError("WKRKitConstants: No BannedURLFragments value") | ||
} | ||
|
||
self.version = version | ||
self.quickRace = quickRace | ||
|
||
self.pageTitleStringToReplace = pageTitleStringToReplace | ||
self.pageTitleCharactersToRemove = pageTitleCharactersToRemove | ||
|
||
self.baseURLString = baseURLString | ||
self.randomURLString = randomURLString | ||
self.whatLinksHereURLString = whatLinksHereURLString | ||
|
||
self.bannedURLFragments = bannedURLFragments | ||
} | ||
|
||
static public func updateConstants() { | ||
copyBundledResourcesToDocuments() | ||
|
||
let publicDB = CKContainer.default().publicCloudDatabase | ||
let recordID = CKRecordID(recordName: "WKRKitConstantsRecord") | ||
|
||
publicDB.fetch(withRecordID: recordID) { record, _ in | ||
guard let record = record else { | ||
return | ||
} | ||
|
||
guard let recordConstantsAsset = record["ConstantsFile"] as? CKAsset, | ||
let recordArticlesAsset = record["ArticlesFile"] as? CKAsset, | ||
let recordGetLinksScriptAsset = record["GetLinksScriptFile"] as? CKAsset else { | ||
return | ||
} | ||
|
||
copyIfNewer(newConstantsFileURL: recordConstantsAsset.fileURL, | ||
newArticlesFileURL: recordArticlesAsset.fileURL, | ||
newGetLinksScriptFileURL: recordGetLinksScriptAsset.fileURL) | ||
} | ||
} | ||
|
||
static private func copyIfNewer(newConstantsFileURL: URL, | ||
newArticlesFileURL: URL, | ||
newGetLinksScriptFileURL: URL) { | ||
|
||
guard FileManager.default.fileExists(atPath: newConstantsFileURL.path), | ||
FileManager.default.fileExists(atPath: newArticlesFileURL.path), | ||
FileManager.default.fileExists(atPath: newGetLinksScriptFileURL.path) else { | ||
return | ||
} | ||
|
||
guard let newConstants = NSDictionary(contentsOf: newConstantsFileURL), | ||
let newConstantsVersion = newConstants["Version"] as? Int, | ||
let documentsDirectory = FileManager.default.documentsDirectory else { | ||
return | ||
} | ||
|
||
let documentsArticlesURL = documentsDirectory.appendingPathComponent("WKRArticlesData.plist") | ||
let documentsConstantsURL = documentsDirectory.appendingPathComponent("WKRKitConstants.plist") | ||
let documentsGetLinksScriptURL = documentsDirectory.appendingPathComponent("WKRGetLinks.js") | ||
|
||
var shouldReplaceExisitingConstants = true | ||
if FileManager.default.fileExists(atPath: documentsConstantsURL.path), | ||
let documentsConstants = NSDictionary(contentsOf: documentsConstantsURL), | ||
let documentsConstantsVersions = documentsConstants["Version"] as? Int { | ||
|
||
if newConstantsVersion <= documentsConstantsVersions { | ||
shouldReplaceExisitingConstants = false | ||
} | ||
} | ||
|
||
if shouldReplaceExisitingConstants { | ||
do { | ||
try? FileManager.default.removeItem(at: documentsArticlesURL) | ||
try FileManager.default.copyItem(at: newArticlesFileURL, to: documentsArticlesURL) | ||
|
||
try? FileManager.default.removeItem(at: documentsGetLinksScriptURL) | ||
try FileManager.default.copyItem(at: newGetLinksScriptFileURL, to: documentsGetLinksScriptURL) | ||
|
||
try? FileManager.default.removeItem(at: documentsConstantsURL) | ||
try FileManager.default.copyItem(at: newConstantsFileURL, to: documentsConstantsURL) | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
|
||
current = WKRKitConstants() | ||
} | ||
|
||
static private func copyBundledResourcesToDocuments() { | ||
guard let bundle = Bundle(identifier: "com.andrewfinke.WKRKit"), | ||
let bundledPlistURL = bundle.url(forResource: "WKRKitConstants", withExtension: "plist"), | ||
let bundledArticlesURL = bundle.url(forResource: "WKRArticlesData", withExtension: "plist"), | ||
let bundledGetLinksScriptURL = bundle.url(forResource: "WKRGetLinks", withExtension: "js") else { | ||
fatalError() | ||
} | ||
|
||
copyIfNewer(newConstantsFileURL: bundledPlistURL, | ||
newArticlesFileURL: bundledArticlesURL, | ||
newGetLinksScriptFileURL: bundledGetLinksScriptURL) | ||
} | ||
|
||
internal func finalArticles() -> [String] { | ||
//swiftlint:disable:next line_length | ||
guard let documentsArticlesURL = FileManager.default.documentsDirectory?.appendingPathComponent("WKRArticlesData.plist"), | ||
let arrayFromURL = NSArray(contentsOf: documentsArticlesURL), | ||
let array = arrayFromURL as? [String] else { | ||
fatalError() | ||
} | ||
return array | ||
} | ||
|
||
internal func getLinksScript() -> String { | ||
//swiftlint:disable:next line_length | ||
guard let documentsScriptURL = FileManager.default.documentsDirectory?.appendingPathComponent("WKRGetLinks.js"), | ||
let source = try? String(contentsOf: documentsScriptURL) else { | ||
fatalError() | ||
} | ||
return source | ||
} | ||
|
||
} | ||
|
||
extension FileManager { | ||
var documentsDirectory: URL? { | ||
return urls(for: .documentDirectory, in: .userDomainMask).first | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.