Skip to content

Commit

Permalink
sitelinks_helpers: fix support for multi-part language codes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlath committed Aug 24, 2022
1 parent f9e13c7 commit ccb2bae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/helpers/sitelinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const getSitelinkData = site => {
lang = 'en'
project = key = 'commons'
} else {
// Support multi-parts language codes, such as be_x_old
lang = lang.replace(/-/g, '_')
key = `${lang}${project}`.replace('wikipedia', 'wiki')
}
return { lang, project, key, title, url }
Expand All @@ -61,7 +63,7 @@ const getSitelinkData = site => {
const specialProjectName = specialSites[key]
if (specialProjectName) return { lang: 'en', project: specialProjectName, key }

const [ lang, projectSuffix, rest ] = key.split('wik')
let [ lang, projectSuffix, rest ] = key.split('wik')

// Detecting cases like 'frwikiwiki' that would return [ 'fr', 'i', 'i' ]
if (rest != null) throw new Error(`invalid sitelink key: ${key}`)
Expand All @@ -70,6 +72,9 @@ const getSitelinkData = site => {
throw new Error(`sitelink lang not found: ${lang}. Updating wikibase-sdk to a more recent version might fix the issue.`)
}

// Support keys such as be_x_oldwiki, which refers to be-x-old.wikipedia.org
lang = lang.replace(/_/g, '-')

const project = projectsBySuffix[projectSuffix]
if (!project) throw new Error(`sitelink project not found: ${project}`)

Expand Down
17 changes: 17 additions & 0 deletions test/sitelinks_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ describe('getSitelinkUrl', () => {
getSitelinkUrl.bind(null, 'frperlinpinpin', 'Lyon').should.throw()
getSitelinkUrl.bind(null, 'frwikiwiki', 'Lyon').should.throw()
})

it('should support multi-part language codes', () => {
getSitelinkUrl({ site: 'zh_classicalwiki', title: '編訂名詞館' })
.should.startWith('https://zh-classical.wikipedia.org/')
getSitelinkUrl({ site: 'be_x_oldwiki', title: 'Віктор_Юго' })
.should.startWith('https://be-x-old.wikipedia.org/')
})
})

describe('getSitelinkData', () => {
Expand Down Expand Up @@ -132,11 +139,20 @@ describe('getSitelinkData', () => {
getSitelinkData('https://de.wikipedia.org/wiki/The_Score_%282001%29').project.should.equal('wikipedia')
getSitelinkData('https://de.wikipedia.org/wiki/The_Score_%282001%29').key.should.equal('dewiki')
})

it('should support multi-part language codes', () => {
const data = getSitelinkData('https://be-x-old.wikipedia.org/wiki/Беларускі_клясычны_правапіс')
data.title.should.equal('Беларускі_клясычны_правапіс')
data.lang.should.equal('be_x_old')
data.project.should.equal('wikipedia')
data.key.should.equal('be_x_oldwiki')
})
})

describe('isSitelinkKey', () => {
it('should return true for valid sitelink keys', () => {
isSitelinkKey('frwiki').should.be.true()
isSitelinkKey('be_x_oldwiki').should.be.true()
isSitelinkKey('commonswiki').should.be.true()
isSitelinkKey('wikidatawiki').should.be.true()
isSitelinkKey('commons').should.be.false()
Expand All @@ -148,5 +164,6 @@ describe('isSitelinkKey', () => {
isSitelinkKey('frwikilinpinpin').should.be.false()
isSitelinkKey('imaginarylangwiki').should.be.false()
isSitelinkKey('frwikiwiki').should.be.false()
isSitelinkKey('be-x-oldwiki').should.be.false()
})
})

0 comments on commit ccb2bae

Please sign in to comment.