Mòdul:Dimensions/proves
Aparença
Podeu crear la pàgina d'ús per documentar aquest mòdul Lua. |
-- version 20200120 from master @cawiki
local p = {}
local claim = require('Module:Wikidata/proves').claim
function p.main(frame)
local args = frame.args or frame -- via invoke or require
local pargs = frame.args and frame:getParent().args or {}
local id = args.item or pargs.item
if id == nil or id == '' then
id = mw.wikibase.getEntityIdForCurrentPage()
end
local unit = args.unit or pargs.unit; if unit == "" then unit = nil end
local blacklist = {}
for v in mw.text.gsplit(args.blacklist or "", "/", true) do
blacklist[v] = true
end
local lang = args.lang or pargs.lang
-- Fetch all data and fill a table
local data = {}
local labels = {}
local function fillData(prop, dimension)
local fetch = claim{
item = id, property = prop, qualifier = 'P518 OR P1013',
lang = lang, editicon = 'false', formatting = 'table',
rowformat = '$0($1)', colformat0 = 'unitcode', convert0 = unit, colformat1 = 'label', separator = '<and>'
}
if fetch then
for p in mw.text.gsplit(prop, ' OR ', true) do
if #mw.wikibase.getBestStatements(id, p) > 0 then
labels[dimension] = mw.wikibase.getLabel(p)
break
end
end
for part in mw.text.gsplit(fetch, '<and>', true) do
data[#data + 1] = {}
data[#data].dimension = dimension
local group = mw.ustring.match(part, '%b()')
local dim = part
if group ~= '()' then
data[#data].group = mw.ustring.sub(group, 2, -2)
dim = mw.ustring.sub(part, 1, mw.ustring.find(part, group, 1, true) - 1)
end
if blacklist[group] then
blacklist[data[#data].group] = true
end
data[#data].amount = string.match(dim, '[^%a]+')
data[#data].unit = string.match(dim, '[%a]+')
end
end
end
-- diameter
fillData('P2386', 'diameter')
-- height and alike
fillData('P2048', 'height')
fillData('P2262', 'draft')
fillData('P2793', 'clearance')
-- width or beam
fillData('P2049 OR P2261', 'width')
-- length or depth and alike
fillData('P2043 OR P5524', 'length')
fillData('P2787', 'span')
fillData('P2610', 'thickness')
-- group by qualifier
local dimensions = {}
local global_unit
for i, v in ipairs(data) do
local index = v.group or 1
if blacklist[index] == nil then
if dimensions[index] == nil then
dimensions[index] = {}
end
table.insert(dimensions[index], {["dimension"]=v.dimension, ["amount"]=v.amount, ["unit"]=v.unit})
-- global unit
if i == 1 then
global_unit = v.unit
elseif global_unit and global_unit ~= v.unit then
global_unit = nil
end
end
end
-- format output
local icons = {
["diameter"] = "[[File:Durchschnittszeichen.png|10px|link=|",
["height"] = "[[File:Chess uat45.svg|15px|link=|",
["draft"] = "[[File:Breezeicons-actions-22-draw-halfcircle4.svg|15px|link=|",
["clearance"] = "[[File:Breezeicons-actions-22-format-align-vertical-top.svg|15px|link=|",
["width"] = "[[File:Chess lrt45.svg|15px|link=|",
["length"] = "[[File:Chess urt45.svg|15px|link=|",
["span"] = "[[File:Breezeicons-actions-22-draw-halfcircle3.svg|15px|link=|",
["thickness"] = "[[File:Breezeicons-actions-22-format-align-vertical-center.svg|15px|link=|",
}
local dim_sort = {["diameter"]=1, ["height"]=2, ["draft"]=3, ["clearance"]=4, ["width"]=5, ["length"]=6, ["span"]=7, ["thickness"]=8}
local out = {}
for q, t in pairs(dimensions) do
if type(q) == "string" then
table.insert(out, q .. ":")
end
table.sort(t, function(a, b) return dim_sort[a.dimension] < dim_sort[b.dimension] end)
for i, v in ipairs(t) do
local suffix = " (" .. icons[v.dimension] .. labels[v.dimension] .. "]])"
if i == #t or not global_unit then
suffix = suffix .. " " .. v.unit
end
if i < #t then
suffix = suffix .. " ×"
end
table.insert(out, '<span style="white-space:nowrap;">' .. v.amount .. suffix .. '</span>')
end
table.insert(out, "<br />")
end
return table.concat(out, " ")
end
return p