From 5c8bd466b29cf4265367fb9569ee2734bf0ccf89 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Sat, 7 Sep 2024 19:20:25 +0530 Subject: [PATCH] gadgets-sync: support wikis with non-local interwiki mappings --- gadgets-sync/gadgets-sync.ts | 37 +++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/gadgets-sync/gadgets-sync.ts b/gadgets-sync/gadgets-sync.ts index fc2225e..0820fa0 100644 --- a/gadgets-sync/gadgets-sync.ts +++ b/gadgets-sync/gadgets-sync.ts @@ -31,16 +31,20 @@ async function getConfig(): Promise { } function parseGithubLink(link: string) { - const [_, parts] = link.split(':') - const [org, repo, branch, path] = parts.split('/', 4) - return [org, repo, branch, path] + const [_, ...parts] = link.split(':') + const [org, repo, branch, ...path] = parts.join(':').split('/') + return [org, repo, branch, path.join('/')] } -function getRawLink(link: string) { +function getRawLink(link: string, interWikis: Record) { if (link.startsWith('github:')) { const [org, repo, branch, path] = parseGithubLink(link) return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${path}` } + const [prefix, ...page] = link.split(':') + if (interWikis[prefix]) { + return interWikis[prefix].replace('$1', page.join(':')) + '?action=raw' + } return `https://en.wikipedia.org/w/index.php?title=${link}&action=raw` } function getHumanLink(link: string) { @@ -50,7 +54,7 @@ function getHumanLink(link: string) { } return '[[' + link.replace('/-/raw/', '/-/blob/') + ']]' } -function getHistoryLink(link: string) { +function getHistoryLink(link: string, interWikis: Record) { if (link.startsWith('github:')) { const [org, repo, branch, path] = parseGithubLink(link) return `https://github.com/${org}/${repo}/commits/${branch}/${path}` @@ -60,13 +64,28 @@ function getHistoryLink(link: string) { } else if (link.startsWith('toolforge:')) { return '' } else { + const [prefix, ...page] = link.split(':') + if (interWikis[prefix]) { + return interWikis[prefix].replace('$1', page.join(':')) + '?action=history' + } return `https://en.wikipedia.org/w/index.php?title=${link}&action=history` } } +async function getInterwikiMap() { + const interwikis = (await bot.query({ + "meta": "siteinfo", + "siprop": "interwikimap" + })).query.interwikimap + return Object.fromEntries(interwikis.map(iw => [iw.prefix, iw.url])) +} + (async function () { await bot.getTokensAndSiteInfo() - const allConfigs = await getConfig() + const [interWikis, allConfigs] = await Promise.all([ + getInterwikiMap(), + getConfig() + ]) for (const conf of allConfigs.list) { // Validations const talkTitle = bot.Title.newFromText(conf.talkPage) @@ -80,7 +99,7 @@ function getHistoryLink(link: string) { let localCode, remoteCode try { let remote = await bot.rawRequest({ - url: getRawLink(conf.source), + url: getRawLink(conf.source, interWikis), timeout: 5000 }) remoteCode = remote.data.trim() @@ -94,7 +113,7 @@ function getHistoryLink(link: string) { try { let local = await bot.rawRequest({ - url: getRawLink(conf.page), + url: getRawLink(conf.page, interWikis), timeout: 5000 }) localCode = local.data.replace(substitutedHeader, '') @@ -129,7 +148,7 @@ function getHistoryLink(link: string) { const isMatchingTalk = new bot.Page(conf.page).toText() === new bot.Title(conf.talkPage).getSubjectPage().toText() const header = `Sync request ${date}` + (isMatchingTalk ? '' : ` for ${conf.page}`) - let histLink = getHistoryLink(conf.source) + let histLink = getHistoryLink(conf.source, interWikis) const body = REQUEST_BODY .replaceAll('LOCAL', conf.page) .replaceAll('REMOTE', getHumanLink(conf.source) + (histLink ? ` ([${histLink} hist])` : ''))