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

p = {}

function p.test(frame)
	local entityid, propertyid = frame.args.entityid, frame.args.propertyid
	local statementstbl = mw.wikibase.getBestStatements( entityid, propertyid)
	local t = {}
	for k, v in pairs(statementstbl) do
		local v = v.mainsnak.datavalue.value.id
		local sitelink = mw.wikibase.getSitelink(v) or ""
		local a = string.find(sitelink, "%(") or ""	
		local b = string.match(sitelink, "(%w+) (%w+) (%w+) (%w+)") or ""
		if sitelink == "" then
			local label = mw.wikibase.getLabel(v) 
			tables = table.insert(t, label)
		end	
		if sitelink ~= "" and b ~= "" then
			local display_text = mw.wikibase.getLabel(v)
			local statement = "[["..sitelink.."|"..display_text.."]]"
			tables = table.insert(t, statement)
		end	
		if sitelink ~= "" and a ~= "" and b == "" then
			local sitelink = ""..mw.wikibase.getSitelink(v)..""
			local display_text = sitelink:gsub( '%b()', '')
			local statement = "[["..sitelink.."|"..display_text.."]]"
			tables = table.insert(t, statement)
		end
		if sitelink ~= "" and a == "" and b == "" then 
			local statement = "[["..sitelink.."]]"		
			tables = table.insert(t, statement)
		end
	
	end
	
	return table.concat(t, "<br>")
	
end	

return p