Module:Official website: Difference between revisions

Content deleted Content added
wrap pcall to make it easier to use, seeing as we will probably be calling it multiple times and we don't need the error messages
add code to detect the rank of URLs from Wikidata
Line 3:
local p = {}
 
-- Makes a category from a string s.
local function makeCategory(s)
return string.format('[[Category:%s]]', s)
end
 
-- Wrapper for pcall which returns nil on failure.
local function quickPcall(func)
local success, result = pcall(func)
Line 14 ⟶ 16:
end
 
-- Fetches the official website URL from Wikidata.
local fetchWikidataUrl
fetchWikidataUrl = function()
-- Get objects for all official sites on Wikidata.
local websites = quickPcall(function ()
return mw.wikibase.getEntityObject().claims.P856[1].mainsnak.datavalue.value
end)
websites = websites or {}
-- Find the website object with the highest rank, or the first one if there
-- is a tie. Then get the URL from that object.
local ranks = {}
for i, website in ipairs(websites) do
if website.rank and not ranks[website.rank] then
ranks[website.rank] = i
end
end
local url = quickPcall(function ()
local website = websites[ranks.preferred or ranks.normal or ranks.deprecated]
return mw.wikibase.getEntityObject().claims.P856[1].mainsnak.datavalue.value
return website.mainsnak.datavalue.value
end)
-- Cache the result so that we only do the heavy lifting once per #invoke.
fetchWikidataUrl = function ()
return url
end
 
return url
end
 
-- Render the URL link, plus other visible output.
local function formatUrlrenderUrl(options)
local ret = {}
ret[#ret + 1] = string.format(
Line 43 ⟶ 65:
end
 
-- Render the tracking category.
local function renderTrackingCategory(url)
if mw.title.getCurrentTitle().namespace ~= 0 then
Line 66 ⟶ 89:
makeCategory('Official website missing URL')
end
local formattedUrl = formatUrlrenderUrl{
url = url,
display = args[2] or args.name or 'Official website',