Modulu:Wikidata: berrikuspenen arteko aldeak

Ezabatutako edukia Gehitutako edukia
update, coordinates with globe, function getParentValues
update, minor fixes, new function years_old
1. lerroa:
-- Master version: [[:ca:Module:Wikidata]]
-- compatible version: 2017031320170016
 
local p = {}
408. lerroa:
end
 
-- AOn ladebug consolaconsole de depuració useuuse: =p._debug({item="Q...", property="P...", ...})
function p._debug(args)
return p._main(args)
434. lerroa:
local property = args["property"] or ""
local id = args["item"]; if id == "" then id = nil end
local idgender = args["itemgender"]; if idgender == "" then idgender = nil end
if idgender and not string.match(idgender, "^Q%d+$") then -- id malformed, maybe "unknown value"
idgender = nil
end
local qualifierId = {}
qualifierId[1] = args["qualifier"]
443 ⟶ 446 lerroa:
local case = args.case
local list = args["list"] or true; if list == "false" then list = false end
local sortingsorting_col = args.tablesort
local sorting_up = (args.sorting or "") ~= "-1"
local separator = args.separator
local conjunction = args.conjunction or args.separator
454 ⟶ 458 lerroa:
if args["rank"] and args["rank"] ~= '' then list = false end -- compatibilitat de transició
if args["rank"] then track("wikidata/rank") end
local parameters = {["formatting"] = parameter, ["list"] = list, ["sorting"] = sorting, ["case"] = case,
["separator"] = separator, ["conjunction"] = conjunction, ["qseparator"] = separator, ["qconjunction"] = conjunction}
local preformat = ""
562 ⟶ 566 lerroa:
sortindices[#sortindices + 1] = idx
end
if parameters.sortingsorting_col then
local comparator = function(a, b)
local valuea = sortkeys[a]["col" .. parameters.sortingsorting_col] or ''
local valueb = sortkeys[b]["col" .. parameters.sortingsorting_col] or ''
if sorting_up then
return valuea < valueb
return valuea < valueb
end
return valuea > valueb
end
table.sort(sortindices, comparator)
750 ⟶ 757 lerroa:
return entityId, result
end
 
function p.getInstanceValue(frame)
track("wikidata/getInstanceValue")
return p.getParentValues(frame)
end
 
820 ⟶ 822 lerroa:
end
return mw.text.listToText(ret, separator, separator)
end
 
function p.linkWithParentLabel(frame)
local args = {}
for k, v in pairs(frame.args) do -- metatable
args[k] = v
end
args.list = "false"
args.formatting = "internallink"
local link = p._main(args) -- get internal link of property/qualifier
args.formatting = "raw"
args.item = p._main(args) -- get item of property/qualifier
args.property = args.parent
args.qualifier = nil
args.formatting = "label"
local link_label = p._main(args) -- get label of parent property
if link_label then
link = mw.ustring.gsub(link or '', "%[%[(.*)%|.+%]%]", "[[%1|" .. link_label .. "]]")
end
return link
end
 
function p.years_old(frame)
local args = frame.args
local id = args.item; if id == '' then id = nil end
local lang = mw.language.new('en')
local function fetchsnak(id, snak)
local ret = mw.wikibase.getEntityObject(id)
for i, v in ipairs(snak) do
if ret == nil then break end
ret = ret[v]
end
return ret
end
local birth = fetchsnak(id, {'claims', 'P569', 1, 'mainsnak', 'datavalue', 'value'})
if type(birth) ~= 'table' or birth.time == nil or birth.precision == nil or birth.precision < 8 then
return
end
local death = fetchsnak(args.item, {'claims', 'P570', 1, 'mainsnak', 'datavalue', 'value'})
if type(death) ~= 'table' or death.time == nil or death.precision == nil then
death = {['time'] = lang:formatDate('c'), ['precision'] = 11} -- current date
elseif death.precision < 8 then
return
end
local dates = {}
dates[1] = {['min'] = {}, ['max'] = {}, ['precision'] = birth.precision}
dates[1].min.year = tonumber(mw.ustring.match(birth.time, "^[+-]?%d+"))
dates[1].min.month = tonumber(mw.ustring.match(birth.time, "\-(%d%d)\-"))
dates[1].min.day = tonumber(mw.ustring.match(birth.time, "\-(%d%d)T"))
dates[1].max = mw.clone(dates[1].min)
dates[2] = {['min'] = {}, ['max'] = {}, ['precision'] = death.precision}
dates[2].min.year = tonumber(mw.ustring.match(death.time, "^[+-]?%d+"))
dates[2].min.month = tonumber(mw.ustring.match(death.time, "\-(%d%d)\-"))
dates[2].min.day = tonumber(mw.ustring.match(death.time, "\-(%d%d)T"))
dates[2].max = mw.clone(dates[2].min)
for i, d in ipairs(dates) do
if d.precision == 10 then -- month
d.min.day = 1
local timestamp = string.format("%04d", tostring(math.abs(d.max.year)))
.. string.format("%02d", tostring(d.max.month))
.. "01"
d.max.day = tonumber(lang:formatDate("j", timestamp .. " + 1 month - 1 day"))
elseif d.precision < 10 then -- year or decade
d.min.day = 1
d.min.month = 1
d.max.day = 31
d.max.month = 12
if d.precision == 8 then -- decade
d.max.year = d.max.year + 9
end
end
end
local function age(d1, d2)
local years = d2.year - d1.year
if d2.month < d1.month or (d2.month == d1.month and d2.day < d1.day) then
years = years - 1
end
if d2.year > 0 and d1.year < 0 then
years = years - 1 -- no year 0
end
return years
end
local old_min = age(dates[1].max, dates[2].min)
local old_max = age(dates[1].min, dates[2].max)
local old = old_min == old_max and old_min or old_min .. "/" .. old_max
if args.formatting then
old = mw.ustring.gsub(args.formatting, '$1', old)
end
return old
end