Jump to content

Module:Is instance and Module:Is instance/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
m Changed protection settings for "Module:Is instance": High-risk template or module: 8007 transclusions (more info) ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
 
make callable from module
 
Line 1: Line 1:
local p = {};
local p = {};


function p._main(args)
local function checklayer(depth) -- check subclasses of items at current depth
--local pargs = frame:getParent().args
checklist[depth+1] = {} -- setup next layer of table
value = args.value
local j = 0
local qid = args.qid or ""
for j = 1,#checklist[depth] do -- loop over items at current depth
if qid == '' then
local property
if depth == 1 then
property = "P31" -- use P31 for depth 1
else
property = "P279" -- use P279 for depths 2+
end
local subclasses = mw.wikibase.getBestStatements(checklist[depth][j],property) -- get P31 or P279 statements
for i,statement in ipairs(subclasses) do -- loop through items at next depth
if statement.mainsnak.snaktype == 'value' then
local newitem = statement.mainsnak.datavalue.value.id
if newitem == value then
isInstance = true
else
checklist[depth+1][#checklist[depth+1]+1] = newitem -- add item to next depth of checklist table
end
end
end
end
return isInstance
end

function p.main(frame)
local args = frame.args
local pargs = frame:getParent().args
value = args.value or pargs.value
local qid = args.qid or pargs.qid or ""
if qid == "" then
qid = mw.wikibase.getEntityIdForCurrentPage()
qid = mw.wikibase.getEntityIdForCurrentPage()
end
end
Line 37: Line 11:
return 0
return 0
end
end
checklist = {{qid}}
local checklist,depth,maxdepth,isInstance = {{qid}},0,nil,false
local md = args.maxdepth or pargs.maxdepth
if args.maxdepth then
maxdepth = tonumber(args.maxdepth)
if md then
maxdepth = tonumber(md)
else
else
maxdepth = 5
maxdepth = 5
end
end
depth = 0 -- current depth
local function checklayer(depth) -- check subclasses of items at current depth
checklist[depth+1] = {} -- setup next layer of table
isInstance = false -- assume false until match found
local j = 0
for j = 1,#checklist[depth] do -- loop over items at current depth
local property
if depth == 1 then
property = "P31" -- use P31 for depth 1
else
property = "P279" -- use P279 for depths 2+
end
local subclasses = mw.wikibase.getBestStatements(checklist[depth][j],property) -- get P31 or P279 statements
for i,statement in ipairs(subclasses) do -- loop through items at next depth
if statement.mainsnak.snaktype == 'value' then
local newitem = statement.mainsnak.datavalue.value.id
if newitem == value then
isInstance = true
else
checklist[depth+1][#checklist[depth+1]+1] = newitem -- add item to next depth of checklist table
end
end
end
end
return isInstance
end
while not isInstance and depth<maxdepth do
while not isInstance and depth<maxdepth do
depth = depth+1
depth = depth+1
Line 54: Line 49:
end
end
return depth -- return depth that item was found
return depth -- return depth that item was found
end

function p.main(frame)
return p._main(frame.args)
end
end