Module:Wikidata: Difference between revisions

From Random Island Wiki
Jump to navigation Jump to search
>Bennylin
(catch error)
>Jackmcbarn
m (use tabs for indentation)
Line 3: Line 3:
-- This is used to get a normal wiki-linked value, or a comma separated list of them if multiple values exist
-- 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)
p.getValue = function(frame)
    local propertyID = mw.text.trim(frame.args[1] or "")
local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
    if input_parm == "FETCH_WIKIDATA" then
if input_parm == "FETCH_WIKIDATA" then
        local entity = mw.wikibase.getEntity()
local entity = mw.wikibase.getEntity()
        if entity.claims[propertyID] ~= nil then
if entity.claims[propertyID] ~= nil then
            local out = {}
local out = {}
            for k, v in pairs(entity.claims[propertyID]) do
for k, v in pairs(entity.claims[propertyID]) do
                if (mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])) 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"]) .. "]]"
out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
                else
else
                    out[#out + 1] = "[[:d:" .. "Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "]]<small><abbr title='Article is not yet available in this wiki'>[?]</abbr></small>"
out[#out + 1] = "[[:d:" .. "Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "]]<small><abbr title='Article is not yet available in this wiki'>[?]</abbr></small>"
                end
end
            end
end
            return table.concat(out, ", ")
return table.concat(out, ", ")
        else
else
            return ""
return ""
        end
end
    else
else
        return input_parm
return input_parm
    end
end
end
end


-- This is used to get a value like 'male' (for property p21) which won't be linked
-- This is used to get a value like 'male' (for property p21) which won't be linked
p.getRawValue = function(frame)
p.getRawValue = function(frame)
    local propertyID = mw.text.trim(frame.args[1] or "")
local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
    if input_parm == "FETCH_WIKIDATA" then
if input_parm == "FETCH_WIKIDATA" then
        local entity = mw.wikibase.getEntity()
local entity = mw.wikibase.getEntity()
        if entity.claims[propertyID] ~= nil then
if entity.claims[propertyID] ~= nil then
            local out = {}
local out = {}
            for k, v in pairs(entity.claims[propertyID]) do
for k, v in pairs(entity.claims[propertyID]) do
                out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
            end
end
            return table.concat(out, ", ")
return table.concat(out, ", ")
        else
else
            return ""
return ""
        end
end
    else
else
        return input_parm
return input_parm
    end
end
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"
-- 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)
p.getDateValue = function(frame)
    local propertyID = mw.text.trim(frame.args[1] or "")
local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
    local date_format = mw.text.trim(frame.args[3] or "dmy")
local date_format = mw.text.trim(frame.args[3] or "dmy")
    if input_parm == "FETCH_WIKIDATA" then
if input_parm == "FETCH_WIKIDATA" then
        local entity = mw.wikibase.getEntity()
local entity = mw.wikibase.getEntity()
        if entity.claims[propertyID] ~= nil then
if entity.claims[propertyID] ~= nil then
            local out = {}
local out = {}
            local dt = {}
local dt = {}
            for k, v in pairs(entity.claims[propertyID]) do
for k, v in pairs(entity.claims[propertyID]) do
                local d = v.mainsnak.datavalue.value.time
local d = v.mainsnak.datavalue.value.time
                dt.year = string.sub(d, 9, 12)
dt.year = string.sub(d, 9, 12)
                dt.month = string.sub(d, 14, 15)
dt.month = string.sub(d, 14, 15)
                dt.day = string.sub(d, 17, 18)
dt.day = string.sub(d, 17, 18)
                if date_format == "dmy" then
if date_format == "dmy" then
                    out[#out + 1] = os.date("%e %B %Y", os.time(dt))
out[#out + 1] = os.date("%e %B %Y", os.time(dt))
                else
else
                    out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
                end
end
            end
end
            return table.concat(out, ", ")
return table.concat(out, ", ")
        else
else
            return ""
return ""
        end
end
    else
else
        return input_parm
return input_parm
    end
end
end
end


return p
return p

Revision as of 14:54, 25 April 2014

Documentation for this module may be created at Module:Wikidata/doc

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)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			for k, v in pairs(entity.claims[propertyID]) do
				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"]) .. "]]"
				else
					out[#out + 1] = "[[:d:" .. "Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "]]<small><abbr title='Article is not yet available in this wiki'>[?]</abbr></small>"
				end
			end
			return table.concat(out, ", ")
		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)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		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, ", ")
		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)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	local date_format = mw.text.trim(frame.args[3] or "dmy")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			local dt = {}
			for k, v in pairs(entity.claims[propertyID]) do
				local d = v.mainsnak.datavalue.value.time
				dt.year = string.sub(d, 9, 12)
				dt.month = string.sub(d, 14, 15)
				dt.day = string.sub(d, 17, 18)
				if date_format == "dmy" then
					out[#out + 1] = os.date("%e %B %Y", os.time(dt))
				else
					out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
				end
			end
			return table.concat(out, ", ")
		else
			return ""
		end
	else
		return input_parm
	end
end

return p