2023-03-29 17:21:20 +02:00
|
|
|
import Combine from "../../Base/Combine"
|
|
|
|
import Wikidata from "../../../Logic/Web/Wikidata"
|
|
|
|
import WikidataSearchBox from "../../Wikipedia/WikidataSearchBox"
|
2023-03-29 17:56:42 +02:00
|
|
|
import { Validator } from "../Validator"
|
2024-02-25 18:17:52 +01:00
|
|
|
import { Translation } from "../../i18n/Translation"
|
|
|
|
import Translations from "../../i18n/Translations"
|
2023-03-29 17:21:20 +02:00
|
|
|
|
|
|
|
export default class WikidataValidator extends Validator {
|
|
|
|
constructor() {
|
2023-04-16 03:42:26 +02:00
|
|
|
super("wikidata", new Combine(["A wikidata identifier, e.g. Q42.", WikidataSearchBox.docs]))
|
2023-03-29 17:21:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public isValid(str): boolean {
|
|
|
|
if (str === undefined) {
|
|
|
|
return false
|
|
|
|
}
|
2024-02-25 18:17:52 +01:00
|
|
|
if (str.length == 1) {
|
2023-03-29 17:21:20 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !str.split(";").some((str) => Wikidata.ExtractKey(str) === undefined)
|
|
|
|
}
|
|
|
|
|
2024-02-25 18:17:52 +01:00
|
|
|
getFeedback(s: string, _?: () => string): Translation | undefined {
|
|
|
|
const t = Translations.t.validation.wikidata
|
|
|
|
if (s === "") {
|
|
|
|
return t.empty
|
|
|
|
}
|
|
|
|
if (!s.match(/(Q[0-9]+)(;Q[0-9]+)*/)) {
|
|
|
|
return t.startsWithQ
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2023-03-29 17:21:20 +02:00
|
|
|
public reformat(str) {
|
|
|
|
if (str === undefined) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
let out = str
|
|
|
|
.split(";")
|
|
|
|
.map((str) => Wikidata.ExtractKey(str))
|
|
|
|
.join("; ")
|
|
|
|
if (str.endsWith(";")) {
|
|
|
|
out = out + ";"
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|