Modul:Wikibase

Från Wikivoyage, den fria resehandboken.

Dokumentationen för denna modul kan skapas på Modul:Wikibase/dok

local item = mw.wikibase.getEntityObject()

local function is_defined(s)
	if s and s ~= '' then return s end
	return nil
end

function getId( id )	
	if id then
		return id
	end
	if item then
		return item.id
	end
	return nil
end

-- Restituisce il collegamento corrispondente al codice fornito.
function sitelink(dbname)
	if is_defined(dbname) and item and item:getSitelink(dbname) then
		return item:getSitelink(dbname)
	end
	return ''
end

-- Restituisce le coordinate geografiche corrispondenti all'elemento
function coords(typ, fallback)
	if is_defined(fallback) and string.match(fallback, '^<%!%-%-.*%-%->$') == nil then
		return fallback
	end
	if item and item.claims then
		local coords = item.claims.P625
		if coords and #coords == 1 and coords[1].mainsnak and coords[1].mainsnak.datavalue and coords[1].mainsnak.datavalue.value then
			return coords[1].mainsnak.datavalue.value[typ]
		end
	end
	return ''
end

function normalize_timezone(tz)
	local k = string.gsub(tz, '%+0(%d)$', '+%1')
	k = string.gsub(k, '%+0(%d:%d%d)$', '+%1')
	return string.gsub(k, '(%+%d%d?):00$', '%1')
end

function normalize_timezone_list(list)
	--alphaindexline
	return mw.text.listToText(list)
end

-- Restituisce l'elemento più aggiornato tra una serie di asserzioni
-- TODO: ranks
function aggiornato(prop, frame)
	if item and item.claims and item.claims[prop] then
		for index, claim in pairs(item.claims[prop]) do
			local qual = claim.qualifiers
			if qual == nil or qual.P582 == nil then
				-- P582 è la data di fine, significa che non è il valore attuale
				if claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value then
					local val = claim.mainsnak.datavalue.value
					if val['numeric-id'] then
						local id = 'Q'..val['numeric-id']
						local sl = mw.wikibase.sitelink(id)
						local lb = mw.wikibase.label(id)
						if is_defined(sl) then
							return frame:preprocess('[['..sl..'|'..lb..']]')
						end
						return lb
					end
					return val
				end
			end
		end
	end
	return ''
end

local p = {}

-- Restituisce l'ID dell'elemento collegato alla pagina corrente.
function p.id(frame)
	id = getId(frame.args[1])
	if id then
		   return id
	end
	return '(nessun elemento trovato)'
end


-- Restituisce l'ID dell'elemento collegato alla pagina corrente sotto forma di link a Wikidata.
function p.idLink(frame)
	local id = getId(frame.args[1])
	if id then
		return '[[d:' .. id .. '|' .. id .. ']]'
	end
	return '(nessun elemento trovato)'
end


-- Restituisce l'etichetta di un elemento.
function p.label(frame)
	local id = getId(frame.args[1])
	if id then
		   return mw.wikibase.label( id )
	end
	return '(nessun elemento trovato)'
end

-- Restituisce il primo valore non nullo (nell'ordine) tra il: parametro passato in ingresso, etichetta dell'entità Wikidata, nome della pagina.
function p.pagename(frame)
	if frame and is_defined(frame.args[1]) then
		return frame.args[1]
	end
	local id = getId()
	if id then
		   return mw.wikibase.label( id )
	end
	return mw.title.getCurrentTitle().text
end

-- Restituisce la pagina locale dell'elemento di Wikidata fornito.
function p.page(frame)
	local id = getId(frame.args[1])
	if id then
		   return mw.wikibase.sitelink( id )
	end
	return '(nessun elemento trovato)'
end

-- Restituisce il collegamento corrispondente al codice fornito.
function p.sitelink(frame)
	return sitelink(frame.args.dbname or frame.args[1])
end

function p.wikilink(wikiname, frame)
	if frame and is_defined(frame.args[1]) then
		return frame.args[1]
	end
	local sl = sitelink(wikiname)
	if is_defined(sl) then
		return sl
	end
--[[	local t = mw.title.getCurrentTitle().text
	if is_defined(t) then
		return t
	end
	]]
	return ''
end

-- Restituisce il collegamento corrispondente a Wikipedia in italiano.
function p.itwikilink(frame)
	return p.wikilink('itwiki', frame)
end
 
-- Restituisce il collegamento corrispondente a Wikiquote in italiano.
function p.itwikiquotelink(frame)
	return p.wikilink('itwikiquote', frame)
end
 
-- Restituisce il collegamento corrispondente a Wikisource in italiano.
function p.itwikisourcelink(frame)
	return p.wikilink('itwikisource', frame)
end
 
-- Restituisce il collegamento corrispondente a Wikinotizie in italiano.
function p.itwikinewslink(frame)
	return p.wikilink('itwikinews', frame)
end

-- Restituisce il nome della categoria di Commons collegata all'articolo attraverso Wikidata.
function p.commonslink(frame)
	if frame and is_defined(frame.args[1]) then
		return 'Category:'..frame.args[1]
	end
	if item and item.claims then
		local cat = item.claims.P373
		if cat and #cat == 1 and cat[1].mainsnak and cat[1].mainsnak.datavalue and cat[1].mainsnak.datavalue.value then
			return 'Category:'..(cat[1].mainsnak.datavalue.value)
		end
	end
	return ''
end

function p.Webbplats(frame)
	if frame then
		if is_defined(frame.args[1]) then
			return frame.args[1]
		end
		local parent = frame:getParent()
		if parent then
			local args = parent.args
			if is_defined(args.Webbplats) then
				return args.Webbplats
			end
			if is_defined(args['Officiell webbplats']) then
				return args['Officiell webbplats']
			end
		end
	end
	return aggiornato('P856', frame)
end

function p.WDProperty(PID, ExtVar, frame)
	if frame then
		if is_defined(frame.args[1]) then
			return frame.args[1]
		end
		local parent = frame:getParent()
		if parent and is_defined(parent.args[ExtVar]) then
			return parent.args[ExtVar]
		end
	end
	return aggiornato(PID, frame)
end

-- Restituisce il nome della capitale o del capoluogo attuale dell'elemento.
function p.huvudstad(frame)
	return p.WDProperty('P36', 'Huvudstad', frame)
end
function p.huvudort(frame)
	return p.WDProperty('P36', 'Huvudort', frame)
end

function p.valuta(frame)
	return p.WDProperty('P38', 'Valuta', frame)
end

function p.banner(frame)
	return p.WDProperty('P948', 'Banner', frame)
end

function p.vapen(frame)
	return p.WDProperty('P94', 'Vapen', frame)
end

function p.flagga(frame)
	return p.WDProperty('P41', 'Flagga', frame)
end

function p.abitanti(frame)
	return p.WDProperty('P1082', 'Abitanti', frame)
end
 
function p.land(frame)
	return p.WDProperty('P17', 'Land', frame)
end
 
function p.lokalisering(frame)
	return p.WDProperty('P242', 'Lokalisering', frame)
end

-- Restituisce l'etichetta dell'elemento rappresentante la bandiera relativa all'elemento dell'articolo
function p.flagga_titel(frame)
	if item then
		local claims = item.claims
		if claims and claims.P163 and claims.P163[0] and claims.P163[1] == nil then
			return mw.wikibase.label('Q'..claims.P163[0].mainsnak.datavalue.value['numeric-id'])
		end
	end
	return mw.title.getCurrentTitle().text..' - Flagga'
end

-- Restituisce l'etichetta dell'elemento rappresentante lo stemma relativo all'elemento dell'articolo
function p.vapen_titel(frame)
	if item then
		local claims = item.claims
		if claims and claims.P237 and claims.P237[0] and claims.P237[1] == nil then
			return mw.wikibase.label('Q'..claims.P237[0].mainsnak.datavalue.value['numeric-id'])
		end
	end
	return mw.title.getCurrentTitle().text..' - Vapen'
end

-- Restituisce la latitudine corrispondente all'articolo.
function p.latitud(frame)
	return coords('latitude',frame.args[1])
end

-- Restituisce la longitudine corrispondente all'articolo.
function p.longitud(frame)
	return coords('longitude',frame.args[1])
end

function p.fuso_orario(frame)
	local link = false
	for i, e in pairs(frame.args) do
		if e == 'link' then
			link = true
			break
		end
	end
	if item and item.claims and item.claims.P421 then
		local zone = {}
		for i, e in pairs(item.claims.P421) do
			local id = e.mainsnak.datavalue.value['numeric-id']
			local z = normalize_timezone(mw.wikibase.label('Q'..id))
			if link then
				z = frame:preprocess('[[d:Q'..id..'|'..z..']]')
			end
			table.insert(zone, z)
		end
		return normalize_timezone_list(zone)
	end
	return ''
end

function p.disambig(frame)
	if p.instanceof(4167410) then
		return true
	end
end

function p.instanceof(arg)
	arg = tonumber(arg.args[1] or arg)
	if item and item.claims and item.claims.P31 then
		for index, claim in pairs(item.claims.P31) do
			if claim.mainsnak and claim.mainsnak.datavalue then
				local val = claim.mainsnak.datavalue.value
				if val and val['numeric-id'] and arg == val['numeric-id'] then
					return true
				end
			end
		end
	end
	return false
end

return p