Module:Delta functions
De Semantic MediaWiki - Sandbox
La documentation pour ce module peut être créée à Module:Delta functions/doc
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
local p = {}
-- compute the difference between two numbers
function p.numDelta(frame)
x = frame.args[1];
y = frame.args[2];
return y - x;
end
-- compute a numeric or temporal difference between the properties of a set of pages (last - first)
-- unit: n = numeric, d,m,y = temporal
-- cat: the category of the pages to retrieve
-- num_prop: the property containing the number of the pages
-- curr_data: the current data
-- prop: the property
function p.smwDelta(frame)
if not mw.smw then
return "mw.smw module not found: install and activate SeamnticScribunto extension"
end
if frame.args.unit == nil or frame.args.cat == nil or frame.args.num_prop == nil
or frame.args.curr_data == nil or frame.args.prop == nil then
return "specify all the parameters"
end
local title = mw.title.getCurrentTitle()
local num = mw.text.split(title.text, ' ', true)[3] * 1
local curr_data = frame.args.curr_data
local parent = frame:getParent().args['Has main']
local queryText = '[[Category:' .. frame.args.cat .. ']][[Has main::' .. parent
.. ']][[' .. frame.args.num_prop .. '::<' .. num .. ']] |?' .. frame.args.prop .. ' |sort= |mainlabel=-'
if false then -- for debugging
queryText = '[Category:' .. frame.args.cat .. '][Has main::' .. parent
.. '][' .. frame.args.num_prop .. '::<' .. num .. '] |?' .. frame.args.prop .. ' |sort= |mainlabel=-'
--return title.text .. ', ' .. num .. ', ' .. dump(frame:getParent().args)
return ': <nowiki>' .. queryText .. '</nowiki>'
end
local queryResult = mw.smw.ask(queryText)
if queryResult == nil then
return "(no values)"
end
if true then -- for debugging
return dump(queryResult)
end
if type(queryResult) == "table" then
local myResult = 0
local timeFrom = nil
local reference = nil
local pattern = "(%d+)-(%d+)-(%d+)"
local unit = trim(frame.args.unit)
-- num is from 1
for i, row in pairs(queryResult) do
for property, data in pairs(row) do
--if i == num then
-- data = curr_data
--end
if unit == "n" then
if i < 2 then
reference = tonumber(data)
myResult = 0
else
myResult = tonumber(data) - reference
end
else
local runyear, runmonth, runday = data:match(pattern)
--mw.log(runyear)
if num == 1 then
reference = os.time{day=runday, year=runyear, month=runmonth}
else
local to = os.time{day=runday, year=runyear, month=runmonth}
if trim(frame.args.unit) == "d" then
timeFrom = os.difftime(to, reference) / (24 * 60 * 60) -- seconds in a day
elseif trim(frame.args.unit) == "m" then
timeFrom = os.difftime(to, reference) / (24 * 60 * 60 * 30) -- seconds in a month
elseif trim(frame.args.unit) == "y" then
timeFrom = os.difftime(to, reference) / (24 * 60 * 60 * 30 * 12) -- seconds in a year
end
myResult = math.floor(timeFrom)
end
end
end
end
return myResult
end
return queryResult
end
return p