Module:Official website/sandbox: Difference between revisions

Content deleted Content added
fixed logic for 'lang' arg
update to use new mw.wikibase API changes
 
(8 intermediate revisions by 4 users not shown)
Line 40:
end
 
-- RetrievesFetches the nameofficial ofwebsite a languageURL from a Wikipedia templateWikidata.
local fetchWikidataUrl
local function fetchLabelForLangCode(code)
fetchWikidataUrl = function()
return quickPcall(function ()
return mw.getCurrentFrame():expandTemplate{
title = 'ISO 639 name ' .. code
}
end)
end
 
-- Fetches the official website data from Wikidata
local fetchWikidata
fetchWikidata = function()
-- Get objects for all official sites on Wikidata.
local websites = quickPcall(function ()
return mw.wikibase.getEntityObjectgetAllStatements()mw.claimswikibase.getEntityIdForCurrentPage(), 'P856')
end)
 
Line 81 ⟶ 72:
end
return ws1._index < ws2._index
end)
local url = quickPcall(function ()
return websites[1].mainsnak.datavalue.value
end)
 
-- Cache the result so that we only do the heavy lifting once per #invoke.
fetchWikidatafetchWikidataUrl = function ()
return websites[1]url
end
 
return websites[1]url
end
 
-- Retrieves the URL of the website
local function fetchWikidataUrl()
return quickPcall(function ()
return fetchWikidata().mainsnak.datavalue.value
end)
end
 
-- Retrieves the name of the language for the website
local function fetchWikidataLang()
return quickPcall(function ()
local website = fetchWikidata()
if not isEnglish(website) then
local id = website.qualifiers.P407[1].datavalue.value.id
local lang = mw.wikibase.getEntity(id)
local iso639_3 = quickPcall(function ()
return lang:getBestStatements('P220')[1].mainsnak.datavalue.value
end)
local ietf = quickPcall(function ()
return lang:getBestStatements('P305')[1].mainsnak.datavalue.value
end)
 
local name = fetchLabelForLangCode(iso639_3)
name = name or fetchLabelForLangCode(ietf)
local label = name or lang:getLabel('en')
 
return {
label = label,
name = name,
iso639 = iso639_3,
ietf = ietf
}
end
end)
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()
return '<strong class="error">' ..
local 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 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 cats = {}category
 
-- Official website categories
if not url and not wikidataurl then
cats[#cats + 1]category = 'Official website missing URL'
elseif not url and not wikidataurl then
return ''
cats[#cats + 1] = 'Official website not in Wikidata'
elseif url and wikidataurl then
if url:gsub('/%s*$', '') ~= wikidataurl:gsub('/%s*$', '') then
cats[#cats + 1]category = 'Official website different in Wikidata and Wikipedia'
end
else
category = 'Official website not in Wikidata'
end
return category and string.format('[[Category:%s]]', category) or ''
 
-- Foreign language categories
if lang.name and lang.iso639 ~= "en" and lang.iso639 ~= "eng" then
cats[#cats + 1] = string.format(
"Articles with %s-language external links",
lang.name
)
end
 
for i, cat in ipairs(cats) do
cats[i] = string.format('[[Category:%s]]', cat)
end
return table.concat(cats)
end
 
function p._main(args)
local url = args[1] or args.___URL or args.url
local langwikidataurl = args.langfetchWikidataUrl()
 
-- only use Wikidata if there's no URL arg
if not url then
url = fetchWikidataUrl()
lang = lang or fetchWikidataLang()
end
if type(lang) == "string" then
lang = {
name = fetchLabelForLangCode(lang),
iso639 = lang
}
lang.label = lang.name
end
 
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, fetchWikidataUrl(), langwikidataurl)
end