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:20, 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 checklayer(depth) -- check subclasses of items at current layer
	for j=1,#checklist[depth],1 do -- loop over items ar current layer
		local subclasses
		if depth == 0 then
			subclasses = mw.wikibase.getBestStatements(checklist[depth][j],"P31") -- use P31 for layer 0
		else
			subclasses = mw.wikibase.getBestStatements(checklist[depth][j],"P279") -- use P279 for layers 1+
		end
		checklist[depth+1] = {} -- setup next layer of table
		for i,statement in ipairs(subclasses) do -- loop through items in next layer
			local newitem = statement.mainsnak.datavalue.value.id
			if newitem == value then
				isInstance = true
				break -- break away from loop if instance found
			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

function p.main(frame)
	local args = frame.args
	local pargs = frame:getParent().args
	maxdepth = 5
	depth = 0
	isInstance = false
	local qid = args.qid or pargs.qid
	checklist = {}
	checklist[0] = {qid}
	value = args.value or pargs.value
	while not isInstance and depth<maxdepth do
		checklayer(depth)
		depth = depth+1
	end
	if not isInstance then
		depth = -1 -- indicates not isInstance
	end
	return depth-1 -- return depth that item was found
end

return p