Skip to content

Commit

Permalink
gadgets-sync: support wikis with non-local interwiki mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Sep 7, 2024
1 parent 3721876 commit 5c8bd46
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions gadgets-sync/gadgets-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ async function getConfig(): Promise<Config> {
}

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<string, string>) {
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) {
Expand All @@ -50,7 +54,7 @@ function getHumanLink(link: string) {
}
return '[[' + link.replace('/-/raw/', '/-/blob/') + ']]'
}
function getHistoryLink(link: string) {
function getHistoryLink(link: string, interWikis: Record<string, string>) {
if (link.startsWith('github:')) {
const [org, repo, branch, path] = parseGithubLink(link)
return `https://github.com/${org}/${repo}/commits/${branch}/${path}`
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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, '')
Expand Down Expand Up @@ -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])` : ''))
Expand Down

0 comments on commit 5c8bd46

Please sign in to comment.