Module:Wikibase

De Wikivoyage

La documentation pour ce module peut être créée à Module:Wikibase/doc

-- Module:Wikibase
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 not mw.wikibase then
           return "wikibase module not found"
    end
 
    if id then return id end
 
    entity = mw.wikibase.getEntity()
    if not entity then return nil end
    return entity.id
end
 
-- Returns the link corresponding to the code provided.
function sitelink(dbname)
    if dbname==nil or dbname=='' then
        return ''
    end
    local sl = mw.wikibase.getEntity()
    if sl and sl.sitelinks[dbname] then
        return sl.sitelinks[dbname].title
    end
    return ''
end
 
-- Returns the corresponding geographical coordinates of the item
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
 
-- Returns the most updated info from a series of statements
function updated(item,prop,frame)
    if item~=nil then
        local claims = item.claims
        if claims~=nil and claims[prop]~=nil then
            for index,claim in pairs(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
                    local val = claim.mainsnak.datavalue.value
                    if val['numeric-id']~=nil then
                        local id = 'Q'..val['numeric-id']
                        local sl = mw.wikibase.sitelink(id)
                        local lb = mw.wikibase.label(id)
                        if sl~=nil and sl~='' then
                            return frame:preprocess('[['..sl..'|'..lb..']]')
                        end
                        return lb
                    end
                    return val
                end
            end
        end
    end
    return ''
end

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 = {}
 
-- Returns the ID number of the data item linked to the current page.
function p.id(frame)
    id = getId(frame.args[1])
    if id == nil then
           return "(no element found)"
    end
    return id
end
 
 
-- Returns the ID number of the data item linked to the current page in the form of a link to Wikidata.
function p.idLink(frame)
    id = getId(frame.args[1])
    if id == nil then
           return "(no element found)"
    end
    return "[[d:" .. id .. "|" .. string.upper(id) .. "]]"
end
 
 
-- Returns the label of the data item.
function p.label(frame)
    id = getId(frame.args[1])
    if id == nil then
           return "(no element found)"
    end
    return mw.wikibase.label( id )
end
 
-- Returns the local page of the data item provided.
function p.page(frame)
    id = getId(frame.args[1])
    if id == nil then
           return "(no element found)"
    end
    return mw.wikibase.sitelink( id )
end
 
-- Returns the link corresponding to the code provided.
function p.sitelink(frame)
    return sitelink(frame.args.dbname or frame.args[1])
end
 
-- Returns the corresponding link to Wikipedia in English.
function p.enwikilink(frame)
    if frame.args[1]~=nil and frame.args[1]~='' then
        return frame.args[1]
    end
    local sl = sitelink('enwiki')
    if sl~=nil and sl~='' then
        return sl
    end
    local t = mw.title.getCurrentTitle().text
    if t~=nil and t~='' then
        return t
    end
    return ''
end
 
-- Returns the Commons category of the data item.
function p.commonslink(frame)
    if frame.args[1]~=nil and frame.args[1]~='' then
        return 'Category:'..frame.args[1]
    end
    if item~=nil and item.claims~=nil then
        local cat = item.claims.p373
        if cat~=nil and cat[0]~=nil and cat[1]==nil and cat[0].mainsnak.datavalue.value~=nil then
            return 'Category:'..(cat[0].mainsnak.datavalue.value)
        end
    end
    local t = mw.title.getCurrentTitle().text
    if t~=nil and t~='' then
        return 'Category:'..t
    end
    return ''
end

function p.sito(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.Sito) then
				return args.Sito
			end
			if is_defined(args['URL officiel']) then
				return args['URL officiel']
			end
		end
	end
	return aggiornato('P856',frame)
end

-- Returns the name of the capital or main city.
function p.capital(frame)
    if frame.args[1]~=nil and frame.args[1]~='' then
        return frame.args[1]
    end
    return updated(item,'p36',frame)
end
 
function p.capoluogo(frame)
    return p.capital(frame)
end
 
function p.valuta(frame)
    if frame.args[1]~=nil and frame.args[1]~='' then
        return frame.args[1]
    end
    return updated(item,'p38',frame)
end
 
function p.flag(frame)
    if frame.args[1]~=nil and frame.args[1]~='' then
        return frame.args[1]
    end
    return updated(item,'p41',frame)
end
 
-- Restituisce l'etichetta dell'elemento rappresentante la bandiera relativa all'elemento dell'articolo
function p.bandiera_titolo(frame)
    if item~=nil then
        local claims = item.claims
        if claims~=nil and claims.p163~=nil and claims.p163[0]~=nil 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..' - Bandiera'
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 updated(PID, frame)
end

-- Restituisce il nome della capitale o del capoluogo attuale dell'elemento.
function p.capitale(frame)
	return p.WDProperty('P36', 'Capitale', frame)
end
function p.cheflieu(frame)
	return p.WDProperty('P36', 'Chef-lieu', frame)
end

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

function p.disambig(frame)
	if item~=nil and item.descriptions~=nil then
		local desc = item.descriptions.en
		if desc~=nil and desc.value~=nil and desc.value:lower():find('disambiguation page')~=nil then
			return true
		end
	end
	return false
end
 
return p