Hopp til innhold

Modul:Official links

Fra Wikipedia, den frie encyklopedi
Moduldokumentasjon

require('Module:No globals')

local messages = {}
messages['en'] = {
	['preferred'] = 'preferred',
	['ext-link'] = '[$1 $2]',
	['optionals'] = '($1)',
	['initial-combiner'] = ',',
	['final-combiner'] = 'and',
	['first-list-item'] = '$1',
	['rest-list-item'] = '* $1',
	['no-list-available'] = 'No $1 available',
	['cat-inclusion'] = 'Articles with $1 from Wikidata',
	['cat-exclusion'] = 'Articles without $1 from Wikidata',
}
messages['nb'] = {
	['preferred'] = 'foretrukket',
	['ext-link'] = '[$1 $2]',
	['optionals'] = '($1)',
	['initial-combiner'] = ',',
	['final-combiner'] = 'og',
	['first-list-item'] = '$1',
	['rest-list-item'] = '* $1',
	['no-list-available'] = 'Ikke noe $1 tilgjengelig',
	['cat-inclusion'] = 'Artikler med $1 fra Wikidata',
	['cat-exclusion'] = 'Artikler uten $1 fra Wikidata',
}

local contLang = mw.language.getContentLanguage()

local labels = {}

local function getEntityLabel( id )
	local entity = mw.wikibase.getEntity( id )
	if entity then
		return entity:getLabel()
	end
end

local function findEntityLabel( id )
	if not labels[id] then
		local label = getEntityLabel( id )
		if label then
			labels[id] = label -- labels is an outer structure
		end
	end
	return labels[id]
end

local mainFormatter = {}
mainFormatter['string'] = function( pid, datavalue )
	if datavalue['type'] ~= 'string' then
		return nil
	end
	return '[' .. datavalue.value .. ' ' .. contLang:ucfirst( findEntityLabel( pid ) ) .. ']'
end

local qualFormatter = {}
qualFormatter['wikibase-entityid'] = function( pid, datavalue )
	if datavalue['type'] ~= 'wikibase-entityid' then
		return nil
	end
	if datavalue.value['entity-type'] ~= 'item' then
		return nil
	end
	return findEntityLabel( 'Q'..datavalue.value["numeric-id"] )
end

local main = {}
main.P856 = {
	types = {
		snaktype = 'value',
		datatype = 'url',
	},
	formatter = mainFormatter['string']
}

local qual = {}
qual.P407 = {
	types = {
		snaktype = 'value',
		datatype = 'wikibase-item',
	},
	formatter = qualFormatter['wikibase-entityid']
}

local qorder = {'P407'}

local p = {}

function p.findMainLinks(pid, qid)
	local links = {}
	local entity = mw.wikibase.getEntityObject( qid )
	if entity then
		local statements = entity:getBestStatements( pid )
		if statements then
			for _, claim in ipairs( statements ) do
				if claim then
					if claim['type'] ~= 'statement' then
						break
					end
					local mainsnak = claim.mainsnak
					if not mainsnak or not main[pid] then
						break
					end
					if (mainsnak.snaktype ~= main[pid].types.snaktype
						or mainsnak.datatype ~= main[pid].types.datatype)
					then
						break
					end
					local optionals = {}
					if claim['rank'] ~= 'normal' then
						optionals[1+#optionals] = claim['rank']
					end
					local qualifiers = claim.qualifiers
					if qualifiers then
						for _, qualid in ipairs( qorder ) do
							for _, qualsnak in ipairs( qualifiers[qualid] ) do
								if not qualsnak or not qual[qualid] then
									break
								end
								if (qualsnak.snaktype ~= qual[qualid].types.snaktype
									or qualsnak.datatype ~= qual[qualid].types.datatype)
								then
									break
								end
								optionals[1+#optionals] = qual[qualid].formatter(qualsnak.property, qualsnak.datavalue)
							end
						end
					end
					mw.logObject(optionals)
					local str = main[pid].formatter(pid, mainsnak.datavalue)
					if #optionals > 0 then
						str = str .. ' (' .. table.concat(optionals, ', ') .. ')'
					end
					links[1+#links] = str
				end
			end
		end
	end
	return links
end

function p.links( frame )
	local seq = {}
	for _,v in ipairs(frame.args) do
		local _, _, ch, num= v:find("([pP])(%d+)")
		if ch then
			local pid = ch:upper()..num
			local label = findEntityLabel( pid )
			if label then
				seq[1+#seq] = pid
			end
		end
	end
	local items = {}
	for _,v in ipairs(seq) do
		local links = p.findMainLinks(v)
		if links then
			for _,link in ipairs( links ) do
				items[1+#items] = (#items > 0 and '* ' or '') .. link
			end
		end
	end
	if #items > 0 then
		return table.concat(items, "\n") .. '[[Kategori:Artikler med offisielt nettsted fra Wikidata]]'
	end
	return "''Ikke noe offisielt nettsted tilgjengelig''" .. '[[Kategori:Artikler uten offisielt nettsted]]'
end

return p