Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions InternetArchiveKit/InternetArchiveQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
booleanOperator: QueryBooleanOperator = .and
) {
let params: [QueryClause] = clauses.compactMap {
(param: (key: String, value: String)) -> QueryClause? in

Check warning on line 71 in InternetArchiveKit/InternetArchiveQuery.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Closure parameters should be on the same line as opening brace (closure_parameter_position)
return QueryClause(field: param.key, value: param.value)
}
self.init(clauses: params, booleanOperator: booleanOperator)
Expand All @@ -85,7 +85,7 @@

public enum QueryBooleanOperator: String, Sendable {
case and = "AND"
case or = "OR"

Check warning on line 88 in InternetArchiveKit/InternetArchiveQuery.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Enum element name 'or' should be between 3 and 40 characters long (identifier_name)
}

/**
Expand Down Expand Up @@ -127,10 +127,14 @@
return "\(booleanOperator.rawValue)\(fieldKey)\(finalValue)"
}

/// Lucene syntax characters that get backslash-escaped in values. `*` and
/// `?` are deliberately absent so wildcard searches keep working.
/// Lucene syntax characters that get backslash-escaped in non-exactMatch
/// values. The query operators `+ - & | "` and the wildcards `* ?` are
/// deliberately absent: archive.org's Elasticsearch backend rejects the
/// escaped forms of the operators (`\-`, `\+`, `\&`, `\|`, `\"` come back
/// as a backend error) and they don't need escaping mid-value there, while
/// `* ?` stay raw so wildcard searches keep working.
private static let luceneSpecialCharacters: Set<Character> = [
"+", "-", "&", "|", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", ":", "\\", "/",
"(", ")", "{", "}", "[", "]", "^", "~", ":", "\\", "/", "!",

Check warning on line 137 in InternetArchiveKit/InternetArchiveQuery.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Collection literals should not have trailing commas (trailing_comma)
]

static func escapingLuceneSyntax(in value: String) -> String {
Expand Down
15 changes: 12 additions & 3 deletions InternetArchiveKitTests/InternetArchiveQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,21 @@ class InternetArchiveQueryTests: XCTestCase {
XCTAssertEqual(clause.asURLString, "venue:(foo fest \\[bar stage\\])")
}

func testQueryClauseEscapesEveryLuceneSpecialCharacter() {
func testQueryClauseEscapesParserSpecialCharacters() {
let clause: InternetArchive.QueryClause = InternetArchive.QueryClause(
field: "foo", value: "a+b-c&d|e!f(g)h{i}j[k]l^m\"n~o:p\\q/r")
field: "foo", value: "a!b(c)d{e}f[g]h^i~j:k\\l/m")
XCTAssertEqual(
clause.asURLString,
"foo:(a\\+b\\-c\\&d\\|e\\!f\\(g\\)h\\{i\\}j\\[k\\]l\\^m\\\"n\\~o\\:p\\\\q\\/r)")
"foo:(a\\!b\\(c\\)d\\{e\\}f\\[g\\]h\\^i\\~j\\:k\\\\l\\/m)")
}

/// archive.org's Elasticsearch backend errors on the escaped forms of these
/// operator characters (`\-`, `\+`, `\&`, `\|`, `\"`), so they pass through
/// unescaped — verified live against advancedsearch.php.
func testQueryClauseLeavesElasticsearchOperatorsRaw() {
let clause: InternetArchive.QueryClause = InternetArchive.QueryClause(
field: "foo", value: "a+b-c&d|e\"f")
XCTAssertEqual(clause.asURLString, "foo:(a+b-c&d|e\"f)")
}

func testQueryClauseKeepsWildcards() {
Expand Down
Loading