-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPluginDirectoryEntry.swift
More file actions
299 lines (245 loc) · 11.3 KB
/
PluginDirectoryEntry.swift
File metadata and controls
299 lines (245 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import Foundation
#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif
public struct PluginDirectoryEntry {
public let name: String
public let slug: String
public let version: String?
public let lastUpdated: Date?
public let icon: URL?
public let banner: URL?
public let author: String
public let authorURL: URL?
let descriptionHTML: String?
let installationHTML: String?
let faqHTML: String?
let changelogHTML: String?
public var descriptionText: NSAttributedString? {
return extractHTMLText(self.descriptionHTML)
}
public var installationText: NSAttributedString? {
return extractHTMLText(self.installationHTML)
}
public var faqText: NSAttributedString? {
return extractHTMLText(self.faqHTML)
}
public var changelogText: NSAttributedString? {
return extractHTMLText(self.changelogHTML)
}
let rating: Double
public var starRating: Double {
return (rating / 10).rounded() / 2
// rounded to nearest half.
}
}
extension PluginDirectoryEntry: Equatable {
public static func ==(lhs: PluginDirectoryEntry, rhs: PluginDirectoryEntry) -> Bool {
return lhs.name == rhs.name
&& lhs.slug == rhs.slug
&& lhs.version == rhs.version
&& lhs.lastUpdated == rhs.lastUpdated
&& lhs.icon == rhs.icon
}
}
extension PluginDirectoryEntry: Codable {
private enum CodingKeys: String, CodingKey {
case name
case slug
case version
case lastUpdated = "last_updated"
case icons
case author
case rating
case banners
case sections
}
private enum BannersKeys: String, CodingKey {
case high
case low
}
private enum SectionKeys: String, CodingKey {
case description
case installation
case faq
case changelog
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let decodedName = try container.decode(String.self, forKey: .name)
name = decodedName.wpkit_stringByDecodingXMLCharacters()
slug = try container.decode(String.self, forKey: .slug)
version = try? container.decode(String.self, forKey: .version)
lastUpdated = try? container.decode(Date.self, forKey: .lastUpdated)
rating = try container.decode(Double.self, forKey: .rating)
let icons = try? container.decodeIfPresent([String: String].self, forKey: .icons)
icon = icons?["2x"].flatMap({ (s) -> URL? in
URL(string: s)
})
// If there's no hi-res version of the banner, the API returns `high: false`, instead of something more logical,
// like an empty string or `null`, hence the dance below.
let banners = try? container.nestedContainer(keyedBy: BannersKeys.self, forKey: .banners)
if let highRes = try? banners?.decodeIfPresent(String.self, forKey: .high) {
banner = URL(string: highRes)
} else if let lowRes = try? banners?.decodeIfPresent(String.self, forKey: .low) {
banner = URL(string: lowRes)
} else {
banner = nil
}
(author, authorURL) = try extractAuthor(container.decode(String.self, forKey: .author))
let sections = try? container.nestedContainer(keyedBy: SectionKeys.self, forKey: .sections)
descriptionHTML = try trimTags(sections?.decodeIfPresent(String.self, forKey: .description))
installationHTML = try trimTags(sections?.decodeIfPresent(String.self, forKey: .installation))
faqHTML = try trimTags(sections?.decodeIfPresent(String.self, forKey: .faq))
let changelog = try sections?.decodeIfPresent(String.self, forKey: .changelog)
changelogHTML = trimTags(trimChangelog(changelog))
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name.wpkit_stringByEncodingXMLCharacters(), forKey: .name)
try container.encode(slug, forKey: .slug)
try container.encodeIfPresent(version, forKey: .version)
try container.encodeIfPresent(lastUpdated, forKey: .lastUpdated)
try container.encode(rating, forKey: .rating)
if icon != nil {
try container.encode(["2x": icon], forKey: .icons)
}
if banner != nil {
try container.encode([BannersKeys.high.rawValue: banner], forKey: .banners)
}
if let url = authorURL {
try container.encode("<a href=\"\(url)\">\(author)</a>", forKey: .author)
} else {
try container.encode(author, forKey: .author)
}
let sections: [String: String] = [SectionKeys.changelog: changelogHTML,
SectionKeys.description: descriptionHTML,
SectionKeys.faq: faqHTML,
SectionKeys.installation: installationHTML].reduce([:]) {
var newValue = $0
if let value = $1.value {
newValue[$1.key.rawValue] = value
}
return newValue
}
try container.encode(sections, forKey: .sections)
}
internal init(responseObject: [String: AnyObject]) throws {
// Data returned by the featured plugins API endpoint is almost exactly in the same format
// as the data from the Plugin Directory, with few fields missing (updateDate, version, etc).
// In order to avoid duplicating almost identical entites, we provide a special initializer
// that `nil`s out those fields.
guard let name = responseObject["name"] as? String,
let slug = responseObject["slug"] as? String,
let authorString = responseObject["author"] as? String,
let rating = responseObject["rating"] as? Double else {
throw PluginServiceRemote.ResponseError.decodingFailure
}
self.name = name
self.slug = slug
self.rating = rating
self.author = extractAuthor(authorString).name
if let icon = (responseObject["icons"]?["2x"] as? String).flatMap({ URL(string: $0) }) {
self.icon = icon
} else {
self.icon = (responseObject["icons"]?["1x"] as? String).flatMap { URL(string: $0) }
}
self.authorURL = nil
self.version = nil
self.lastUpdated = nil
self.banner = nil
self.descriptionHTML = nil
self.installationHTML = nil
self.faqHTML = nil
self.changelogHTML = nil
}
}
// Since the WPOrg API returns `author` as a HTML string (or freeform text), we need to get ugly and parse out the important bits out of it ourselves.
// Using the built-in NSAttributedString API for it is too slow — it's required to run on main thread and it calls out to WebKit APIs,
// making the context switches excessively expensive when trying to display a list of plugins.
typealias Author = (name: String, link: URL?)
internal func extractAuthor(_ author: String) -> Author {
// Because the `author` field is so free-form, there's cases of it being
// * regular string ("Gutenberg")
// * URL ("https://wordpress.org/plugins/gutenberg/#reviews&arg=1")
// * HTML link ("<a href="https://wordpress.org/plugins/gutenberg/#reviews">Gutenberg</a>"
// but also fun things like
// * malformed HTML: "<a href="">Gutenberg</a>".
// To save ourselves a headache of trying to support all those edge-cases when parsing out the
// user-facing name, let's just employ honest to god XMLParser and be done with it.
// (h/t @koke for suggesting and writing the XMLParser approach)
guard let data = author
.replacingOccurrences(of: "&", with: "&") // can't have naked "&" in XML, but they're valid in URLs.
.data(using: .utf8) else {
return (author, nil)
}
let parser = XMLParser(data: data)
let delegate = AuthorParser()
parser.delegate = delegate
guard parser.parse() else {
if let url = URL(string: author),
url.scheme != nil {
return (author, url)
} else {
return (author, nil)
}
}
return (delegate.author, delegate.url)
}
internal func extractHTMLText(_ text: String?) -> NSAttributedString? {
guard Thread.isMainThread,
let data = text?.data(using: .utf16),
let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
return nil
}
return attributedString
}
internal func trimTags(_ htmlString: String?) -> String? {
// Because the HTML we get from backend can contain literally anything, we need to set some limits as to what we won't even try to parse.
let tagsToRemove = ["script", "iframe"]
guard var html = htmlString else { return nil }
for tag in tagsToRemove {
let openingTag = "<\(tag)"
let closingTag = "/\(tag)>"
if let openingRange = html.range(of: openingTag),
let closingRange = html.range(of: closingTag) {
let rangeToRemove = openingRange.lowerBound..<closingRange.upperBound
html.removeSubrange(rangeToRemove)
}
}
return html
}
internal func trimChangelog(_ changelog: String?) -> String? {
// The changelog that some plugins return is HUGE — Gutenberg as of 2.0 for example returns over 50KiB of text and 1000s of lines,
// Akismet has changelog going back to 2009, etc — there isn't any backend-enforced limit, but thankfully there is backend-enforced structure.
// Showing more than last versions rel-notes seems somewhat poinless (and unlike how, e.g. App Store works), so we trim it to just the
// latest version here. If the user wants to see the whole thing, they can open the plugin's page in WPOrg directory in a browser.
guard let log = changelog else { return nil }
guard let firstOccurence = log.range(of: "<h4>") else {
// Each "version" in the changelog is delineated by the version number wrapped in `<h4>`.
// If the payload doesn't follow the format we're expecting, let's not trim it and return what we received.
return log
}
let rangeAfterFirstOccurence = firstOccurence.upperBound ..< log.endIndex
guard let secondOccurence = log.range(of: "<h4>", range: rangeAfterFirstOccurence) else {
// Same as above. If the data doesn't the format we're expecting, bail.
return log
}
return String(log[log.startIndex ..< secondOccurence.lowerBound])
}
private final class AuthorParser: NSObject, XMLParserDelegate {
var author = ""
var url: URL?
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String: String] = [:]) {
guard elementName == "a",
let href = attributeDict["href"] else {
return
}
url = URL(string: href)
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
author.append(string)
}
}