Hopp til innhold

Modul:Official links

Fra Wikipedia, den frie encyklopedi
Moduldokumentasjon

require('Module:No globals')

local function formatter(link)
	return '[' .. link .. ' Offisiell side]'
end

local p = {}

function p.findOfficialLinks(qid)
	local links = {}
	local entity = mw.wikibase.getEntityObject( qid )
	if entity then
		local statements = entity:getBestStatements( 'P856' )
		if statements then
			for _, claim in ipairs( statements ) do
				if claim then
					if claim['type'] ~= 'statement' then
						break
					end
					local mainsnak = claim["mainsnak"]
					if mainsnak['snaktype'] ~= 'value' then
						break
					end
					if mainsnak['datatype'] ~= 'url' then
						break
					end
					local datavalue = mainsnak["datavalue"]
					if datavalue['type'] ~= 'string' then
						break
					end
					links[1+#links] = formatter(datavalue['value'])
				end
			end
		end
	end
	return links
end

function p.links( frame )
	local items = {}
	local links = p.findOfficialLinks()
	if links then
		for _,link in ipairs( links ) do
			items[1+#items] = (#items > 0 and '* ' or '') .. link
		end
		return table.concat(items, "\n")
	end
	return "''Ingen offisiell side tilgjengelig''"
end

return p