Jump to content

Module:Official website

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 15:11, 5 January 2016 (get Wikidata code working). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local makeUrl = require('Module:URL')._url

local p = {}

local fetchWikidataUrl
fetchWikidataUrl = function()
	local success, url = pcall(function ()
		return mw.wikibase.getEntityObject().claims.P856[1].mainsnak.datavalue.value
	end)
	if not success then
		url = nil
	end
	fetchWikidataUrl = function ()
		return url
	end
	return url
end

local function formatUrl(options)
	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

local function renderTrackingCategory(url)
	if mw.title.getCurrentTitle().namespace ~= 0 then
		return ''
	end
	local category
	if 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 = formatUrl{
		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