Module:Sandbox/marius851000/dub

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Sandbox/marius851000/dub/doc

Code

local mkey = require("Module:Sandbox/marius851000/mkey");
local wikitable3d = require("Module:Sandbox/marius851000/wikitable3d");

function forEachStatementExpectEntity(claims, callback)
	for _, claim in ipairs(claims) do
		assert(claim["mainsnak"]["datavalue"]["type"] == "wikibase-entityid", "One claim of an object was expected to be an object, but it isn't") -- TODO: better, clearer and more usefull error message
		callback(claim, claim["mainsnak"]["datavalue"]["value"]["id"])
	end
end

function forEachQualifierIfExistExpectEntity(claim, propertyId, callback)
	if (claim["qualifiers"] ~= nil) then
		if (claim["qualifiers"][propertyId] ~= nil) then
			for _, statement in ipairs(claim["qualifiers"][propertyId]) do
				assert(statement["datatype"] == "wikibase-item", "A qualifier with the property " .. propertyId .. " was expected to be an object, but isn't") -- TODO: better error message
				callback(statement, statement["datavalue"]["value"]["id"])
			end
		end
	end
end

r = {}

r.generateTableData = function(object)
	result = {}
	-- P725 = voice actor
	assert(object["claims"]["P725"] ~= nil, "there are no voice actor (P725) for the given object !")
	forEachStatementExpectEntity(object["claims"]["P725"], function(actorClaim, actorQid)
		languages = {} -- TODO this isn't deterministic (should this function be ?)
		roles = {}
		-- P453 = role
		forEachQualifierIfExistExpectEntity(actorClaim, "P453", function(_, roleId)
			roles[roleId] = true;
		end)
		-- P407 = language
		forEachQualifierIfExistExpectEntity(actorClaim, "P407", function(_, langId)
			languages[langId] = true;
		end)
		
		for role, _ in pairs(roles) do
			if (result[role] == nil) then
				result[role] = {}
			end
			for language, _ in pairs(languages) do
				result[role][language] = actorQid --TODO: use a list instead of a single statement, where multiple perform performed the character or when unsure
			end
		end
	end)
	return result
end

r.makeTable = function(frame)
	object = mw.wikibase.getEntity(frame.args["qid"]) --TODO: get from argument
	data = r.generateTableData(object)
	return wikitable3d.formatTable(data)
end

r.test = function()
	mw.logObject(r.makeTable({}));
end

return r