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 09:03, 22 March 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {};

local function checksubclass(depth) -- check subclasses of items at current depth
	for j=1,#checklist[depth],1 do
		local subclasses = mw.wikibase.getBestStatements(checklist[depth][j],"P279")
		checklist[depth+1] = {}
		for i,statement in ipairs(subclasses) do
			local newitem = statement.mainsnak.datavalue.value.id
			if newitem == value then
				isInstance = true
				break
			else
				checklist[depth+1][#checklist[depth+1]+1] = newitem -- Add item to next depth of checklist table
			end
		end
		if isInstance then
			break -- break away from loop if instance found
		end
	end
	return isInstance
end

local function checkitem(depth)
	local instances = mw.wikibase.getBestStatements(checklist[depth][1],"P31")
	checklist[1] = {}
	for i,statement in ipairs(instances) do
		local newitem = statement.mainsnak.datavalue.value.id
		if newitem == value then
			isInstance = true
			break
		end
		checklist[1][#checklist[depth+1]+1] = newitem
	end
	while not isInstance and depth<maxdepth do
		depth = depth+1
		checksubclass(depth)
	end
	return isInstance
end

function p.main(frame)
	local args = frame.args
	local pargs = frame:getParent().args
	maxdepth = 5
	isInstance = false
	local qid = args.qid or pargs.qid
	checklist = {}
	checklist[0] = {qid}
	value = args.value or pargs.value
	return checkitem(0)
end

return p