Module:Official website/sandbox: Difference between revisions

Content deleted Content added
fix frame error; ignore lang code from Wikidata if it's English
update to use new mw.wikibase API changes
 
(18 intermediate revisions by 4 users not shown)
Line 40:
end
 
-- Fetches the official website URL/language from Wikidata.
local fetchWikidatafetchWikidataUrl
fetchWikidatafetchWikidataUrl = function()
-- Get objects for all official sites on Wikidata.
local websites = quickPcall(function ()
return mw.wikibase.getEntityObjectgetAllStatements()mw.claimswikibase.getEntityIdForCurrentPage(), 'P856')
end)
 
Line 71:
return e1
end
return ws1._index < ws2._index
end)
local url = quickPcall(function ()
return websites[1].mainsnak.datavalue.value
end)
local lang = quickPcall(function()
local langItemId = websites[1].qualifiers.P407.datavalue.value.id
local langItem = mw.wikibase.getEntity(langItemId)
local iso639_3 = langItem.claims.P220.mainsnak.datavalue.value
if iso639_3 ~= "eng" then
return iso639_3
end
end)
 
local ret = {
url = url,
lang = lang
}
 
-- Cache the result so that we only do the heavy lifting once per #invoke.
fetchWikidatafetchWikidataUrl = function ()
return returl
end
 
return returl
end
 
-- Render the URL link, plus other visible output.
local function renderUrl(options)
if not options.url and not options.wikidataurl then
local qid = mw.wikibase.getEntityIdForCurrentPage()
returnlocal result = '<strong class="error">' ..
'No URL found. Please specify a URL here or add one to Wikidata.' ..
'</strong>'
if qid then
result = result.. ' [[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/' .. qid .. '#P856|Edit this at Wikidata]]'
end
return iso639_3result
end
local ret = {}
ret[#ret + 1] = string.format(
'<span class="official-website">%s</span>',
makeUrl(options.url or options.wikidataurl, options.display)
)
if options.wikidataurl and not options.url then
local qid = mw.wikibase.getEntityIdForCurrentPage()
if qid then
ret[#ret + 1] = '[[File:OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt=Edit this at Wikidata |link=https://www.wikidata.org/wiki/' .. qid .. '#P856|Edit this at Wikidata]]'
end
end)
if options.format == 'flash' then
ret[#ret + 1] = mw.getCurrentFrame():expandTemplate{
title = 'Link noteColor',
args = {note ='#505050', '(Requires [[Adobe Flash Player]])'}
}
end
if options.mobile then
ret[#ret + 1] = '(' .. makeUrl(options.mobile, 'Mobile') .. ')'
end
if options.lang then
ret[#ret + 1] = ' ' .. mw.getCurrentFrame():expandTemplate{
title = 'Link language',
args = { options.lang }
}
end
return table.concat(ret, ' ')
Line 149 ⟶ 141:
 
function p._main(args)
local wikidataurl = fetchWikidata()args[1] or args.___URL or args.url
local wikidataurl = fetchWikidataUrl()
local url = args[1] or args.___URL or args.url or wikidata.url
local formattedUrl = renderUrl{
url = url,
wikidataurl = wikidataurl,
display = args[2] or args.name or 'Official website',
mobile = args.mobile,
format = args.format,
langmobile = args.lang or wikidata.langmobile
}
return formattedUrl .. renderTrackingCategory(url, wikidata.urlwikidataurl)
end