“模块:Inherit”的版本间的差异
跳到导航
跳到搜索
(未显示同一用户的7个中间版本) | |||
第1行: | 第1行: | ||
− | + | local SMWUtils = require("Module:SMWUtils") | |
− | + | local ListUtils = require("Module:ListUtils") | |
− | + | local p = {} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
function _merge_keys(a, b) | function _merge_keys(a, b) | ||
第20行: | 第14行: | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | function p.query_inherited_property( | + | function p.query_inherited_property(query_property, parent_list) |
local results = {} | local results = {} | ||
for idx, parent in ipairs(parent_list) do | for idx, parent in ipairs(parent_list) do | ||
− | local anchor_query = "[[ | + | local anchor_query = "[["..parent.."]]" |
local property_query = "[[" .. query_property .. "::+]]" | local property_query = "[[" .. query_property .. "::+]]" | ||
− | + | local query = anchor_query .. property_query | |
− | local query = anchor_query .. property_query | + | local res = SMWUtils.query_page_properties(query, {query_property}) |
− | local res = | + | for j, r in ipairs(res) do |
− | for j, r in ipairs(res | + | results[r.properties[query_property]] = true |
− | |||
− | |||
− | |||
end | end | ||
end | end | ||
第47行: | 第29行: | ||
end | end | ||
− | function p. | + | function p.query_by_reference(name, query_property) |
− | return | + | local query = "[["..query_property.."::"..name.."]]" |
+ | return ListUtils.list_to_set(SMWUtils.query_pages(query)) | ||
end | end | ||
− | |||
− | |||
− | |||
function _create_list(root, title, list) | function _create_list(root, title, list) | ||
第63行: | 第43行: | ||
end | end | ||
end | end | ||
+ | |||
function _split_or_empty(value) | function _split_or_empty(value) | ||
第77行: | 第58行: | ||
local parent_property = frame.args.parent_property | local parent_property = frame.args.parent_property | ||
local parent_title = frame.args.parent_title | local parent_title = frame.args.parent_title | ||
− | |||
local properties = mw.text.split(frame.args.properties, ",") | local properties = mw.text.split(frame.args.properties, ",") | ||
− | |||
local children_title = frame.args.children_title | local children_title = frame.args.children_title | ||
第85行: | 第64行: | ||
_create_list(root, parent_title, parents) | _create_list(root, parent_title, parents) | ||
− | |||
for i, parent in ipairs(parents) do | for i, parent in ipairs(parents) do | ||
mw.smw.set({[parent_property]=parent}) | mw.smw.set({[parent_property]=parent}) | ||
第93行: | 第71行: | ||
local title = frame.args[prop.."_title"] | local title = frame.args[prop.."_title"] | ||
local values = _split_or_empty(frame.args[prop.."_values"]) | local values = _split_or_empty(frame.args[prop.."_values"]) | ||
− | local values_set = | + | local reversed = frame.args[prop.."_reversed"] |
− | local inherited_values = p.query_inherited_property( | + | local values_set = ListUtils.list_to_set(values) |
+ | local inherited_values = p.query_inherited_property(prop, parents) | ||
local final_values = _merge_keys(values_set, inherited_values) | local final_values = _merge_keys(values_set, inherited_values) | ||
− | local | + | if reversed then |
− | + | local reversed_values = p.query_by_reference(name, reversed) | |
− | + | final_values = _merge_keys(final_values, reversed_values) | |
− | |||
end | end | ||
− | |||
− | + | local final_list = ListUtils.set_to_list(final_values) | |
− | local | ||
− | |||
− | |||
− | |||
_create_list(root, title, final_list) | _create_list(root, title, final_list) | ||
for i, k in ipairs(final_list) do | for i, k in ipairs(final_list) do | ||
第115行: | 第88行: | ||
if children_title then | if children_title then | ||
− | local children = p. | + | local children = p.query_by_reference(name, parent_property) |
− | local children_list = | + | local children_list = ListUtils.set_to_list(children) |
_create_list(root, children_title, children_list) | _create_list(root, children_title, children_list) | ||
end | end |
2021年3月24日 (三) 11:58的最新版本
{{#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 SMWUtils = require("Module:SMWUtils") local ListUtils = require("Module:ListUtils") local p = {} 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 p.query_inherited_property(query_property, parent_list) local results = {} for idx, parent in ipairs(parent_list) do local anchor_query = "[["..parent.."]]" local property_query = "[[" .. query_property .. "::+]]" local query = anchor_query .. property_query local res = SMWUtils.query_page_properties(query, {query_property}) for j, r in ipairs(res) do results[r.properties[query_property]] = true end end return results end function p.query_by_reference(name, query_property) local query = "[["..query_property.."::"..name.."]]" return ListUtils.list_to_set(SMWUtils.query_pages(query)) 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 properties = mw.text.split(frame.args.properties, ",") local children_title = frame.args.children_title local root = mw.html.create("div") _create_list(root, parent_title, parents) 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 reversed = frame.args[prop.."_reversed"] local values_set = ListUtils.list_to_set(values) local inherited_values = p.query_inherited_property(prop, parents) local final_values = _merge_keys(values_set, inherited_values) if reversed then local reversed_values = p.query_by_reference(name, reversed) final_values = _merge_keys(final_values, reversed_values) end local final_list = ListUtils.set_to_list(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_by_reference(name, parent_property) local children_list = ListUtils.set_to_list(children) _create_list(root, children_title, children_list) end return root:done() end return p