Moduuli:Cs:Wikidata/Tree
Siirry navigaatioon
Siirry hakuun
[ muokkaa ] wikiprojekti:wikidata/cswiki
|
local p = {}
local lib = require 'Module:cs:Wikidata/lib'
p.instance_prop = 'P31'
p.abovetaxa_prop = 'P171'
p.class_prop = 'P279'
function p.GoInTree(item, prop, level, holder, callback)
if level == 0 then
return holder
end
local entity = mw.wikibase.getEntity(item)
local Statements = entity:getBestStatements(prop)
for i, statement in pairs(Statements) do
local new_holder, ok = callback(holder, Statements, i)
if ok and lib.IsSnakValue(statement.mainsnak) then
local item = lib.getEntityIdFromValue(statement.mainsnak.datavalue.value)
return p.GoInTree(item, prop, level - 1, new_holder, callback)
end
end
return holder
end
function p.IsInTree(item, root, prop, level, item_cache)
if item == root then
return true
end
if level == 0 then
return false
end
local entity = mw.wikibase.getEntity(item)
local Statements = entity:getBestStatements(prop)
for _, statement in pairs(Statements) do
if lib.IsSnakValue(statement.mainsnak) then
local item = lib.getEntityIdFromValue(statement.mainsnak.datavalue.value)
if not item_cache[item] then
item_cache[item] = item
if p.IsInTree(item, root, prop, level - 1, item_cache) then
return true
end
end
end
end
return false
end
function p.IsInstance(item, options)
local Classes = lib.textToTable(options.instance:upper())
local entity = mw.wikibase.getEntity(item)
local Statements = entity:getBestStatements(p.instance_prop)
local level = tonumber(options.level) or 10
for _, class in pairs(Classes) do
for _, statement in pairs(Statements) do
if lib.IsSnakValue(statement.mainsnak) then
local item = lib.getEntityIdFromValue(statement.mainsnak.datavalue.value)
if p.IsInTree(item, class, p.class_prop, level, {}) then
return true
end
end
end
end
return false
end
return p