From ccb2baee822121b9507386242b919d27c2291cf9 Mon Sep 17 00:00:00 2001 From: maxlath Date: Wed, 24 Aug 2022 18:41:58 +0200 Subject: [PATCH] sitelinks_helpers: fix support for multi-part language codes --- lib/helpers/sitelinks.js | 7 ++++++- test/sitelinks_helpers.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/helpers/sitelinks.js b/lib/helpers/sitelinks.js index 873f1ce1..335d5227 100644 --- a/lib/helpers/sitelinks.js +++ b/lib/helpers/sitelinks.js @@ -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 } @@ -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}`) @@ -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}`) diff --git a/test/sitelinks_helpers.js b/test/sitelinks_helpers.js index 9885cf80..50076141 100644 --- a/test/sitelinks_helpers.js +++ b/test/sitelinks_helpers.js @@ -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', () => { @@ -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() @@ -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() }) })