Module:Is instance and Module:Is instance/sandbox: Difference between pages
(Difference between pages)
imported>MusikBot II 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)) |
imported>MSGJ make callable from module |
||
| Line 1: | Line 1: | ||
local p = {}; | local p = {}; | ||
function p._main(args) | |||
--local pargs = frame:getParent().args | |||
value = args.value | |||
local qid = args.qid or "" | |||
if qid == '' then | |||
value = args | |||
local qid = args | |||
if qid == | |||
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 | ||
if args.maxdepth then | |||
maxdepth = tonumber(args.maxdepth) | |||
maxdepth = tonumber( | |||
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 | |||
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 | ||
return p | return p | ||