Jump to content

Module:Is instance

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 21:42, 21 March 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {};
require('Module:No globals')

local function checksubclass(qid,value)
	local subclasses = mw.wikibase.getBestStatements( qid, "P279" )
	local isInstance = false
	for i,statement in ipairs(subclasses) do
		if statement.mainsnak.datavalue.value.id == value then
			isInstance = true
			break
		end
	end
	return isInstance
end

local function checkitem(qid,value)
	local instances = mw.wikibase.getBestStatements( qid, "P31" )
	local isInstance = false
	for i,statement in ipairs(instances) do
		if statement.mainsnak.datavalue.value.id == value then
			isInstance = true
			break
		end
	end
	if not isInstance then
		for i,statement in ipairs(instances) do
			if checksubclass(statement.mainsnak.datavalue.value.id,value) then
				isInstance = true
				break
			end
		end
	end
	return isInstance
end

function p.main(frame)
	local args = frame.args
	local pargs = frame:getParent().args
	local qid = args.qid or pargs.qid
	local value = args.value or pargs.value
	return checkitem(qid,value)
end

return p