« Module:Delta functions » : différence entre les versions
De Semantic MediaWiki - Sandbox
Aucun résumé des modifications |
Aucun résumé des modifications |
||
Ligne 48 : | Ligne 48 : | ||
local queryText = '[[Category:' .. frame.args.cat .. ']][[Has main::' .. parent | local queryText = '[[Category:' .. frame.args.cat .. ']][[Has main::' .. parent | ||
.. ']][[' .. frame.args.num .. '::<' .. num .. ']] |?' .. frame.args.prop .. ' |sort= |mainlabel=-' | .. ']][[' .. frame.args.num .. '::<' .. num .. ']] |?' .. frame.args.prop .. ' |sort= |mainlabel=-' | ||
if | if true then -- for debugging | ||
return title.text .. ', ' .. num .. ', ' .. dump(frame:getParent().args) .. ': ' .. queryText | return title.text .. ', ' .. num .. ', ' .. dump(frame:getParent().args) .. ': ' .. queryText | ||
end | end |
Version du 26 juin 2021 à 18:21
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: the property containing the number of the pages
-- 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 == 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 parent = frame:getParent().args['Has main']
local queryText = '[[Category:' .. frame.args.cat .. ']][[Has main::' .. parent
.. ']][[' .. frame.args.num .. '::<' .. num .. ']] |?' .. frame.args.prop .. ' |sort= |mainlabel=-'
if true then -- for debugging
return title.text .. ', ' .. num .. ', ' .. dump(frame:getParent().args) .. ': ' .. queryText
end
local queryResult = mw.smw.ask(queryText)
if queryResult == nil then
return "(no values)"
end
if type(queryResult) == "table" then
local myResult = 0
local timeFrom = nil
local reference = nil
local pattern = "(%d+)-(%d+)-(%d+)"
local i = 0
for num, row in pairs(queryResult) do
for property, data in pairs(row) do
if trim(frame.args.unit) == "n" then
if i < 1 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 < 2 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
i = i + 1
end
return myResult
end
return queryResult
end
return p