Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Sandbox/RexxS/WdRefs

From Wikipedia, the free encyclopedia

--[[
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.getEntity(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.getLabel(k1) or "nolabel") .. " || "
			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.getLabel("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