<commit_before>public struct WikidataAPI {
    public static let host = "www.wikidata.org"
    public static let path = "/w/api.php"
    public static let scheme = "https"

    public static var urlWithoutAPIPath: URL? {
        var components = URLComponents()
        components.scheme = scheme
        components.host = host
        return components.url
    }
}

struct WikidataAPIResult: Decodable {
    struct Error: Decodable {
        let code, info: String?
    }
    let error: Error?
    let success: Int?
}

extension WikidataAPIResult.Error: LocalizedError {
    var errorDescription: String? {
        return info
    }
}

extension WikidataAPIResult {
    var succeeded: Bool {
        return success == 1
    }
}

enum WikidataPublishingError: LocalizedError {
    case invalidArticleURL
    case apiResultNotParsedCorrectly
    case blacklistedLanguage
    case unknown
}

@objc public final class WikidataDescriptionEditingController: NSObject {
    weak var viewContext: NSManagedObjectContext?
    private let semaphore = DispatchSemaphore(value: 1)

    @objc public init(with viewContext: NSManagedObjectContext) {
        self.viewContext = viewContext
    }

    private let BlacklistedLanguagesKey = "WMFWikidataDescriptionEditingBlacklistedLanguagesKey"
    private var blacklistedLanguages: NSSet {
        assert(Thread.isMainThread)
        semaphore.wait()
        defer {
            semaphore.signal()
        }
        let fallback = NSSet(set: ["en"])
        guard
            let viewContext = viewContext,
            let keyValue = viewContext.wmf_keyValue(forKey: BlacklistedLanguagesKey),
            let value = keyValue.value as? NSSet else {
                assertionFailure("Failed to retrieve WMFKeyValue for key \(BlacklistedLanguagesKey), returning fallback object \(fallback)")
                return fallback
        }
        return value
    }

    @objc public func setBlacklistedLanguages(_ blacklistedLanguagesFromRemoteConfig: Array<String>) {
        assert(Thread.isMainThread)
        assert(viewContext != nil)
        semaphore.wait()
        defer {
            semaphore.signal()
        }
        let blacklistedLanguages = NSSet(array: blacklistedLanguagesFromRemoteConfig)
        viewContext?.wmf_setValue(blacklistedLanguages, forKey: BlacklistedLanguagesKey)
    }

    public func isBlacklisted(_ languageCode: String) -> Bool {
        guard blacklistedLanguages.count > 0 else {
            return false
        }
        return blacklistedLanguages.contains(languageCode)
    }

    @objc(publishNewWikidataDescription:forArticleURL:completion:)<commit_msg>Revert "add semaphore to protect access"<commit_after>public struct WikidataAPI {
    public static let host = "www.wikidata.org"
    public static let path = "/w/api.php"
    public static let scheme = "https"

    public static var urlWithoutAPIPath: URL? {
        var components = URLComponents()
        components.scheme = scheme
        components.host = host
        return components.url
    }
}

struct WikidataAPIResult: Decodable {
    struct Error: Decodable {
        let code, info: String?
    }
    let error: Error?
    let success: Int?
}

extension WikidataAPIResult.Error: LocalizedError {
    var errorDescription: String? {
        return info
    }
}

extension WikidataAPIResult {
    var succeeded: Bool {
        return success == 1
    }
}

enum WikidataPublishingError: LocalizedError {
    case invalidArticleURL
    case apiResultNotParsedCorrectly
    case blacklistedLanguage
    case unknown
}

@objc public final class WikidataDescriptionEditingController: NSObject {
    weak var viewContext: NSManagedObjectContext?

    @objc public init(with viewContext: NSManagedObjectContext) {
        self.viewContext = viewContext
    }

    private let BlacklistedLanguagesKey = "WMFWikidataDescriptionEditingBlacklistedLanguagesKey"
    private var blacklistedLanguages: NSSet {
        assert(Thread.isMainThread)
        let fallback = NSSet(set: ["en"])
        guard
            let viewContext = viewContext,
            let keyValue = viewContext.wmf_keyValue(forKey: BlacklistedLanguagesKey),
            let value = keyValue.value as? NSSet else {
                assertionFailure("Failed to retrieve WMFKeyValue for key \(BlacklistedLanguagesKey), returning fallback object \(fallback)")
                return fallback
        }
        return value
    }

    @objc public func setBlacklistedLanguages(_ blacklistedLanguagesFromRemoteConfig: Array<String>) {
        assert(Thread.isMainThread)
        assert(viewContext != nil)
        let blacklistedLanguages = NSSet(array: blacklistedLanguagesFromRemoteConfig)
        viewContext?.wmf_setValue(blacklistedLanguages, forKey: BlacklistedLanguagesKey)
    }

    public func isBlacklisted(_ languageCode: String) -> Bool {
        guard blacklistedLanguages.count > 0 else {
            return false
        }
        return blacklistedLanguages.contains(languageCode)
    }

    @objc(publishNewWikidataDescription:forArticleURL:completion:)
{'fraction_non_alphanumeric': 0.06791707798617966, 'fraction_numerical': 0.0009871668311944718, 'mean_word_length': 2.9210526315789473, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4889618', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1365, 'n_words': 345}