Modul:Wikidata2

Frå Wikipedia – det frie oppslagsverket
Hopp til navigering Hopp til søk

Dokumentasjon for modulen kan opprettast på Modul:Wikidata2/dok


local p = {}

-- This is used to get a normal wiki-linked value, or a comma separated list of them if multiple values exist
p.getValue = function(frame)
	if not mw.wikibase then
        return ""
    end
    local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
    local table_separator = mw.text.trim(frame.args[3] or ",")
    if input_parm == "hent_wikidata" then
        local entity = mw.wikibase.getEntityObject()
        if not entity then
        	return ""
    	end
        if entity.claims and entity.claims[propertyID] ~= nil then
            local out = {}
            for k, v in pairs(entity.claims[propertyID]) do
            	if v.mainsnak.datavalue then
                	if (mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])) then
                    	out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "|" .. (mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) or ("Q" .. v.mainsnak.datavalue.value["numeric-id"])).. "]]"
                	else
                    	out[#out + 1] = "[[Special:AboutTopic/Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. (mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) or ("Q" .. v.mainsnak.datavalue.value["numeric-id"])).. "]]" .. "[[Kategori:Sporing av AboutTopic]]"
                	end
                end
            end
            return table.concat(out, table_separator .. ' ')
        else
            return ""
        end
    else
        return input_parm
    end
end

-- This is used to get a value like 'male' (for property p21) which won't be linked
p.getRawValue = function(frame)
	if not mw.wikibase then
        return ""
    end
    local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
    local table_separator = mw.text.trim(frame.args[3] or ",")
    if input_parm == "hent_wikidata" then
        local entity = mw.wikibase.getEntityObject()
                if not entity then
        	return ""
    	end
        if entity.claims[propertyID] ~= nil then
            local out = {}
            for k, v in pairs(entity.claims[propertyID]) do
                out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
            end
            return table.concat(out, table_separator .. ' ')
        else
            return ""
        end
    else
        return input_parm
    end
end

-- This is used to get a date value for date_of_birth (p569), etc. which won't be linked -- consolidate by testing if entity.claims[propertyID].mainsnak.datavalue.type is "time"
p.getDateValue = function(frame)
	if not mw.wikibase then
        return ""
    end
    local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
    local table_separator = mw.text.trim(frame.args[3] or "")
    if input_parm == "hent_wikidata" then
        local entity = mw.wikibase.getEntityObject()
                if not entity then
        	return ""
    	end
        if entity.claims[propertyID] ~= nil then
        	local date_temp = 'ISOtilNorskdato'
            local out = {}
            local dt = {}
            for k, v in pairs(entity.claims[propertyID]) do
                local d = v.mainsnak.datavalue.value.time
                lyear = string.sub(d, 9, 12)
                lmonth = string.sub(d, 14, 15)
                lday = string.sub(d, 17, 18)
                out[#out + 1] = mw.getCurrentFrame():expandTemplate{ title = date_temp, args = { lyear .. "-" .. lmonth ..  "-" .. lday, 'n' } }
            end
            return table.concat(out, table_separator .. ' ')
        else
            return ""
        end
    else
        return input_parm
    end
end

return p