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 22:48, 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 currentdepth = 1
	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(checklist,value,currentdepth,maxdepth)
	local instances = mw.wikibase.getBestStatements(checklist[currentdepth][1],"P31")
	local isInstance = false
	checklist[currentdepth+1] = {}
	for i,statement in ipairs(instances) do
		local newitem = statement.mainsnak.datavalue.value.id
		if newitem == value then
			isInstance = true
			break
		end
		checklist[currentdepth+1][#checklist[currentdepth+1]+1]=newitem
	end
	if not isInstance then
		for i=1,#checklist[currentdepth+1],1 do
			if checksubclass(checklist[currentdepth+1][i],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 maxdepth = 5
	local currentdepth = 0
	local qid = args.qid or pargs.qid
	local checklist = {}
	checklist[currentdepth] = {qid}
	local value = args.value or pargs.value
	return checkitem(checklist,value,currentdepth,maxdepth)
end

return p