Module:WDfetch

From WikiProjectMed
Jump to navigation Jump to search

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

-- Small module for use in MDwiki infoboxes to get a Wikidata value cleanly
-- example usage: {{#invoke:WDfetch|getvalue|pid=P12081}}

local p = {}

function p.getvalue(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		if v ~= "" then args[k] = v end
	end
	for k, v in pairs(frame.args) do
		if v ~= "" then args[k] = v end
	end

	local propertyId = args[1] or args.pid
	local out = frame:callParserFunction("#statements", propertyId)
	-- if not found, we get an html comment beginning "<!--"
	if string.sub(out, 1, 4) ~= "<!--" then
		-- strip html markup: <span class="ext-UnlinkedWikibase-statements"> & <span>
		local p1 = string.find(out, ">", 1, true) or 0
		local p2 = string.find(out, "<", -1, true) or #out + 1
		out = string.sub(out, p1 + 1, p2 - 1)
		-- uppercase first letter
		out = out:gsub("^%l", string.upper)
	else
		out = nil
	end

	return out
end

return p