forked from MapComplete/MapComplete
Full code cleanup
This commit is contained in:
parent
8e6ee8c87f
commit
bd21212eba
246 changed files with 19418 additions and 11729 deletions
|
@ -55,8 +55,8 @@ export class QueryParameters {
|
|||
return source;
|
||||
}
|
||||
|
||||
public static GetBooleanQueryParameter(key: string, deflt: string, documentation?: string): UIEventSource<boolean>{
|
||||
return QueryParameters.GetQueryParameter(key, deflt, documentation).map(str => str === "true", [], b => ""+b)
|
||||
public static GetBooleanQueryParameter(key: string, deflt: string, documentation?: string): UIEventSource<boolean> {
|
||||
return QueryParameters.GetQueryParameter(key, deflt, documentation).map(str => str === "true", [], b => "" + b)
|
||||
}
|
||||
|
||||
public static GenerateQueryParameterDocs(): string {
|
||||
|
|
|
@ -64,11 +64,11 @@ export class WikidataResponse {
|
|||
}
|
||||
|
||||
static extractClaims(claimsJson: any): Map<string, Set<string>> {
|
||||
|
||||
const simplified = wds.simplify.claims(claimsJson, {
|
||||
|
||||
const simplified = wds.simplify.claims(claimsJson, {
|
||||
timeConverter: 'simple-day'
|
||||
})
|
||||
|
||||
|
||||
const claims = new Map<string, Set<string>>();
|
||||
for (const claimId in simplified) {
|
||||
const claimsList: any[] = simplified[claimId]
|
||||
|
@ -98,11 +98,11 @@ export class WikidataLexeme {
|
|||
for (const sense of json.senses) {
|
||||
const glosses = sense.glosses
|
||||
for (const language in glosses) {
|
||||
let previousSenses = this.senses.get(language)
|
||||
if(previousSenses === undefined){
|
||||
let previousSenses = this.senses.get(language)
|
||||
if (previousSenses === undefined) {
|
||||
previousSenses = ""
|
||||
}else{
|
||||
previousSenses = previousSenses+"; "
|
||||
} else {
|
||||
previousSenses = previousSenses + "; "
|
||||
}
|
||||
this.senses.set(language, previousSenses + glosses[language].value ?? "")
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ export default class Wikidata {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static async searchAndFetch(
|
||||
search: string,
|
||||
options?: WikidataSearchoptions
|
||||
|
@ -248,7 +248,7 @@ export default class Wikidata {
|
|||
for (const identifierPrefix of Wikidata._identifierPrefixes) {
|
||||
if (value.startsWith(identifierPrefix)) {
|
||||
const trimmed = value.substring(identifierPrefix.length);
|
||||
if(trimmed === ""){
|
||||
if (trimmed === "") {
|
||||
return undefined
|
||||
}
|
||||
const n = Number(trimmed)
|
||||
|
@ -266,14 +266,14 @@ export default class Wikidata {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
public static IdToArticle(id: string){
|
||||
if(id.startsWith("Q")){
|
||||
return "https://wikidata.org/wiki/"+id
|
||||
public static IdToArticle(id: string) {
|
||||
if (id.startsWith("Q")) {
|
||||
return "https://wikidata.org/wiki/" + id
|
||||
}
|
||||
if(id.startsWith("L")){
|
||||
return "https://wikidata.org/wiki/Lexeme:"+id
|
||||
if (id.startsWith("L")) {
|
||||
return "https://wikidata.org/wiki/Lexeme:" + id
|
||||
}
|
||||
throw "Unknown id type: "+id
|
||||
throw "Unknown id type: " + id
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -289,7 +289,7 @@ export default class Wikidata {
|
|||
|
||||
const url = "https://www.wikidata.org/wiki/Special:EntityData/" + id + ".json";
|
||||
const entities = (await Utils.downloadJsonCached(url, 10000)).entities
|
||||
const firstKey = <string> Array.from(Object.keys(entities))[0] // Roundabout way to fetch the entity; it might have been a redirect
|
||||
const firstKey = <string>Array.from(Object.keys(entities))[0] // Roundabout way to fetch the entity; it might have been a redirect
|
||||
const response = entities[firstKey]
|
||||
|
||||
if (id.startsWith("L")) {
|
||||
|
|
|
@ -9,8 +9,8 @@ export default class Wikimedia {
|
|||
* @param continueParameter: if the page indicates that more pages should be loaded, this uses a token to continue. Provided by wikimedia
|
||||
*/
|
||||
public static async GetCategoryContents(categoryName: string,
|
||||
maxLoad = 10,
|
||||
continueParameter: string = undefined): Promise<string[]> {
|
||||
maxLoad = 10,
|
||||
continueParameter: string = undefined): Promise<string[]> {
|
||||
if (categoryName === undefined || categoryName === null || categoryName === "") {
|
||||
return [];
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ export default class Wikipedia {
|
|||
private static readonly classesToRemove = [
|
||||
"shortdescription",
|
||||
"sidebar",
|
||||
"infobox","infobox_v2",
|
||||
"infobox", "infobox_v2",
|
||||
"noprint",
|
||||
"ambox",
|
||||
"mw-editsection",
|
||||
|
@ -22,26 +22,27 @@ export default class Wikipedia {
|
|||
"mw-empty-elt",
|
||||
"hatnote" // Often redirects
|
||||
]
|
||||
|
||||
|
||||
private static readonly idsToRemove = [
|
||||
"sjabloon_zie"
|
||||
]
|
||||
|
||||
private static readonly _cache = new Map<string, UIEventSource<{ success: string } | { error: any }>>()
|
||||
|
||||
|
||||
public static GetArticle(options: {
|
||||
pageName: string,
|
||||
language?: "en" | string}): UIEventSource<{ success: string } | { error: any }>{
|
||||
const key = (options.language ?? "en")+":"+options.pageName
|
||||
language?: "en" | string
|
||||
}): UIEventSource<{ success: string } | { error: any }> {
|
||||
const key = (options.language ?? "en") + ":" + options.pageName
|
||||
const cached = Wikipedia._cache.get(key)
|
||||
if(cached !== undefined){
|
||||
if (cached !== undefined) {
|
||||
return cached
|
||||
}
|
||||
const v = UIEventSource.FromPromiseWithErr(Wikipedia.GetArticleAsync(options))
|
||||
Wikipedia._cache.set(key, v)
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
public static async GetArticleAsync(options: {
|
||||
pageName: string,
|
||||
language?: "en" | string
|
||||
|
@ -57,24 +58,22 @@ export default class Wikipedia {
|
|||
const content = Array.from(div.children)[0]
|
||||
|
||||
for (const forbiddenClass of Wikipedia.classesToRemove) {
|
||||
const toRemove = content.getElementsByClassName(forbiddenClass)
|
||||
const toRemove = content.getElementsByClassName(forbiddenClass)
|
||||
for (const toRemoveElement of Array.from(toRemove)) {
|
||||
toRemoveElement.parentElement?.removeChild(toRemoveElement)
|
||||
}
|
||||
}
|
||||
|
||||
for (const forbiddenId of Wikipedia.idsToRemove) {
|
||||
const toRemove = content.querySelector("#"+forbiddenId)
|
||||
const toRemove = content.querySelector("#" + forbiddenId)
|
||||
toRemove?.parentElement?.removeChild(toRemove)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const links = Array.from(content.getElementsByTagName("a"))
|
||||
|
||||
// Rewrite relative links to absolute links + open them in a new tab
|
||||
links.filter(link => link.getAttribute("href")?.startsWith("/") ?? false).
|
||||
forEach(link => {
|
||||
links.filter(link => link.getAttribute("href")?.startsWith("/") ?? false).forEach(link => {
|
||||
link.target = '_blank'
|
||||
// note: link.getAttribute("href") gets the textual value, link.href is the rewritten version which'll contain the host for relative paths
|
||||
link.href = `https://${language}.wikipedia.org${link.getAttribute("href")}`;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue