Module:Official website
Appearance
| This Lua module is used on approximately 347,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
This module uses the Wikidata property:
official website (P856) (see uses)
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
This module implements multiple templates:
- {{Official website}} -
{{#invoke:Official website|main}} - {{Official URL}} -
{{#invoke:Official website|url}}
Tracking categories
- Category:Official website missing URL (0)
- Category:Official website different in Wikidata and Wikipedia (100,878)
- Category:Official website not in Wikidata (57,168)
See also
local makeUrl = require('Module:URL')._url
local p = {}
-- Wrapper for pcall which returns nil on failure.
local function quickPcall(func)
local success, result = pcall(func)
if success then
return result
end
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
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 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 renderUrl(options)
if not options.url then
return '<strong class="error">' ..
'No URL found. Please specify a URL here or add one to Wikidata.' ..
'</strong>'
end
local ret = {}
ret[#ret + 1] = string.format(
'<span class="official-website">%s</span>',
makeUrl(options.url, options.display)
)
if options.format == 'flash' then
ret[#ret + 1] = mw.getCurrentFrame():expandTemplate{
title = 'Link note',
args = {note = 'Requires [[Adobe Flash player]]'}
}
end
if options.mobile then
ret[#ret + 1] = '(' .. makeUrl(options.mobile, 'Mobile') .. ')'
end
return table.concat(ret, ' ')
end
-- Render the tracking category.
local function renderTrackingCategory(url)
if mw.title.getCurrentTitle().namespace ~= 0 then
return ''
end
local category
if not url then
category = 'Official website missing URL'
elseif fetchWikidataUrl() then
if url and url ~= fetchWikidataUrl() then
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 ''
end
function p._main(args)
local url = args[1] or args.URL or fetchWikidataUrl()
local formattedUrl = renderUrl{
url = url,
display = args[2] or args.name or 'Official website',
mobile = args.mobile,
format = args.format
}
return formattedUrl .. renderTrackingCategory(url)
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:Official website'
})
return p._main(args)
end
return p