第1行:
第1行:
−
p = {}
+
local SMWUtils = require("Module:SMWUtils")
−
+
local ListUtils = require("Module:ListUtils")
−
function _table_to_keys(tb)
+
local p = {}
−
local res = {}
−
for i, k in ipairs(tb) do
−
res[k] = true
−
end
−
return res
−
end
function _merge_keys(a, b)
function _merge_keys(a, b)
local res = {}
local res = {}
−
for k, _ in ipairs(a) do
+
for k, _ in pairs(a) do
res[k] = true
res[k] = true
end
end
−
for k, _ in ipairs(b) do
+
for k, _ in pairs(b) do
res[k] = true
res[k] = true
end
end
第20行:
第14行:
end
end
−
function _keys_to_table(set)
−
local res = {}
−
for k, v in ipairs(set) do
−
table.insert(set, k)
−
end
−
return res
−
end
−
function p.query_inherited_property(anchor_property, query_property, parent_list)
+
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 = "[["..anchor_property.."::"..parent.."]]"
+
local anchor_query = "[["..parent.."]]"
local property_query = "[[" .. query_property .. "::+]]"
local property_query = "[[" .. query_property .. "::+]]"
−
local content_query = "?"..query_property
+
local query = anchor_query .. property_query
−
local query = anchor_query .. property_query .. "|" ..content_query .. "|format=plainlist"
+
local res = SMWUtils.query_page_properties(query, {query_property})
−
local res = mw.smw.getQueryResult(query)
+
for j, r in ipairs(res) do
−
for j, r in ipairs(res.results) do
+
results[r.properties[query_property]] = true
−
for k, t in ipairs(r.printouts[query_property]) do
−
results[t] = true
−
end
end
end
end
end
第45行:
第29行:
end
end
−
function p.query_children(name, anchor_property, parent_property)
+
function p.query_by_reference(name, query_property)
−
local results = {}
+
local query = "[["..query_property.."::"..name.."]]"
−
local anchor_query = "[["..parent_property.."::"..name.."]]"
+
return ListUtils.list_to_set(SMWUtils.query_pages(query))
−
local property_query = "[[" .. anchor_property .. "::+]]"
−
local content_query = "?"..anchor_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
−
local res = r.printouts[query_property]
−
if res then
−
for k, t in ipairs(res) do
−
results[t] = true
−
end
−
end
−
end
−
return results
end
end
+
function _create_list(root, title, list)
function _create_list(root, title, list)
第69行:
第41行:
for k, v in ipairs(list) do
for k, v in ipairs(list) do
ul:tag("li"):wikitext("[["..v.."]]")
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
end
end
第74行:
第55行:
function p.inherited_lists(frame)
function p.inherited_lists(frame)
local name = frame.args.name
local name = frame.args.name
−
local parents = (frame.args.parents and #frame.args.parents > 0) and mw.text.split(frame.args.parents, ",") or {}
+
local parents = _split_or_empty(frame.args.parents)
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 anchor_property = frame.args.anchor_property
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
第84行:
第64行:
_create_list(root, parent_title, parents)
_create_list(root, parent_title, parents)
−
mw.smw.set({[anchor_property]=name})
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})
第91行:
第70行:
for i, prop in ipairs(properties) do
for i, prop in ipairs(properties) do
local title = frame.args[prop.."_title"]
local title = frame.args[prop.."_title"]
−
local values_str = frame.args[prop.."_values"]
+
local values = _split_or_empty(frame.args[prop.."_values"])
−
local values = (values_str and #values_str > 0) and mw.text.split(values_str, ",") or {}
+
local reversed = frame.args[prop.."_reversed"]
−
local values_set = _table_to_keys(values)
+
local values_set = ListUtils.list_to_set(values)
−
local inherited_values = p.query_inherited_property(anchor_property, prop, parents)
+
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 final_list = _keys_to_table(final_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)
_create_list(root, title, final_list)
−
root:wikitext(prop..">>>>>"..mw.dumpObject(values_set)..mw.dumpObject(inherited_values))
for i, k in ipairs(final_list) do
for i, k in ipairs(final_list) do
mw.smw.set({[prop]=k})
mw.smw.set({[prop]=k})
第105行:
第88行:
if children_title then
if children_title then
−
local children = p.query_children(name, anchor_property, parent_property)
+
local children = p.query_by_reference(name, parent_property)
−
local children_list = _keys_to_table(children)
+
local children_list = ListUtils.set_to_list(children)
_create_list(root, children_title, children_list)
_create_list(root, children_title, children_list)
end
end