“模块:Inherit”的版本间的差异
跳到导航
跳到搜索
| 第1行: | 第1行: | ||
| − | p = {} | + | local p = {} |
function _table_to_keys(tb) | function _table_to_keys(tb) | ||
2020年12月31日 (四) 22:22的版本
{{#invoke:Inherit|inherited_lists
|name=页面名称
|properties=属性1,属性2,属性3
|anchor_property=页面标题属性
|parent_property=父类属性名称
|parent_title=父类标题
|parents=父类1,父类2,父类3
|children_title=子类标题(可选)
|属性1_title=属性1的标题
|属性1_values=标签1,标签2
|属性2_title=属性2的标题
|属性2_values=标签3,标签4
|属性3_title=属性3的标题
|属性3_values=标签5,标签6
|属性1_reversed=属性2
|属性2_reversed=属性1
}}
local p = {}
function _table_to_keys(tb)
local res = {}
for i, k in ipairs(tb) do
res[k] = true
end
return res
end
function _merge_keys(a, b)
local res = {}
for k, _ in pairs(a) do
res[k] = true
end
for k, _ in pairs(b) do
res[k] = true
end
return res
end
function _keys_to_table(set)
local res = {}
local ctr = 1
for k, v in pairs(set) do
res[ctr] = k
ctr = ctr + 1
end
return res
end
function p.query_inherited_property(anchor_property, query_property, parent_list)
local results = {}
for idx, parent in ipairs(parent_list) do
local anchor_query = "[["..anchor_property.."::"..parent.."]]"
local property_query = "[[" .. query_property .. "::+]]"
local content_query = "?"..query_property
local query = anchor_query .. property_query .. "|" ..content_query .. "|format=plainlist"
local res = mw.smw.getQueryResult(query)
for j, r in ipairs(res.results) do
for k, t in ipairs(r.printouts[query_property]) do
results[t.fulltext] = true
end
end
end
return results
end
function p.query_reversed_inherited_property(name, anchor_property, query_property)
return p.query_inherited_property(query_property, anchor_property, {name})
end
function p.query_children(name, anchor_property, parent_property)
return p.query_inherited_property(parent_property, anchor_property, {name})
end
function _create_list(root, title, list)
local node = root:tag("div")
node:wikitext([[ ''' ]] .. title .. [[''':]])
local ul = node:tag("ul")
for k, v in ipairs(list) do
ul:tag("li"):wikitext("[["..v.."]]")
end
end
function _split_or_empty(value)
if value and #value > 0 then
return mw.text.split(value, ",")
else
return {}
end
end
function p.inherited_lists(frame)
local name = frame.args.name
local parents = _split_or_empty(frame.args.parents)
local parent_property = frame.args.parent_property
local parent_title = frame.args.parent_title
local anchor_property = frame.args.anchor_property
local properties = mw.text.split(frame.args.properties, ",")
local reversed_properties = _split_or_empty(frame.args.reversed_properties)
local children_title = frame.args.children_title
local root = mw.html.create("div")
_create_list(root, parent_title, parents)
mw.smw.set({[anchor_property]=name})
for i, parent in ipairs(parents) do
mw.smw.set({[parent_property]=parent})
end
for i, prop in ipairs(properties) do
local title = frame.args[prop.."_title"]
local values = _split_or_empty(frame.args[prop.."_values"])
local values_set = _table_to_keys(values)
local inherited_values = p.query_inherited_property(anchor_property, prop, parents)
local final_values = _merge_keys(values_set, inherited_values)
local final_list = _keys_to_table(final_values)
_create_list(root, title, final_list)
for i, k in ipairs(final_list) do
mw.smw.set({[prop]=k})
end
end
for i, prop in ipairs(reversed_properties) do
local title = frame.args[prop.."_title"]
local rev_prop = frame.args[prop.."_property"]
local final_values = p.query_reversed_inherited_property(name, anchor_property, rev_prop)
local final_list = _keys_to_table(final_values)
_create_list(root, title, final_list)
for i, k in ipairs(final_list) do
mw.smw.set({[prop]=k})
end
end
if children_title then
local children = p.query_children(name, anchor_property, parent_property)
local children_list = _keys_to_table(children)
_create_list(root, children_title, children_list)
end
return root:done()
end
return p