Module:Sandbox/RexxS/WdRefs

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

Lua error in package.lua at line 80: module 'strict' not found.

Test module to retrieve all claims from Wikidata, then display them in a table. It shows: any qualifiers; any references and the source of the reference

Usage

Paste:

{{#invoke:Sandbox/RexxS/WdRefs|seeRefs}}

into an article and preview it. There is an expensive version which allows access to any article from anywhere by giving the article's Wikidata id:

{{#invoke:Sandbox/RexxS/WdRefs|seeRefs|qid=<qid>}}

where <qid> would be Q151973 for Richard Burton, for example.

See Module talk:Sandbox/RexxS/WdRefs for a couple of examples.

Limitations

It doesn't pick apart multiple instances of dates, so if somebody has two dates of death, it won't deal properly with second one. Best not to trust it on Jon Snow.



--[[
Returns a list of all claims and references, if any, from Wikidata

]]

local p = {}

p.seeRefs = function(frame)
	-- look for named parameter qid; if it's blank make it nil
	local qid = frame.args.qid
	if qid and (#qid == 0) then
		qid = nil
	end
	
	-- look for named parameter lang
	-- it should contain a two-character ISO-639 language code
	-- if it's blank fetch the language of the local wiki
	local lang = frame.args.lang
	if (not lang) or (#lang < 2) then
		lang = mw.language.getContentLanguage().code
	end
	
	local result = "{| class='wikitable sortable'\r\n! scope='col' | Property\r\n! scope='col' | Value\r\n"
	local numclaims = 0
	local ent = mw.wikibase.getEntityObject(qid)
	if ent and ent.claims then
		for k1, v1 in pairs(ent.claims) do
			-- code to look for claims
			numclaims = numclaims + 1
			result = result .. "|-\r\n| " .. mw.wikibase.label(k1) .. " || "
			local numvals = 0
			local val = "<pre>\r\n"
			for k2, v2 in pairs(v1) do
				numvals = numvals + 1
				local valtype = v2.mainsnak.datatype
				if valtype == "wikibase-item" then
					local label = "Q" .. v2.mainsnak.datavalue.value["numeric-id"]
					label = mw.wikibase.label("Q" .. v2.mainsnak.datavalue.value["numeric-id"]) or label
					val = val .. k2 .. ". " .. label
				elseif valtype == "string" or valtype == "external-id" or valtype == "url" or valtype == "commonsMedia" then
					val = val .. k2 .. ". " .. v2.mainsnak.datavalue.value
				elseif valtype == "monolingualtext" then
					val = val .. k2 .. ". " .. v2.mainsnak.datavalue.value.text
				else
					val = val .. k2 .. ". <" .. valtype .. "> " .. ent:formatPropertyValues(k1, mw.wikibase.entity.claimRanks).value  -- expand this later
				end	
				if v2.qualifiers then
					val = val .. " -- " .. mw.wikibase.renderSnaks(v2.qualifiers)
				end
				if v2.references then
					for k3, v3 in pairs(v2.references) do
						val = val .. " ++ Ref = " .. mw.wikibase.renderSnaks(v3.snaks)
					end
					val = val .. " \r\n"
				else
					val = val .. " -- NO REFS\r\n"
				end
			end
			val = val .. "</pre>\r\n"
			result = result .. val
		end
	end
	result = result .. "|}\r\n" .. numclaims .. " claims\r\n\r\n"
	if qid then
		result = result .. "[[d:" .. qid .. "|Edit this on Wikidata]]"
	else
		result = result .. "[[d:" .. ent.id .. "|Edit this on Wikidata]]"
	end
	return result
end

return p