Module:Official website/sandbox: Difference between revisions

Content deleted Content added
fix dumb copy-paste mistake
update to use new mw.wikibase API changes
 
(13 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 76:
return websites[1].mainsnak.datavalue.value
end)
local lang = quickPcall(function()
local langItemId = websites[1].qualifiers.P407[1].datavalue.value.id
if langItemId ~= 'Q1860' then -- skip English
local langItem = mw.wikibase.getEntity(langItemId)
local label = langItem:getLabel('en')
local iso639_3 = langItem:getBestStatements('P220')[1].mainsnak.datavalue.value
return {
label = label,
code = 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 langNameqid 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 result
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] = string.format(
'<span class="languageicon">(in %s)</span>',
options.lang.label
)
end
return table.concat(ret, ' ')
end
 
-- Render the tracking categoriescategory.
local function renderTrackingCategoriesrenderTrackingCategory(url, wikidataurl, lang)
if mw.title.getCurrentTitle().namespace ~= 0 then
return ''
end
local categories = {}category
 
-- Official website categories
if not url and not wikidataurl then
categories[#categories + 1]category = 'Official website missing URL'
elseif not url and wikidataurl then
return {''
-- do nothing
elseif url and wikidataurl then
if url:gsub('/%s*$', '') ~= wikidataurl:gsub('/%s*$', '') then
categories[#categories + 1]category = 'Official website different in Wikidata and Wikipedia'
end
else
categories[#categories + 1]category = 'Official website not in Wikidata'
end
 
-- Foreign language categories
if lang.code ~= "en" and lang.code ~= "eng" then
local langName = quickPcall(function ()
return mw.getCurrentFrame():expandTemplate{
title = 'ISO 639 name ' .. lang.code
}
end)
if langName then
categories[#categories + 1] = string.format(
"Articles with %s-language external links",
langName
)
end
end
 
for i, cat in ipairs(categories) do
categories[i] = string.format('[[Category:%s]]', cat)
end
categories[i] return =category and string.format('[[Category:%s]]', catcategory) or ''
return table.concat(categories)
end
 
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 lang = args.lang or wikidata.lang
local formattedUrl = renderUrl{
url = url,
wikidataurl = wikidataurl,
display = args[2] or args.name or 'Official website',
mobile = args.mobile,
format = args.format,
langmobile = langargs.mobile
}
return formattedUrl .. renderTrackingCategoriesrenderTrackingCategory(url, wikidata.url, langwikidataurl)
end