Module:Wikidata dimension
Lua
Documentation for this module may be created at Module:Wikidata dimension/doc
Code
local p = {}
--=========
local getArgs = require('Module:Arguments').getArgs
local unit_names = {
["millimetre"] = "mm",
["centimetre"] = "cm",
["metre"] = "m" ,
["kilometre"] = "km",
["inch"] = "in",
["foot"] = "fr",
["yard"] = "yd",
["mile"] = "mi",
["gram"] = "g" ,
["kilogram"] = "kg",
};
----
local length_dimensions_pid = { -- Clustering dimensions of same physical property makes it easier to iterate over them.
["length"] = "P2043",
["height"] = "P2048",
["width"] = "P2049",
["depth"] = "P2610",
["diameter"] = "P2386"
}
local other_dimensions_pid = {
["mass"] = "P2067",
["scale"] = "P1752"
}
----
function p._get_unit_qid( qid, pid )
local entity = mw.wikibase.getEntityObject( qid )
if entity['claims'][pid] == nil then return nil end
if entity['claims'][pid][1] == nil then return nil end
if entity['claims'][pid][1]['mainsnak']['datavalue']['value']['unit'] == nil then return nil end
local work = entity['claims'][pid][1]['mainsnak']['datavalue']['value']['unit']
work = string.sub(work, 32)
return work
end
----
function p._get_amount( qid, pid )
local entity = mw.wikibase.getEntityObject(qid)
if entity['claims'][pid] == nil then return nil end
if entity['claims'][pid][1] == nil then return nil end
if entity['claims'][pid][1]['mainsnak']['datavalue']['value']['amount'] == nil then return nil end
local work = entity['claims'][pid][1]['mainsnak']['datavalue']['value']['amount']
-- For some weird reason Wikidata returns "+2" instead of "2"; removing leading +
if string.sub(work, 1, 1) == "+" then work = string.sub(work, 2) end
return work
end
----
function p.get_unit_qid( frame )
local qid = getArgs(frame)["qid"]
local pid = getArgs(frame)["pid"]
local unit_qid = p._get_unit_qid( qid, pid )
return unit_qid
end
----
function p.get_unit_label( frame )
local qid = getArgs(frame)["qid"]
local pid = getArgs(frame)["pid"]
local unit_qid = p._get_unit_qid( qid, pid )
return mw.wikibase.label( unit_qid )
-- return mw.wikibase.getLabelByLang( unit_qid, 'ru' )
end
----
function p._get_unit_label_en( qid, pid )
local unit_qid = p._get_unit_qid( qid, pid )
if not unit_qid then return nil end
return mw.wikibase.getLabelByLang( unit_qid, 'en' )
end
function p.get_unit_label_en( frame )
local qid = getArgs(frame)["qid"]
local pid = getArgs(frame)["pid"]
--local unit_qid = p._get_unit_qid( qid, pid )
--return mw.wikibase.getLabelByLang( unit_qid, 'en' )
return p._get_unit_label_en( qid, pid )
end
----
function p._get_unit_abbreviation( qid, pid )
-- return "plouf " .. qid .. "--" .. pid
local unit_name = p._get_unit_label_en( qid, pid )
return unit_names[unit_name]
end
function p.get_unit_abbreviation( frame )
local qid = getArgs(frame)["qid"]
local pid = getArgs(frame)["pid"]
return p._get_unit_abbreviation( qid, pid )
end
----
function p._get_length_unit_in_use( qid )
-- Detects the unit used to express lengths.
-- This assumes that the same unit is used for length, width etc. (seriously why would you do otherwise?)
for dim_name, dim_pid in next, length_dimensions_pid do
local this_unit_abbr = p._get_unit_abbreviation( qid, dim_pid )
if this_unit_abbr then return this_unit_abbr end
end
return "No length unit was found"
end
function p.get_length_unit_in_use( frame )
local qid = getArgs(frame)["qid"]
return p._get_length_unit_in_use( qid )
end
----
function p.display_size_template( frame )
local qid = getArgs(frame)["qid"]
local reference_unit = p._get_length_unit_in_use( qid )
local length_value = p._get_amount( qid, length_dimensions_pid["length"] )
local height_value = p._get_amount( qid, length_dimensions_pid["height"] )
local width_value = p._get_amount( qid, length_dimensions_pid["width"] )
local depth_value = p._get_amount( qid, length_dimensions_pid["depth"] )
local diameter_value = p._get_amount( qid, length_dimensions_pid["diameter"] )
return frame:expandTemplate{ title = 'Size', args = { unit = reference_unit , length = length_value , height = height_value, width = width_value, thickness = depth_value, diameter = diameter_value } }
end
---- This will start by trying to display a Size template,
-- then a Weight template for the mass if given,
-- and finally the scale if given
function p.display_all_sizes( frame )
local qid = getArgs(frame)["qid"]
local reference_unit = p._get_length_unit_in_use( qid )
local length_value = p._get_amount( qid, length_dimensions_pid["length"] )
local height_value = p._get_amount( qid, length_dimensions_pid["height"] )
local width_value = p._get_amount( qid, length_dimensions_pid["width"] )
local depth_value = p._get_amount( qid, length_dimensions_pid["depth"] )
local diameter_value = p._get_amount( qid, length_dimensions_pid["diameter"] )
work = frame:expandTemplate{ title = 'Size', args = { unit = reference_unit , length = length_value , height = height_value, width = width_value, thickness = depth_value, diameter = diameter_value } }
-- Mass
local mass_value = p._get_amount( qid, other_dimensions_pid["mass"] )
local mass_unit = p._get_unit_abbreviation( qid, other_dimensions_pid["mass"] )
if mass_value then
if work ~= "" then work = work .. "<br />" end
work = work .. frame:expandTemplate{ title = 'Weight', args = { mass_unit, mass_value } }
end
-- Scale
local scale_value = p._get_amount( qid, other_dimensions_pid["scale"] )
if scale_value then
scale_string = ""
if (tonumber(scale_value) == 1) then scale_string = "1:1"
elseif (tonumber(scale_value) > 1) then scale_string = "1:" .. scale_value
-- Convention in Wikidata is that larger-than-lifesize scales have a negative number
elseif (tonumber(scale_value) < 1) then scale_string = abs(scale_value) .. ":1"
else return "Error in determing scale" end
if work ~= "" then work = work .. "<br />" end
work = work .. mw.wikibase.label( "Q193642" ) .. ": " .. scale_string
end
return work
end
----
function p.get_unit_label_by_lang( frame )
local qid = getArgs(frame)["qid"]
local pid = getArgs(frame)["pid"]
local lang = getArgs(frame)["lang"]
if not lang then lang = "en" end
local unit_qid = p._get_unit_qid( qid, pid )
return mw.wikibase.getLabelByLang( unit_qid, lang )
end
----
function p.get_amount( frame )
local qid = getArgs(frame)["qid"]
local pid = getArgs(frame)["pid"]
local amount = p._get_amount( qid, pid )
return amount
end
--=========
return p